Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
| 72 |
if "find" in text and "restaurant" in text:
|
| 73 |
-
for cuisine in df[
|
| 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
|
| 79 |
if "best" in text:
|
| 80 |
-
for cuisine in df[
|
| 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
|
| 86 |
-
if "cheap" in text or "
|
| 87 |
return cheapest_high_rated().to_html(index=False)
|
| 88 |
|
| 89 |
-
# 4
|
| 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
|
| 96 |
day = m.group(1)
|
| 97 |
-
|
| 98 |
-
if
|
| 99 |
-
return "No
|
| 100 |
-
return
|
| 101 |
|
| 102 |
-
# 5) Fallback semantic
|
| 103 |
return semantic_search(message).to_html(index=False)
|
| 104 |
|
| 105 |
|
| 106 |
# -------------------------------
|
| 107 |
-
#
|
| 108 |
# -------------------------------
|
| 109 |
def chatbot_fn(history, message, customer_id):
|
| 110 |
reply_html = handle_query(message, customer_id)
|
| 111 |
|
| 112 |
-
#
|
| 113 |
-
history.append(
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
return history, "", reply_html
|
| 116 |
|
| 117 |
|
| 118 |
# -------------------------------
|
| 119 |
-
#
|
| 120 |
# -------------------------------
|
| 121 |
with gr.Blocks() as demo:
|
| 122 |
gr.Markdown("## 🍽️ Restaurant Guide Chatbot")
|
| 123 |
|
| 124 |
-
chat = gr.Chatbot(label="Chat History")
|
| 125 |
-
|
| 126 |
|
| 127 |
with gr.Row():
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
|
| 132 |
-
|
| 133 |
chatbot_fn,
|
| 134 |
-
inputs=[chat,
|
| 135 |
-
outputs=[chat,
|
| 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()
|