Пример #1
0
def test_should_throw_language_not_implemented_error_on_handle():
    lang = Language(value='mr')
    request = SpeechRecognitionRequest(config=RecognitionConfig(language=lang))
    with pytest.raises(NotImplementedError) as e:
        handle_request(request)

    assert e.value.args[0] == 'Language not implemented yet'
Пример #2
0
def test_should_throw_transcript_not_implemented_error_on_handle():
    request = SpeechRecognitionRequest(
        config=RecognitionConfig(transcriptionFormat='ALTERNATIVES'),
        audio=RecognitionAudio(audioUri='www.abc.com/demo.wav'))
    with pytest.raises(NotImplementedError) as e:
        handle_request(request)

    assert e.value.args[0] == 'Transcription Format not implemented yet'
Пример #3
0
def test_should_throw_fileid_is_not_implemented_error_on_handle():
    request = SpeechRecognitionRequest(
        config=RecognitionConfig(audioFormat='MP3'),
        audio=RecognitionAudio(fileId='123'))
    with pytest.raises(NotImplementedError) as e:
        handle_request(request)

    assert e.value.args[0] == 'fileId is not implemented yet'
Пример #4
0
def test_should_throw_empty_audio_source_is_not_implemented_error_on_handle():
    request = SpeechRecognitionRequest(config=RecognitionConfig(
        audioFormat='MP3'))
    with pytest.raises(NotImplementedError) as e:
        handle_request(request)

    assert e.value.args[
        0] == 'empty audio source is not implemented yet, send valid attributes only for audioUri or audioContent'
Пример #5
0
def test_should_throw_audio_and_transcript_format_not_implemented_error_on_handle_ogg_srt(
):
    request = SpeechRecognitionRequest(
        config=RecognitionConfig(audioFormat='OGG', transcriptionFormat='SRT'),
        audio=RecognitionAudio(audioUri='www.abc.com/demo.wav'))
    with pytest.raises(NotImplementedError) as e:
        handle_request(request)

    assert e.value.args[0] == 'Audio Format not implemented yet'
Пример #6
0
 def recognize(self, request, context):
     try:
         handle_request(request)
     except NotImplementedError as e:
         context.set_details(str(e))
         context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
         return SpeechRecognitionResult(status='ERROR', status_text=str(e))
     punctuate = request.config.enableAutomaticPunctuation
     itn = request.config.enableInverseTextNormalization
     language = Language.LanguageCode.Name(request.config.language.value)
     audio_format = RecognitionConfig.AudioFormat.Name(
         request.config.audioFormat)
     out_format = RecognitionConfig.TranscriptionFormat.Name(
         request.config.transcriptionFormat)
     file_name = 'audio_input_{}.{}'.format(
         str(get_current_time_in_millis()), audio_format.lower())
     try:
         if len(request.audio.audioUri) != 0:
             audio_path = download_from_url_to_file(file_name,
                                                    request.audio.audioUri)
         elif len(request.audio.audioContent) != 0:
             audio_path = create_wav_file_using_bytes(
                 file_name, request.audio.audioContent)
         if out_format == 'SRT':
             response = self.model_service.get_srt(audio_path, language,
                                                   punctuate, itn)
             result = SpeechRecognitionResult(status='SUCCESS',
                                              srt=response['srt'])
         else:
             response = self.model_service.transcribe(
                 audio_path, language, punctuate, itn)
             result = SpeechRecognitionResult(
                 status='SUCCESS', transcript=response['transcription'])
         os.remove(audio_path)
         return result
     except requests.exceptions.RequestException as e:
         context.set_details(str(e))
         context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
         return SpeechRecognitionResult(status='ERROR', status_text=str(e))
     except Exception as e:
         context.set_details(
             "An unknown error has occurred.Please try again.")
         context.set_code(grpc.StatusCode.UNKNOWN)
         return SpeechRecognitionResult(
             status='ERROR',
             status_text="An unknown error has occurred.Please try again.")
Пример #7
0
def test_should_not_throw_audio_and_transcript_format_not_implemented_error_on_handle_mp3_srt(
):
    request = SpeechRecognitionRequest(
        config=RecognitionConfig(audioFormat='MP3', transcriptionFormat='SRT'),
        audio=RecognitionAudio(audioUri='www.abc.com/demo.wav'))
    handle_request(request)
Пример #8
0
def test_should_not_throw_audio_format_not_implemented_error_on_handle_wav():
    request = SpeechRecognitionRequest(
        config=RecognitionConfig(audioFormat='WAV'),
        audio=RecognitionAudio(audioUri='www.abc.com/demo.wav'))
    handle_request(request)
Пример #9
0
def test_should_not_throw_transcript_not_implemented_error_on_handle_transcript(
):
    request = SpeechRecognitionRequest(
        config=RecognitionConfig(transcriptionFormat='TRANSCRIPT'),
        audio=RecognitionAudio(audioUri='www.abc.com/demo.wav'))
    handle_request(request)
Пример #10
0
def test_should_not_throw_language_not_implemented_error_on_handle():
    lang = Language(value='en')
    request = SpeechRecognitionRequest(
        config=RecognitionConfig(language=lang),
        audio=RecognitionAudio(audioUri='www.abc.com/demo.wav'))
    handle_request(request)