File size: 4,192 Bytes
f44bd4d
 
 
 
 
 
 
63ceba5
 
f44bd4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63ceba5
 
f44bd4d
 
 
 
7af473f
 
f44bd4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad209e2
63ceba5
f44bd4d
 
 
104e098
f44bd4d
 
 
 
 
 
 
 
 
ad209e2
f44bd4d
 
 
ad209e2
f44bd4d
 
 
 
 
 
63ceba5
f44bd4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a8531b
f44bd4d
 
 
 
 
 
 
63ceba5
 
8a8531b
f44bd4d
8a8531b
1
2
3
4
5
6
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import os
import time
import random

import googlemaps
import pandas as pd
import plotly.express as px
import gradio as gr

# =====================================================
# 1. Google Maps API
# =====================================================
API_KEY = os.getenv("GOOGLE_MAPS_API_KEY")
if API_KEY is None:
    raise RuntimeError("GOOGLE_MAPS_API_KEY not set")

gmaps = googlemaps.Client(key=API_KEY)

# =====================================================
# 2. Data Fetch
# =====================================================
def fetch_places():
    query = "Hawaiian pizza in Seoul"
    rows = []

    res = gmaps.places(query=query, language="ko")

    while True:
        for r in res["results"]:
            rows.append({
                "name": r["name"],
                "address": r.get("formatted_address", ""),
                "rating": r.get("rating", 0),
                "lat": r["geometry"]["location"]["lat"],
                "lon": r["geometry"]["location"]["lng"],
            })

        if "next_page_token" not in res:
            break

        time.sleep(2)
        res = gmaps.places(
            page_token=res["next_page_token"],
            language="ko",
        )

    return pd.DataFrame(rows)

DATA = fetch_places()

# =====================================================
# 3. Entertainment
# =====================================================
QUOTES = [
    "🍍 νŒŒμΈμ• ν”Œμ€ λ…ΌμŸμ μ΄μ§€λ§Œ λ§›μžˆμŠ΅λ‹ˆλ‹€",
    "πŸ• μ΄νƒˆλ¦¬μ•„μΈμ—κ²ŒλŠ” λΉ„λ°€λ‘œβ€¦",
    "πŸ”₯ ν•˜μ™€μ΄μ•ˆ ν”Όμž μ°¬μ„± 1ν‘œ",
    "🀝 λ‹¨μ§ μ˜ ν‰ν™”ν˜‘μ •",
    "🧠 미각은 μžμœ μž…λ‹ˆλ‹€",
]

# =====================================================
# 4. Plotly Map Builder
# =====================================================
def build_map(df, zoom=11):
    if df.empty:
        center = dict(lat=37.5665, lon=126.9780)
    else:
        center = dict(lat=df.lat.mean(), lon=df.lon.mean())

    fig = px.scatter_mapbox(
        df,
        lat="lat",
        lon="lon",
        hover_name="name",
        hover_data={
            "rating": True,
            "address": True,
            "lat": False,
            "lon": False,
        },
        color="rating",
        size="rating",
        size_max=18,
        zoom=zoom,
        center=center,
        height=650,
    )

    # OpenStreetMap β†’ Mapbox 토큰 ν•„μš” μ—†μŒ
    fig.update_layout(
        mapbox_style="open-street-map",
        margin={"r": 0, "t": 0, "l": 0, "b": 0},
    )

    return fig

# =====================================================
# 5. UI Logic
# =====================================================
def update(min_rating):
    df = DATA[DATA["rating"] >= min_rating]

    pct = int(100 * len(df) / len(DATA)) if len(DATA) else 0

    return (
        build_map(df),
        f"**{len(df)}개 λ§€μž₯ ν‘œμ‹œ 쀑**",
        f"🍍 Pineapple Power: {pct}%",
        random.choice(QUOTES),
    )

def random_pick():
    row = DATA.sample(1)
    r = row.iloc[0]

    return (
        build_map(row, zoom=15),
        f"""
### 🎲 였늘의 ν•˜μ™€μ΄μ•ˆ 🍍
**{r.name}**  
⭐ {r.rating}  
πŸ“ {r.address}
""",
    )

# =====================================================
# 6. Gradio UI
# =====================================================
with gr.Blocks() as demo:
    gr.Markdown(
        """
## 🍍 μ„œμšΈ ν•˜μ™€μ΄μ•ˆ ν”Όμž 지도
**Plotly 기반 Β· Gradio/HF Spaces μ•ˆμ • 버전**
"""
    )

    with gr.Row():
        with gr.Column(scale=1):
            rating = gr.Slider(0, 5, 3.5, 0.1, label="μ΅œμ†Œ 평점")
            count = gr.Markdown()
            power = gr.Markdown()
            quote = gr.Markdown()
            btn = gr.Button("🍍 였늘의 ν•˜μ™€μ΄μ•ˆ")
            rec = gr.Markdown()

        with gr.Column(scale=3):
            plot = gr.Plot()

    rating.change(
        fn=update,
        inputs=rating,
        outputs=[plot, count, power, quote],
    )

    btn.click(
        fn=random_pick,
        inputs=None,
        outputs=[plot, rec],
    )

    demo.load(
        fn=update,
        inputs=rating,
        outputs=[plot, count, power, quote],
    )

demo.launch(
    ssr_mode=False  # 지도 μ•ˆμ •μ„± ↑
)