Spaces:
Runtime error
Runtime error
Commit
·
ba7714e
1
Parent(s):
203292e
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import speech_recognition as sr
|
| 3 |
+
import pyttsx3
|
| 4 |
+
engine = pyttsx3.init()
|
| 5 |
+
listener =sr.Recognizer()
|
| 6 |
+
openai.api_key = "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"
|
| 7 |
+
|
| 8 |
+
while True:
|
| 9 |
+
with sr.Microphone() as source:
|
| 10 |
+
print("speak now...")
|
| 11 |
+
voice = listener.listen(source)
|
| 12 |
+
data = listener.recognize_google(voice)
|
| 13 |
+
model = "text-davinci-003"
|
| 14 |
+
|
| 15 |
+
if "exit" in data:
|
| 16 |
+
break
|
| 17 |
+
|
| 18 |
+
completion = openai.Completion.create(model ="text-davinci-002",
|
| 19 |
+
prompt = data,
|
| 20 |
+
max_tokens = 1024,
|
| 21 |
+
temperature = 0.5,
|
| 22 |
+
n = 1,
|
| 23 |
+
stop = None)
|
| 24 |
+
response = completion.choices[0].text
|
| 25 |
+
choice = int(input("press 1 to print the response or press 2 to print and hear the response: "))
|
| 26 |
+
|
| 27 |
+
if choice == 1:
|
| 28 |
+
print(response)
|
| 29 |
+
else:
|
| 30 |
+
print(response)
|
| 31 |
+
engine.say(response)
|
| 32 |
+
engine.runAndWait()
|
| 33 |
+
|
| 34 |
+
repeat = input("do you want to ask more questions?: ")
|
| 35 |
+
if repeat in ["no","No","NO"]:
|
| 36 |
+
break
|