import gradio as gr import pyttsx3 import speech_recognition as sr from utils.state_management import state from utils.excel_operations import read_excel def voice_assistant(): recognizer = sr.Recognizer() engine = pyttsx3.init() def speak_response(text): engine.say(text) engine.runAndWait() def process_voice_command(audio): try: # Recognize speech command = recognizer.recognize_google(audio) if "menu" in command.lower(): return fetch_menu() elif "cart" in command.lower(): return view_cart() elif "place order" in command.lower(): return place_order() else: return "Sorry, I didn't understand that command." except Exception as e: return f"Error processing voice command: {str(e)}" def fetch_menu(): if not state.get("user"): return "Please log in first to view the menu." menu = read_excel("data/menu.xlsx") menu_list = "\n".join([f"{item['Name']} - {item['Price']} ₹" for item in menu]) speak_response(menu_list) return menu_list def view_cart(): cart = state.get("cart", []) if not cart: return "Your cart is empty." cart_details = "\n".join([f"{item['Item']} - {item['Spice Level']}" for item in cart]) speak_response(cart_details) return cart_details def place_order(): if not state.get("cart"): return "Your cart is empty." speak_response("Your order has been placed successfully!") state["cart"] = [] return "Order placed successfully!" def record_and_process_voice(): with sr.Microphone() as source: speak_response("Listening for your command...") audio = recognizer.listen(source) return process_voice_command(audio) with gr.Group(): gr.Markdown("### Voice Assistant") gr.Button("Start Listening").click(record_and_process_voice, outputs="Response")