-
Notifications
You must be signed in to change notification settings - Fork 0
/
Max_Assistant.py
74 lines (58 loc) · 1.9 KB
/
Max_Assistant.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
import speech_recognition as sr
import pyttsx3
import openai
import webbrowser
import os
openai.api_key = "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"
def speak(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
def listen():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
try:
print("Recognizing...")
command = recognizer.recognize_google(audio)
print("You said:", command)
return command
except sr.UnknownValueError:
print("Sorry, could not understand audio.")
return ""
except sr.RequestError as e:
print(f"Error with the speech recognition service; {e}")
return ""
def generate_response(prompt):
response = openai.Completion.create(
engine="text-davinci-002",
prompt=prompt,
max_tokens=150
)
return response.choices[0].text.strip()
def process_command(command):
if "hello" in command:
speak("Hello! How can I help you?")
elif "goodbye" in command:
speak("Goodbye! Have a great day.")
exit()
elif "your name" in command:
speak("My name is Max.")
elif "open website" in command:
speak("Sure, opening a website. Please provide the URL.")
url = listen()
if url:
webbrowser.open(url)
else:
speak("Sorry, I didn't get the URL.")
else:
prompt = f"User command: {command}"
response = generate_response(prompt)
speak(response)
if __name__ == "__main__":
speak("Hello! I'm Max, your voice assistant. How can I assist you today?")
while True:
command = listen()
process_command(command)