DSatishchandra commited on
Commit
c744a6d
·
verified ·
1 Parent(s): 762e547

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -27
app.py CHANGED
@@ -17,10 +17,11 @@ conversation_history = []
17
  context = {"last_action": None, "order": []}
18
 
19
  # Text-to-Speech Function
20
- def speak_and_save(text, filename="response.mp3"):
21
  tts = gTTS(text=text, lang='en')
22
- tts.save(filename)
23
- return filename
 
24
 
25
  # Speech Recognition Function
26
  def recognize_speech(audio_file_path):
@@ -39,7 +40,7 @@ def recognize_speech(audio_file_path):
39
  def process_order(audio_file_path):
40
  user_input = recognize_speech(audio_file_path)
41
  conversation_history.append(f"Customer: {user_input}")
42
-
43
  if user_input.startswith("Sorry") or user_input.startswith("I didn't"):
44
  response = user_input
45
  conversation_history.append(f"AI: {response}")
@@ -50,53 +51,47 @@ def process_order(audio_file_path):
50
 
51
  # Greet the customer
52
  if context["last_action"] is None:
53
- response = "Welcome to our restaurant. How can I assist you today?"
54
  context["last_action"] = "greet"
55
-
56
  # Show menu
57
  elif "menu" in user_input.lower():
58
  response = "Here is our menu:\n"
59
  for category, items in menu.items():
60
- response += f"{category}: {', '.join(items)}\n"
61
  response += "What would you like to order?"
62
  context["last_action"] = "show_menu"
63
-
64
  # Place an order
65
- elif "order" in user_input.lower() or any(item.lower() in user_input.lower() for item in sum(menu.values(), [])):
66
  for category, items in menu.items():
67
  for item in items:
68
  if item.lower() in user_input.lower():
69
  context["order"].append(item)
70
  break
71
- response = f"I’ve added {', '.join(context['order'])} to your order. Would you like anything else?"
72
  context["last_action"] = "place_order"
73
-
74
- # Suggest complementary items
75
- elif "recommend" in user_input.lower() or "suggest" in user_input.lower():
76
- response = "Based on your order, I suggest:"
77
- if "Paneer Butter Masala" in context["order"]:
78
- response += " Mango Lassi or Roti."
79
- elif "Chicken Curry" in context["order"]:
80
- response += " Naan or Soda."
81
- else:
82
- response += " Gulab Jamun for dessert."
83
- context["last_action"] = "recommend"
84
-
85
  # Finalize the order
86
- elif "no" in user_input.lower() or "that’s it" in user_input.lower():
87
  response = f"Your final order is: {', '.join(context['order'])}. Thank you for your order. Your food will arrive shortly. Enjoy your meal!"
88
  conversation_history.append(f"Order Summary: {context['order']}")
89
  context["last_action"] = "final_order"
90
  context["order"] = [] # Reset the order
91
-
92
  # Mid-meal check
93
  elif "everything alright" in user_input.lower() or "anything else" in user_input.lower():
94
- response = "I hope you’re enjoying your meal. Can I bring you anything else?"
95
  context["last_action"] = "check_meal"
96
 
97
  # Default response
98
  else:
99
- response = "I’m not sure what you meant. Could you clarify?"
100
  context["last_action"] = "clarify"
101
 
102
  conversation_history.append(f"AI: {response}")
@@ -112,7 +107,7 @@ def save_conversation():
112
  # Gradio Interface
113
  iface = gr.Interface(
114
  fn=process_order,
115
- inputs=gr.Audio(type="filepath"),
116
  outputs=[gr.Textbox(label="Text Response"), gr.Audio(label="Audio Response")],
117
  title="Restaurant Voice Assistant",
118
  description="Talk to our voice assistant to place your order or ask about the menu!",
 
17
  context = {"last_action": None, "order": []}
18
 
19
  # Text-to-Speech Function
20
+ def speak_and_save(text):
21
  tts = gTTS(text=text, lang='en')
22
+ temp_file = "response.mp3"
23
+ tts.save(temp_file)
24
+ return temp_file
25
 
26
  # Speech Recognition Function
27
  def recognize_speech(audio_file_path):
 
40
  def process_order(audio_file_path):
41
  user_input = recognize_speech(audio_file_path)
42
  conversation_history.append(f"Customer: {user_input}")
43
+
44
  if user_input.startswith("Sorry") or user_input.startswith("I didn't"):
45
  response = user_input
46
  conversation_history.append(f"AI: {response}")
 
51
 
52
  # Greet the customer
53
  if context["last_action"] is None:
54
+ response = "Hello and welcome to Biryani Hub! How can I assist you today?"
55
  context["last_action"] = "greet"
56
+
57
  # Show menu
58
  elif "menu" in user_input.lower():
59
  response = "Here is our menu:\n"
60
  for category, items in menu.items():
61
+ response += f"{category}: {', '.join(items)}.\n"
62
  response += "What would you like to order?"
63
  context["last_action"] = "show_menu"
64
+
65
  # Place an order
66
+ elif any(item.lower() in user_input.lower() for item in sum(menu.values(), [])):
67
  for category, items in menu.items():
68
  for item in items:
69
  if item.lower() in user_input.lower():
70
  context["order"].append(item)
71
  break
72
+ response = f"I've added {', '.join(context['order'])} to your order. Would you like to customize it? For example, spice level or toppings?"
73
  context["last_action"] = "place_order"
74
+
75
+ # Customization
76
+ elif "customize" in user_input.lower() or "spice" in user_input.lower() or "topping" in user_input.lower():
77
+ response = "What customizations would you like? For example, less spicy, extra butter, or no onions?"
78
+ context["last_action"] = "customize_order"
79
+
 
 
 
 
 
 
80
  # Finalize the order
81
+ elif "no" in user_input.lower() or "that's it" in user_input.lower():
82
  response = f"Your final order is: {', '.join(context['order'])}. Thank you for your order. Your food will arrive shortly. Enjoy your meal!"
83
  conversation_history.append(f"Order Summary: {context['order']}")
84
  context["last_action"] = "final_order"
85
  context["order"] = [] # Reset the order
86
+
87
  # Mid-meal check
88
  elif "everything alright" in user_input.lower() or "anything else" in user_input.lower():
89
+ response = "I hope you're enjoying your meal. Can I bring you anything else, like dessert or a drink?"
90
  context["last_action"] = "check_meal"
91
 
92
  # Default response
93
  else:
94
+ response = "I'm not sure what you meant. Could you clarify?"
95
  context["last_action"] = "clarify"
96
 
97
  conversation_history.append(f"AI: {response}")
 
107
  # Gradio Interface
108
  iface = gr.Interface(
109
  fn=process_order,
110
+ inputs=gr.Audio(source="microphone", type="filepath"),
111
  outputs=[gr.Textbox(label="Text Response"), gr.Audio(label="Audio Response")],
112
  title="Restaurant Voice Assistant",
113
  description="Talk to our voice assistant to place your order or ask about the menu!",