File size: 2,098 Bytes
80078b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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")