Spaces:
Sleeping
Sleeping
Update app.py
Browse files10 unmodified lines1112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919210 unmodified lines1031041051061071081092 unmodified lines1121131141151161171181191202 unmodified lines1231241251261271281291301311321331341351361371389 unmodified lines1481491501511521531549 unmodified lines1641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472487 unmodified lines25625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628710 unmodified linesinitial_sidebar_state="expanded")# ── Custom CSS ────────────────────────────────────────────────────────────────st.markdown("""<style>@import url('https://fonts.googleapis.com/css2?family=Space+Mono:wght@400;700&family=DM+Sans:wght@300;400;600&display=swap');html, body, [class*="css"] {font-family: 'DM Sans', sans-serif;}/* Dark background */.stApp {background-color: #0D1117;color: #E6EDF3;}/* Sidebar */[data-testid="stSidebar"] {background-color: #161B22;border-right: 1px solid #30363D;}/* Metric Cards */.metric-card {background: linear-gradient(135deg, #1C2128 0%, #161B22 100%);border: 1px solid #30363D;border-radius: 12px;padding: 20px 24px;text-align: center;transition: border-color 0.2s;}.metric-card:hover { border-color: #58A6FF; }.metric-value {font-family: 'Space Mono', monospace;font-size: 2.2rem;font-weight: 700;color: #58A6FF;line-height: 1;}.metric-label {font-size: 0.78rem;color: #8B949E;margin-top: 6px;text-transform: uppercase;letter-spacing: 0.08em;}.metric-sub {font-size: 0.85rem;color: #3FB950;margin-top: 4px;font-weight: 600;}/* Header */.header-title {font-family: 'Space Mono', monospace;font-size: 1.8rem;font-weight: 700;color: #E6EDF3;letter-spacing: -0.02em;}.header-sub {color: #8B949E;font-size: 0.9rem;margin-top: 4px;}/* Tags */.tag-green { background:#1B4332; color:#3FB950; padding:2px 10px; border-radius:20px; font-size:0.78rem; font-weight:600; }.tag-orange { background:#3D2B00; color:#E3B341; padding:2px 10px; border-radius:20px; font-size:0.78rem; font-weight:600; }.tag-red { background:#3D0000; color:#FF7B72; padding:2px 10px; border-radius:20px; font-size:0.78rem; font-weight:600; }/* Table */.dataframe { font-size: 0.82rem !important; }/* Divider */hr { border-color: #30363D; }</style>""", unsafe_allow_html=True)10 unmodified linesst.markdown("### ⚡ EV Site Optimizer")st.markdown("<hr>", unsafe_allow_html=True)st.markdown("**🎛️ ตัวกรองผลลัพธ์**")payback_max = st.slider("Payback Period สูงสุด (ปี)",2 unmodified lines)zone_options = ["ทั้งหมด", "CBD", "URBAN"]zone_filter = st.selectbox("โซนพื้นที่", zone_options)flood_only = st.toggle("เฉพาะพื้นที่ปลอดภัย (ไม่เสี่ยงน้ำท่วม)", value=True)score_min = st.slider("Total Score ขั้นต่ำ",2 unmodified lines)st.markdown("<hr>", unsafe_allow_html=True)st.markdown("**🗺️ การแสดงผล**")show_top = st.slider("แสดง Top N จุด", 5, 50, 20)st.markdown("<hr>", unsafe_allow_html=True)st.markdown("""<div style='font-size:0.75rem; color:#8B949E'><b>Legend</b><br>🟢 Payback < 2 ปี — ลงทุนได้เลย<br>🟡 Payback 2–3.5 ปี — พิจารณาได้<br>🔴 Payback > 3.5 ปี — รอดูก่อน</div>""", unsafe_allow_html=True)9 unmodified lines# ── Header ────────────────────────────────────────────────────────────────────st.markdown("""<div class="header-title">⚡ EV Site Optimizer</div><div class="header-sub">ระบบคัดกรองทำเลสถานีชาร์จรถยนต์ไฟฟ้า — กรุงเทพฯ + ปริมณฑล</div>""", unsafe_allow_html=True)st.markdown("<hr>", unsafe_allow_html=True)9 unmodified linesst.markdown(f"""<div class="metric-card"><div class="metric-value">{total_golden:,}</div><div class="metric-label">Golden Gap ทั้งหมด</div><div class="metric-sub">ผ่านเกณฑ์ทุกด้าน</div></div>""", unsafe_allow_html=True)with col2:st.markdown(f"""<div class="metric-card"><div class="metric-value">{best_payback:.2f}Y</div><div class="metric-label">Best Payback</div><div class="metric-sub">คืนทุนเร็วที่สุด</div></div>""", unsafe_allow_html=True)with col3:st.markdown(f"""<div class="metric-card"><div class="metric-value">{avg_payback:.2f}Y</div><div class="metric-label">Payback เฉลี่ย</div><div class="metric-sub">กลุ่มที่ผ่านเกณฑ์</div></div>""", unsafe_allow_html=True)with col4:st.markdown(f"""<div class="metric-card"><div class="metric-value">{filtered_count}</div><div class="metric-label">ผลลัพธ์ที่กรองแล้ว</div><div class="metric-sub">ตาม Slider ที่เลือก</div></div>""", unsafe_allow_html=True)st.markdown("<br>", unsafe_allow_html=True)# ── Map + Table ───────────────────────────────────────────────────────────────map_col, table_col = st.columns([3, 2])with map_col:st.markdown(f"**🗺️ แผนที่ Top {show_top} ทำเล**")m = folium.Map(location=[13.7563, 100.5018],zoom_start=10,tiles="CartoDB dark_matter")for rank, (_, row) in enumerate(df_filtered.iterrows(), 1):p = row["payback_years"]if p < 2:color, hex_color = "green", "#3FB950"elif p <= 3.5:color, hex_color = "orange", "#E3B341"else:color, hex_color = "red", "#FF7B72"flood_text = "ปลอดภัย ✅" if row["is_flood_safe"] else "เสี่ยง ❌"popup_html = f"""<div style='font-family:monospace; font-size:12px;background:#161B22; color:#E6EDF3;padding:10px; border-radius:8px; width:190px;border:1px solid #30363D'><b style='color:{hex_color}'>#{rank} — {p:.2f} ปี</b><br><hr style='border-color:#30363D; margin:5px 0'>🏙️ Zone : {row['zone_type']}<br>🌊 Flood : {flood_text}<br>📊 Score : {row['total_score']:.3f}<br>🚗 Cars/Day: {int(row['est_cars_per_day'])}<br>💰 Revenue : ฿{int(row['year_1_revenue']):,}</div>"""folium.Marker(location=[row["grid_lat"], row["grid_lon"]],popup=folium.Popup(popup_html, max_width=210),tooltip=f"#{rank} | Payback {p:.2f}Y | {row['zone_type']}",icon=folium.Icon(color=color, icon="bolt", prefix="fa")).add_to(m)st_folium(m, width=None, height=480)with table_col:st.markdown(f"**📋 Top {show_top} Locations รายละเอียด**")def payback_tag(p):if p < 2:7 unmodified lines"total_score", "est_cars_per_day","year_1_revenue", "is_flood_safe"]].copy()df_display.insert(0, "Rank", range(1, len(df_display)+1))df_display["Payback"] = df_display["payback_years"].apply(lambda p: f"{'🟢' if p < 2 else '🟡' if p <= 3.5 else '🔴'} {p:.2f}Y")df_display["Flood"] = df_display["is_flood_safe"].apply(lambda x: "✅" if x else "❌")df_display["Revenue/Y"] = df_display["year_1_revenue"].apply(lambda x: f"฿{int(x):,}")df_display["Cars/Day"] = df_display["est_cars_per_day"].apply(int)df_display["Score"] = df_display["total_score"].apply(lambda x: f"{x:.3f}")df_show = df_display[["Rank", "Payback", "zone_type","Score", "Cars/Day", "Revenue/Y", "Flood"]]df_show.columns = ["#", "Payback", "Zone", "Score","Cars/Day", "Revenue/Y", "Flood"]st.dataframe(df_show, use_container_width=True, hide_index=True, height=480)# ── Footer ────────────────────────────────────────────────────────────────────st.markdown("<hr>", unsafe_allow_html=True)st.markdown("""<div style='text-align:center; color:#8B949E; font-size:0.78rem'>EV Site Optimizer v2.0 | Databricks + PySpark + MLflow | Data: OSM · WorldPop · World Bank | Cost: ฿0 Free 100%</div>""", unsafe_allow_html=True)10 unmodified lines111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910 unmodified lines1101111121131141151162 unmodified lines1191201211221231241251261272 unmodified lines1301311321331341351361371381391401411421431441459 unmodified lines1551561571581591601619 unmodified lines1711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582597 unmodified lines26726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930010 unmodified linesinitial_sidebar_state="expanded")# ── Custom CSS (Modern Light Theme) ──────────────────────────────────────────st.markdown("""<style>@import url('https://fonts.googleapis.com/css2?family=Prompt:wght@300;400;600;700&family=Space+Mono:wght@400;700&display=swap');html, body, [class*="css"] {font-family: 'Prompt', sans-serif;}/* Light background */.stApp {background-color: #F8FAFC; /* Slate 50 */color: #1E293B; /* Slate 800 */}/* Sidebar */[data-testid="stSidebar"] {background-color: #FFFFFF;border-right: 1px solid #E2E8F0;}/* Metric Cards */.metric-card {background: #FFFFFF;border: 1px solid #E2E8F0;border-radius: 16px;padding: 24px;text-align: center;box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05), 0 2px 4px -1px rgba(0, 0, 0, 0.03);transition: transform 0.2s, box-shadow 0.2s, border-color 0.2s;}.metric-card:hover {transform: translateY(-2px);box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);border-color: #0EA5E9; /* Sky 500 */}.metric-value {font-family: 'Space Mono', monospace;font-size: 2.5rem;font-weight: 700;color: #0EA5E9;line-height: 1;}.metric-label {font-size: 0.85rem;color: #64748B; /* Slate 500 */margin-top: 8px;font-weight: 600;text-transform: uppercase;letter-spacing: 0.05em;}.metric-sub {font-size: 0.9rem;color: #10B981; /* Emerald 500 */margin-top: 6px;font-weight: 600;}/* Header */.header-title {font-family: 'Space Mono', monospace;font-size: 2.2rem;font-weight: 700;color: #0F172A; /* Slate 900 */letter-spacing: -0.02em;}.header-sub {color: #64748B;font-size: 1.1rem;margin-top: 4px;font-weight: 300;}/* Tags (Pastel Style) */.tag-green { background:#DCFCE7; color:#16A34A; padding:4px 12px; border-radius:20px; font-size:0.8rem; font-weight:700; border: 1px solid #BBF7D0; }.tag-orange { background:#FEF3C7; color:#D97706; padding:4px 12px; border-radius:20px; font-size:0.8rem; font-weight:700; border: 1px solid #FDE68A; }.tag-red { background:#FEE2E2; color:#DC2626; padding:4px 12px; border-radius:20px; font-size:0.8rem; font-weight:700; border: 1px solid #FECACA; }/* Table styling override */.dataframe { font-size: 0.9rem !important; }/* Divider */hr { border-color: #E2E8F0; }</style>""", unsafe_allow_html=True)10 unmodified linesst.markdown("### ⚡ EV Site Optimizer")st.markdown("<hr>", unsafe_allow_html=True)st.markdown("**🎛️ ตัวกรองผลลัพธ์ (Filters)**")payback_max = st.slider("Payback Period สูงสุด (ปี)",2 unmodified lines)zone_options = ["ทั้งหมด", "CBD", "URBAN"]zone_filter = st.selectbox("โซนพื้นที่ (Zoning)", zone_options)flood_only = st.toggle("🛡️ ปลอดภัยจากน้ำท่วม 100%", value=True)score_min = st.slider("Total Score ขั้นต่ำ",2 unmodified lines)st.markdown("<hr>", unsafe_allow_html=True)st.markdown("**🗺️ การแสดงผล (Display)**")show_top = st.slider("แสดง Top N ทำเล", 5, 50, 20)st.markdown("<hr>", unsafe_allow_html=True)st.markdown("""<div style='font-size:0.85rem; color:#64748B; background:#F1F5F9; padding:15px; border-radius:10px; border:1px solid #E2E8F0;'><b>📊 แผนผังสี (Legend)</b><br><br>🟢 <b>คืนทุน < 2 ปี</b><br><span style='font-size:0.75rem'>แนะนำลงทุนทันที</span><br><br>🟡 <b>คืนทุน 2–3.5 ปี</b><br><span style='font-size:0.75rem'>มีศักยภาพ พิจารณาได้</span><br><br>🔴 <b>คืนทุน > 3.5 ปี</b><br><span style='font-size:0.75rem'>ความเสี่ยงระยะยาว</span></div>""", unsafe_allow_html=True)9 unmodified lines# ── Header ────────────────────────────────────────────────────────────────────st.markdown("""<div class="header-title">⚡ EV Site Optimizer</div><div class="header-sub">ระบบอัจฉริยะจำลองกลยุทธ์และคัดกรองทำเลสถานีชาร์จรถยนต์ไฟฟ้า — กรุงเทพมหานครและปริมณฑล</div>""", unsafe_allow_html=True)st.markdown("<hr>", unsafe_allow_html=True)9 unmodified linesst.markdown(f"""<div class="metric-card"><div class="metric-value">{total_golden:,}</div><div class="metric-label">Golden Gap Locations</div><div class="metric-sub">✓ ทำเลผ่านเกณฑ์ทั้งหมด</div></div>""", unsafe_allow_html=True)with col2:st.markdown(f"""<div class="metric-card"><div class="metric-value">{best_payback:.2f}Y</div><div class="metric-label">Fastest Payback</div><div class="metric-sub">🚀 คืนทุนเร็วที่สุด</div></div>""", unsafe_allow_html=True)with col3:st.markdown(f"""<div class="metric-card"><div class="metric-value">{avg_payback:.2f}Y</div><div class="metric-label">Average Payback</div><div class="metric-sub">📈 ค่าเฉลี่ยทั้งระบบ</div></div>""", unsafe_allow_html=True)with col4:st.markdown(f"""<div class="metric-card"><div class="metric-value" style="color: #8B5CF6;">{filtered_count}</div><div class="metric-label">Filtered Results</div><div class="metric-sub" style="color: #8B5CF6;">🎯 ตรงตามเงื่อนไขที่เลือก</div></div>""", unsafe_allow_html=True)st.markdown("<br><br>", unsafe_allow_html=True)# ── Map + Table ───────────────────────────────────────────────────────────────map_col, table_col = st.columns([1.2, 1])with map_col:st.markdown(f"**🗺️ แผนที่พิกัด Top {show_top} ทำเลศักยภาพ**")# เปลี่ยน Map Style เป็นแบบ���ว่าง สะอาดตาm = folium.Map(location=[13.7563, 100.5018],zoom_start=11,tiles="CartoDB positron")for rank, (_, row) in enumerate(df_filtered.iterrows(), 1):p = row["payback_years"]if p < 2:color, hex_color = "green", "#10B981"elif p <= 3.5:color, hex_color = "orange", "#F59E0B"else:color, hex_color = "red", "#EF4444"flood_text = "ปลอดภัย ✅" if row["is_flood_safe"] else "เสี่ยง ❌"# ปรับ Popup ให้ดูสว่างและมินิมอลpopup_html = f"""<div style='font-family:"Prompt", sans-serif; font-size:13px;background:#FFFFFF; color:#1E293B;padding:12px; border-radius:12px; width:220px;border:1px solid #E2E8F0; box-shadow: 0 4px 6px rgba(0,0,0,0.1);'><div style='font-size:16px; font-weight:700; color:{hex_color}; text-align:center; margin-bottom:8px;'>🏆 อันดับ #{rank} <br> คืนทุนใน {p:.2f} ปี</div><hr style='border-color:#F1F5F9; margin:8px 0'><b>🏙️ โซนพื้นที่:</b> {row['zone_type']}<br><b>🌊 ความเสี่ยงน้ำท่วม:</b> {flood_text}<br><b>📊 ศักยภาพรวม:</b> {row['total_score']:.3f}<br><b>🚗 รถที่คาดการณ์:</b> {int(row['est_cars_per_day'])} คัน/วัน<br><b>💰 รายได้ปีแรก:</b> ฿{int(row['year_1_revenue']):,}</div>"""folium.Marker(location=[row["grid_lat"], row["grid_lon"]],popup=folium.Popup(popup_html, max_width=250),tooltip=f"Rank #{rank} | Payback {p:.2f}Y",icon=folium.Icon(color=color, icon="bolt", prefix="fa")).add_to(m)st_folium(m, width=None, height=520)with table_col:st.markdown(f"**📋 รายละเอียดเจาะลึก (Top {show_top} Locations)**")def payback_tag(p):if p < 2:7 unmodified lines"total_score", "est_cars_per_day","year_1_revenue", "is_flood_safe"]].copy()df_display.insert(0, "Rank", range(1, len(df_display)+1))# ใส่ Emoji ให้ตารางดูมีชีวิตชีวาdf_display["Payback"] = df_display["payback_years"].apply(lambda p: f"{'🟢' if p < 2 else '🟡' if p <= 3.5 else '🔴'} {p:.2f} Y")df_display["Flood Safe"] = df_display["is_flood_safe"].apply(lambda x: "✅" if x else "❌")df_display["Revenue/Year"] = df_display["year_1_revenue"].apply(lambda x: f"฿ {int(x):,}")df_display["Cars/Day"] = df_display["est_cars_per_day"].apply(int)df_display["Total Score"] = df_display["total_score"].apply(lambda x: f"{x:.3f}")df_show = df_display[["Rank", "Payback", "zone_type","Total Score", "Cars/Day", "Revenue/Year", "Flood Safe"]]df_show.columns = ["#", "Payback", "Zone", "Score","Cars/Day", "Revenue/Y", "Flood"]st.dataframe(df_show, use_container_width=True, hide_index=True, height=520)# ── Footer ────────────────────────────────────────────────────────────────────st.markdown("<br><hr>", unsafe_allow_html=True)st.markdown("""<div style='text-align:center; color:#94A3B8; font-size:0.85rem; padding-bottom: 20px;'><b>EV Site Optimizer v2.0</b> | Powered by Databricks, PySpark & MLflow | Open Data: OSM · WorldPop · World Bank <br><br><i>Developed by Kittikhun (Plam) — Super AI Engineer Season 6 (604905)</i></div>""", unsafe_allow_html=True)
app.py+104-91