geethareddy commited on
Commit
79b737e
·
verified ·
1 Parent(s): 9bf2807

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -1
app.py CHANGED
@@ -1,7 +1,106 @@
1
  import gradio as gr
2
  import pandas as pd
3
  import edge_tts
4
- import asyncio
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  import tempfile
6
 
7
  # Load menu data
 
1
  import gradio as gr
2
  import pandas as pd
3
  import edge_tts
4
+ import asynciimport gradio as gr
5
+ from gtts import gTTS
6
+ import os
7
+ import tempfile
8
+ import json
9
+ import speech_recognition as sr
10
+
11
+ # Store cart in a temporary storage
12
+ cart = []
13
+
14
+ # Define the menu items dynamically
15
+ menu_items = {
16
+ "Pizza": 10.99,
17
+ "Burger": 8.49,
18
+ "Pasta": 12.99,
19
+ "Salad": 7.99,
20
+ "Soda": 2.49
21
+ }
22
+
23
+ def generate_voice_response(text):
24
+ tts = gTTS(text)
25
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
26
+ temp_file.close()
27
+ tts.save(temp_file.name)
28
+ return temp_file.name
29
+
30
+ def calculate_total(cart):
31
+ return sum(menu_items[item] for item in cart)
32
+
33
+ def restaurant_voice_assistant(audio, state_json):
34
+ global cart
35
+ state = json.loads(state_json) if state_json else {}
36
+ response = ""
37
+ voice_path = None
38
+
39
+ # Convert audio input to text
40
+ if audio:
41
+ recognizer = sr.Recognizer()
42
+ with sr.AudioFile(audio) as source:
43
+ try:
44
+ input_text = recognizer.recognize_google(recognizer.record(source))
45
+ except sr.UnknownValueError:
46
+ input_text = ""
47
+ else:
48
+ input_text = ""
49
+
50
+ if not state.get("menu_shown", False):
51
+ # Show menu dynamically
52
+ response = "Welcome to our restaurant! Here is our menu:\n"
53
+ for item, price in menu_items.items():
54
+ response += f"{item}: ${price:.2f}\n"
55
+ response += "\nPlease tell me the item you would like to add to your cart."
56
+ state["menu_shown"] = True
57
+ elif any(item.lower() in input_text.lower() for item in menu_items):
58
+ # Check if input matches a menu item
59
+ for item in menu_items:
60
+ if item.lower() in input_text.lower():
61
+ cart.append(item)
62
+ total = calculate_total(cart)
63
+ response = f"{item} has been added to your cart. Your current cart includes:\n"
64
+ for cart_item in cart:
65
+ response += f"- {cart_item}: ${menu_items[cart_item]:.2f}\n"
66
+ response += f"\nTotal: ${total:.2f}. Would you like to add anything else?"
67
+ break
68
+ elif "menu" in input_text.lower():
69
+ response = "Here is our menu again:\n"
70
+ for item, price in menu_items.items():
71
+ response += f"{item}: ${price:.2f}\n"
72
+ response += "\nWhat would you like to add to your cart?"
73
+ elif "final order" in input_text.lower() or "submit order" in input_text.lower():
74
+ if cart:
75
+ total = calculate_total(cart)
76
+ response = "Your final order includes:\n"
77
+ for item in cart:
78
+ response += f"- {item}: ${menu_items[item]:.2f}\n"
79
+ response += f"\nTotal: ${total:.2f}.\nThank you for ordering!"
80
+ cart = [] # Clear cart after finalizing order
81
+ else:
82
+ response = "Your cart is empty. Would you like to order something?"
83
+ else:
84
+ response = "I didn’t quite catch that. Please tell me what you’d like to order."
85
+
86
+ voice_path = generate_voice_response(response)
87
+ return response, voice_path, json.dumps(state)
88
+
89
+ with gr.Blocks() as demo:
90
+ state = gr.State(value=json.dumps({}))
91
+
92
+ with gr.Row():
93
+ user_audio = gr.Audio(type="filepath", label="Your Voice Input")
94
+ output_text = gr.Textbox(label="Response Text")
95
+
96
+ with gr.Row():
97
+ voice_output = gr.Audio(label="Response Audio", autoplay=True)
98
+
99
+ # Automatically process audio when recording stops
100
+ user_audio.change(restaurant_voice_assistant, inputs=[user_audio, state], outputs=[output_text, voice_output, state])
101
+
102
+ demo.launch()
103
+ o
104
  import tempfile
105
 
106
  # Load menu data