forked from SkylarWiderski/Pepper_Chat_GPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpt.py
executable file
·74 lines (63 loc) · 2 KB
/
gpt.py
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
from lwe import ApiBackend
from livewhisper import StreamHandler
import os
bot = ApiBackend()
handler = StreamHandler()
import paramiko
def put_file(machinename, username, password, dirname, filename, data):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(machinename, username=username, password=password)
sftp = ssh.open_sftp()
try:
sftp.mkdir(dirname)
except IOError:
pass
f = sftp.open(dirname + '/' + filename, 'w')
f.write(data)
f.close()
ssh.close()
#get the question in text
def get_question():
try:
question = handler.listen()
except (KeyboardInterrupt, SystemExit): pass
finally:
print("\n\033[93mLoading response...\033[0m")
if os.path.exists('dictate.wav'): os.remove('dictate.wav')
# import whisper
# model = whisper.load_model("base")
# result = model.transcribe("sound-question.mp3 or .wav")
# with open("transcription.txt", "w") as file:
# question = file.write(result["text"])
#return "What is the capital of france?"
return question
#send the question to chat gpt and get the answer
def ask_question(question):
success, response, message = bot.ask(question)
if success:
print(response)
else:
print("No response")
raise RuntimeError(message)
return response
#send answer to pepeper
def output_response(response):
print("\n\033[93mSending to Pepper..\033[0m")
put_file("192.168.0.110", "nao", "cogvis", "/home/nao/", "stuff.txt", response)
#with open("stuff.txt", "w") as file:
# file.write(response)
print("\n\033[93mResponse Sent..\033[0m")
old_response = "hi"
try:
while True:
question = get_question() #always get question in text
response = ask_question(question) #store chat gpts answer
if response != old_response:
old_response = response
output_response(response) #send response
except (KeyboardInterrupt, SystemExit): pass
finally:
print("\n\033[93mQuitting...\033[0m")
if os.path.exists('dictate.wav'): os.remove('dictate.wav')
output_response("Goodbye")