geethareddy commited on
Commit
aaa46cf
·
verified ·
1 Parent(s): d6c923a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -108
app.py CHANGED
@@ -1,137 +1,90 @@
1
  import gradio as gr
2
  import pandas as pd
3
  import asyncio
4
- import tempfile
5
- from edge_tts import Communicate
 
6
 
7
- # Global cart to store ordered items
8
- cart = []
9
- order_processing = True # Global flag to enable/disable order processing
10
-
11
- # Load Menu Data
12
  def load_menu():
13
- menu_file = "menu.xlsx" # Ensure this file exists in the same directory
14
  try:
15
- return pd.read_excel(menu_file)
 
16
  except Exception as e:
17
- raise ValueError(f"Error loading menu file: {e}")
18
-
19
- # Generate Text-to-Speech Response
20
- async def generate_tts_response(text):
21
- communicate = Communicate(text, "en-US-JennyNeural")
22
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
23
- await communicate.save(tmp_file.name)
24
- return tmp_file.name
25
 
26
- # Search for Dish
27
- def search_dish(dish_name):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  menu_data = load_menu()
29
- dish = menu_data[menu_data["Dish Name"].str.contains(dish_name, case=False, na=False)]
30
- return dish
31
-
32
- # Handle Initial Greeting
33
- async def initial_greeting():
34
- greeting_text = "Hello, welcome to Biryani Hub. Can you please tell me what you want?"
35
- audio_response = await generate_tts_response(greeting_text)
36
- return audio_response, "Hello! Welcome to Biryani Hub."
37
-
38
- # Handle Voice Commands
39
- async def handle_voice_command(audio_path):
40
- global order_processing
41
  transcription = transcribe_audio(audio_path)
42
- transcription = transcription.lower()
43
-
44
- if "stop" in transcription:
45
- order_processing = False
46
- stop_text = "Order processing has been stopped."
47
- audio_response = await generate_tts_response(stop_text)
48
- return audio_response, "Order processing stopped.", "Your cart is safe. You can resume ordering later."
49
-
50
- if not order_processing:
51
- stop_text = "Order processing is currently stopped. Please say 'start' to resume."
52
- audio_response = await generate_tts_response(stop_text)
53
- return audio_response, "Order processing stopped.", stop_text
54
-
55
- if "start" in transcription:
56
- order_processing = True
57
- start_text = "Order processing has been resumed."
58
- audio_response = await generate_tts_response(start_text)
59
- return audio_response, "Order processing resumed.", "Please proceed with your orders."
60
-
61
- if transcription.startswith("order"):
62
- dish_name = transcription.replace("order", "").strip()
63
- dish = search_dish(dish_name)
64
-
65
- if not dish.empty:
66
- # Add to cart
67
- item_name = dish.iloc[0]["Dish Name"]
68
- item_price = dish.iloc[0]["Price ($)"]
69
- cart.append({"name": item_name, "price": item_price})
70
 
71
- confirmation_text = f"{item_name} is available for ${item_price}. Order confirmed and added to cart."
72
- audio_response = await generate_tts_response(confirmation_text)
73
- return audio_response, "Order Confirmed", confirmation_text
 
 
 
 
 
 
 
 
 
 
74
  else:
75
- unavailable_text = f"Sorry, {dish_name} is not available in the menu."
76
- audio_response = await generate_tts_response(unavailable_text)
77
- return audio_response, "Order Unavailable", unavailable_text
78
-
79
- if "menu details" in transcription:
80
- menu_data = load_menu()
81
- menu_items = "Here are the menu items: " + ", ".join(menu_data["Dish Name"].tolist())
82
- audio_response = await generate_tts_response(menu_items)
83
- return audio_response, "Menu Details", menu_items
84
 
85
- menu_text = "I didn't understand your request. Please say a valid command."
86
- audio_response = await generate_tts_response(menu_text)
87
- return audio_response, "Command not recognized", menu_text
88
 
89
- # Transcribe Audio Placeholder
90
  def transcribe_audio(audio_path):
91
- # Replace with actual transcription logic (e.g., Whisper API or Google Speech-to-Text)
92
- return "order veg samosa" # Replace this with the transcription logic
93
 
94
- # Update Cart Content
95
- def update_cart():
96
- if cart:
97
- cart_content = "\n".join([f"{item['name']} - ${item['price']}" for item in cart])
98
- else:
99
- cart_content = "Your cart is empty."
100
- return cart_content
101
-
102
- # Gradio App
103
  def app():
 
 
104
  with gr.Blocks() as demo:
 
105
  with gr.Row():
106
- gr.Markdown("## Welcome to the Menu")
 
 
 
107
 
