AashishKumar commited on
Commit
358411f
·
1 Parent(s): e40dbad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -61
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import openai
2
- import requests
3
  import speech_recognition as sr
4
  import gradio as gr
5
 
@@ -20,7 +19,7 @@ restaurants = {
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,
@@ -62,19 +61,32 @@ def recognize_speech(audio):
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]
@@ -85,27 +97,7 @@ def identify_food_command(text):
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:
@@ -113,27 +105,39 @@ def main(audio):
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.title() 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
@@ -152,8 +156,12 @@ def main(audio):
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)
@@ -161,15 +169,22 @@ def main(audio):
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"),
@@ -178,6 +193,5 @@ interface = gr.Interface(
178
  description="Talk to the Foodie Chatbot and get restaurant recommendations and menus!",
179
  )
180
  if __name__ == "__main__":
181
- interface.launch(inline=False)
182
-
183
 
 
1
  import openai
 
2
  import speech_recognition as sr
3
  import gradio as gr
4
 
 
19
  "Chicken Sandwich": 4.99,
20
  "Onion Rings": 2.99,
21
  "Fountain Drink": 1.99,
22
+ "Apple Pie": 1.49
23
  },
24
  "Taco Bell": {
25
  "Crunchwrap Supreme": 4.99,
 
61
 
62
  return transcript["text"]
63
 
 
64
  def chatbot(command):
65
+ response = openai.ChatCompletion.create(
66
+ model="gpt-3.5-turbo",
67
+ messages=[
68
+ {"role": "system", "content": "You are a Restaurants chatbot Who takes order,shows menus and answers users food related querry's presiously"},
69
+ {"role": "user", "content": command},
70
+ ]
71
+ )
72
+ result = ''
73
+ for choice in response.choices:
74
+ result += choice.message.content
75
+ return result
76
 
77
+ def nlp(txt_summ):
78
+ completion = openai.Completion.create(
79
+ model="text-davinci-003",
80
+ prompt= txt_summ ,
81
+ temperature=0.7,
82
+ max_tokens=64,
83
+ top_p=1.0,
84
+ frequency_penalty=0.0,
85
+ presence_penalty=0.0
86
+ )
87
+ response = completion.choices[0].text
88
+
89
+ return response
90
  # Get menu for a specific restaurant
91
  def get_menu_items(restaurant):
92
  return restaurants[restaurant]
 
97
  if keyword in text:
98
  return keyword
99
  return None
100
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  # Main function to handle user inputs and chatbot responses
102
  def main(audio):
103
  while True:
 
105
  print("What can I help you with?")
106
  command = recognize_speech(audio)
107
  print(f"You said: {command}")
108
+ txt_command ="extract the food related command keyword from the sentence :\n\n"+command
109
+ food_command = nlp(txt_command).strip()
110
+
111
+ restaurant_name = ''
112
+ txt_extract = "extract the restaurants name keyword from the sentence :\n\n"+command
113
+ restaurant_name = ((nlp(txt_extract)).strip()).title()
114
+ found_rest = False
115
+ if(restaurant_name in restaurants.keys()):
116
+ found_rest = True
117
+
118
+ item_name = ''
119
+ txt_extract = "extract the food item from the sentence :\n\n"+command
120
+ item_name = ((nlp(txt_extract)).strip()).title()
121
+ found_item = False
122
+ if(restaurant_name in restaurants.keys()):
123
+ found_item = True
124
+
125
+
126
+ if food_command in ['order', 'eat' , 'want' , 'serve' , 'prepare' ]:
127
+ if not found_rest and found_item:
128
+ temp_val = {}
129
+ for restaurant, rest_info in restaurants.items():
130
+ if item_name in rest_info:
131
+ temp_val[restaurant] = int(rest_info[item_name])
132
+ if temp_val:
133
+ min_price = min(temp_val.values())
134
+ res = [key for key in temp_val if temp_val[key] == min_price]
135
+ response = f"You have ordered {item_name} from {res[0]} with price of {min_price}"
136
+
137
+ elif found_rest and found_item:
138
+ response = f"\nYou ordered {item_name} from {restaurant_name}\nGreat! Thank you for ordering."
139
+
140
+ elif found_rest and not found_item :
141
  menu_items = get_menu_items(restaurant_name)
142
  print(f"Sure, here's the menu for {restaurant_name}: {menu_items} What would you like to order?")
143
  max_tries = 3
 
156
  break
157
  else:
158
  print("Sorry, that item is not available at this restaurant. Please try again.")
159
+ else :
160
+ resp = "Respond properly and Try to make the Customer buy some food and for the valid response"
161
+ response = chatbot(resp)
162
+
163
 
164
+ elif food_command in ['menu' , 'menus' , 'catalogue' , 'items' , 'something']:
165
  for restaurant in restaurants:
166
  if restaurant in command:
167
  menu_items = get_menu_items(restaurant)
 
169
  break
170
  else:
171
  response= "Sorry, I didn't catch the restaurant name. Could you please repeat that?"
172
+ elif identify_food_command(command) == 'food':
173
+ response=chatbot("Respond a person properly who has come to your restaurant asking food")
174
  else:
175
+ response=chatbot("Response, as if you cannot understand and make the person salivate so that he buys a food . Also Give proper reply for the output\n"+command)
176
 
177
  return response
178
 
179
  except sr.UnknownValueError:
180
  print("Sorry, I did not understand what you said.")
181
+ except sr.RequestError:
182
+ print("Sorry, I am unable to process your request.")
183
+ except Exception as e:
184
+ print("An error occurred:", e)
185
+
186
+
187
+
188
  interface = gr.Interface(
189
  main,
190
  inputs=gr.Audio(source="microphone", type="filepath", label="Input Audio"),
 
193
  description="Talk to the Foodie Chatbot and get restaurant recommendations and menus!",
194
  )
195
  if __name__ == "__main__":
196
+ interface.launch(share=True ,debug=True)
 
197