Advertisement
Guest User

Untitled

a guest
Mar 30th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. from __future__ import print_function
  2. from watson_developer_cloud import ConversationV1
  3. from watson_developer_cloud import SpeechToTextV1
  4. from watson_developer_cloud.websocket import RecognizeCallback
  5. from time import sleep
  6.  
  7. import os, sys, subprocess, threading, time, re, base64, json, ssl, signal
  8.  
  9. STTusername = os.environ['WTTSusername']
  10. STTpassword = os.environ['WTTSpassword']
  11.  
  12. #sets the environment variables
  13. os.environ['WCpassword'] = '*redacted*'
  14. os.environ['WCusername'] = '*redacted*'
  15. os.environ['WCworkspace'] = '*redacted*'
  16.  
  17.  
  18. #initalises conversation
  19. conversation = ConversationV1(
  20. username=os.environ['WCusername'],
  21. password=os.environ['WCpassword'],
  22. version='2018-02-16')
  23.  
  24. workspace_id = os.environ['WCworkspace']
  25.  
  26. speech_to_text = SpeechToTextV1(
  27. username=STTusername,
  28. password=STTpassword,
  29. url='https://stream.watsonplatform.net/speech-to-text/api')
  30.  
  31. print ("Username: " + str(STTusername))
  32. print ("Password: " + str(STTpassword))
  33.  
  34. # Example using websockets
  35. class MyRecognizeCallback(RecognizeCallback):
  36. def __init__(self):
  37. RecognizeCallback.__init__(self)
  38.  
  39. def on_transcription(self, transcript):
  40. print(transcript)
  41.  
  42. def on_connected(self):
  43. print('Connection was successful')
  44.  
  45. def on_error(self, error):
  46. print('Error received: {}'.format(error))
  47.  
  48. def on_inactivity_timeout(self, error):
  49. print('Inactivity timeout: {}'.format(error))
  50.  
  51. def on_listening(self):
  52. print('Service is listening')
  53.  
  54. def on_transcription_complete(self):
  55. print('Transcription completed')
  56. finished = True
  57.  
  58.  
  59. def on_hypothesis(self, hypothesis):
  60. print(hypothesis)
  61.  
  62. finished = False
  63. record = "arecord voicedata.wav -d 15 -f S16_LE -r 44100 -t wav"
  64. transcript = "silence"
  65. print ('speak now')
  66. mycallback = MyRecognizeCallback()
  67. p = subprocess.Popen(record, shell=True)
  68. time.sleep(10)
  69. with open('voicedata.wav') as f:
  70. speechtext = speech_to_text.recognize_with_websocket(audio=f,content_type='audio/l16; rate=44100', recognize_callback=mycallback)
  71. print ('Audio Transcribed')
  72. response = conversation.message(workspace_id=workspace_id, input={'text':transcript})
  73. results = re.search('\], u\'text\': \[u\'(.*)\'\]\}, u\'alt', str(response))
  74. answer = results
  75. speak = './tts ' + answer
  76. subprocess.call(speak, shell=True)
  77. while not finished:
  78. time.sleep(0.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement