Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,110 +1,151 @@
|
|
| 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 |
-
#
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
#
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
"
|
| 30 |
-
"
|
| 31 |
-
"
|
| 32 |
-
"
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 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 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
#
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
<b>{r.name}</b><br>
|
| 67 |
-
β {r.rating}<br>
|
| 68 |
-
{r.address}
|
| 69 |
-
|
| 70 |
-
|
| 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.
|
| 81 |
-
# ===============================
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 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 |
+
import time
|
| 8 |
+
|
| 9 |
+
# ===============================
|
| 10 |
+
# 1. Google Maps API
|
| 11 |
+
# ===============================
|
| 12 |
+
API_KEY = os.getenv("GOOGLE_MAPS_API_KEY")
|
| 13 |
+
if API_KEY is None:
|
| 14 |
+
raise ValueError("GOOGLE_MAPS_API_KEY not found")
|
| 15 |
+
|
| 16 |
+
gmaps = googlemaps.Client(key=API_KEY)
|
| 17 |
+
|
| 18 |
+
# ===============================
|
| 19 |
+
# 2. Data fetch
|
| 20 |
+
# ===============================
|
| 21 |
+
def fetch_places():
|
| 22 |
+
query = "Hawaiian pizza in Seoul"
|
| 23 |
+
results = []
|
| 24 |
+
response = gmaps.places(query=query, language="ko")
|
| 25 |
+
|
| 26 |
+
while True:
|
| 27 |
+
for r in response["results"]:
|
| 28 |
+
results.append({
|
| 29 |
+
"name": r.get("name"),
|
| 30 |
+
"address": r.get("formatted_address"),
|
| 31 |
+
"rating": r.get("rating", 0),
|
| 32 |
+
"lat": r["geometry"]["location"]["lat"],
|
| 33 |
+
"lng": r["geometry"]["location"]["lng"],
|
| 34 |
+
})
|
| 35 |
+
|
| 36 |
+
if "next_page_token" not in response:
|
| 37 |
+
break
|
| 38 |
+
|
| 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 |
+
DATA = fetch_places()
|
| 48 |
+
|
| 49 |
+
# ===============================
|
| 50 |
+
# 3. Map rendering
|
| 51 |
+
# ===============================
|
| 52 |
+
def render_map(min_rating):
|
| 53 |
+
df = DATA[DATA["rating"] >= min_rating]
|
| 54 |
+
|
| 55 |
+
m = folium.Map(
|
| 56 |
+
location=[37.5665, 126.9780],
|
| 57 |
+
zoom_start=11,
|
| 58 |
+
tiles="CartoDB positron"
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
for _, r in df.iterrows():
|
| 62 |
+
folium.Marker(
|
| 63 |
+
[r.lat, r.lng],
|
| 64 |
+
popup=f"""
|
| 65 |
+
<div style="font-size:13px">
|
| 66 |
+
<b>{r.name}</b><br>
|
| 67 |
+
β {r.rating}<br>
|
| 68 |
+
{r.address}
|
| 69 |
+
</div>
|
| 70 |
+
"""
|
| 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. UI
|
| 81 |
+
# ===============================
|
| 82 |
+
CSS = """
|
| 83 |
+
#title {
|
| 84 |
+
font-size: 28px;
|
| 85 |
+
font-weight: 700;
|
| 86 |
+
margin-bottom: 0.2em;
|
| 87 |
+
}
|
| 88 |
+
#subtitle {
|
| 89 |
+
color: #666;
|
| 90 |
+
margin-bottom: 1.5em;
|
| 91 |
+
}
|
| 92 |
+
.card {
|
| 93 |
+
background: white;
|
| 94 |
+
border-radius: 12px;
|
| 95 |
+
padding: 16px;
|
| 96 |
+
box-shadow: 0 4px 14px rgba(0,0,0,0.06);
|
| 97 |
+
}
|
| 98 |
+
"""
|
| 99 |
+
|
| 100 |
+
with gr.Blocks(css=CSS) as demo:
|
| 101 |
+
# ---------- Header ----------
|
| 102 |
+
gr.Markdown(
|
| 103 |
+
"""
|
| 104 |
+
<div id="title">π μμΈ νμμ΄μ νΌμ μ§λ</div>
|
| 105 |
+
<div id="subtitle">
|
| 106 |
+
Google Maps κΈ°λ° Β· νμ νν°λ§ κ°λ₯
|
| 107 |
+
</div>
|
| 108 |
+
""",
|
| 109 |
+
elem_id="header"
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
with gr.Row():
|
| 113 |
+
# ---------- Left panel ----------
|
| 114 |
+
with gr.Column(scale=1):
|
| 115 |
+
with gr.Group(elem_classes="card"):
|
| 116 |
+
gr.Markdown("### π νν°")
|
| 117 |
+
|
| 118 |
+
rating = gr.Slider(
|
| 119 |
+
minimum=0,
|
| 120 |
+
maximum=5,
|
| 121 |
+
value=3.5,
|
| 122 |
+
step=0.1,
|
| 123 |
+
label="μ΅μ νμ "
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
count = gr.Markdown()
|
| 127 |
+
|
| 128 |
+
# ---------- Right panel ----------
|
| 129 |
+
with gr.Column(scale=3):
|
| 130 |
+
with gr.Group(elem_classes="card"):
|
| 131 |
+
map_html = gr.HTML()
|
| 132 |
+
|
| 133 |
+
# ---------- Logic ----------
|
| 134 |
+
def update(min_rating):
|
| 135 |
+
html = render_map(min_rating)
|
| 136 |
+
n = (DATA["rating"] >= min_rating).sum()
|
| 137 |
+
return html, f"**{n}κ° λ§€μ₯ νμ μ€**"
|
| 138 |
+
|
| 139 |
+
rating.change(
|
| 140 |
+
fn=update,
|
| 141 |
+
inputs=rating,
|
| 142 |
+
outputs=[map_html, count]
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
demo.load(
|
| 146 |
+
fn=update,
|
| 147 |
+
inputs=rating,
|
| 148 |
+
outputs=[map_html, count]
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
demo.launch()
|