raygiles3 commited on
Commit
bf1aafe
·
verified ·
1 Parent(s): f61c042

Update worker.py

Browse files
Files changed (1) hide show
  1. worker.py +62 -3
worker.py CHANGED
@@ -4,13 +4,72 @@ import requests
4
  openai_client = OpenAI()
5
 
6
 
 
7
  def speech_to_text(audio_binary):
8
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
 
11
  def text_to_speech(text, voice=""):
12
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
 
15
  def openai_process_message(user_message):
16
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  openai_client = OpenAI()
5
 
6
 
7
+
8
  def speech_to_text(audio_binary):
9
+
10
+ # Set up Watson Speech-to-Text HTTP Api url
11
+ base_url = 'https://sn-watson-stt.labs.skills.network'
12
+ api_url = base_url+'/speech-to-text/api/v1/recognize'
13
+
14
+ # Set up parameters for our HTTP reqeust
15
+ params = {
16
+ 'model': 'en-US_Multimedia',
17
+ }
18
+
19
+ # Set up the body of our HTTP request
20
+ body = audio_binary
21
+
22
+ # Send a HTTP Post request
23
+ response = requests.post(api_url, params=params, data=audio_binary).json()
24
+
25
+ # Parse the response to get our transcribed text
26
+ text = 'null'
27
+ while bool(response.get('results')):
28
+ print('speech to text response:', response)
29
+ text = response.get('results').pop().get('alternatives').pop().get('transcript')
30
+ print('recognised text: ', text)
31
+ return text
32
 
33
 
34
  def text_to_speech(text, voice=""):
35
+ # Set up Watson Text-to-Speech HTTP Api url
36
+ base_url = 'https://sn-watson-tts.labs.skills.network'
37
+ api_url = base_url + '/text-to-speech/api/v1/synthesize?output=output_text.wav'
38
+
39
+ # Adding voice parameter in api_url if the user has selected a preferred voice
40
+ if voice != "" and voice != "default":
41
+ api_url += "&voice=" + voice
42
+
43
+ # Set the headers for our HTTP request
44
+ headers = {
45
+ 'Accept': 'audio/wav',
46
+ 'Content-Type': 'application/json',
47
+ }
48
+
49
+ # Set the body of our HTTP request
50
+ json_data = {
51
+ 'text': text,
52
+ }
53
+
54
+ # Send a HTTP Post request to Watson Text-to-Speech Service
55
+ response = requests.post(api_url, headers=headers, json=json_data)
56
+ print('text to speech response:', response)
57
+ return response.content
58
 
59
 
60
  def openai_process_message(user_message):
61
+ # Set the prompt for OpenAI Api
62
+ prompt = "Act like a personal assistant. You can respond to questions, translate sentences, summarize news, and give recommendations."
63
+ # Call the OpenAI Api to process our prompt
64
+ openai_response = openai_client.chat.completions.create(
65
+ model="gpt-3.5-turbo",
66
+ messages=[
67
+ {"role": "system", "content": prompt},
68
+ {"role": "user", "content": user_message}
69
+ ],
70
+ max_tokens=4000
71
+ )
72
+ print("openai response:", openai_response)
73
+ # Parse the response to get the response message for our prompt
74
+ response_text = openai_response.choices[0].message.content
75
+ return response_text