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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -65
app.py CHANGED
@@ -1,90 +1,70 @@
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()
 
 
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
8
  def load_menu():
9
+ menu_file = "menu.xlsx"
10
  try:
11
+ return pd.read_excel(menu_file)
 
12
  except Exception as e:
13
+ raise ValueError(f"Error loading menu file: {e}")
14
 
15
+ # Filter menu items based on preference
16
+ def filter_menu(preference):
17
  menu_data = load_menu()
18
+ menu_details = ""
19
+ for _, item in menu_data.iterrows():
20
+ menu_details += f"{item['Dish Name']} - ${item['Price ($)']}. "
21
+ return menu_details
22
 
23
+ # Text-to-speech response
24
+ def generate_tts_response(text):
25
+ communicate = edge_tts.Communicate(text)
26
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
27
+ tmp_path = tmp_file.name
28
+ asyncio.run(communicate.save(tmp_path))
29
+ return tmp_path
 
 
 
 
30
 
31
+ # Process user audio input and respond
32
+ def process_audio(audio_file, preference):
33
+ if "menu" in preference.lower():
34
+ menu_details = filter_menu("All")
35
+ response_text = f"Here are the menu details: {menu_details}"
36
+ elif "veg samosa" in preference.lower():
37
+ response_text = "Order confirmed for Veg Samosa. It has been added to your cart."
38
  else:
39
+ response_text = "I'm sorry, I didn't understand that. Please try again."
 
 
 
 
 
 
 
40
 
41
+ tts_path = generate_tts_response(response_text)
42
+ return tts_path, response_text
43
 
44
+ # Build Gradio app
 
 
 
 
 
45
  def app():
 
 
46
  with gr.Blocks() as demo:
47
+ gr.Markdown("# Welcome to the Menu")
 
 
 
 
 
48
 
49
+ # Audio input and output
50
+ audio_input = gr.Audio(label="Speak your preference or order", type="filepath")
51
+ audio_output = gr.Audio(label="Assistant Response", autoplay=True)
 
52
 
53
+ # Textbox for transcription
54
+ transcription = gr.Textbox(label="Transcription", placeholder="Detected text will appear here")
55
 
56
+ # Cart output
57
+ cart_output = gr.Textbox(label="Cart", placeholder="Your cart details will appear here")
58
+
59
+ # Audio processing
60
  audio_input.change(
61
+ process_audio,
62
+ inputs=[audio_input, transcription],
63
+ outputs=[audio_output, transcription]
64
  )
65
 
66
  return demo
67
 
 
68
  if __name__ == "__main__":
69
+ demo = app()
70
+ demo.launch()