slashdemonofthesword

Python and IBM Cloud Watson School Assignment

May 5th, 2018
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.79 KB | None | 0 0
  1.  
  2. from ws4py.client.threadedclient import WebSocketClient
  3. import base64, json, ssl, subprocess, threading, time
  4. from requests.auth import HTTPBasicAuth
  5. import requests
  6. import webbrowser
  7.  
  8. import subprocess
  9.  
  10. # These are from the Audio Example
  11. import pyaudio
  12. import time
  13. import wave
  14.  
  15. # open stream
  16. FORMAT = pyaudio.paInt16
  17. CHANNELS = 1
  18. RATE = 16000
  19. RATE = 48000
  20. CHUNK = 24000  # was 2048, made this 2000 to divide evenly into 16000, smoothed out the playback
  21.  
  22. # https://2001archive.org/
  23. wf_greeting = wave.open('Greeting.wav', 'rb')
  24. wf_goodbye =  wave.open('goodbye.wav', 'rb')
  25. wf_ignore = wave.open('sure.wav','rb')
  26.  
  27.  
  28. '''
  29. Port Audio seems to have some known issues.
  30. '''
  31.  
  32.  
  33. def play_uncompressed_wave(wave_object):
  34.     # define callback (2)
  35.     def callback(in_data, frame_count, time_info, status):
  36.         data = wave_object.readframes(frame_count)
  37.         return data, pyaudio.paContinue
  38.  
  39.     # instantiate PyAudio (1)
  40.     p_out = pyaudio.PyAudio()
  41.     stream_out = p_out.open(format=p_out.get_format_from_width(wave_object.getsampwidth()),
  42.                             channels=wave_object.getnchannels(),
  43.                             rate=wave_object.getframerate(),
  44.                             output=True,
  45.                             stream_callback=callback)
  46.  
  47.     # start the stream (4)
  48.     stream_out.start_stream()
  49.  
  50.     # wait for stream to finish (5)
  51.     while stream_out.is_active():
  52.         time.sleep(0.1)
  53.  
  54.     # stop stream (6)
  55.     stream_out.stop_stream()
  56.     stream_out.close()
  57.     wave_object.close()
  58.     p_out.terminate()
  59.     pass
  60.  
  61.  
  62. class SpeechToTextClient(WebSocketClient):
  63.     def __init__(self):
  64.         ws_url = "wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize"
  65.  
  66.         username = "1ec0732b-b3eb-4dad-b422-499d318f4f3f"
  67.         password = "6FU8j4Vsxnnu"
  68.  
  69.         authstring = "{0}:{1}".format(username, password)
  70.         base64string = base64.b64encode(authstring.encode('utf-8')).decode('utf-8')
  71.  
  72.         self.Command_State = None
  73.         self.listening = False
  74.         self.empty_count = 0
  75.         self.Gathered_String = ''
  76.         self.stream_audio_thread = threading.Thread(target=self.stream_audio)
  77.         try:
  78.             WebSocketClient.__init__(self, ws_url,
  79.                                      headers=[("Authorization", "Basic %s" % base64string)])
  80.             self.connect()
  81.         except:
  82.             print("Failed to open WebSocket.")
  83.  
  84.     def opened(self):
  85.         #self.send('{"action": "start", "content-type": "audio/l16;rate=44100;channels=1" }')
  86.         data = {"action": "start",
  87.                 "content-type": "audio/l16;rate=44100;channels=1",
  88.                 'max_alternatives': 3,
  89.                 "keywords": ["Watson","quit"],
  90.                 "keywordsThreshold":0.5,
  91.                 'timestamps': True,
  92.                 'word_confidence': True}
  93.  
  94.         print("sendMessage(init)")
  95.         # send the initialization parameters
  96.         print(json.dumps(data).encode('utf8'))
  97.         self.send(json.dumps(data).encode('utf8'))
  98.         self.stream_audio_thread.start()
  99.  
  100.     def received_message(self, message):
  101.  
  102.         message = json.loads(str(message))
  103.  
  104.         if "state" in message:
  105.             if message["state"] == "listening" and self.Command_State is None:
  106.                 play_uncompressed_wave(wf_greeting)
  107.                 self.listening = True
  108.         if "results" in message:
  109.             print(self.Command_State)
  110.             if message["results"]:
  111.                 x = message['results']
  112.                 print(x)
  113.                 if x[0]['alternatives'][0]['transcript'] == 'Watson ' and self.Command_State is None:
  114.                     print("found a command")
  115.                     self.Command_State = 'Started'
  116.                 if x[0]['alternatives'][0]['transcript'] == 'ignore ' and self.Command_State is 'Started':
  117.                     play_uncompressed_wave(wf_ignore)
  118.                     self.Command_State = None
  119.                     self.listening = True
  120.                 if x[0]['alternatives'][0]['transcript'] == 'go ' and self.Command_State is 'Started':
  121.                     self.Command_State = 'Gather'
  122.                     self.Gathered_String = ''
  123.                     self.listening = True
  124.                     self.empty_count = 0
  125.  
  126.                 if x[0]['alternatives'][0]['transcript'] == 'exit ' and self.Command_State is 'Started':
  127.                     self.listening = False
  128.                     play_uncompressed_wave(wf_goodbye)
  129.                     self.Command_State = 'Exit'
  130.                     self.stream_audio_thread.join()
  131.  
  132.                 if x[0]['alternatives'][0]['transcript'] == 'open ' and self.Command_State is 'Started':
  133.                     self.Command_State = None
  134.                     subprocess.Popen("explorer.exe")
  135.                     self.listening = True
  136.  
  137.                 if self.Command_State == 'Gather':
  138.                     self.Gathered_String = self.Gathered_String + x[0]['alternatives'][0]['transcript']
  139.  
  140.  
  141.             else:
  142.                 if self.Command_State == 'Gather':
  143.                     self.empty_count = self.empty_count + 1
  144.                     if self.empty_count >= 3:
  145.                         self.Command_State = None
  146.                         self.listening = True
  147.  
  148.                         # HERE IS WHERE YOU WILL SEND THE Gathered_String
  149.  
  150.                         headers = {'content-type': 'text/plain'}
  151.                         feedback = {'content-type': 'audio/wav'}
  152.  
  153.                         response = requests.get('https://gateway.watsonplatform.net/language-translator/api/v2/translate?model_id=en-fr&text={0}'.format(self.Gathered_String), headers=headers, auth=HTTPBasicAuth('b6250682-46ad-4d0d-b72c-2e441232bc98', '2HDFBLJet2TK'))
  154.                         voiceResponse = requests.get('https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?accept=audio/wav&text={0}&voice=fr-FR_ReneeVoice'.format(response.text), headers=feedback, auth=HTTPBasicAuth('e7c96ad4-95f0-4239-8fc9-1a025a54d8a7', '5LTLgMflcJBN'))
  155.  
  156.                         with open('audio.wav', 'wb') as f:
  157.                             f.write(voiceResponse.content)
  158.  
  159.  
  160.                         audio = wave.open('audio.wav', 'rb')
  161.  
  162.                         play_uncompressed_wave(audio)
  163.                         # webbrowser.open(voiceResponse)
  164.  
  165.                         # print("Results")
  166.                         # print(response.text)
  167.                         self.empty_count = 0
  168.  
  169.  
  170.     def stream_audio(self):
  171.         print("Waiting for Watson")
  172.         while not self.listening:
  173.             time.sleep(0.1)
  174.         print("Hello Watson")
  175.         p_in = pyaudio.PyAudio()
  176.         iChunk = 4410
  177.         iSec = 1
  178.         stream_in = p_in.open(format=pyaudio.paInt16, channels=1, rate=44100, input=True, frames_per_buffer=iChunk)
  179.  
  180.         while self.listening:
  181.             for i in range(0, int(44100 / iChunk * iSec)):
  182.                 data = stream_in.read(CHUNK, exception_on_overflow=False)
  183.                 if data:
  184.                     try:
  185.                         self.send(bytearray(data), binary=True)
  186.                     except ssl.SSLError:
  187.                         pass
  188.                     except ConnectionAbortedError:
  189.                         pass
  190.             if self.listening:
  191.                 try:
  192.                     self.send('{"action": "stop"}')
  193.                 except ssl.SSLError:
  194.                     pass
  195.                 except ConnectionAbortedError:
  196.                     pass
  197.             time.sleep(0.5)
  198.  
  199.         stream_in.stop_stream()
  200.         stream_in.close()
  201.         p_in.terminate()
  202.         self.close()
  203.  
  204.     def close(self,code=1000, reason=''):
  205.  
  206.         self.listening = False
  207.         WebSocketClient.close(self)
  208.  
  209.  
  210. if __name__ == "__main__":
  211.  
  212.     stt_client = SpeechToTextClient()
  213.  
  214.     while not stt_client.terminated:
  215.         pass
Add Comment
Please, Sign In to add comment