Spaces:
Runtime error
Runtime error
main.py
Browse files
main.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import pyttsx3
|
3 |
+
import speech_recognition as sr
|
4 |
+
|
5 |
+
# Set up OpenAI API
|
6 |
+
openai.api_key = "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"
|
7 |
+
|
8 |
+
# Set up Text-to-Speech engine
|
9 |
+
engine = pyttsx3.init()
|
10 |
+
|
11 |
+
# Set up Speech Recognition engine
|
12 |
+
r = sr.Recognizer()
|
13 |
+
|
14 |
+
# Set up Microphone
|
15 |
+
mic = sr.Microphone()
|
16 |
+
|
17 |
+
# Define function to convert text to speech
|
18 |
+
def speak(text):
|
19 |
+
engine.say(text)
|
20 |
+
engine.runAndWait()
|
21 |
+
|
22 |
+
# Define function to recognize speech
|
23 |
+
def recognize_speech():
|
24 |
+
with mic as source:
|
25 |
+
r.adjust_for_ambient_noise(source)
|
26 |
+
audio = r.listen(source)
|
27 |
+
try:
|
28 |
+
text = r.recognize_google(audio)
|
29 |
+
return text
|
30 |
+
except:
|
31 |
+
return ""
|
32 |
+
|
33 |
+
# Define function to get answer from OpenAI API
|
34 |
+
def get_answer(question):
|
35 |
+
response = openai.Completion.create(
|
36 |
+
engine="text-davinci-003",
|
37 |
+
prompt=f"Q: {question}\nA:",
|
38 |
+
temperature=0.5,
|
39 |
+
max_tokens=50,
|
40 |
+
top_p=1,
|
41 |
+
frequency_penalty=0,
|
42 |
+
presence_penalty=0,
|
43 |
+
)
|
44 |
+
answer = response.choices[0].text.strip()
|
45 |
+
return answer
|
46 |
+
|
47 |
+
# Define main function
|
48 |
+
def main():
|
49 |
+
# Greet the user
|
50 |
+
speak("Hello! I'm your personal assistant. How can I help you today?")
|
51 |
+
|
52 |
+
while True:
|
53 |
+
# Listen for user's question
|
54 |
+
speak("Please ask me a question.")
|
55 |
+
question = recognize_speech()
|
56 |
+
|
57 |
+
# Get answer from OpenAI API
|
58 |
+
answer = get_answer(question)
|
59 |
+
|
60 |
+
# Speak the answer
|
61 |
+
speak(answer)
|
62 |
+
|
63 |
+
if __name__ == '__main__':
|
64 |
+
main()
|