108
- with gr.Row():
109
- greet_button = gr.Button("Click to Start")
110
- audio_input = gr.Audio(label="Speak your preference or order", type="filepath")
111
- tts_output = gr.Audio(label="Assistant Response", autoplay=True)
112
- cart_output = gr.Textbox(label="Cart", value="Your cart is empty.", interactive=False)
113
- status_output = gr.Textbox(label="Status", value="Order processing active.", interactive=False)
114
 
115
- greet_button.click(
116
- initial_greeting,
117
- inputs=[],
118
- outputs=[tts_output, status_output]
119
- )
120
 
 
121
  audio_input.change(
122
- handle_voice_command,
123
  inputs=[audio_input],
124
- outputs=[tts_output, status_output, cart_output],
125
- )
126
-
127
- gr.Button("View Cart").click(
128
- update_cart,
129
- inputs=[],
130
- outputs=cart_output
131
  )
132
 
133
  return demo
134
 
 
135
  if __name__ == "__main__":
136
- demo = app()
137
- demo.launch()
 
1
  import gradio as gr
2
  import pandas as pd
3
  import asyncio
4
+ from pydub import AudioSegment
5
+ import numpy as np
6
+ import edge_tts
7
 
8
+ # Load the menu data
 
 
 
 
9
  def load_menu():
 
10
  try:
11
+ menu_data = pd.read_excel("menu.xlsx")
12
+ return menu_data
13
  except Exception as e:
14
+ raise ValueError("Menu file not found or unreadable!")
 
 
 
 
 
 
 
15
 
16
+ # Generate menu details
17
+ def get_menu_details():
18
+ menu_data = load_menu()
19
+ details = "Here is the menu: "
20
+ for _, row in menu_data.iterrows():
21
+ details += f"{row['Dish Name']} for ${row['Price ($)']}, "
22
+ return details.strip(", ")
23
+
24
+ # Text-to-Speech Conversion
25
+ async def generate_audio(text):
26
+ communicate = edge_tts.Communicate(text, "en-US")
27
+ with open("response_audio.mp3", "wb") as audio_file:
28
+ await communicate.save(audio_file.name)
29
+ return "response_audio.mp3"
30
+
31
+ # Handle user input and provide a response
32
+ async def handle_input(audio_path, cart):
33
  menu_data = load_menu()
 
 
 
 
 
 
 
 
 
 
 
 
34
  transcription = transcribe_audio(audio_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ if transcription.lower() == "menu details":
37
+ response_text = get_menu_details()
38
+ elif transcription.lower() == "stop":
39
+ response_text = "Stopping interaction. Let me know when you're ready."
40
+ elif transcription.lower() == "start":
41
+ response_text = "Resuming interaction. What can I do for you?"
42
+ else:
43
+ item = menu_data[menu_data["Dish Name"].str.lower() == transcription.lower()]
44
+ if not item.empty:
45
+ dish_name = item.iloc[0]["Dish Name"]
46
+ price = item.iloc[0]["Price ($)"]
47
+ response_text = f"{dish_name} is available for ${price}. Adding it to your cart."
48
+ cart.append(f"{dish_name} - ${price}")
49
  else:
50
+ response_text = f"Sorry, {transcription} is not on the menu."
 
 
 
 
 
 
 
 
51
 
52
+ audio_response = await generate_audio(response_text)
53
+ return audio_response, cart, response_text
 
54
 
55
+ # Transcribe audio input (stubbed for simplicity)
56
  def transcribe_audio(audio_path):
57
+ # Mock transcription for now
58
+ return "veg samosa" # Replace this with the transcription from an audio processing library.
59
 
60
+ # Gradio UI setup
 
 
 
 
 
 
 
 
61
  def app():
62
+ cart = []
63
+
64
  with gr.Blocks() as demo:
65
+ gr.Markdown("### Welcome to the Menu")
66
  with gr.Row():
67
+ audio_input = gr.Audio(label="Speak your preference or order", source="microphone", type="filepath")
68
+ assistant_response = gr.Audio(label="Assistant Response")
69
+ cart_output = gr.Textbox(label="Cart", value="")
70
+ status = gr.Textbox(label="Status", value="")
71
 
72
+ # Initialize with a greeting message
73
+ async def start_greeting():
74
+ audio_response = await generate_audio("Hello, welcome to Biryani Hub. Can you please tell me what you want?")
75
+ return audio_response
 
 
76
 
77
+ demo.load(lambda: asyncio.run(start_greeting()), inputs=[], outputs=assistant_response)
 
 
 
 
78
 
79
+ # Handle voice input
80
  audio_input.change(
81
+ lambda audio_path: asyncio.run(handle_input(audio_path, cart)),
82
  inputs=[audio_input],
83
+ outputs=[assistant_response, cart_output, status],
 
 
 
 
 
 
84
  )
85
 
86
  return demo
87
 
88
+
89
  if __name__ == "__main__":
90
+ app().launch()