Spaces:
Sleeping
Sleeping
Create app
Browse files
app
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY") # Use environment variable for security
|
| 7 |
+
GROQ_MODEL = "mixtral-8x7b-32768"
|
| 8 |
+
|
| 9 |
+
with open("data/restaurants.json", "r") as f:
|
| 10 |
+
RESTAURANTS = json.load(f)
|
| 11 |
+
|
| 12 |
+
def query_groq(prompt: str) -> dict:
|
| 13 |
+
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 14 |
+
headers = {
|
| 15 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 16 |
+
"Content-Type": "application/json"
|
| 17 |
+
}
|
| 18 |
+
data = {
|
| 19 |
+
"model": GROQ_MODEL,
|
| 20 |
+
"messages": [
|
| 21 |
+
{"role": "user", "content": prompt}
|
| 22 |
+
],
|
| 23 |
+
"temperature": 0.3
|
| 24 |
+
}
|
| 25 |
+
res = requests.post(url, headers=headers, json=data)
|
| 26 |
+
res.raise_for_status()
|
| 27 |
+
result = res.json()["choices"][0]["message"]["content"]
|
| 28 |
+
return json.loads(result)
|
| 29 |
+
|
| 30 |
+
def extract_preferences(query: str) -> dict:
|
| 31 |
+
prompt = f'''
|
| 32 |
+
Extract the following preferences from this user query:
|
| 33 |
+
- cuisine
|
| 34 |
+
- budget (in TZS)
|
| 35 |
+
- location
|
| 36 |
+
- ambiance
|
| 37 |
+
|
| 38 |
+
Query: "{query}"
|
| 39 |
+
Return as JSON with those exact keys.
|
| 40 |
+
'''
|
| 41 |
+
return query_groq(prompt)
|
| 42 |
+
|
| 43 |
+
def score_restaurant(r: dict, prefs: dict) -> float:
|
| 44 |
+
score = 0
|
| 45 |
+
if prefs.get("cuisine") and prefs["cuisine"].lower() in r["cuisine"].lower():
|
| 46 |
+
score += 1
|
| 47 |
+
if prefs.get("location") and prefs["location"].lower() in r["location"].lower():
|
| 48 |
+
score += 1
|
| 49 |
+
if prefs.get("ambiance") and prefs["ambiance"].lower() in r.get("ambiance", "").lower():
|
| 50 |
+
score += 1
|
| 51 |
+
if prefs.get("budget") and r.get("average_price", 0) <= prefs["budget"]:
|
| 52 |
+
score += 1
|
| 53 |
+
return score
|
| 54 |
+
|
| 55 |
+
def recommend_restaurants(query: str) -> str:
|
| 56 |
+
try:
|
| 57 |
+
prefs = extract_preferences(query)
|
| 58 |
+
scored = [(r, score_restaurant(r, prefs)) for r in RESTAURANTS]
|
| 59 |
+
ranked = sorted(scored, key=lambda x: x[1], reverse=True)
|
| 60 |
+
top = [r for r, s in ranked if s > 0][:5]
|
| 61 |
+
if not top:
|
| 62 |
+
return "Sorry, no matches found. Try another query."
|
| 63 |
+
return "\n\n".join([
|
| 64 |
+
f"🍽️ {r['name']}\n📍 {r['location']}\n💰 Avg Price: {r['average_price']} TZS\n🥘 Cuisine: {r['cuisine']}\n🌟 Features: {', '.join(r.get('features', []))}" for r in top
|
| 65 |
+
])
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return f"Error: {str(e)}"
|
| 68 |
+
|
| 69 |
+
iface = gr.Interface(
|
| 70 |
+
fn=recommend_restaurants,
|
| 71 |
+
inputs=gr.Textbox(label="Ask for a restaurant (e.g. Romantic place under 30,000 TZS)"),
|
| 72 |
+
outputs=gr.Textbox(label="Top Recommendations"),
|
| 73 |
+
title="🍴 Arusha Restaurant Recommender",
|
| 74 |
+
description="Powered by Groq LLM — Get smart, personalized restaurant suggestions in Arusha, Tanzania."
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
iface.launch()
|