Ahmad-01 commited on
Commit
fe7b22f
·
verified ·
1 Parent(s): e2d40a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -25
app.py CHANGED
@@ -11,67 +11,68 @@ df = pd.read_csv("food_order_cleaned.csv")
11
 
12
  model = SentenceTransformer("all-MiniLM-L6-v2")
13
 
14
- TEXT_FIELDS = ["restaurant_name", "cuisine_type", "rating", "cost_of_the_order"]
15
-
16
  def create_text(row):
17
- text = (
18
  f"Restaurant: {row['restaurant_name']}. "
19
  f"Cuisine: {row['cuisine_type']}. "
20
  f"Rating: {row['rating']}. "
21
  f"Cost: {row['cost_of_the_order']}."
22
  )
23
- return text
24
 
25
  df["text"] = df.apply(create_text, axis=1)
26
  corpus_embeddings = model.encode(df["text"].tolist(), normalize_embeddings=True)
27
 
28
  # -----------------------------
29
- # CHAT LOGIC
30
  # -----------------------------
31
  def bot_reply(user_query, history):
32
  if not user_query.strip():
33
- return history + [("","Please enter a valid question.")]
 
34
 
 
35
  q_emb = model.encode([user_query], normalize_embeddings=True)
36
  sims = cosine_similarity(q_emb, corpus_embeddings)[0]
37
- idx = np.argmax(sims)
38
- best_match = df.iloc[idx]
39
 
 
40
  response = (
41
  f"🍽 **Recommended Restaurant**\n\n"
42
- f"**Name:** {best_match['restaurant_name']}\n"
43
- f"**Cuisine:** {best_match['cuisine_type']}\n"
44
- f"**Rating:** ⭐ {best_match['rating']}\n"
45
- f"**Avg Cost:** {best_match['cost_of_the_order']}\n\n"
46
- f"Let me know if you want more options!"
47
  )
48
 
49
- history = history + [(user_query, response)]
 
50
  return history
51
 
52
 
53
  # -----------------------------
54
- # GRADIO UI (Safe Version)
55
  # -----------------------------
56
  with gr.Blocks() as demo:
57
 
58
  gr.Markdown(
59
  """
60
  # 🍴 Restaurant Guide Chatbot
61
- Ask me for restaurant recommendations!
62
  """
63
  )
64
 
65
- chat = gr.Chatbot(label="Chat with Restaurant Guide", height=500)
 
 
 
 
 
66
 
67
- with gr.Row():
68
- user_in = gr.Textbox(
69
- placeholder="Ask: Find me a Thai restaurant, show me cheap Italian...",
70
- label="Your Question"
71
- )
72
- clear = gr.Button("Clear Chat")
73
 
74
- user_in.submit(bot_reply, [user_in, chat], chat)
75
- clear.click(lambda: [], None, chat)
76
 
77
  demo.launch()
 
11
 
12
  model = SentenceTransformer("all-MiniLM-L6-v2")
13
 
 
 
14
  def create_text(row):
15
+ return (
16
  f"Restaurant: {row['restaurant_name']}. "
17
  f"Cuisine: {row['cuisine_type']}. "
18
  f"Rating: {row['rating']}. "
19
  f"Cost: {row['cost_of_the_order']}."
20
  )
 
21
 
22
  df["text"] = df.apply(create_text, axis=1)
23
  corpus_embeddings = model.encode(df["text"].tolist(), normalize_embeddings=True)
24
 
25
  # -----------------------------
26
+ # CHATBOT LOGIC
27
  # -----------------------------
28
  def bot_reply(user_query, history):
29
  if not user_query.strip():
30
+ history.append(["", "Please enter a valid question."])
31
+ return history
32
 
33
+ # Encode and find best match
34
  q_emb = model.encode([user_query], normalize_embeddings=True)
35
  sims = cosine_similarity(q_emb, corpus_embeddings)[0]
36
+ idx = int(np.argmax(sims))
37
+ best = df.iloc[idx]
38
 
39
+ # Build chatbot response
40
  response = (
41
  f"🍽 **Recommended Restaurant**\n\n"
42
+ f"**Name:** {best['restaurant_name']}\n"
43
+ f"**Cuisine:** {best['cuisine_type']}\n"
44
+ f"**Rating:** ⭐ {best['rating']}\n"
45
+ f"**Avg Cost:** {best['cost_of_the_order']}\n\n"
46
+ f"Let me know if you want more recommendations!"
47
  )
48
 
49
+ # IMPORTANT Must return LISTS, not tuples
50
+ history.append([user_query, response])
51
  return history
52
 
53
 
54
  # -----------------------------
55
+ # GRADIO UI
56
  # -----------------------------
57
  with gr.Blocks() as demo:
58
 
59
  gr.Markdown(
60
  """
61
  # 🍴 Restaurant Guide Chatbot
62
+ Ask me anything—I'll help you find the best place to eat!
63
  """
64
  )
65
 
66
+ chatbox = gr.Chatbot(label="Chat Window", height=450)
67
+
68
+ user_input = gr.Textbox(
69
+ label="Ask something",
70
+ placeholder="Examples: Find me a Thai restaurant, Best Italian options, cheapest good food..."
71
+ )
72
 
73
+ clear = gr.Button("Clear Chat")
 
 
 
 
 
74
 
75
+ user_input.submit(bot_reply, [user_input, chatbox], chatbox)
76
+ clear.click(lambda: [], None, chatbox)
77
 
78
  demo.launch()