def transcribe(request, id): u = User.objects.get(id=1) aud = AudioMeta.objects.get(id=id) t = request.POST["t"] m = request.POST["data"] an = Transcription(user_id=u, aud_id=aud, st_time=int(t), end_time=0, msg=m) an.save() return HttpResponse(content="0")
def transcribe(request, id): u = User.objects.get(id=1) aud = AudioMeta.objects.get(id=id) t = request.POST['t'] m = request.POST['data'] an = Transcription(user_id=u, aud_id=aud, st_time=int(t), end_time=0, msg=m) an.save() return HttpResponse(content="0")
def transcription_english_post_json(): if not request.is_json: return jsonify({"msg": "Missing JSON in request"}), 400 json_transcription = request.get_json() #model = Punctuator(app.config['punctuate_model_path']) user_id = get_jwt_identity() transcription_text = json_transcription['transcription_text'] processed_text = json_transcription['transcription_text'] categorized_sentences = categorize_sentences(processed_text) subjective = "" objective = "" assessment = "" plan = "" for sentence in categorized_sentences: if sentence[1] == 'Subjective': subjective += " " + sentence[0] elif sentence[1] == 'Objective': objective += " " + sentence[0] elif sentence[1] == 'Assessment': assessment += " " + sentence[0] elif sentence[1] == 'Plan': plan += " " + sentence[0] else: continue transcription = Transcription(user_id, transcription_text, processed_text, subjective=subjective, objective=objective, assessment=assessment, plan=plan) db.session.add(transcription) failed = False try: db.session.commit() except Exception as e: db.session.rollback() db.session.flush() # for resetting non-commited .add() failed = True return json.dumps({'success': False}), 500, { 'ContentType': 'application/json' } return json.dumps({'success': True}), 200, { 'ContentType': 'application/json' }
def store_and_save_transcription_files(self, auto_file, ref_file, user_id): transcription = Transcription(user_id=user_id) self.db.session.add(transcription) self.db.session.commit() auto_file.filename = "{}_auto_{}".format(transcription.id, auto_file.filename) ref_file.filename = "{}_ref_{}".format(transcription.id, ref_file.filename) (auto_status, auto_filename) = self.store_and_save_uploaded_file( auto_file, user_id, 'transcriptions') (ref_status, ref_filename) = self.store_and_save_uploaded_file( ref_file, user_id, 'transcriptions') if (auto_status == FileStatus.Success and ref_status == FileStatus.Success): transcription.auto_filename = os.path.basename(auto_filename) transcription.ref_filename = os.path.basename(ref_filename) self.db.session.commit() return (FileStatus.Success, transcription) else: return (FileStatus.Error, None)
def transcription_post_json(): if not request.is_json: return jsonify({"msg": "Missing JSON in request"}), 400 json_transcription = request.get_json() #model = Punctuator(app.config['punctuate_model_path']) user_id = get_jwt_identity() transcription_text = json_transcription['transcription_text'] processed_text = punctuateText(transcription_text) subjective = "" objective = "" assessment = "" plan = "" transcription = Transcription(user_id, transcription_text, processed_text, subjective=subjective, objective=objective, assessment=assessment, plan=plan) db.session.add(transcription) failed = False try: db.session.commit() except Exception as e: #log your exception in the way you want -> log to file, log as error with default logging, send by email. It's upon you db.session.rollback() db.session.flush() # for resetting non-commited .add() failed = True return json.dumps({'success': False}), 500, { 'ContentType': 'application/json' } return json.dumps({'success': True}), 200, { 'ContentType': 'application/json' }
def load_all_fixtures(db, app): user_datastore = SQLAlchemyUserDatastore(db, User, Role) # Load default fixtures role = user_datastore.create_role(name=app.config['VOXO_ROLE_USER'], description='User role') admin_role = user_datastore.create_role(name=app.config['VOXO_ROLE_ADMIN'], description='Admin role') server_role = user_datastore.create_role( name=app.config['VOXO_ROLE_SERVER'], description='Server role') user_vjousse = registerable.register_user(email='*****@*****.**', password='******') user_datastore.add_role_to_user(user_vjousse, role) print("{user} with token {token} created.".format( user=user_vjousse.email, token=user_vjousse.get_auth_token())) user = registerable.register_user(email='*****@*****.**', password='******') user_datastore.add_role_to_user(user, admin_role) user_datastore.add_role_to_user(user, role) print("{user} with token {token} created.".format( user=user.email, token=user.get_auth_token())) server = registerable.register_user(email='*****@*****.**', password='******') user_datastore.add_role_to_user(server, server_role) print("{user} with token {token} created.".format( user=server.email, token=server.get_auth_token())) asr_model_french = AsrModel(name="french.studio.fr_FR", description="General purpose french model") asr_model_french.users.append(user_vjousse) db.session.add(asr_model_french) db.session.flush() asr_model_english = AsrModel(name="english.studio", description="General purpose english model") asr_model_english.users.append(user_vjousse) db.session.add(asr_model_english) db.session.flush() media_file = MediaFile(filename='fixture_file.wav', status=FileStatus.Success, user_id=user_vjousse.id, size=2500, generated_filename='fixture_file_UUID.wav', duration=70) db.session.add(media_file) db.session.flush() process = Process( file_id=media_file.id, status=DecodeStatus.Queued, ) db.session.add(process) db.session.flush() transcription = Transcription( auto_filename='transcription.xml', ref_filename='transcription.txt', user_id=user_vjousse.id, ) db.session.add(transcription) db.session.flush() process = Process(file_id=media_file.id, status=DecodeStatus.Queued, type=ProcessType.TranscriptionAlignment, transcription_id=transcription.id) db.session.add(process) db.session.flush() db.session.commit()
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from models import Base, Transcription import const import os if not os.path.exists(const.TRANSCRIBED_DATA_PATH): os.makedirs(const.TRANSCRIBED_DATA_PATH) engine = create_engine('sqlite:///%s' % (const.TRANSCRIBED_SPEECH_SQLITE_DB_PATH)) # Create all tables in the engine. This is equivalent to "Create Table" # statements in raw SQL. Base.metadata.create_all(bind=engine) DBSession = sessionmaker(bind=engine) session = DBSession() t = Transcription(media_type="test", media_id="4gdfgdf2", time_start=0, time_end=1.51, transcription="test") session.add(t) session.commit()
def process_video(yt_video_id, video_title): print("process video %s" % (yt_video_id)) video_data_path = os.path.join(const.VIDEO_DATA_DIR, yt_video_id) video_done_flag_path = os.path.join(video_data_path, "DONE") if os.path.exists(video_done_flag_path): print 'already processed' return # download video try: original_audio_path = download_yt_audio(yt_video_id) except Exception as ex: print 'Error downloading: %s\nRetrying in 5 sec...' % (str(ex)) time.sleep(5) process_video(yt_video_id, video_title) # convert audio and apply filters wav_audio_path = os.path.join(video_data_path, "audio.wav") if not os.path.exists(wav_audio_path): audio_utils.convert_to_wav(original_audio_path, wav_audio_path) wav_vol_corr_path = os.path.join(video_data_path, "audio_vol_corr.wav") print("correct_volume") if not os.path.exists(wav_vol_corr_path): audio_utils.correct_volume(wav_audio_path, wav_vol_corr_path, db=-12) wav_filtered_path = os.path.join(video_data_path, "audio_filtered.wav") if not os.path.exists(wav_filtered_path): print("apply_bandpass_filter") audio_utils.apply_bandpass_filter(wav_vol_corr_path, wav_filtered_path, low=2500) wave_o = wave.open(wav_filtered_path, "r") print("slice audio") pieces, avg_len_sec = slice_audio_by_silence(wave_o) print("total pieces: %i, avg_len_sec: %f" % (len(pieces), avg_len_sec)) pieces_folder_path = os.path.join(video_data_path, "pieces/") if not os.path.exists(pieces_folder_path): os.makedirs(pieces_folder_path) print("start transcribing...") for i, piece in enumerate(pieces): piece_procesing_path = os.path.join( pieces_folder_path, "piece_%i_%.2f_processing.wav" % (i, piece["start"])) piece_done_path = os.path.join( pieces_folder_path, "piece_%i_%.2f_done.wav" % (i, piece["start"])) if not (os.path.exists(piece_procesing_path) or os.path.exists(piece_done_path)): audio_utils.save_wave_samples_to_file( piece["samples"], n_channels=1, byte_width=2, sample_rate=16000, file_path=piece_procesing_path) if not os.path.exists(piece_done_path): # run inference start_t = timer() print("Transcribing piece %s" % piece_procesing_path) transcript = file_transcriber.transcribe_file( piece_procesing_path).decode("utf-8").strip() print("transcription took %.f seconds" % (timer() - start_t)) print(transcript) video_title = video_title.decode("utf-8").strip() t = Transcription(media_type="youtube", media_name=video_title, media_id=yt_video_id, time_start=piece["start"], time_end=piece["end"], transcription=transcript) db_util.add_item(t) os.rename(piece_procesing_path, piece_done_path) with open(video_done_flag_path, 'w'): pass print 'DONE'
def process_video(yt_video_id): curr_dir_path = os.getcwd() video_data_path = os.path.join(const.VIDEO_DATA_DIR, yt_video_id) # download video original_audio_path = download_yt_audio(yt_video_id) # convert audio and apply filters wav_audio_path = os.path.join(video_data_path, "audio.wav") if not os.path.exists(wav_audio_path): audio_utils.convert_to_wav(original_audio_path, wav_audio_path) wav_vol_corr_path = os.path.join(video_data_path, "audio_vol_corr.wav") print("correct_volume") if not os.path.exists(wav_vol_corr_path): audio_utils.correct_volume(wav_audio_path, wav_vol_corr_path, db=-12) wav_filtered_path = os.path.join(video_data_path, "audio_filtered.wav") if not os.path.exists(wav_filtered_path): print("apply_bandpass_filter") audio_utils.apply_bandpass_filter(wav_vol_corr_path, wav_filtered_path, low=2500) wave_o = wave.open(wav_filtered_path, "r") print("slice audio") pieces, avg_len_sec = slice_audio_by_silence(wave_o) print("total pieces: %i, avg_len_sec: %f" % (len(pieces), avg_len_sec)) pieces_folder_path = os.path.join(video_data_path, "pieces/") if not os.path.exists(pieces_folder_path): os.makedirs(pieces_folder_path) print("start transcribing...") for i, piece in enumerate(pieces): piece_procesing_path = os.path.join( pieces_folder_path, "piece_%i_%.2f_processing.wav" % (i, piece["start"])) piece_done_path = os.path.join( pieces_folder_path, "piece_%i_%.2f_done.wav" % (i, piece["start"])) if not (os.path.exists(piece_procesing_path) or os.path.exists(piece_done_path)): audio_utils.save_wave_samples_to_file( piece["samples"], n_channels=1, byte_width=2, sample_rate=16000, file_path=piece_procesing_path) if not os.path.exists(piece_done_path): # run inference start_t = timer() print("Transcribing piece %s" % piece_procesing_path) transcript = run_deepspeech_for_wav( piece_procesing_path, use_lm=False).decode("utf-8").strip() print("transcription took %.f seconds" % (timer() - start_t)) print(transcript) t = Transcription(media_type="youtube", media_id=yt_video_id, time_start=piece["start"], time_end=piece["end"], transcription=transcript) db_util.add_item(t) os.rename(piece_procesing_path, piece_done_path)