|
| 1 | +import openai |
| 2 | +import speech_recognition as sr |
| 3 | +from tkinter.constants import DISABLED, NORMAL |
| 4 | +import tkinter as tk |
| 5 | + |
| 6 | +# Set up OpenAI API credentials |
| 7 | +openai.api_key = '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' |
| 8 | + |
| 9 | +def ask_openai(question): |
| 10 | + model_engine = "text-davinci-002" |
| 11 | + prompt = f"Q: {question}\nA:" |
| 12 | + completions = openai.Completion.create( |
| 13 | + engine=model_engine, |
| 14 | + prompt=prompt, |
| 15 | + max_tokens=1024, |
| 16 | + n=1, |
| 17 | + stop=None, |
| 18 | + temperature=0.7, |
| 19 | + ) |
| 20 | + |
| 21 | + message = completions.choices[0].text.strip() |
| 22 | + return message |
| 23 | + |
| 24 | +# Function to recognize speech using microphone |
| 25 | +def recognize_speech(): |
| 26 | + r = sr.Recognizer() |
| 27 | + with sr.Microphone() as source: |
| 28 | + audio = r.listen(source) |
| 29 | + |
| 30 | + try: |
| 31 | + text = r.recognize_google(audio) |
| 32 | + return text |
| 33 | + except sr.UnknownValueError: |
| 34 | + return "Sorry, I could not understand you." |
| 35 | + except sr.RequestError: |
| 36 | + return "Sorry, my speech recognition service is currently down." |
| 37 | + |
| 38 | +class ChatbotGUI: |
| 39 | + def __init__(self): |
| 40 | + self.window = tk.Tk() |
| 41 | + self.window.title("Alalay nang Hari") |
| 42 | + self.window.geometry("400x600") |
| 43 | + |
| 44 | + self.scroll_frame = tk.Frame(self.window) |
| 45 | + self.scroll_frame.pack(side="top", fill="both", expand=True) |
| 46 | + |
| 47 | + self.chat_history = tk.Text(self.scroll_frame, wrap="word", state="disabled") |
| 48 | + self.chat_history.pack(side="left", fill="both", expand=True) |
| 49 | + |
| 50 | + self.scrollbar = tk.Scrollbar(self.scroll_frame, orient="vertical", command=self.chat_history.yview) |
| 51 | + self.scrollbar.pack(side="right", fill="y") |
| 52 | + |
| 53 | + self.chat_history.configure(yscrollcommand=self.scrollbar.set) |
| 54 | + |
| 55 | + self.question_entry = tk.Entry(self.window, width=200, font=("Arial", 12)) |
| 56 | + self.question_entry.pack(pady=10) |
| 57 | + |
| 58 | + self.ask_button = tk.Button(self.window, text="Ask", width=200, command=self.ask_question, font=("Arial", 12)) |
| 59 | + self.ask_button.pack(pady=10) |
| 60 | + |
| 61 | + self.clear_button = tk.Button(self.window, text="Clear", width=200, command=self.clear_all, font=("Arial", 12)) |
| 62 | + self.clear_button.pack(pady=10) |
| 63 | + |
| 64 | + self.listen_button = tk.Button(self.window, text="Speak",width=200, command=self.listen_question, font=("Arial", 12)) |
| 65 | + self.listen_button.pack(pady=10) |
| 66 | + |
| 67 | + self.window.mainloop() |
| 68 | + |
| 69 | + |
| 70 | + def clear_all(self): |
| 71 | + self.chat_history.configure(state="normal") |
| 72 | + self.chat_history.delete("1.0", tk.END) |
| 73 | + self.chat_history.configure(state="disabled") |
| 74 | + |
| 75 | + def ask_question(self): |
| 76 | + question = self.question_entry.get().strip() |
| 77 | + if question != "": |
| 78 | + response = ask_openai(question) |
| 79 | + self.update_chat_history(question, response) |
| 80 | + |
| 81 | + def listen_question(self): |
| 82 | + question = recognize_speech() |
| 83 | + self.question_entry.delete(0, tk.END) |
| 84 | + self.question_entry.insert(0, question) |
| 85 | + response = ask_openai(question) |
| 86 | + self.update_chat_history(question, response) |
| 87 | + |
| 88 | + def update_chat_history(self, question, response): |
| 89 | + self.chat_history.configure(state="normal") |
| 90 | + if self.chat_history.index('end') != None: |
| 91 | + self.chat_history.insert('end', "You: " + question + "\n", 'bold') |
| 92 | + self.chat_history.insert('end', "Alalay: " + response + "\n\n") |
| 93 | + self.chat_history.tag_configure('bold', font=("Arial", 12, 'bold')) |
| 94 | + self.chat_history.configure(state="disabled") |
| 95 | + self.chat_history.yview('end') |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + gui = ChatbotGUI() |
0 commit comments