AashishKumar commited on
Commit
2a0f5f8
·
1 Parent(s): 8e14ec0

Create app.py

Browse files

The above code is the restaurant chatbot Using ChatGPT , WhisperAI and zomato dataset

Files changed (1) hide show
  1. app.py +183 -0
app.py ADDED
@@ -0,0 +1,183 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import requests
3
+ import speech_recognition as sr
4
+ import gradio as gr
5
+
6
+ restaurants = {
7
+ "Joe's Pizza": {
8
+ "Margherita Pizza": 14.99,
9
+ "Pepperoni Pizza": 16.99,
10
+ "Vegetable Pizza": 15.99,
11
+ "Caesar Salad": 8.99,
12
+ "Garlic Knots": 5.99,
13
+ "Soda (Coke, Diet Coke, Sprite)": 2.99,
14
+ "Chocolate Chip Cookie": 2.99
15
+ },
16
+ "Burger King": {
17
+ "Whopper Meal": 7.99,
18
+ "Chicken Fries": 3.99,
19
+ "Impossible Whopper": 5.99,
20
+ "Chicken Sandwich": 4.99,
21
+ "Onion Rings": 2.99,
22
+ "Fountain Drink": 1.99,
23
+ "Apple Pie": 1.49
24
+ },
25
+ "Taco Bell": {
26
+ "Crunchwrap Supreme": 4.99,
27
+ "Beef Quesarito": 3.99,
28
+ "Nachos Supreme": 2.99,
29
+ "Cheesy Gordita Crunch": 4.99,
30
+ "Soft Taco": 1.29,
31
+ "Baja Blast Freeze": 2.49,
32
+ "Cinnamon Twists": 1.99
33
+ },
34
+ "McDonald's": {
35
+ "Big Mac Meal": 6.49,
36
+ "10-Piece Chicken McNuggets": 4.29,
37
+ "Filet-O-Fish": 3.79,
38
+ "Quarter Pounder with Cheese": 5.19,
39
+ "French Fries": 1.89,
40
+ "Soft Drink": 1.00,
41
+ "Apple Pie": 0.99
42
+ },
43
+ "Chipotle": {
44
+ "Burrito Bowl": 7.99,
45
+ "Steak Quesadilla": 4.99,
46
+ "Crispy Tacos": 3.99,
47
+ "Barbacoa Salad": 8.99,
48
+ "Chips and Guac": 3.99,
49
+ "Soda": 2.29,
50
+ "Chocolate Brownie": 2.25
51
+ }
52
+ }
53
+
54
+
55
+ # ChatGPT API setup
56
+ openai.api_key = "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"
57
+
58
+ def recognize_speech(audio):
59
+ audio_file = open(audio,"rb")
60
+ transcript=openai.Audio.transcribe("whisper-1",audio_file)
61
+ print(transcript)
62
+
63
+ return transcript["text"]
64
+
65
+ # Chatbot function
66
+ def chatbot(command):
67
+ prompt = f"I want to {command}."
68
+ response = openai.Completion.create(
69
+ engine="davinci",
70
+ prompt=prompt,
71
+ max_tokens=1024,
72
+ n=1,
73
+ stop=None,
74
+ temperature=0.5,
75
+ )
76
+ return response.choices[0].text.strip()
77
+
78
+ # Get menu for a specific restaurant
79
+ def get_menu_items(restaurant):
80
+ return restaurants[restaurant]
81
+
82
+ def identify_food_command(text):
83
+ keywords = ['order', 'menu', 'food']
84
+ for keyword in keywords:
85
+ if keyword in text:
86
+ return keyword
87
+ return None
88
+ def find_restaurant(food_item):
89
+ restaurant_prices = {}
90
+ # Iterate through the `restaurants` dictionary and create a list of restaurants that contain the food item
91
+ for restaurant, menu_items in restaurants.items():
92
+ if food_item.lower() in menu_items:
93
+ restaurant_prices[restaurant] = menu_items[food_item.lower()]
94
+
95
+ if len(restaurant_prices) == 0:
96
+ # If no restaurants contain the food item, return an appropriate message
97
+ return "Sorry, we could not find any restaurants that serve that item."
98
+
99
+ # Find the restaurant with the least price for the food item
100
+ min_price = min(restaurant_prices.values())
101
+ restaurant_names = [restaurant for restaurant, price in restaurant_prices.items() if price == min_price]
102
+
103
+ # If there are multiple restaurants with the same price, return all of them
104
+ if len(restaurant_names) > 1:
105
+ return "You can order that item from the following restaurants: " + ', '.join(restaurant_names)
106
+ else:
107
+ return restaurant_names[0]
108
+
109
+ # Main function to handle user inputs and chatbot responses
110
+ def main(audio):
111
+ while True:
112
+ try:
113
+ print("What can I help you with?")
114
+ command = recognize_speech(audio)
115
+ print(f"You said: {command}")
116
+ food_command = identify_food_command(command)
117
+ if food_command in ['order', 'eat']:
118
+ restaurant_name = ''
119
+ for restaurant in restaurants:
120
+ if restaurant in command :
121
+ restaurant_name=restaurant
122
+ break;
123
+ if restaurant_name == '':
124
+ food_item = chatbot(command)
125
+ print(food_item)
126
+ # Find the best restaurant for the food item based on price
127
+ best_restaurant = find_restaurant(food_item)
128
+
129
+ # Return a message to the user confirming the order
130
+ if isinstance(best_restaurant, str):
131
+ response = f"You ordered {food_item} from {best_restaurant}."
132
+
133
+ elif any(food in command for food in restaurants[restaurant_name]):
134
+ item = next((food for food in restaurants[restaurant_name] if food in command), None)
135
+ response = f"\nYou ordered {item} from {restaurant_name.title()}\nGreat! Thank you for ordering."
136
+ else :
137
+ menu_items = get_menu_items(restaurant_name)
138
+ print(f"Sure, here's the menu for {restaurant_name}: {menu_items} What would you like to order?")
139
+ max_tries = 3
140
+ for i in range(max_tries):
141
+ order_audio = input()
142
+ item = order_audio
143
+ if item == "nothing":
144
+ response = "Okay! You don't want anything"
145
+ break
146
+ elif item in restaurants[restaurant_name].keys():
147
+ response = f"\nYou ordered {item} from {restaurant_name.title()}\nGreat! Thank you for ordering."
148
+ break
149
+ else:
150
+ if i == max_tries - 1:
151
+ response = "Sorry, you didn't provide any valid input. Goodbye!"
152
+ break
153
+ else:
154
+ print("Sorry, that item is not available at this restaurant. Please try again.")
155
+
156
+ elif food_command in ['menu' , 'menus']:
157
+ for restaurant in restaurants:
158
+ if restaurant in command:
159
+ menu_items = get_menu_items(restaurant)
160
+ response = f"Here's the menu for {restaurant}: {menu_items}"
161
+ break
162
+ else:
163
+ response= "Sorry, I didn't catch the restaurant name. Could you please repeat that?"
164
+ elif food_command == 'food':
165
+ response=chatbot(command)
166
+ else:
167
+ response=chatbot(command)
168
+
169
+ return response
170
+
171
+ except sr.UnknownValueError:
172
+ print("Sorry, I did not understand what you said.")
173
+ interface = gr.Interface(
174
+ main,
175
+ inputs=gr.Audio(source="microphone", type="filepath", label="Input Audio"),
176
+ outputs= gr.Textbox(label="Foodie Chatbot's response"),
177
+ title="Foodie Chatbot",
178
+ description="Talk to the Foodie Chatbot and get restaurant recommendations and menus!",
179
+ )
180
+ if __name__ == "__main__":
181
+ interface.launch(share=True ,inline=False)
182
+
183
+