Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- api_key.py +1 -0
- gpt3Bot.py +44 -0
api_key.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
API_KEY =
|
gpt3Bot.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import pyttsx3
|
| 3 |
+
import speech_recognition as sr
|
| 4 |
+
from api_key import API_KEY
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
openai.api_key = API_KEY
|
| 8 |
+
|
| 9 |
+
engine = pyttsx3.init()
|
| 10 |
+
|
| 11 |
+
r = sr.Recognizer()
|
| 12 |
+
mic = sr.Microphone(device_index=1)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
conversation = ""
|
| 16 |
+
user_name = "You"
|
| 17 |
+
bot_name = "Jarvis"
|
| 18 |
+
|
| 19 |
+
while True:
|
| 20 |
+
with mic as source:
|
| 21 |
+
print("\nlistening...")
|
| 22 |
+
r.adjust_for_ambient_noise(source, duration=0.2)
|
| 23 |
+
audio = r.listen(source)
|
| 24 |
+
print("no longer listening.\n")
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
user_input = r.recognize_google(audio)
|
| 28 |
+
except:
|
| 29 |
+
continue
|
| 30 |
+
|
| 31 |
+
prompt = user_name + ": " + user_input + "\n" + bot_name+ ": "
|
| 32 |
+
|
| 33 |
+
conversation += prompt # allows for context
|
| 34 |
+
|
| 35 |
+
# fetch response from open AI api
|
| 36 |
+
response = openai.Completion.create(engine='text-davinci-003', prompt=conversation, max_tokens=100)
|
| 37 |
+
response_str = response["choices"][0]["text"].replace("\n", "")
|
| 38 |
+
response_str = response_str.split(user_name + ": ", 1)[0].split(bot_name + ": ", 1)[0]
|
| 39 |
+
|
| 40 |
+
conversation += response_str + "\n"
|
| 41 |
+
print(response_str)
|
| 42 |
+
|
| 43 |
+
engine.say(response_str)
|
| 44 |
+
engine.runAndWait()
|