def test_success(): voices_url = 'https://stream.watsonplatform.net/text-to-speech/api/v1/voices' voices_response = '{"voices": [{"url": "https://stream.watsonplatform.net/text-to-speech/api/v1/voices/' \ 'VoiceEnUsLisa", "gender": "female", "name": "VoiceEnUsLisa", "language": "en-US"}, {"url": ' \ '"https://stream.watsonplatform.net/text-to-speech/api/v1/voices/VoiceEsEsEnrique", ' \ '"gender": "male", "name": "VoiceEsEsEnrique", "language": "es-ES"}, {"url": ' \ '"https://stream.watsonplatform.net/text-to-speech/api/v1/voices/VoiceEnUsMichael", ' \ '"gender": "male", "name": "VoiceEnUsMichael", "language": "en-US"}, {"url": ' \ '"https://stream.watsonplatform.net/text-to-speech/api/v1/voices/VoiceEnUsAllison", ' \ '"gender": "female", "name": "VoiceEnUsAllison", "language": "en-US"}]}' responses.add(responses.GET, voices_url, body=voices_response, status=200, content_type='application/json') text_to_speech = watson_developer_cloud.TextToSpeechV1( username="******", password="******") text_to_speech.voices() assert responses.calls[0].request.url == voices_url assert responses.calls[0].response.text == voices_response synthesize_text = 'hello' synthesize_url = 'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize' synthesize_response_body = '<binary response>' responses.add(responses.POST, synthesize_url, body=synthesize_response_body, status=200, content_type='application/json', match_querystring=True) text_to_speech.synthesize(synthesize_text) assert responses.calls[1].request.url == synthesize_url assert responses.calls[1].response.text == synthesize_response_body assert len(responses.calls) == 2
def text_to_speech(text): tts = watson_developer_cloud.TextToSpeechV1(username=USERNAME,password=PASSWORD) #text = 'Hi! This is a reminder from your doctor. Please take your medicine now' with open(join(dirname(__file__), AUDIO_FILE), 'wb') as audio_file: audio_file.write( tts.synthesize(text, accept='audio/wav',voice="en-US_AllisonVoice").content)
def test_customization_words(): base_url = 'https://stream.watsonplatform.net/text-to-speech/api/v1/customizations' responses.add(responses.GET, "{0}/{1}/words".format(base_url, "custid"), body='{"customizations": "yep" }', status=200, content_type='application_json'); responses.add(responses.POST, "{0}/{1}/words".format(base_url, "custid"), body='{"customizations": "yep" }', status=200, content_type='application_json'); responses.add(responses.GET, "{0}/{1}/words/{2}".format(base_url, "custid","word"), body='{"customization": "yep, just one" }', status=200, content_type='application_json'); responses.add(responses.POST, "{0}/{1}/words/{2}".format(base_url, "custid","word"), body='{"customizations": "yep" }', status=200, content_type='application_json'); responses.add(responses.PUT, "{0}/{1}/words/{2}".format(base_url, "custid","word"), body='{"customizations": "yep" }', status=200, content_type='application_json'); responses.add(responses.DELETE, "{0}/{1}/words/{2}".format(base_url, "custid","word"), body='{"customizations": "yep" }', status=200, content_type='application_json'); text_to_speech = watson_developer_cloud.TextToSpeechV1(username="******", password="******") text_to_speech.get_customization_words(customization_id="custid") text_to_speech.add_customization_words(customization_id="custid", words=["one", "two", "three"]) text_to_speech.get_customization_word(customization_id="custid", word="word") text_to_speech.set_customization_word(customization_id='custid', word="word", translation="I'm translated") text_to_speech.delete_customization_word(customization_id="custid", word="word") assert len(responses.calls) == 5
def test_customizations(): responses.add(responses.GET, 'https://stream.watsonplatform.net/text-to-speech/api/v1/customizations', body='{"customizations": "yep" }', status=200, content_type='application_json'); responses.add(responses.POST, 'https://stream.watsonplatform.net/text-to-speech/api/v1/customizations', body='{"customizations": "yep" }', status=200, content_type='application_json'); responses.add(responses.GET, 'https://stream.watsonplatform.net/text-to-speech/api/v1/customizations/custid', body='{"customization": "yep, just one" }', status=200, content_type='application_json'); responses.add(responses.POST, 'https://stream.watsonplatform.net/text-to-speech/api/v1/customizations/custid', body='{"customizations": "yep" }', status=200, content_type='application_json'); responses.add(responses.DELETE, 'https://stream.watsonplatform.net/text-to-speech/api/v1/customizations/custid', body='{"customizations": "yep" }', status=200, content_type='application_json'); text_to_speech = watson_developer_cloud.TextToSpeechV1( username="******", password="******") text_to_speech.customizations() text_to_speech.customizations(language="en-US") assert len(responses.calls) == 2 text_to_speech.create_customization(name="name", description="description") text_to_speech.get_customization(customization_id='custid') text_to_speech.update_customization(customization_id="custid", name="name", description="description") text_to_speech.delete_customization(customization_id="custid") assert len(responses.calls) == 6
def setUp(self): self.text_to_speech = watson_developer_cloud.TextToSpeechV1( username=os.getenv('TEXT_TO_SPEECH_USERNAME'), password=os.getenv('TEXT_TO_SPEECH_PASSWORD')) self.original_customizations = self.text_to_speech.customizations() self.created_customization = self.text_to_speech.create_customization( name="test_integration_customization", description="customization for tests")
def setUp(self): self.text_to_speech = watson_developer_cloud.TextToSpeechV1( username="******", password="******") self.text_to_speech.set_default_headers({ 'X-Watson-Learning-Opt-Out': '1', 'X-Watson-Test': '1' }) self.original_customizations = self.text_to_speech.list_voice_models() self.created_customization = self.text_to_speech.create_voice_model( name="test_integration_customization", description="customization for tests")
def test_delete_user_data(): url = 'https://stream.watsonplatform.net/text-to-speech/api/v1/user_data' responses.add(responses.DELETE, url, body='{"description": "success" }', status=204, content_type='application_json') text_to_speech = watson_developer_cloud.TextToSpeechV1(username="******", password="******") response = text_to_speech.delete_user_data('id').get_result() assert response is None assert len(responses.calls) == 1
def test_pronounciation(): responses.add(responses.GET, 'https://stream.watsonplatform.net/text-to-speech/api/v1/pronunciation', body='{"pronunciation": "pronunciation info" }', status=200, content_type='application_json'); text_to_speech = watson_developer_cloud.TextToSpeechV1( username="******", password="******") text_to_speech.pronunciation(text="this is some text") text_to_speech.pronunciation(text="yo", voice="VoiceEnUsLisa") text_to_speech.pronunciation(text="yo", voice="VoiceEnUsLisa", pronunciation_format='ipa') assert len(responses.calls) == 3
def authenticate( self, api, user, pswd, ): # Watson 音声認識 if (api == 'stt'): try: self.stt = watson.SpeechToTextV1( url='https://stream.watsonplatform.net/speech-to-text/api', username=user, password=pswd, ) return True except: pass # Watson 翻訳機能 if (api == 'tra'): try: self.tra = watson.LanguageTranslatorV3( version='2018-05-01', url= 'https://gateway.watsonplatform.net/language-translator/api', username=user, password=pswd, ) return True except: pass # Watson 音声合成 if (api == 'tts'): try: self.tts = watson.TextToSpeechV1( url='https://stream.watsonplatform.net/text-to-speech/api', username=user, password=pswd, ) return True except: pass return False
def say(text): text_to_speech = wat.TextToSpeechV1( username='******', password='******') with open(TTS.TEMP_FILE, 'wb') as audio_file: audio_file.write( text_to_speech.synthesize(text, accept='audio/wav', voice="en-US_AllisonVoice").content) chunk = 1024 #open a wav format music f = wave.open(TTS.TEMP_FILE, "rb") #instantiate PyAudio p = pyaudio.PyAudio() #open stream stream = p.open(format=p.get_format_from_width(f.getsampwidth()), channels=f.getnchannels(), rate=f.getframerate(), output=True) #read data data = f.readframes(chunk) #play stream while data: stream.write(data) data = f.readframes(chunk) #stop stream stream.stop_stream() stream.close() #close PyAudio p.terminate() return
#Conversation Service WatsonAssistantUSERNAME = os.environ.get('CONVERSATION_USERNAME','b128ddce-d3e9-4d61-80b8-37c378eee542') WatsonAssistantPASSWORD = os.environ.get('CONVERSATION_PASSWORD','QbyfsYYu4mjF') conversation = watson_developer_cloud.ConversationV1(username=WatsonAssistantUSERNAME, password=WatsonAssistantPASSWORD, version='2017-04-21') workspace_id ='b62aea5d-cfcc-436d-8d4c-498ac4a0ddbc' workspace = conversation.get_workspace(workspace_id=workspace_id, export=True) #Text To Speech TextSpeechUSERNAME = os.environ.get('TextSpeech_USERNAME','1bd5685a-fd86-4fb2-934a-c2558dd56e03') TextSpeechPASSWORD = os.environ.get('TextSpeech_PASSWORD','Xbl5yYC6LdhX') text_to_speech = watson_developer_cloud.TextToSpeechV1(username=TextSpeechUSERNAME, password=TextSpeechPASSWORD) #Speech To Text SpeechTextUSERNAME = os.environ.get('SpeechText_USERNAME','189b1a52-38d6-43fc-9482-47cc3823a3fb') SpeechTextPASSWORD = os.environ.get('SpeechText_PASSWORD','cN4I5hR5vEm2') speech_to_text = watson_developer_cloud.SpeechToTextV1(username=SpeechTextUSERNAME, password=SpeechTextPASSWORD) # check workspace status (wait for training to complete) print('The workspace status is: {0}'.format(workspace['status'])) if workspace['status'] == 'Available': print('Ready to chat!') else: print('The workspace should be available shortly. Please try again in 30s.')
def test_success(): voices_url = 'https://stream.watsonplatform.net/text-to-speech/api/v1/voices' voices_response = { "voices": [{ "url": "https://stream.watsonplatform.net/text-to-speech/api/v1/voices/VoiceEnUsLisa", "gender": "female", "name": "VoiceEnUsLisa", "language": "en-US" }, { "url": "https://stream.watsonplatform.net/text-to-speech/api/v1/voices/VoiceEsEsEnrique", "gender": "male", "name": "VoiceEsEsEnrique", "language": "es-ES" }, { "url": "https://stream.watsonplatform.net/text-to-speech/api/v1/voices/VoiceEnUsMichael", "gender": "male", "name": "VoiceEnUsMichael", "language": "en-US" }, { "url": "https://stream.watsonplatform.net/text-to-speech/api/v1/voices/VoiceEnUsAllison", "gender": "female", "name": "VoiceEnUsAllison", "language": "en-US" }] } voice_url = 'https://stream.watsonplatform.net/text-to-speech/api/v1/voices/en-us_AllisonVoice' voice_response = { "url": "https://stream.watsonplatform.net/text-to-speech/api/v1/voices/en-US_AllisonVoice", "name": "en-US_AllisonVoice", "language": "en-US", "customizable": True, "gender": "female", "description": "Allison: American English female voice.", "supported_features": { "custom_pronunciation": True, "voice_transformation": True } } synthesize_url = 'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize' synthesize_response_body = '<binary response>' responses.add(responses.GET, voices_url, body=json.dumps(voices_response), status=200, content_type='application/json') responses.add(responses.GET, voice_url, body=json.dumps(voice_response), status=200, content_type='application/json') responses.add(responses.POST, synthesize_url, body=synthesize_response_body, status=200, content_type='application/json', match_querystring=True) text_to_speech = watson_developer_cloud.TextToSpeechV1(username="******", password="******") text_to_speech.list_voices() assert responses.calls[0].request.url == voices_url assert responses.calls[0].response.text == json.dumps(voices_response) text_to_speech.get_voice('en-us_AllisonVoice') assert responses.calls[1].request.url == voice_url assert responses.calls[1].response.text == json.dumps(voice_response) text_to_speech.synthesize('hello') assert responses.calls[2].request.url == synthesize_url assert responses.calls[2].response.text == synthesize_response_body assert len(responses.calls) == 3