Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,69 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
data =
|
|
|
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
else:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
st.markdown(f"
|
| 28 |
-
st.markdown(f"
|
|
|
|
| 29 |
else:
|
| 30 |
-
st.info("Please enter a city name to
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
import pandas as pd
|
| 4 |
|
| 5 |
+
# -----------------------
|
| 6 |
+
# Overpass API function
|
| 7 |
+
# -----------------------
|
| 8 |
+
def fetch_restaurants(city_name):
|
| 9 |
+
query = f"""
|
| 10 |
+
[out:json][timeout:25];
|
| 11 |
+
area["name"="{city_name}"]->.searchArea;
|
| 12 |
+
(
|
| 13 |
+
node["amenity"="restaurant"](area.searchArea);
|
| 14 |
+
way["amenity"="restaurant"](area.searchArea);
|
| 15 |
+
relation["amenity"="restaurant"](area.searchArea);
|
| 16 |
+
);
|
| 17 |
+
out center tags;
|
| 18 |
+
"""
|
| 19 |
+
url = "https://overpass-api.de/api/interpreter"
|
| 20 |
+
response = requests.post(url, data={"data": query})
|
| 21 |
+
if response.status_code != 200:
|
| 22 |
+
return None
|
| 23 |
|
| 24 |
+
data = response.json()
|
| 25 |
+
restaurants = []
|
| 26 |
|
| 27 |
+
for element in data["elements"]:
|
| 28 |
+
tags = element.get("tags", {})
|
| 29 |
+
name = tags.get("name", "Unnamed")
|
| 30 |
+
cuisine = tags.get("cuisine", "Not listed")
|
| 31 |
+
phone = tags.get("phone", "N/A")
|
| 32 |
+
street = tags.get("addr:street", "")
|
| 33 |
+
housenumber = tags.get("addr:housenumber", "")
|
| 34 |
+
city = tags.get("addr:city", city_name)
|
| 35 |
+
address = f"{housenumber} {street}, {city}".strip()
|
| 36 |
|
| 37 |
+
restaurants.append({
|
| 38 |
+
"Name": name,
|
| 39 |
+
"Cuisine": cuisine,
|
| 40 |
+
"Contact": phone,
|
| 41 |
+
"Address": address
|
| 42 |
+
})
|
| 43 |
|
| 44 |
+
return pd.DataFrame(restaurants)
|
| 45 |
|
| 46 |
+
# -----------------------
|
| 47 |
+
# Streamlit UI
|
| 48 |
+
# -----------------------
|
| 49 |
+
st.set_page_config(page_title="Live City Restaurant Guide", layout="wide")
|
| 50 |
+
st.title("π½οΈ Live City Restaurant Guide")
|
| 51 |
+
st.subheader("Enter a city to find real-time restaurant data (OpenStreetMap API)")
|
| 52 |
|
| 53 |
+
city = st.text_input("Enter city name (e.g., Lahore, Karachi, Paris, New York):").strip().title()
|
| 54 |
+
|
| 55 |
+
if city:
|
| 56 |
+
with st.spinner("π Fetching data... please wait..."):
|
| 57 |
+
df = fetch_restaurants(city)
|
| 58 |
+
|
| 59 |
+
if df is None or df.empty:
|
| 60 |
+
st.warning("β No restaurant data found or API limit reached. Try another city.")
|
| 61 |
else:
|
| 62 |
+
st.success(f"β
Found {len(df)} restaurants in {city}")
|
| 63 |
+
for idx, row in df.iterrows():
|
| 64 |
+
with st.expander(f"{row['Name']} π΄"):
|
| 65 |
+
st.markdown(f"**π Address:** {row['Address']}")
|
| 66 |
+
st.markdown(f"**π± Cuisine:** {row['Cuisine']}")
|
| 67 |
+
st.markdown(f"**π Contact:** {row['Contact']}")
|
| 68 |
else:
|
| 69 |
+
st.info("π Please enter a city name to search.")
|