Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +110 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import googlemaps
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import folium
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from tempfile import NamedTemporaryFile
|
| 7 |
+
|
| 8 |
+
# ===============================
|
| 9 |
+
# 1. Google Maps API client
|
| 10 |
+
# ===============================
|
| 11 |
+
API_KEY = os.getenv("GOOGLE_MAPS_API_KEY")
|
| 12 |
+
if API_KEY is None:
|
| 13 |
+
raise ValueError("GOOGLE_MAPS_API_KEY not found in environment variables")
|
| 14 |
+
|
| 15 |
+
gmaps = googlemaps.Client(key=API_KEY)
|
| 16 |
+
|
| 17 |
+
# ===============================
|
| 18 |
+
# 2. Data collection
|
| 19 |
+
# ===============================
|
| 20 |
+
def fetch_hawaiian_pizza_seoul():
|
| 21 |
+
query = "Hawaiian pizza in Seoul"
|
| 22 |
+
results = []
|
| 23 |
+
response = gmaps.places(query=query, language="ko")
|
| 24 |
+
|
| 25 |
+
while True:
|
| 26 |
+
for r in response["results"]:
|
| 27 |
+
results.append({
|
| 28 |
+
"name": r.get("name"),
|
| 29 |
+
"address": r.get("formatted_address"),
|
| 30 |
+
"rating": r.get("rating", 0),
|
| 31 |
+
"lat": r["geometry"]["location"]["lat"],
|
| 32 |
+
"lng": r["geometry"]["location"]["lng"],
|
| 33 |
+
})
|
| 34 |
+
|
| 35 |
+
if "next_page_token" not in response:
|
| 36 |
+
break
|
| 37 |
+
|
| 38 |
+
import time
|
| 39 |
+
time.sleep(2)
|
| 40 |
+
response = gmaps.places(
|
| 41 |
+
page_token=response["next_page_token"],
|
| 42 |
+
language="ko"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
return pd.DataFrame(results)
|
| 46 |
+
|
| 47 |
+
# μΊμ (Spaces μ¬μμ μ κΉμ§ μ μ§)
|
| 48 |
+
DATA = fetch_hawaiian_pizza_seoul()
|
| 49 |
+
|
| 50 |
+
# ===============================
|
| 51 |
+
# 3. Map generator
|
| 52 |
+
# ===============================
|
| 53 |
+
def generate_map(min_rating):
|
| 54 |
+
df = DATA[DATA["rating"] >= min_rating]
|
| 55 |
+
|
| 56 |
+
m = folium.Map(
|
| 57 |
+
location=[37.5665, 126.9780],
|
| 58 |
+
zoom_start=11,
|
| 59 |
+
tiles="OpenStreetMap"
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
for _, r in df.iterrows():
|
| 63 |
+
folium.Marker(
|
| 64 |
+
[r.lat, r.lng],
|
| 65 |
+
popup=f"""
|
| 66 |
+
<b>{r.name}</b><br>
|
| 67 |
+
β {r.rating}<br>
|
| 68 |
+
{r.address}
|
| 69 |
+
""",
|
| 70 |
+
icon=folium.Icon(icon="cutlery", prefix="fa")
|
| 71 |
+
).add_to(m)
|
| 72 |
+
|
| 73 |
+
tmp = NamedTemporaryFile(delete=False, suffix=".html")
|
| 74 |
+
m.save(tmp.name)
|
| 75 |
+
|
| 76 |
+
with open(tmp.name, "r", encoding="utf-8") as f:
|
| 77 |
+
return f.read()
|
| 78 |
+
|
| 79 |
+
# ===============================
|
| 80 |
+
# 4. Gradio UI
|
| 81 |
+
# ===============================
|
| 82 |
+
with gr.Blocks() as demo:
|
| 83 |
+
gr.Markdown("""
|
| 84 |
+
# π μμΈ νμμ΄μ νΌμ μ§λ
|
| 85 |
+
Google Maps λ°μ΄ν°λ₯Ό κΈ°λ°μΌλ‘ ν νμμ΄μ νΌμ μλΉ μ§λ
|
| 86 |
+
""")
|
| 87 |
+
|
| 88 |
+
rating = gr.Slider(
|
| 89 |
+
minimum=0,
|
| 90 |
+
maximum=5,
|
| 91 |
+
value=3.5,
|
| 92 |
+
step=0.1,
|
| 93 |
+
label="μ΅μ νμ "
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
map_html = gr.HTML()
|
| 97 |
+
|
| 98 |
+
rating.change(
|
| 99 |
+
fn=generate_map,
|
| 100 |
+
inputs=rating,
|
| 101 |
+
outputs=map_html
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
demo.load(
|
| 105 |
+
fn=generate_map,
|
| 106 |
+
inputs=rating,
|
| 107 |
+
outputs=map_html
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
googlemaps
|
| 3 |
+
folium
|
| 4 |
+
pandas
|