Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from sentence_transformers import SentenceTransformer, util
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import pickle
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# 1. Load the Dataset and Embeddings
|
| 8 |
+
dataset_path = "cleaned_dataset_10k.csv"
|
| 9 |
+
embeddings_path = "final_embeddings_10k.pkl"
|
| 10 |
+
|
| 11 |
+
# Check if files exist
|
| 12 |
+
if not os.path.exists(dataset_path) or not os.path.exists(embeddings_path):
|
| 13 |
+
raise FileNotFoundError("Files not found. Please upload cleaned_dataset_10k.csv and final_embeddings_10k.pkl")
|
| 14 |
+
|
| 15 |
+
# Load Data
|
| 16 |
+
df = pd.read_csv(dataset_path)
|
| 17 |
+
|
| 18 |
+
# Load Embeddings
|
| 19 |
+
with open(embeddings_path, "rb") as fIn:
|
| 20 |
+
stored_data = pickle.load(fIn)
|
| 21 |
+
stored_embeddings = stored_data['embeddings']
|
| 22 |
+
|
| 23 |
+
# 2. Load the Model
|
| 24 |
+
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
| 25 |
+
|
| 26 |
+
# 3. Define the Search Function
|
| 27 |
+
def search_restaurant(query):
|
| 28 |
+
# Encode the user's query
|
| 29 |
+
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 30 |
+
|
| 31 |
+
# Perform semantic search (find top 3 matches)
|
| 32 |
+
hits = util.semantic_search(query_embedding, stored_embeddings, top_k=3)
|
| 33 |
+
hits = hits[0]
|
| 34 |
+
|
| 35 |
+
results = []
|
| 36 |
+
for hit in hits:
|
| 37 |
+
row_id = hit['corpus_id']
|
| 38 |
+
row = df.iloc[row_id]
|
| 39 |
+
|
| 40 |
+
# Create a nice text output for each result
|
| 41 |
+
result_text = (
|
| 42 |
+
f"🍽️ **Name:** {row['Restaurant Name']}\n"
|
| 43 |
+
f"🥘 **Cuisine:** {row['Food Type']}\n"
|
| 44 |
+
f"⭐ **Rating:** {row['Rating']}\n"
|
| 45 |
+
f"📍 **Address:** {row['Address']}\n"
|
| 46 |
+
f"💬 **Review:** \"{row['Review']}\"\n"
|
| 47 |
+
f"----------------------------------------"
|
| 48 |
+
)
|
| 49 |
+
results.append(result_text)
|
| 50 |
+
|
| 51 |
+
return "\n\n".join(results)
|
| 52 |
+
|
| 53 |
+
# 4. Create the App Interface
|
| 54 |
+
# These examples satisfy instruction #7 ("3 Quick Starters")
|
| 55 |
+
examples = [
|
| 56 |
+
["I want a romantic italian dinner"],
|
| 57 |
+
["Best sushi place with good service"],
|
| 58 |
+
["Cheap fast food for lunch"]
|
| 59 |
+
]
|
| 60 |
+
|
| 61 |
+
interface = gr.Interface(
|
| 62 |
+
fn=search_restaurant,
|
| 63 |
+
inputs=gr.Textbox(lines=2, placeholder="Type here... (e.g., 'Spicy Mexican food')"),
|
| 64 |
+
outputs=gr.Markdown(label="Recommended Restaurants"),
|
| 65 |
+
title="Restaurant Recommendation System 🍔",
|
| 66 |
+
description="Describe what you want to eat, and I'll find the best match!",
|
| 67 |
+
examples=examples,
|
| 68 |
+
theme="default"
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# 5. Launch
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
interface.launch()
|