-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextProcess.py
More file actions
105 lines (85 loc) · 2.83 KB
/
textProcess.py
File metadata and controls
105 lines (85 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import subprocess
import json
import listen
import select
openai_api_key = [x[1] for x in [x.split('=') for x in open('keys.txt').read().split()] if x[0] == 'OpenAIKey'][0]
def needHelp(question):
if u'no' in question.lower()[:3]:
return False
return True
def isWhere(question):
with open("maps/locations.json", "r") as file:
content = file.read()
locations = list(json.loads(content).keys())
formatted_question = ''
for word in question:
formatted_question += word.encode('ascii', 'ignore').lower()
if '?' in formatted_question:
formatted_question = formatted_question[:-1]
if 'where' in formatted_question.split():
for location in locations:
if location in formatted_question.split():
return location
return False
def GPTreply(question):
openai_api_key = [x[1] for x in [x.split('=') for x in open('keys.txt').read().split()] if x[0] == 'OpenAIKey'][0]
url = "https://api.openai.com/v1/chat/completions"
json_payload = {
"model": "gpt-3.5-turbo",
"messages": [
{"role": "system", "content": "You are a helpful cute shopping assistant named Pepper."},
{"role": "user", "content": question}
],
"temperature": 0.3,
"stream": True
}
with open("request_payload.json", "w") as json_file:
json.dump(json_payload, json_file)
curl_command = [
"curl",
url,
"-H", "Content-Type: application/json",
"-H", "Authorization: Bearer {}".format(openai_api_key),
"--data-binary", "@request_payload.json"
]
process = subprocess.Popen(curl_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1, universal_newlines=True)
dialogue = ""
word_count = 0
while process.poll() is None:
ready_to_read, _, _ = select.select([process.stdout], [], [], 0.1)
if ready_to_read:
for line in process.stdout:
if line.startswith("data: "):
try:
json_content = json.loads(line[6:])
content = json_content.get("choices", [{}])[0].get("delta", {}).get("content", "").encode('ascii', 'ignore')
if any(char.isalnum() for char in content) or word_count == 10:
dialogue += content
word_count += 1
else:
print(dialogue)
listen.speak(dialogue)
dialogue = ""
word_count = 0
except ValueError:
print("[DONE]")
# Read any remaining output after the process has finished
for line in process.stdout:
if line.startswith("data: "):
try:
json_content = json.loads(line[6:])
content = json_content.get("choices", [{}])[0].get("delta", {}).get("content", "").encode('ascii', 'ignore')
if any(char.isalnum() for char in content) or word_count == 10:
dialogue += content
word_count += 1
else:
print(dialogue)
listen.speak(dialogue)
dialogue = ""
word_count = 0
except ValueError:
print("[DONE]")
# Check for errors
if process.returncode != 0:
print("Error:", process.stderr.read())
return 'hi'