-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoicebot_with_GUI.py
120 lines (101 loc) · 4.44 KB
/
voicebot_with_GUI.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
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
118
119
120
import threading
import tkinter as tk
from tkinter import filedialog
import openai
import speech_recognition as sr
import pyttsx3
class Voice_ChatBot:
def __init__(self):
self.window = tk.Tk()
self.window.geometry("700x550")
self.window.title("Voice ChatBot")
self.window.resizable(False, False)
# Create a label and entry box for the OpenAPI key
self.api_key_label = tk.Label(self.window, text="OpenAI API key:")
self.api_key_entry = tk.Entry(self.window, width=40)
self.api_key_label.pack()
self.api_key_entry.pack()
self.button1 = tk.Button(self.window, text="Submit", command=self.getkey)
self.button1.pack(padx=10, pady=10)
self.label = tk.Label(self.window, text="")
self.label.pack(padx=10, pady=10)
# Create a textbox for displaying text
self.textbox = tk.Text(self.window, width=100, height=20, bg='#F6F6F6', fg='black', wrap="word")
self.textbox.tag_config('user', foreground='green')
self.textbox.tag_config('bot', foreground='blue')
self.textbox.tag_config('system', foreground='red')
self.textbox.yview_pickplace("end")
self.textbox.pack(padx=10)
# Create buttons for executing actions
self.button2 = tk.Button(self.window, text="Speak", command=self.start_thread, background="gray")
self.button2.pack(padx=10, pady=10)
self.button3 = tk.Button(self.window, text="Save To Text File", command=self.save_file, background="skyblue")
self.button3.pack(padx=20, pady=10)
self.window.mainloop()
# get api_key
def getkey(self):
global openaikey
openaikey = self.api_key_entry.get()
openai.api_key = openaikey
# self.textbox.insert(tk.END, "Click Speak to speak: \n", 'system')
self.label.config(text="Click Speak Button once to speak", foreground="red")
self.api_key_entry.delete(0, 'end')
# define function to capture user's voice input
def get_voice_input(self):
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
try:
text_input = r.recognize_google(audio)
self.textbox.insert(tk.END, "You: " + text_input + "\n", 'user')
response = self.generate_response(text_input)
self.generate_speech_response(response)
except sr.UnknownValueError:
self.textbox.insert(tk.END, "Could not recognize input. \n", 'system')
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
return ""
# define function to generate text response using OpenAI API
def generate_response(self, input_text):
response = openai.Completion.create(
engine="text-davinci-003",
prompt=input_text,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
output = response.choices[0].text.strip()
self.textbox.insert(tk.END, "Chatbot: " + output + "\n", 'bot')
return output
# define function to convert text response to speech
def generate_speech_response(self, text_response):
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
engine.say(text_response)
engine.runAndWait()
self.textbox.insert(tk.END, "Speak: \n", 'system')
def start(self):
self.label.config(text="")
while True:
user_input = self.get_voice_input()
if user_input:
response = self.generate_response(user_input)
self.generate_speech_response(response)
def start_thread(self):
self.textbox.insert(tk.END, "Speak: \n", 'system')
threading.Thread(target=self.start).start()
def save_file(self):
# Get the text from the Text widget
text = self.textbox.get("1.0", "end-1c")
# Open a file dialog to choose the save location
file_path = filedialog.asksaveasfilename(defaultextension=".txt")
# Check if a file was chosen and write the text to it
if file_path:
with open(file_path, "w") as f:
f.write(text)
self.label.config(text="File saved successfully!", foreground="darkblue")
else:
self.label.config(text="File not saved.!!!", foreground="red")
gui = Voice_ChatBot()