HmlFTR / app.py
Jofax's picture
Create app.py
dc1a426 verified
import gradio as gr
from sentence_transformers import SentenceTransformer, util
import pandas as pd
import torch
# 1. Load a lightweight, fast model suitable for Hugging Face Free Tier
model = SentenceTransformer('all-MiniLM-L6-v2')
# 2. Local Data (You can expand this list or link a Google Sheet)
data = [
{"name": "Redwave Mega Store", "location": "Phase 2, Vinares", "items": "Groceries, Electronics, Furniture, Home decor"},
{"name": "Authentic Maldives", "location": "Centro Mall, Phase 1", "items": "Gifts, Local crafts, Souvenirs"},
{"name": "Hiyaa Coffee", "location": "Tower H12, Phase 2", "items": "Short eats, Coffee, Tea, Breakfast"},
{"name": "Hulhumale Hospital", "location": "Phase 1, Near Central Park", "items": "Doctor, Pharmacy, Emergency, Clinic"},
{"name": "Local Hardware", "location": "Phase 1, Fitron Magu", "items": "Pipes, Paint, Tools, AC repair parts"},
{"name": "Quick Fix Mobile", "location": "Phase 2, Near Hiyaa H7", "items": "Phone repair, Screen replacement, Chargers"}
]
df = pd.DataFrame(data)
# Pre-calculate embeddings for speed
descriptions = df['items'].tolist()
description_embeddings = model.encode(descriptions, convert_to_tensor=True)
def search_hulhumale(query):
# Encode user query
query_embedding = model.encode(query, convert_to_tensor=True)
# Compute similarity scores
cos_scores = util.cos_sim(query_embedding, description_embeddings)[0]
# Get top 3 results
top_results = torch.topk(cos_scores, k=min(3, len(df)))
results_text = ""
for score, idx in zip(top_results.values, top_results.indices):
row = df.iloc[int(idx)]
results_text += f"### 📍 {row['name']}\n**Location:** {row['location']}\n**Known for:** {row['items']}\n\n---\n"
return results_text if results_text else "Sorry, I couldn't find a match for that in Hulhumalé."
# 3. Create the Gradio Interface
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# 🏝️ Hulhu-Search AI")
gr.Markdown("Find anything in Phase 1 or Phase 2 using AI. Try typing 'broken screen' or 'coffee near Vinares'.")
with gr.Row():
input_text = gr.Textbox(label="What are you looking for?", placeholder="e.g. Where can I buy paint?")
output_html = gr.Markdown(label="Recommended Shops")
submit_btn = gr.Button("Search Hulhumalé")
submit_btn.click(fn=search_hulhumale, inputs=input_text, outputs=output_html)
demo.launch()