Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import time
|
|
|
|
| 3 |
import googlemaps
|
| 4 |
import pandas as pd
|
| 5 |
import folium
|
|
@@ -16,7 +17,7 @@ if API_KEY is None:
|
|
| 16 |
gmaps = googlemaps.Client(key=API_KEY)
|
| 17 |
|
| 18 |
# ===============================
|
| 19 |
-
# 2. Data fetch (
|
| 20 |
# ===============================
|
| 21 |
def fetch_places():
|
| 22 |
query = "Hawaiian pizza in Seoul"
|
|
@@ -47,14 +48,23 @@ def fetch_places():
|
|
| 47 |
DATA = fetch_places()
|
| 48 |
|
| 49 |
# ===============================
|
| 50 |
-
# 3.
|
| 51 |
# ===============================
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
m = folium.Map(
|
| 56 |
location=[37.5665, 126.9780],
|
| 57 |
-
zoom_start=
|
| 58 |
tiles="CartoDB positron"
|
| 59 |
)
|
| 60 |
|
|
@@ -65,7 +75,9 @@ def render_map(min_rating):
|
|
| 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)
|
|
@@ -80,7 +92,41 @@ def render_map(min_rating):
|
|
| 80 |
"""
|
| 81 |
|
| 82 |
# ===============================
|
| 83 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
# ===============================
|
| 85 |
CSS = """
|
| 86 |
#map {
|
|
@@ -102,6 +148,20 @@ CSS = """
|
|
| 102 |
color: #666;
|
| 103 |
margin-bottom: 1.5em;
|
| 104 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
"""
|
| 106 |
|
| 107 |
with gr.Blocks(css=CSS) as demo:
|
|
@@ -109,7 +169,9 @@ with gr.Blocks(css=CSS) as demo:
|
|
| 109 |
gr.Markdown(
|
| 110 |
"""
|
| 111 |
<div id="title">π μμΈ νμμ΄μ νΌμ μ§λ</div>
|
| 112 |
-
<div id="subtitle">
|
|
|
|
|
|
|
| 113 |
"""
|
| 114 |
)
|
| 115 |
|
|
@@ -128,28 +190,34 @@ with gr.Blocks(css=CSS) as demo:
|
|
| 128 |
)
|
| 129 |
|
| 130 |
count = gr.Markdown()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
# ---------- Right panel ----------
|
| 133 |
with gr.Column(scale=3):
|
| 134 |
with gr.Group(elem_classes="card"):
|
| 135 |
map_html = gr.HTML(elem_id="map")
|
| 136 |
|
| 137 |
-
# ----------
|
| 138 |
-
def update(min_rating):
|
| 139 |
-
html = render_map(min_rating)
|
| 140 |
-
n = (DATA["rating"] >= min_rating).sum()
|
| 141 |
-
return html, f"**{n}κ° λ§€μ₯ νμ μ€**"
|
| 142 |
-
|
| 143 |
rating.change(
|
| 144 |
fn=update,
|
| 145 |
inputs=rating,
|
| 146 |
-
outputs=[map_html, count]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
)
|
| 148 |
|
| 149 |
demo.load(
|
| 150 |
fn=update,
|
| 151 |
inputs=rating,
|
| 152 |
-
outputs=[map_html, count]
|
| 153 |
)
|
| 154 |
|
| 155 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
+
import random
|
| 4 |
import googlemaps
|
| 5 |
import pandas as pd
|
| 6 |
import folium
|
|
|
|
| 17 |
gmaps = googlemaps.Client(key=API_KEY)
|
| 18 |
|
| 19 |
# ===============================
|
| 20 |
+
# 2. Data fetch (cache in memory)
|
| 21 |
# ===============================
|
| 22 |
def fetch_places():
|
| 23 |
query = "Hawaiian pizza in Seoul"
|
|
|
|
| 48 |
DATA = fetch_places()
|
| 49 |
|
| 50 |
# ===============================
|
| 51 |
+
# 3. Pineapple quotes π
|
| 52 |
# ===============================
|
| 53 |
+
PINEAPPLE_QUOTES = [
|
| 54 |
+
"π νμΈμ νμ λ
Όμμ μ΄μ§λ§ λ§μμ΅λλ€",
|
| 55 |
+
"π μ΄ν리μμΈμκ²λ λΉλ°λ‘β¦",
|
| 56 |
+
"π₯ νμμ΄μ νΌμ μ°¬μ± 1ν μΆκ°",
|
| 57 |
+
"π€ λ¨μ§ μ νννμ ",
|
| 58 |
+
"π§ λ―Έκ°μ μμ μ
λλ€",
|
| 59 |
+
]
|
| 60 |
|
| 61 |
+
# ===============================
|
| 62 |
+
# 4. Map rendering (iframe)
|
| 63 |
+
# ===============================
|
| 64 |
+
def render_map(df, zoom=11):
|
| 65 |
m = folium.Map(
|
| 66 |
location=[37.5665, 126.9780],
|
| 67 |
+
zoom_start=zoom,
|
| 68 |
tiles="CartoDB positron"
|
| 69 |
)
|
| 70 |
|
|
|
|
| 75 |
<div style="font-size:13px">
|
| 76 |
<b>{r.name}</b><br>
|
| 77 |
β {r.rating}<br>
|
| 78 |
+
{r.address}<br>
|
| 79 |
+
<span class="pineapple">π</span>
|
| 80 |
+
<span class="pineapple">π</span>
|
| 81 |
</div>
|
| 82 |
"""
|
| 83 |
).add_to(m)
|
|
|
|
| 92 |
"""
|
| 93 |
|
| 94 |
# ===============================
|
| 95 |
+
# 5. UI logic
|
| 96 |
+
# ===============================
|
| 97 |
+
def update(min_rating):
|
| 98 |
+
df = DATA[DATA["rating"] >= min_rating]
|
| 99 |
+
html = render_map(df)
|
| 100 |
+
|
| 101 |
+
n = len(df)
|
| 102 |
+
total = len(DATA)
|
| 103 |
+
pct = int(100 * n / total) if total > 0 else 0
|
| 104 |
+
quote = random.choice(PINEAPPLE_QUOTES)
|
| 105 |
+
|
| 106 |
+
return (
|
| 107 |
+
html,
|
| 108 |
+
f"**{n}κ° λ§€μ₯ νμ μ€**",
|
| 109 |
+
f"π **Pineapple Power:** {pct}%",
|
| 110 |
+
f"### {quote}"
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
def random_pick():
|
| 114 |
+
row = DATA.sample(1).iloc[0]
|
| 115 |
+
df = DATA.loc[[row.name]]
|
| 116 |
+
|
| 117 |
+
html = render_map(pd.DataFrame([row]), zoom=15)
|
| 118 |
+
|
| 119 |
+
text = f"""
|
| 120 |
+
### π² μ€λμ νμμ΄μ π
|
| 121 |
+
**{row.name}**
|
| 122 |
+
β {row.rating}
|
| 123 |
+
π {row.address}
|
| 124 |
+
"""
|
| 125 |
+
|
| 126 |
+
return html, text
|
| 127 |
+
|
| 128 |
+
# ===============================
|
| 129 |
+
# 6. UI & CSS
|
| 130 |
# ===============================
|
| 131 |
CSS = """
|
| 132 |
#map {
|
|
|
|
| 148 |
color: #666;
|
| 149 |
margin-bottom: 1.5em;
|
| 150 |
}
|
| 151 |
+
|
| 152 |
+
/* Pineapple animation */
|
| 153 |
+
.pineapple {
|
| 154 |
+
display: inline-block;
|
| 155 |
+
cursor: pointer;
|
| 156 |
+
}
|
| 157 |
+
.pineapple:active {
|
| 158 |
+
animation: pop 0.6s ease-out;
|
| 159 |
+
}
|
| 160 |
+
@keyframes pop {
|
| 161 |
+
0% { transform: scale(1) translateY(0); opacity: 1; }
|
| 162 |
+
50% { transform: scale(1.8) translateY(-10px); }
|
| 163 |
+
100% { transform: scale(1) translateY(0); opacity: 0.8; }
|
| 164 |
+
}
|
| 165 |
"""
|
| 166 |
|
| 167 |
with gr.Blocks(css=CSS) as demo:
|
|
|
|
| 169 |
gr.Markdown(
|
| 170 |
"""
|
| 171 |
<div id="title">π μμΈ νμμ΄μ νΌμ μ§λ</div>
|
| 172 |
+
<div id="subtitle">
|
| 173 |
+
Google Maps κΈ°λ° Β· νμΈμ νμ μμ μ
λλ€
|
| 174 |
+
</div>
|
| 175 |
"""
|
| 176 |
)
|
| 177 |
|
|
|
|
| 190 |
)
|
| 191 |
|
| 192 |
count = gr.Markdown()
|
| 193 |
+
pineapple_power = gr.Markdown()
|
| 194 |
+
quote = gr.Markdown()
|
| 195 |
+
|
| 196 |
+
random_btn = gr.Button("π μ€λμ νμμ΄μ", variant="primary")
|
| 197 |
+
recommendation = gr.Markdown()
|
| 198 |
|
| 199 |
# ---------- Right panel ----------
|
| 200 |
with gr.Column(scale=3):
|
| 201 |
with gr.Group(elem_classes="card"):
|
| 202 |
map_html = gr.HTML(elem_id="map")
|
| 203 |
|
| 204 |
+
# ---------- Events ----------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
rating.change(
|
| 206 |
fn=update,
|
| 207 |
inputs=rating,
|
| 208 |
+
outputs=[map_html, count, pineapple_power, quote]
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
random_btn.click(
|
| 212 |
+
fn=random_pick,
|
| 213 |
+
inputs=None,
|
| 214 |
+
outputs=[map_html, recommendation]
|
| 215 |
)
|
| 216 |
|
| 217 |
demo.load(
|
| 218 |
fn=update,
|
| 219 |
inputs=rating,
|
| 220 |
+
outputs=[map_html, count, pineapple_power, quote]
|
| 221 |
)
|
| 222 |
|
| 223 |
demo.launch()
|