DSatishchandra commited on
Commit
80d3c78
·
verified ·
1 Parent(s): f65fa0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -53
app.py CHANGED
@@ -12,8 +12,7 @@ menu = {
12
  "Drinks": ["Mango Lassi", "Soda", "Water"]
13
  }
14
 
15
- # Conversation history and context
16
- conversation_history = []
17
  context = {"last_action": None, "order": []}
18
 
19
  # Text-to-Speech Function
@@ -38,78 +37,73 @@ def recognize_speech(audio_file_path):
38
  # Voice Assistant Logic
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}")
46
- return response, speak_and_save(response)
47
 
48
  global context
49
- response = ""
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
- # Finalize the order
75
- elif "no" in user_input.lower() or "that's it" in user_input.lower():
76
- response = f"Your final order is: {', '.join(context['order'])}. Thank you for your order. Your food will arrive shortly. Enjoy your meal!"
77
- conversation_history.append(f"Order Summary: {context['order']}")
78
- context["last_action"] = "final_order"
79
- context["order"] = [] # Reset the order
80
-
81
- # Mid-meal check
82
- elif "everything alright" in user_input.lower() or "anything else" in user_input.lower():
83
- response = "I hope you're enjoying your meal. Can I bring you anything else?"
84
- context["last_action"] = "check_meal"
85
 
86
- # Default response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  else:
88
- response = "I'm not sure what you meant. Could you clarify?"
89
- context["last_action"] = "clarify"
90
 
91
- conversation_history.append(f"AI: {response}")
92
  audio_response_path = speak_and_save(response)
93
  return response, audio_response_path
94
 
95
- # Save Conversation History
96
- def save_conversation():
97
- with open("conversation_history.txt", "w") as f:
98
- f.write("\n".join(conversation_history))
99
- return "Conversation history saved successfully!"
100
-
101
  # Gradio Interface
102
  iface = gr.Interface(
103
  fn=process_order,
104
- inputs=gr.Audio(type="filepath"),
105
- outputs=[gr.Textbox(label="Text Response"), gr.Audio(label="Audio Response")],
106
  title="Restaurant Voice Assistant",
107
- description="Talk to our voice assistant to place your order or ask about the menu!",
108
- live=True
109
  )
110
 
111
  if __name__ == "__main__":
112
- try:
113
- iface.launch()
114
- finally:
115
- save_conversation()
 
12
  "Drinks": ["Mango Lassi", "Soda", "Water"]
13
  }
14
 
15
+ # Context to track ongoing conversation
 
16
  context = {"last_action": None, "order": []}
17
 
18
  # Text-to-Speech Function
 
37
  # Voice Assistant Logic
38
  def process_order(audio_file_path):
39
  user_input = recognize_speech(audio_file_path)
 
 
40
  if user_input.startswith("Sorry") or user_input.startswith("I didn't"):
41
+ return user_input, None
 
 
42
 
43
  global context
44
+
45
+ # Default response
46
+ response = f"You said: {user_input}\n"
47
+
48
+ # Process the user input
49
+ if "menu" in user_input.lower():
50
+ response += "Here is our menu:\n"
 
 
 
51
  for category, items in menu.items():
52
  response += f"{category}: {', '.join(items)}\n"
 
53
  context["last_action"] = "show_menu"
54
+
55
+ elif "order" in user_input.lower():
56
+ if context["last_action"] == "show_menu":
57
+ response += "What would you like to order?"
58
+ else:
59
+ response += "Please let me know what you'd like to order. We have starters, main course, breads, desserts, and drinks."
60
+ context["last_action"] = "place_order"
61
+
62
+ elif any(item.lower() in user_input.lower() for item in sum(menu.values(), [])):
63
+ # Add the ordered item to the context
64
  for category, items in menu.items():
65
  for item in items:
66
  if item.lower() in user_input.lower():
67
  context["order"].append(item)
68
+ response += f"I've added {item} to your order. Would you like anything else?"
69
  break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
+ elif "no" in user_input.lower() or "done" in user_input.lower():
72
+ if context["order"]:
73
+ response += f"Your final order is: {', '.join(context['order'])}. Shall I place this order for you?"
74
+ context["last_action"] = "confirm_order"
75
+ else:
76
+ response += "It seems you haven't ordered anything yet."
77
+
78
+ elif "yes" in user_input.lower() and context["last_action"] == "confirm_order":
79
+ response += f"Thank you! Your order for {', '.join(context['order'])} has been placed. Would you like to add a dessert or drink?"
80
+ context["last_action"] = "offer_suggestions"
81
+
82
+ elif any(item.lower() in user_input.lower() for item in menu["Desserts"] + menu["Drinks"]):
83
+ # Add desserts or drinks to the order
84
+ for item in menu["Desserts"] + menu["Drinks"]:
85
+ if item.lower() in user_input.lower():
86
+ context["order"].append(item)
87
+ response += f"I've added {item} to your order. Your updated order is: {', '.join(context['order'])}. Thank you for ordering with us!"
88
+ context["order"] = [] # Reset the order
89
+ context["last_action"] = None
90
+ break
91
+
92
  else:
93
+ response += "I'm not sure what you meant. Could you clarify?"
 
94
 
95
+ # Generate audio response
96
  audio_response_path = speak_and_save(response)
97
  return response, audio_response_path
98
 
 
 
 
 
 
 
99
  # Gradio Interface
100
  iface = gr.Interface(
101
  fn=process_order,
102
+ inputs=gr.Audio(type="filepath"), # Accept audio input as a file path
103
+ outputs=[gr.Textbox(label="Text Response"), gr.Audio(label="Audio Response")], # Include audio output
104
  title="Restaurant Voice Assistant",
105
+ description="Talk to our voice assistant to place your order or ask about the menu!"
 
106
  )
107
 
108
  if __name__ == "__main__":
109
+ iface.launch()