π€ Build an AI Voice Assistant in Python | Speech Recognition & Wikipedia Search
Demo :
Click Video πππ
πΉ Features:
✅ Converts Speech to Text
✅ Wikipedia Search Integration
✅ Opens Websites with Commands
✅ Tkinter GUI for Easy Interaction
Code :
import speech_recognition as sr
import pyttsx3
import wikipedia
import webbrowser
import os
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
# Initialize Speech Engine
engine = pyttsx3.init()
engine.setProperty("rate", 150)
def speak(text):
engine.say(text)
engine.runAndWait()
def take_command():
r = sr.Recognizer()
with sr.Microphone() as source:
status_label.config(text="Listening...")
root.update()
try:
audio = r.listen(source, timeout=5)
query = r.recognize_google(audio, language="en-in")
status_label.config(text=f"You said: {query}")
return query.lower()
except sr.UnknownValueError:
status_label.config(text="Could not understand! Try again.")
return None
except sr.RequestError:
status_label.config(text="Internet issue detected!")
return None
except Exception as e:
status_label.config(text=f"Error: {str(e)}")
return None
def process_command():
query = take_command()
if query:
if "wikipedia" in query:
speak("Searching Wikipedia...")
search_wikipedia(query.replace("wikipedia", ""))
elif "open" in query:
open_website(query.replace("open", "").strip())
elif "exit" in query or "stop" in query:
speak("Goodbye! Have a great day.")
root.destroy()
else:
speak("Sorry, I didn't understand that command.")
def search_wikipedia(query):
try:
result = wikipedia.summary(query, sentences=2)
speak(result)
result_label.config(text=result)
except wikipedia.exceptions.DisambiguationError:
speak("Multiple results found. Please be more specific.")
except wikipedia.exceptions.PageError:
speak("No results found on Wikipedia.")
except Exception as e:
speak("Error occurred while searching Wikipedia.")
result_label.config(text=f"Error: {str(e)}")
def open_website(website):
sites = {
"youtube": "https://www.youtube.com",
"google": "https://www.google.com",
"github": "https://www.github.com",
}
if website in sites:
speak(f"Opening {website}")
webbrowser.open(sites[website])
else:
speak("Website not found!")
os.system(f"start https://www.{website}.com")
# GUI Setup
root = tk.Tk()
root.title("AI Voice Assistant")
root.geometry("500x600")
root.configure(bg="#1e1e1e")
style = ttk.Style()
style.configure("TButton", font=("Arial", 12), padding=10)
# Logo
try:
logo_img = Image.open("mic.png").resize((100, 100))
logo_photo = ImageTk.PhotoImage(logo_img)
logo_label = tk.Label(root, image=logo_photo, bg="#1e1e1e")
logo_label.pack(pady=20)
except Exception as e:
print(f"Error loading image: {e}")
# Status Label
status_label = tk.Label(root, text="Click to Speak", font=("Arial", 14), fg="white", bg="#1e1e1e")
status_label.pack()
# Speak Button
speak_button = ttk.Button(root, text="Start Listening", command=process_command)
speak_button.pack(pady=20)
# Result Label
result_label = tk.Label(root, text="", wraplength=400, font=("Arial", 12), fg="white", bg="#1e1e1e")
result_label.pack(pady=20)
root.mainloop()
Comments
Post a Comment