Spaces:
Sleeping
Sleeping
| 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 # μ§λ μμ μ± β | |
| ) | |