import speech_recognition as sr import tkinter as tk class SpeechToTextWindow: def __init__(self, root): self.root = root self.root.title("Speech to Text") self.label = tk.Label(root, text="Speak into the microphone:") self.label.pack() self.text_box = tk.Text(root, height=10, width=50) self.text_box.pack() self.listen_button = tk.Button(root, text="Start Listening", command=self.start_listening) self.listen_button.pack() self.recognizer = sr.Recognizer() def start_listening(self): with sr.Microphone() as source: self.recognizer.adjust_for_ambient_noise(source) self.text_box.delete('1.0', tk.END) self.text_box.insert(tk.END, "Listening...\n") self.root.update() try: audio = self.recognizer.listen(source) text = self.recognizer.recognize_google(audio) self.text_box.insert(tk.END, f"You said: {text}\n") except sr.UnknownValueError: self.text_box.insert(tk.END, "Sorry, I could not understand what you said.\n") except sr.RequestError as e: self.text_box.insert(tk.END, f"Sorry, an error occurred: {e}\n") if __name__ == "__main__": root = tk.Tk() app = SpeechToTextWindow(root) root.mainloop()