def __init__(self, view): # Initialize App Window self.view = view(self) # AppView(self) self.initial_view() # Initialize Audio self.audio = Audio(self, self.audio_device_in, self.audio_device_out, self.audio_samplerate, self.audio_buffersize) self.audio.start()
def get_audio_transcription(training_id: str) -> (dict, int): """ Endpoint to get an audio transcription by a training identifier. :param training_id: Training identifier. :return: Dictionary with per-slide audio transcription array, 'OK' message, or a dictionary with an explanation and 404 HTTP return code if an audio file was not found, or an empty dictionary with 404 HTTP return code if access was denied. """ if not check_access({'_id': ObjectId(training_id)}): return {}, 404 training_db = TrainingsDBManager().get_training(training_id) audio_id = training_db.audio_id audio_as_json = DBManager().get_file(audio_id) if audio_as_json is None: return { 'message': 'No audio file with audio_id = {}.'.format(audio_id) }, 404 audio = Audio.from_json_file(audio_as_json) audio_slides = audio.audio_slides audio_transcription = [ ' '.join([word.word.value for word in audio_slide.recognized_words]) for audio_slide in audio_slides ] return {'audio_transcription': audio_transcription, 'message': 'OK'}, 200
def test_fillers_ratio_criteria(): criterion = FillersRatioCriterion( parameters={'fillers': ['а', 'ну', 'это самое']}, dependent_criteria=[], ) with open('test_data/test_audio_7.json', 'rb') as test_audio_file: test_audio = Audio.from_json_file(test_audio_file) criterion_result = criterion.apply(test_audio, presentation=None, training_id=None, criteria_results={}) assert criterion_result.result == 1 - 1 / 893
def run(self): while True: try: recognized_audio_db = RecognizedAudioToProcessDBManager( ).extract_recognized_audio_to_process() if not recognized_audio_db: sleep(10) continue training_id = recognized_audio_db.training_id recognized_audio_id = recognized_audio_db.file_id logger.info( 'Extracted recognized audio with recognized_audio_id = {}, training_id = {}.' .format(recognized_audio_id, training_id)) TrainingsDBManager().change_audio_status( training_id, AudioStatus.PROCESSING) json_file = DBManager().get_file(recognized_audio_id) if json_file is None: TrainingsDBManager().change_audio_status( training_id, AudioStatus.PROCESSING_FAILED) verdict = 'Recognized audio file with recognized_audio_id = {} was not found.'\ .format(recognized_audio_id) TrainingsDBManager().append_verdict(training_id, verdict) TrainingsDBManager().set_score(training_id, 0) logger.warning(verdict) continue recognized_audio = RecognizedAudio.from_json_file(json_file) json_file.close() slide_switch_timestamps = TrainingsDBManager( ).get_slide_switch_timestamps(training_id) audio = Audio(recognized_audio, slide_switch_timestamps) audio_id = DBManager().add_file(repr(audio)) TrainingsDBManager().add_audio_id(training_id, audio_id) TrainingsDBManager().change_audio_status( training_id, AudioStatus.PROCESSED) except Exception as e: logger.error('Unknown exception.\n{}'.format(e))
def run(self): while True: try: training_id = TrainingsToProcessDBManager().extract_training_id_to_process() if not training_id: sleep(10) continue logger.info('Extracted training with training_id = {}.'.format(training_id)) training_db = TrainingsDBManager().get_training(training_id) if training_db is None: TrainingsDBManager().change_training_status_by_training_id( training_id, TrainingStatus.PROCESSING_FAILED ) verdict = 'Training with training_id = {} was not found.'.format(training_id) TrainingsDBManager().append_verdict(training_id, verdict) TrainingsDBManager().set_score(training_id, 0) logger.warning(verdict) continue TrainingsDBManager().change_training_status_by_training_id(training_id, TrainingStatus.PROCESSING) audio_file = DBManager().get_file(training_db.audio_id) if audio_file is None: TrainingsDBManager().change_training_status_by_training_id( training_id, TrainingStatus.PROCESSING_FAILED ) verdict = 'Audio file with audio_id = {}, training_id = {} was not found.'\ .format(training_db.audio_id, training_id) TrainingsDBManager().append_verdict(training_id, verdict) TrainingsDBManager().set_score(training_id, 0) logger.warning(verdict) continue audio = Audio.from_json_file(audio_file) audio_file.close() presentation_file = DBManager().get_file(training_db.presentation_id) if presentation_file is None: TrainingsDBManager().change_training_status_by_training_id( training_id, TrainingStatus.PROCESSING_FAILED ) verdict = 'Presentation file with presentation_id = {}, training_id = {} was not found.'\ .format(training_db.presentation_id, training_id) TrainingsDBManager().append_verdict(training_id, verdict) TrainingsDBManager().set_score(training_id, 0) logger.warning(verdict) continue presentation = Presentation.from_json_file(presentation_file) presentation_file.close() criteria_pack_id = training_db.criteria_pack_id criteria_pack = CriteriaPackFactory().get_criteria_pack(criteria_pack_id) criteria_pack_db = CriterionPackDBManager().get_criterion_pack_by_name(criteria_pack.name) feedback_evaluator_id = training_db.feedback_evaluator_id feedback_evaluator = FeedbackEvaluatorFactory().get_feedback_evaluator(feedback_evaluator_id)(criteria_pack_db.criterion_weights) training = Training(training_id, audio, presentation, criteria_pack, feedback_evaluator) try: feedback = training.evaluate_feedback() except Exception as e: TrainingsDBManager().change_training_status_by_training_id( training_id, TrainingStatus.PROCESSING_FAILED ) verdict = 'Feedback evaluation for a training with training_id = {} has failed.\n{}'\ .format(training_id, e) TrainingsDBManager().append_verdict(training_id, verdict) TrainingsDBManager().set_score(training_id, 0) logger.warning(verdict) continue TrainingsDBManager().set_score(training_id, feedback.score) TrainingsDBManager().change_training_status_by_training_id(training_id, PresentationStatus.PROCESSED) task_attempt_id = training_db.task_attempt_id TaskAttemptsDBManager().update_scores(task_attempt_id, training_id, feedback.score) except Exception as e: logger.error('Unknown exception.\n{}'.format(e))
class AppController: # Audio Settings audio_channels_in = 6 audio_channels_out = 2 audio_device_in = 0 audio_device_out = 0 audio_samplerate = 44100 audio_buffersize = 1024 def __init__(self, view): # Initialize App Window self.view = view(self) # AppView(self) self.initial_view() # Initialize Audio self.audio = Audio(self, self.audio_device_in, self.audio_device_out, self.audio_samplerate, self.audio_buffersize) self.audio.start() def run(self): self.view.run() def initial_view(self): self.view.update_state('none') levels = {'input': [], 'output': []} max_levels = {'input': [], 'output': []} for i in range(0, self.audio_channels_in): levels['input'].append( float(i + 1) / float(self.audio_channels_in + 1)) max_levels['input'].append( float(i + 1) / float(self.audio_channels_in)) for i in range(0, self.audio_channels_out): levels['output'].append( float(i + 1) / float(self.audio_channels_out + 1)) max_levels['output'].append( float(i + 1) / float(self.audio_channels_out)) self.view.update_levels(levels, max_levels) # Audio Wrapper Methods def play(self): return self.audio.play() def pause(self): return self.audio.pause() def stop(self): return self.audio.stop() def record(self): return self.audio.record() # Audio Events def state_update(self, state): self.view.update_state(state) def audio_update(self, avg_levels, max_levels): self.view.update_levels(avg_levels, max_levels) # Window Methods def update(self): self.view.update() def destroy(self): # Deinitialize Audio self.audio.destroy() self.view.destroy()
def get_training_statistics(training_id: str) -> (dict, int): """ Endpoint to get statistics of a training by its identifier :param training_id: Training identifier :return: Dictionary with statistics of the training with the given identifier, or a dictionary with an explanation and 404 HTTP return code if something went wrong, or an empty dictionary with 404 HTTP return code if the file was not found or access was denied. """ if not check_access({'_id': ObjectId(training_id)}): return {}, 404 training_db = TrainingsDBManager().get_training(training_id) presentation_file_id = training_db.presentation_file_id presentation_file_name = DBManager().get_file_name(presentation_file_id) if presentation_file_name is None: return { 'message': 'No presentation file with presentation_file_id = {}.'.format( presentation_file_id) }, 404 presentation_record_file_id = training_db.presentation_record_file_id training_status = training_db.status audio_status = training_db.audio_status presentation_status = training_db.presentation_status slides_time = [] if audio_status == AudioStatus.PROCESSED: # here we need to process audio_slides audio = Audio.from_json_file(DBManager().get_file( training_db.audio_id)) slides_time = proccess_training_slides_info(audio) feedback = training_db.feedback criteria_pack_id = training_db.criteria_pack_id feedback_evaluator_id = training_db.feedback_evaluator_id remaining_processing_time_estimation, remaining_processing_time_estimation_code = \ get_remaining_processing_time_by_training_id(training_id) if remaining_processing_time_estimation['message'] != 'OK': return remaining_processing_time_estimation, remaining_processing_time_estimation_code return { 'message': 'OK', 'presentation_file_id': str(presentation_file_id), 'presentation_file_name': presentation_file_name, 'presentation_record_file_id': str(presentation_record_file_id), 'feedback': feedback, 'training_status': training_status, 'audio_status': audio_status, 'presentation_status': presentation_status, 'slides_time': slides_time, 'remaining_processing_time_estimation': remaining_processing_time_estimation['processing_time_remaining'], 'criteria_pack_id': criteria_pack_id, 'feedback_evaluator_id': feedback_evaluator_id, }, 200
from app.recognized_audio import RecognizedAudio TRAINING_ID = ObjectId('60789142b542f0da21e68805') AUDIO_ID = ObjectId('60789430cc5ecfdb1b92f53e') PRESENTATION_FILE_ID = ObjectId('6078dba93f6fba333aa420d4') PRESENTATION_RECORD_FILE_ID = ObjectId('6078ca0e375721468bb08a14') PREVIEW_ID = ObjectId('6079618b8025b6f4c8e22083') TRAINING_WITH_AUDIO_ID = Mock(audio_id=AUDIO_ID) TRAINING_WITH_PRESENTATION_FILE_ID = Mock( presentation_file_id=PRESENTATION_FILE_ID) TIMESTAMP = 1620046125.832949 with open('test_data/recognized_audio_6.json', 'rb') as json_file: ts = TIMESTAMP AUDIO = Audio( RecognizedAudio.from_json_file(json_file), slide_switch_timestamps=[ ts, ts + 10, ts + 20, ts + 100, ts + 250, ts + 340 ], ) class ListHandler(logging.Handler): def __init__(self): super().__init__() self.log_entries = [] def emit(self, record): self.log_entries.append(record) def get_mock_logger(): mock_logger = logging.getLogger('mock_logger')
] k = 0 recognized_words = [] for word in words: new_k = k + round(random.random() * 2, 2) recognized_word = RecognizedWord(word=Word(value=word), begin_timestamp=k, end_timestamp=new_k, probability=round(random.random(), 2)) k = new_k + round(random.random(), 2) recognized_words.append(recognized_word) print(k) recognized_audio = RecognizedAudio(recognized_words) with open('/test_data/recognized_audio_0.json', 'w') as f: f.write(repr(recognized_audio)) ssts = [0] ts = time.time() n = random.randint(1, 20) print(n) t = 0 while True: t += random.random() / 3 if t >= 1: t = 1 ssts.append(k * t) if t >= 1: break audio = Audio(recognized_audio, ssts) with open('/test_data/test_audio_1.json', 'w') as f: f.write(repr(audio))