-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatty.py
98 lines (77 loc) · 3.42 KB
/
chatty.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
import openai
import speech_recognition as sr
from tkinter.constants import DISABLED, NORMAL
import tkinter as tk
# Set up OpenAI API credentials
openai.api_key = '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'
def ask_openai(question):
model_engine = "text-davinci-002"
prompt = f"Q: {question}\nA:"
completions = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.7,
)
message = completions.choices[0].text.strip()
return message
# Function to recognize speech using microphone
def recognize_speech():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
try:
text = r.recognize_google(audio)
return text
except sr.UnknownValueError:
return "Sorry, I could not understand you."
except sr.RequestError:
return "Sorry, my speech recognition service is currently down."
class ChatbotGUI:
def __init__(self):
self.window = tk.Tk()
self.window.title("Alalay nang Hari")
self.window.geometry("400x600")
self.scroll_frame = tk.Frame(self.window)
self.scroll_frame.pack(side="top", fill="both", expand=True)
self.chat_history = tk.Text(self.scroll_frame, wrap="word", state="disabled")
self.chat_history.pack(side="left", fill="both", expand=True)
self.scrollbar = tk.Scrollbar(self.scroll_frame, orient="vertical", command=self.chat_history.yview)
self.scrollbar.pack(side="right", fill="y")
self.chat_history.configure(yscrollcommand=self.scrollbar.set)
self.question_entry = tk.Entry(self.window, width=200, font=("Arial", 12))
self.question_entry.pack(pady=10)
self.ask_button = tk.Button(self.window, text="Ask", width=200, command=self.ask_question, font=("Arial", 12))
self.ask_button.pack(pady=10)
self.clear_button = tk.Button(self.window, text="Clear", width=200, command=self.clear_all, font=("Arial", 12))
self.clear_button.pack(pady=10)
self.listen_button = tk.Button(self.window, text="Speak",width=200, command=self.listen_question, font=("Arial", 12))
self.listen_button.pack(pady=10)
self.window.mainloop()
def clear_all(self):
self.chat_history.configure(state="normal")
self.chat_history.delete("1.0", tk.END)
self.chat_history.configure(state="disabled")
def ask_question(self):
question = self.question_entry.get().strip()
if question != "":
response = ask_openai(question)
self.update_chat_history(question, response)
def listen_question(self):
question = recognize_speech()
self.question_entry.delete(0, tk.END)
self.question_entry.insert(0, question)
response = ask_openai(question)
self.update_chat_history(question, response)
def update_chat_history(self, question, response):
self.chat_history.configure(state="normal")
if self.chat_history.index('end') != None:
self.chat_history.insert('end', "You: " + question + "\n", 'bold')
self.chat_history.insert('end', "Alalay: " + response + "\n\n")
self.chat_history.tag_configure('bold', font=("Arial", 12, 'bold'))
self.chat_history.configure(state="disabled")
self.chat_history.yview('end')
if __name__ == "__main__":
gui = ChatbotGUI()