Skip to content

Commit e6ba222

Browse files
Add files via upload
1 parent b385498 commit e6ba222

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

listen.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import naoqi
2+
import subprocess
3+
import os
4+
import json
5+
import select
6+
7+
myBroker = naoqi.ALBroker("myBroker", "0.0.0.0", 0, "10.1.32.201", 9559)
8+
AD = naoqi.ALProxy("ALAudioDevice")
9+
ATTS = naoqi.ALProxy("ALAnimatedSpeech")
10+
openai_api_key = os.environ["API_KEY"]
11+
12+
def record(output_file):
13+
AD.startMicrophonesRecording(output_file)
14+
while True:
15+
instruction = raw_input("When finished talking, enter 'q': ")
16+
if instruction.lower() == "q":
17+
break
18+
AD.stopMicrophonesRecording()
19+
20+
21+
def hear(output_file):
22+
url = "https://api.openai.com/v1/audio/transcriptions"
23+
file_path = output_file
24+
model = "whisper-1"
25+
language = "en"
26+
27+
curl_command = [
28+
"curl",
29+
url,
30+
"-H", "Authorization: Bearer {}".format(openai_api_key),
31+
"-H", "Content-Type: multipart/form-data",
32+
"-F", "file=@{}".format(file_path),
33+
"-F", "model={}".format(model),
34+
"-F", "language={}".format(language)
35+
]
36+
37+
38+
response = subprocess.check_output(curl_command, stderr=subprocess.STDOUT)
39+
json_response = json.loads(response.splitlines()[-1])
40+
text_value = json_response.get("text", "")
41+
print(response)
42+
return response
43+
44+
def reply(question):
45+
url = "https://api.openai.com/v1/chat/completions"
46+
47+
json_payload = {
48+
"model": "gpt-3.5-turbo",
49+
"messages": [
50+
{"role": "system", "content": "You are a helpful cute shopping assistant named Pepper."},
51+
{"role": "user", "content": question}
52+
],
53+
"temperature": 0.3,
54+
"stream": True
55+
}
56+
57+
with open("request_payload.json", "w") as json_file:
58+
json.dump(json_payload, json_file)
59+
60+
curl_command = [
61+
"curl",
62+
url,
63+
"-H", "Content-Type: application/json",
64+
"-H", "Authorization: Bearer {}".format(os.environ["API_KEY"]),
65+
"--data-binary", "@request_payload.json"
66+
]
67+
68+
process = subprocess.Popen(curl_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1, universal_newlines=True)
69+
70+
dialogue = ""
71+
word_count = 0
72+
73+
while process.poll() is None:
74+
ready_to_read, _, _ = select.select([process.stdout], [], [], 0.1)
75+
if ready_to_read:
76+
for line in process.stdout:
77+
if line.startswith("data: "):
78+
try:
79+
json_content = json.loads(line[6:])
80+
content = json_content.get("choices", [{}])[0].get("delta", {}).get("content", "").encode('ascii', 'ignore')
81+
82+
if any(char.isalnum() for char in content) or word_count == 10:
83+
dialogue += content
84+
word_count += 1
85+
else:
86+
print(dialogue)
87+
speak(dialogue)
88+
dialogue = ""
89+
word_count = 0
90+
except ValueError:
91+
print("[DONE]")
92+
93+
# Read any remaining output after the process has finished
94+
for line in process.stdout:
95+
if line.startswith("data: "):
96+
try:
97+
json_content = json.loads(line[6:])
98+
content = json_content.get("choices", [{}])[0].get("delta", {}).get("content", "").encode('ascii', 'ignore')
99+
100+
if any(char.isalnum() for char in content) or word_count == 10:
101+
dialogue += content
102+
word_count += 1
103+
else:
104+
print(dialogue)
105+
speak(dialogue)
106+
dialogue = ""
107+
word_count = 0
108+
except ValueError:
109+
print("[DONE]")
110+
111+
# Check for errors
112+
if process.returncode != 0:
113+
print("Error:", process.stderr.read())
114+
115+
def speak(answer):
116+
ATTS.say(answer)
117+
118+
def has_text(input_text):
119+
return any(char.isalnum() for char in input_text)
120+
121+
def main():
122+
while True:
123+
instruction = raw_input("When ready to talk, please enter 's', to stop enter 'q': ")
124+
125+
if instruction.lower() == "q":
126+
break
127+
128+
if instruction.lower() == "s":
129+
output_file = "/home/nao/chat_recordings/chat.wav"
130+
record(output_file)
131+
question = hear(output_file)
132+
answer = reply(question)
133+
134+
135+
136+
137+
if __name__ == '__main__':
138+
main()

0 commit comments

Comments
 (0)