Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import requests | |
| import pandas as pd | |
| # ----------------------- | |
| # Overpass API function | |
| # ----------------------- | |
| def fetch_restaurants(city_name): | |
| query = f""" | |
| [out:json][timeout:25]; | |
| area["name"="{city_name}"]->.searchArea; | |
| ( | |
| node["amenity"="restaurant"](area.searchArea); | |
| way["amenity"="restaurant"](area.searchArea); | |
| relation["amenity"="restaurant"](area.searchArea); | |
| ); | |
| out center tags; | |
| """ | |
| url = "https://overpass-api.de/api/interpreter" | |
| response = requests.post(url, data={"data": query}) | |
| if response.status_code != 200: | |
| return None | |
| data = response.json() | |
| restaurants = [] | |
| for element in data["elements"]: | |
| tags = element.get("tags", {}) | |
| name = tags.get("name", "Unnamed") | |
| cuisine = tags.get("cuisine", "Not listed") | |
| phone = tags.get("phone", "N/A") | |
| street = tags.get("addr:street", "") | |
| housenumber = tags.get("addr:housenumber", "") | |
| city = tags.get("addr:city", city_name) | |
| address = f"{housenumber} {street}, {city}".strip() | |
| restaurants.append({ | |
| "Name": name, | |
| "Cuisine": cuisine, | |
| "Contact": phone, | |
| "Address": address | |
| }) | |
| return pd.DataFrame(restaurants) | |
| # ----------------------- | |
| # Streamlit UI | |
| # ----------------------- | |
| st.set_page_config(page_title="Live City Restaurant Guide", layout="wide") | |
| st.title("π½οΈ Live City Restaurant Guide") | |
| st.subheader("Enter a city to find real-time restaurant data (OpenStreetMap API)") | |
| city = st.text_input("Enter city name (e.g., Lahore, Karachi, Paris, New York):").strip().title() | |
| if city: | |
| with st.spinner("π Fetching data... please wait..."): | |
| df = fetch_restaurants(city) | |
| if df is None or df.empty: | |
| st.warning("β No restaurant data found or API limit reached. Try another city.") | |
| else: | |
| st.success(f"β Found {len(df)} restaurants in {city}") | |
| for idx, row in df.iterrows(): | |
| with st.expander(f"{row['Name']} π΄"): | |
| st.markdown(f"**π Address:** {row['Address']}") | |
| st.markdown(f"**π± Cuisine:** {row['Cuisine']}") | |
| st.markdown(f"**π Contact:** {row['Contact']}") | |
| else: | |
| st.info("π Please enter a city name to search.") | |