def upload_file(): if request.method == 'POST': if 'file' not in request.files: flash('Отсутствует файл') return redirect('/') file = request.files['file'] if file.filename == '': flash('Не выбран файл для загрузки') return redirect('/') if not allowed_file(file.filename): flash('Файл должен иметь расширение .WAV') return redirect('/') filename = file.filename.replace(' ', '_') wav = str(app.config['UPLOAD_FOLDER'] / filename) file.save(wav) wav_info = sox.file_info.info(wav) info = {} info['Длительность аудио'] = str(round(wav_info['duration'], 2)) + ' с' info['Число каналов'] = wav_info['channels'] info['Частота дискретизации'] = str(int(wav_info['sample_rate'])) + ' Гц' start_time = time() temp = str(app.config['UPLOAD_FOLDER'] / Path(wav).stem) os.makedirs(temp, exist_ok=True) transcriptions = recognize(temp, wav) waveform = plot_waveform(temp, wav, wav_info['channels']) if request.form.get('plotWaveform') else None delete_folder(temp) os.remove(wav) info['Время выполнения'] = str(round(time() - start_time, 2)) + ' с' transcriptions = transcriptions[['Name', 'Start', 'End', 'Text']] transcriptions.columns = ['Канал', 'Начало', 'Конец', 'Текст'] with pd.option_context('display.max_colwidth', -1): transcriptions_html = transcriptions.to_html(index=False, justify='center', escape=False) return render_template('results.html', filename='.'.join(filename.split('.')[:-1]), info=info, waveform=waveform, transcriptions=transcriptions_html) return render_template('index.html')
def terminate_pipeline(is_error, message): if is_error: LOGGER.error(message) os.rename(wav, str(ERROR_DIR / wav_name)) try: delete_folder(temp) except: LOGGER.error("Не удалось удалить временные файлы для '{}'".format(wav_name))
def perform_conversion(file_id): res = str(app.config['UPLOAD_FOLDER'] / (file_id + ".res")) try: filename = file_id + ".ogg" ogg = str(app.config['UPLOAD_FOLDER'] / filename) os.system('ffmpeg -i {0}.ogg {0}.wav'.format( str(app.config['UPLOAD_FOLDER'] / file_id))) wav = ogg[:-3] + "wav" temp = str(app.config['UPLOAD_FOLDER'] / Path(wav).stem) os.makedirs(temp, exist_ok=True) transcriptions = recognize(temp, wav) delete_folder(temp) os.remove(wav) os.remove(ogg) transcriptions = transcriptions[['Text']] result = transcriptions.values[0][0] with open(res, 'w') as f: f.write(result) except Exception as e: print(e) with open(res, 'w') as f: f.write('xxxFAILxxx')