Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import speech_recognition as sr
|
| 4 |
+
import pyttsx3
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
import tkinter as tk
|
| 7 |
+
from tkinter import messagebox
|
| 8 |
+
|
| 9 |
+
# Load environment variables
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
# Initialize text-to-speech engine
|
| 13 |
+
engine = pyttsx3.init()
|
| 14 |
+
|
| 15 |
+
def speak(text):
|
| 16 |
+
"""Use text-to-speech to speak the given text."""
|
| 17 |
+
engine.say(text)
|
| 18 |
+
engine.runAndWait()
|
| 19 |
+
|
| 20 |
+
def recognize_speech(timeout=5):
|
| 21 |
+
"""Capture and recognize speech from the microphone with a timeout."""
|
| 22 |
+
recognizer = sr.Recognizer()
|
| 23 |
+
with sr.Microphone() as source:
|
| 24 |
+
print("Listening for a wake-up command...")
|
| 25 |
+
audio = recognizer.listen(source, timeout=timeout)
|
| 26 |
+
print("Audio captured.")
|
| 27 |
+
try:
|
| 28 |
+
command = recognizer.recognize_google(audio)
|
| 29 |
+
print(f"You said: {command}")
|
| 30 |
+
return command
|
| 31 |
+
except sr.UnknownValueError:
|
| 32 |
+
print("Could not understand audio.")
|
| 33 |
+
return None
|
| 34 |
+
except sr.RequestError:
|
| 35 |
+
speak("Sorry, there was an error with the speech recognition service.")
|
| 36 |
+
print("Request error.")
|
| 37 |
+
return None
|
| 38 |
+
|
| 39 |
+
def process_command(command):
|
| 40 |
+
"""Generate a response based on the voice command using the AI model."""
|
| 41 |
+
if command:
|
| 42 |
+
response = model.generate_content([command])
|
| 43 |
+
reply = response.text.strip()
|
| 44 |
+
speak(reply)
|
| 45 |
+
print(f"AI Response: {reply}")
|
| 46 |
+
return reply
|
| 47 |
+
|
| 48 |
+
# GUI Functions
|
| 49 |
+
def start_listening():
|
| 50 |
+
command = recognize_speech(timeout=5)
|
| 51 |
+
|
| 52 |
+
if command and wake_word in command.lower():
|
| 53 |
+
speak("How can I assist you?")
|
| 54 |
+
result_label.config(text="Wake word detected! Listening for further commands...")
|
| 55 |
+
command = recognize_speech(timeout=5)
|
| 56 |
+
if command:
|
| 57 |
+
if "stop listening" in command.lower():
|
| 58 |
+
speak("Goodbye.")
|
| 59 |
+
result_label.config(text="Voice assistant stopped.")
|
| 60 |
+
else:
|
| 61 |
+
response = process_command(command)
|
| 62 |
+
result_label.config(text=f"Response: {response}")
|
| 63 |
+
else:
|
| 64 |
+
result_label.config(text="Wake word not detected.")
|
| 65 |
+
|
| 66 |
+
# Main Code with Generative AI Setup
|
| 67 |
+
api_key = os.getenv("MY_API_KEY")
|
| 68 |
+
|
| 69 |
+
if api_key is None:
|
| 70 |
+
raise ValueError("API key not found in environment variables")
|
| 71 |
+
|
| 72 |
+
# Configure the AI model
|
| 73 |
+
genai.configure(api_key=api_key)
|
| 74 |
+
|
| 75 |
+
generation_config = {
|
| 76 |
+
"temperature": 1,
|
| 77 |
+
"top_p": 0.95,
|
| 78 |
+
"top_k": 64,
|
| 79 |
+
"max_output_tokens": 8192,
|
| 80 |
+
"response_mime_type": "text/plain",
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
model = genai.GenerativeModel(
|
| 84 |
+
model_name="gemini-1.5-flash-8b-exp-0827",
|
| 85 |
+
generation_config=generation_config,
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
wake_word = "sema"
|
| 89 |
+
|
| 90 |
+
# Create the GUI
|
| 91 |
+
root = tk.Tk()
|
| 92 |
+
root.title("Sema Voice Assistant")
|
| 93 |
+
|
| 94 |
+
# Create UI elements
|
| 95 |
+
title_label = tk.Label(root, text="SEMA AI", font=("Arial", 16))
|
| 96 |
+
title_label.pack(pady=10)
|
| 97 |
+
|
| 98 |
+
start_button = tk.Button(root, text="Start Listening", command=start_listening, font=("Arial", 14))
|
| 99 |
+
start_button.pack(pady=10)
|
| 100 |
+
|
| 101 |
+
result_label = tk.Label(root, text="", font=("Arial", 12))
|
| 102 |
+
result_label.pack(pady=10)
|
| 103 |
+
|
| 104 |
+
# Run the GUI loop
|
| 105 |
+
root.mainloop()
|