mfraz commited on
Commit
8fe4c3d
·
verified ·
1 Parent(s): ae027e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py CHANGED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import json
4
+ from sentence_transformers import SentenceTransformer
5
+ import chromadb
6
+ from bs4 import BeautifulSoup
7
+
8
+ # Initialize embedding model (Open-Source)
9
+ model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
10
+
11
+ # Connect to ChromaDB (Local Open-Source Vector DB)
12
+ client = chromadb.PersistentClient(path="./recipe_db")
13
+ collection = client.get_or_create_collection("recipes")
14
+
15
+ # Function to scrape restaurant data (Alternative to API)
16
+ def scrape_restaurant_info(city, recipe):
17
+ search_url = f"https://www.foodpanda.pk/restaurants?search={recipe}+{city}"
18
+ headers = {"User-Agent": "Mozilla/5.0"}
19
+ response = requests.get(search_url, headers=headers)
20
+
21
+ restaurant_list = []
22
+ if response.status_code == 200:
23
+ soup = BeautifulSoup(response.text, "html.parser")
24
+ items = soup.find_all("div", class_="vendor-info")
25
+ for item in items[:5]: # Get top 5 restaurants
26
+ name = item.find("span", class_="name").text if item.find("span", class_="name") else "Unknown"
27
+ restaurant_list.append(name)
28
+ return restaurant_list
29
+
30
+ # Streamlit UI
31
+ st.title("Pakistani City Recipe Finder 🍛")
32
+
33
+ city = st.selectbox("Select a City", ["Lahore", "Karachi", "Islamabad", "Peshawar", "Quetta"])
34
+ query = st.text_input("Enter a Recipe Name")
35
+
36
+ if st.button("Find Recipe"):
37
+ if query:
38
+ # Retrieve recipe info from vector DB
39
+ query_embedding = model.encode(query).tolist()
40
+ results = collection.query(query_embedding, n_results=3)
41
+
42
+ if results["documents"]:
43
+ st.subheader(f"Famous {query} Recipes in {city}")
44
+ for recipe in results["documents"][0]:
45
+ st.write(f"**Recipe:** {recipe['name']}")
46
+ st.image(recipe["image"], caption=recipe["name"], use_column_width=True)
47
+ st.write(f"Price: {recipe['price']} PKR")
48
+
49
+ # Fetch restaurant data via scraping
50
+ restaurants = scrape_restaurant_info(city, query)
51
+ if restaurants:
52
+ st.subheader("Available at These Restaurants:")
53
+ for r in restaurants:
54
+ st.write(f"- {r}")
55
+ else:
56
+ st.write("No restaurant data found.")
57
+ else:
58
+ st.write("No matching recipes found.")
59
+ else:
60
+ st.warning("Please enter a recipe name.")