geethareddy commited on
Commit
957967f
·
verified ·
1 Parent(s): b768f95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -34
app.py CHANGED
@@ -12,59 +12,66 @@ def load_menu():
12
  except Exception as e:
13
  raise ValueError(f"Error loading menu file: {e}")
14
 
15
- # Filter menu items based on preference
16
- def get_menu_items():
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 input and respond
32
- def process_input(audio_file):
33
- user_text = "menu" # Assume Whisper transcribes the input here
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- if "menu" in user_text.lower():
36
- menu_details = get_menu_items()
37
- response_text = f"Here are the menu details: {menu_details}"
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="Click the microphone to start", type="filepath")
51
- audio_output = gr.Audio(label="Assistant Response", autoplay=True)
52
 
53
- # 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
- # When microphone is clicked, greet the user
60
- def greet_user():
61
- welcome_message = "Hello, welcome to Biryani Hub. What do you want to order?"
62
- tts_path = generate_tts_response(welcome_message)
63
- return tts_path, welcome_message
64
 
65
- # Add click event to greet user
66
- audio_input.change(
67
- greet_user, inputs=[], outputs=[audio_output, transcription]
 
 
68
  )
69
 
70
  return demo
 
12
  except Exception as e:
13
  raise ValueError(f"Error loading menu file: {e}")
14
 
15
+ # Generate TTS response
16
+ async def generate_tts_response(text):
 
 
 
 
 
 
 
 
17
  communicate = edge_tts.Communicate(text)
18
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
19
  tmp_path = tmp_file.name
20
+ await communicate.save(tmp_path)
21
  return tmp_path
22
 
23
+ # Welcome Note Handler
24
+ async def play_welcome_note():
25
+ welcome_text = "Hello, welcome to Biryani Hub. What do you want to order?"
26
+ return await generate_tts_response(welcome_text), "Hello, welcome to Biryani Hub. What do you want to order?"
27
+
28
+ # Process customer input
29
+ async def process_customer_input(audio_file):
30
+ # Simulated transcription (Replace with actual transcription logic)
31
+ transcription = "menu" # This should be replaced with audio transcription logic
32
+
33
+ if "menu" in transcription.lower():
34
+ menu_data = load_menu()
35
+ menu_details = "Here is our menu: " + ", ".join(
36
+ f"{item['Dish Name']} - ${item['Price ($)']}" for _, item in menu_data.iterrows()
37
+ )
38
+ tts_path = await generate_tts_response(menu_details)
39
+ return tts_path, menu_details
40
+
41
+ elif "veg samosa" in transcription.lower():
42
+ response_text = "Order confirmed for Veg Samosa. It has been added to your cart."
43
+ tts_path = await generate_tts_response(response_text)
44
+ return tts_path, response_text
45
 
 
 
 
46
  else:
47
  response_text = "I'm sorry, I didn't understand that. Please try again."
48
+ tts_path = await generate_tts_response(response_text)
49
+ return tts_path, response_text
 
50
 
51
  # Build Gradio app
52
  def app():
53
  with gr.Blocks() as demo:
54
  gr.Markdown("# Welcome to the Menu")
55
 
56
+ # Welcome note audio output
57
+ welcome_audio_output = gr.Audio(label="Welcome Note", autoplay=True)
 
58
 
59
+ # Customer audio input and response
60
+ customer_audio_input = gr.Audio(label="Click to speak your order", type="filepath")
61
+ customer_audio_output = gr.Audio(label="Assistant Response", autoplay=True)
62
+ transcription_output = gr.Textbox(label="Transcription", placeholder="Detected text will appear here")
63
 
64
+ # Cart details
65
  cart_output = gr.Textbox(label="Cart", placeholder="Your cart details will appear here")
66
 
67
+ # Play welcome note
68
+ welcome_audio_output.update(value=asyncio.run(play_welcome_note())[0])
 
 
 
69
 
70
+ # Process customer input
71
+ customer_audio_input.change(
72
+ lambda audio: asyncio.run(process_customer_input(audio)),
73
+ inputs=[customer_audio_input],
74
+ outputs=[customer_audio_output, transcription_output]
75
  )
76
 
77
  return demo