-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoice_assistant.py
More file actions
117 lines (87 loc) · 3.49 KB
/
voice_assistant.py
File metadata and controls
117 lines (87 loc) · 3.49 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import time
import tkinter as tk
from tkinter import messagebox
import os
import speech_recognizer as sr
import talk_back
commands = {
"open calculator": "open -a Calculator",
"open text edit": "open -a TextEdit",
"open zoom": "open -a zoom.us",
"open terminal": "open -a Terminal"
}
def start_recording():
fs = 44100
duration = int(duration_entry.get())
filename = "output.wav"
mp3_filename = "output.mp3"
try:
sr.record_audio(filename, duration, fs)
speech = sr.recognize_speech_from_audio(filename).lower()
if os.path.exists(filename):
os.remove(filename)
if speech:
recognized_command_label.config(text=f"Recognized Command: {speech}")
if speech in commands:
talk_back.text_to_audio(f"OK done!")
time.sleep(1)
talk_back.play()
if os.path.exists(mp3_filename):
os.remove(mp3_filename)
os.system(commands[speech])
log_action(f"Executed: {speech}")
else:
talk_back.text_to_audio("Sorry I am not able to do that!")
time.sleep(1)
talk_back.play()
if os.path.exists(mp3_filename):
os.remove(mp3_filename)
messagebox.showerror("Error", "Sorry, I could not recognize the command.")
log_action(f"Failed to recognize: {speech}")
else:
talk_back.text_to_audio("You didn't say anything!")
time.sleep(1)
talk_back.play()
if os.path.exists(mp3_filename):
os.remove(mp3_filename)
recognized_command_label.config(text="Could not recognize any command")
log_action("Failed to recognize any command")
except Exception as e:
print(f"An error occurred: {e}")
if os.path.exists(filename):
os.remove(filename)
if os.path.exists(mp3_filename):
os.remove(mp3_filename)
def update_commands_label():
commands_text = "Available Commands:\n" + "\n".join([f"- {cmd}" for cmd in commands.keys()])
commands_label.config(text=commands_text)
def log_action(action):
history_listbox.insert(tk.END, action)
def on_closing():
if messagebox.askokcancel("Quit", "Do you want to quit?"):
root.destroy()
root = tk.Tk()
root.title("Voice Assistant")
root.geometry("400x500")
label = tk.Label(root, text="Voice Assistant", font=("Helvetica", 16))
label.pack(pady=10)
commands_label = tk.Label(root, text="", font=("Helvetica", 12))
commands_label.pack(pady=10)
update_commands_label()
duration_label = tk.Label(root, text="Recording Duration (seconds):", font=("Helvetica", 12))
duration_label.pack(pady=5)
duration_entry = tk.Entry(root, font=("Helvetica", 12))
duration_entry.insert(0, "3")
duration_entry.pack(pady=5)
record_button = tk.Button(root, text="Start Recording", command=start_recording, font=("Helvetica", 14))
record_button.pack(pady=20)
recognized_command_label = tk.Label(root, text="", font=("Helvetica", 12))
recognized_command_label.pack(pady=10)
history_label = tk.Label(root, text="Command History:", font=("Helvetica", 12))
history_label.pack(pady=5)
history_listbox = tk.Listbox(root, font=("Helvetica", 12), height=10, width=50)
history_listbox.pack(pady=5)
exit_button = tk.Button(root, text="Exit", command=on_closing, font=("Helvetica", 14))
exit_button.pack(pady=20)
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()