Ahmad-01 commited on
Commit
6ebb8d6
·
verified ·
1 Parent(s): e724547

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -25
app.py CHANGED
@@ -68,71 +68,73 @@ def semantic_search(query, k=5):
68
  def handle_query(message, customer_id=""):
69
  text = message.lower()
70
 
71
- # 1) Find a cuisine
72
  if "find" in text and "restaurant" in text:
73
- for cuisine in df['cuisine_type'].str.lower().unique():
74
  if cuisine in text:
75
  return find_by_cuisine(cuisine).to_html(index=False)
76
  return semantic_search(message).to_html(index=False)
77
 
78
- # 2) Best places
79
  if "best" in text:
80
- for cuisine in df['cuisine_type'].str.lower().unique():
81
  if cuisine in text:
82
  return best_rated_by_cuisine(cuisine).to_html(index=False)
83
  return semantic_search(message).to_html(index=False)
84
 
85
- # 3) Cheap recommendations
86
- if "cheap" in text or "cheapest" in text or "value" in text:
87
  return cheapest_high_rated().to_html(index=False)
88
 
89
- # 4) Personalized recall
90
  if "what did i order" in text:
91
  m = re.search(r"on (\w+)", text)
92
  if not customer_id:
93
  return "Please enter customer_id."
94
  if not m:
95
- return "Please specify the day, e.g. 'on Tuesday'"
96
  day = m.group(1)
97
- res = personalized_recall(customer_id, day)
98
- if res.empty:
99
- return "No results found."
100
- return res.to_html(index=False)
101
 
102
- # 5) Fallback semantic
103
  return semantic_search(message).to_html(index=False)
104
 
105
 
106
  # -------------------------------
107
- # Chatbot Function (OLD FORMAT)
108
  # -------------------------------
109
  def chatbot_fn(history, message, customer_id):
110
  reply_html = handle_query(message, customer_id)
111
 
112
- # OLD GRADIO FORMAT: append tuple
113
- history.append((message, "Here are the results below 👇"))
 
 
 
114
 
115
  return history, "", reply_html
116
 
117
 
118
  # -------------------------------
119
- # Gradio UI (Compatible with Spaces)
120
  # -------------------------------
121
  with gr.Blocks() as demo:
122
  gr.Markdown("## 🍽️ Restaurant Guide Chatbot")
123
 
124
- chat = gr.Chatbot(label="Chat History")
125
- html_output = gr.HTML(label="Search Results")
126
 
127
  with gr.Row():
128
- user_msg = gr.Textbox(placeholder="Type your message…")
129
- cust_id = gr.Textbox(label="Customer ID (optional)")
130
- send = gr.Button("Send")
131
 
132
- send.click(
133
  chatbot_fn,
134
- inputs=[chat, user_msg, cust_id],
135
- outputs=[chat, user_msg, html_output]
136
  )
137
 
138
  demo.launch()
 
68
  def handle_query(message, customer_id=""):
69
  text = message.lower()
70
 
71
+ # 1. cuisine search
72
  if "find" in text and "restaurant" in text:
73
+ for cuisine in df["cuisine_type"].str.lower().unique():
74
  if cuisine in text:
75
  return find_by_cuisine(cuisine).to_html(index=False)
76
  return semantic_search(message).to_html(index=False)
77
 
78
+ # 2. best-rated query
79
  if "best" in text:
80
+ for cuisine in df["cuisine_type"].str.lower().unique():
81
  if cuisine in text:
82
  return best_rated_by_cuisine(cuisine).to_html(index=False)
83
  return semantic_search(message).to_html(index=False)
84
 
85
+ # 3. cheap places
86
+ if "cheap" in text or "value" in text:
87
  return cheapest_high_rated().to_html(index=False)
88
 
89
+ # 4. personalized recall
90
  if "what did i order" in text:
91
  m = re.search(r"on (\w+)", text)
92
  if not customer_id:
93
  return "Please enter customer_id."
94
  if not m:
95
+ return "Please specify the day (e.g., Tuesday)"
96
  day = m.group(1)
97
+ r = personalized_recall(customer_id, day)
98
+ if r.empty:
99
+ return "No matching records."
100
+ return r.to_html(index=False)
101
 
 
102
  return semantic_search(message).to_html(index=False)
103
 
104
 
105
  # -------------------------------
106
+ # CHATBOT FUNCTION (DICTIONARY FORMAT)
107
  # -------------------------------
108
  def chatbot_fn(history, message, customer_id):
109
  reply_html = handle_query(message, customer_id)
110
 
111
+ # append user message
112
+ history.append({"role": "user", "content": message})
113
+
114
+ # append assistant message
115
+ history.append({"role": "assistant", "content": "Here are the results 👇"})
116
 
117
  return history, "", reply_html
118
 
119
 
120
  # -------------------------------
121
+ # INTERFACE
122
  # -------------------------------
123
  with gr.Blocks() as demo:
124
  gr.Markdown("## 🍽️ Restaurant Guide Chatbot")
125
 
126
+ chat = gr.Chatbot(label="Chat History") # no type arg
127
+ html_out = gr.HTML(label="Search Results")
128
 
129
  with gr.Row():
130
+ msg = gr.Textbox(placeholder="Ask me anything…")
131
+ cid = gr.Textbox(label="Customer ID (optional)")
132
+ btn = gr.Button("Send")
133
 
134
+ btn.click(
135
  chatbot_fn,
136
+ inputs=[chat, msg, cid],
137
+ outputs=[chat, msg, html_out]
138
  )
139
 
140
  demo.launch()