benjamin5607 commited on
Commit
4a0a703
ยท
verified ยท
1 Parent(s): 913da7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -72
app.py CHANGED
@@ -8,7 +8,7 @@ import requests
8
  import io
9
 
10
  # ==========================================
11
- # ๐Ÿ” 1. ์„ค์ • ๋ฐ ์‹œํฌ๋ฆฟ ๋กœ๋“œ
12
  # ==========================================
13
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
14
  SHEET_URL = os.environ.get("PRIVATE_SHEET_URL")
@@ -24,37 +24,33 @@ st.markdown("""
24
  """, unsafe_allow_html=True)
25
 
26
  # ==========================================
27
- # 2. ๋ฐ์ดํ„ฐ ๋กœ๋“œ ๋ฐ "๊ฐ•๋ ฅํ•œ" ์ •์ œ
28
  # ==========================================
29
  PLACE_DB = {}
30
 
31
  if not GROQ_API_KEY:
32
- st.error("๐Ÿšจ 'GROQ_API_KEY'๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค! Settings > Secrets๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”.")
33
 
34
  try:
35
  if SHEET_URL:
36
  response = requests.get(SHEET_URL)
37
  response.raise_for_status()
38
 
39
- # 1. CSV ํŒŒ์‹ฑ
40
  try:
41
  df = pd.read_csv(io.StringIO(response.text), on_bad_lines='skip')
42
  except:
43
  df = pd.read_csv(io.StringIO(response.text), engine='python', on_bad_lines='skip')
44
 
45
- # 2. [๊ฐ€์žฅ ์ค‘์š”] ๋ฐ์ดํ„ฐ ๋Œ€์ฒญ์†Œ (Sanitization)
46
- # ์ค„๋ฐ”๊ฟˆ, ํƒญ, ๋”ฐ์˜ดํ‘œ ๋“ฑ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ๊นจ๋œจ๋ฆด ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ํŠน์ˆ˜๋ฌธ์ž ์ œ๊ฑฐ/์น˜ํ™˜
47
- df = df.replace({r'\r': ' ', r'\n': ' ', r'\t': ' ', r'"': "'"}, regex=True)
48
-
49
- # ์ปฌ๋Ÿผ ์ด๋ฆ„ ์ •๋ฆฌ
50
  df.columns = df.columns.str.strip()
51
  col_mapping = {col: 'Country' for col in df.columns if col.lower() == 'country'}
52
  df.rename(columns=col_mapping, inplace=True)
53
 
54
- # 3. JSON ๊ตฌ์กฐ ๋ณ€ํ™˜
55
  if 'Country' in df.columns:
56
  for country, group in df.groupby('Country'):
57
- # NaN ๊ฐ’์„ ๋นˆ ๋ฌธ์ž์—ด๋กœ ์ฒ˜๋ฆฌ
58
  cleaned_group = group.fillna("").to_dict(orient='records')
59
  PLACE_DB[country] = cleaned_group
60
  else:
@@ -78,12 +74,11 @@ if os.path.exists('emily.jpg'):
78
  emily_base64 = base64.b64encode(f.read()).decode()
79
  except: pass
80
 
81
- # JSON ๋คํ”„ ์‹œ ensure_ascii=False๋กœ ํ•œ๊ธ€ ๊นจ์ง ๋ฐฉ์ง€
82
  db_json_raw = json.dumps(PLACE_DB, ensure_ascii=False)
83
  db_base64 = base64.b64encode(db_json_raw.encode('utf-8')).decode('utf-8')
84
 
85
  # ==========================================
86
- # 4. ํ”„๋ก ํŠธ์—”๋“œ (HTML/JS/CSS) - Safe Logic ์ ์šฉ
87
  # ==========================================
88
  html_code = f"""
89
  <!DOCTYPE html>
@@ -102,43 +97,33 @@ html_code = f"""
102
  body {{ margin: 0; padding: 0; background: var(--dark); color: var(--text); font-family: 'Segoe UI', sans-serif; overflow: hidden; }}
103
  .page {{ position: absolute; top: 0; left: 0; width: 100%; height: 100vh; transition: transform 0.5s ease; display: flex; flex-direction: column; }}
104
  #main-page {{ background: linear-gradient(rgba(0,0,0,0.7), rgba(0,0,0,0.95)), url('https://images.unsplash.com/photo-1516981879613-9f5da904015f?q=80&w=2560&auto=format&fit=crop'); background-size: cover; background-position: center; z-index: 100; align-items: center; justify-content: flex-start; padding-top: 20px; }}
105
-
106
  .bartender-btn {{ position: fixed; bottom: 30%; right: 20px; width: 85px; height: 85px; border-radius: 50%; background-image: url('data:image/jpeg;base64,{emily_base64}'); background-size: cover; background-position: center; background-color: #222; border: 3px solid var(--gold); box-shadow: 0 8px 30px rgba(212, 175, 55, 0.6); cursor: pointer; z-index: 999; animation: bounce-float 3s ease-in-out infinite; }}
107
  @keyframes bounce-float {{ 0%, 100% {{ transform: translateY(0); }} 50% {{ transform: translateY(-15px); }} }}
108
-
109
  .lang-switcher {{ position: absolute; top: 20px; right: 20px; z-index: 200; display: flex; gap: 8px; }}
110
  .lang-btn {{ background: rgba(0,0,0,0.5); border: 1px solid #555; color: #aaa; padding: 5px 10px; border-radius: 15px; cursor: pointer; font-size: 11px; }}
111
  .lang-btn.active {{ background: var(--gold); color: black; border-color: var(--gold); font-weight: bold; }}
112
-
113
  .brand-title {{ font-size: 26px; font-weight: 900; color: var(--gold); letter-spacing: 4px; margin-top: 10px; text-transform: uppercase; }}
114
  .brand-subtitle {{ font-size: 11px; color: #ccc; letter-spacing: 2px; margin-bottom: 15px; }}
115
-
116
  .world-map-btn {{ background: rgba(212, 175, 55, 0.2); border: 1px solid var(--gold); color: var(--gold); padding: 12px 25px; border-radius: 30px; font-weight: bold; cursor: pointer; margin-bottom: 20px; display: flex; align-items: center; gap: 8px; backdrop-filter: blur(5px); }}
117
  .passport-btn {{ background: rgba(0,0,0,0.6); border: 1px solid var(--gold); color: var(--gold); padding: 8px 15px; border-radius: 20px; font-size: 12px; cursor: pointer; margin-top: 10px; display: flex; align-items: center; gap: 5px; }}
118
-
119
  .country-select-wrapper {{ width: 85%; max-width: 400px; margin-bottom: 15px; }}
120
  .country-select {{ width: 100%; padding: 14px; background: rgba(30,30,30,0.9); border: 1px solid var(--gold); color: white; font-size: 16px; border-radius: 10px; outline: none; }}
121
-
122
  .type-filter-container {{ width: 100%; padding: 10px 20px; overflow-x: auto; display: flex; gap: 8px; scrollbar-width: none; }}
123
  .type-btn {{ padding: 8px 16px; border-radius: 20px; font-size: 12px; font-weight: bold; cursor: pointer; background: #222; color: #888; border: 1px solid #444; white-space: nowrap; }}
124
  .type-btn.selected {{ background: var(--gold); color: black; border-color: var(--gold); }}
125
-
126
  .place-list-container {{ width: 100%; max-width: 600px; flex: 1; overflow-y: auto; padding: 0 20px 120px 20px; box-sizing: border-box; }}
127
  .place-card {{ background: rgba(30,30,30,0.6); backdrop-filter: blur(10px); border: 1px solid rgba(255,255,255,0.1); border-radius: 15px; padding: 18px; margin-bottom: 15px; position: relative; transition: all 0.3s ease; }}
128
  .place-card.visited {{ background: linear-gradient(135deg, rgba(40,10,10,0.9), rgba(20,20,20,0.9)); border: 1px solid var(--gold); box-shadow: 0 0 15px rgba(212, 175, 55, 0.2); }}
129
  .visited-badge {{ position: absolute; top: 10px; right: 10px; background: var(--gold); color: black; font-size: 10px; font-weight: bold; padding: 3px 8px; border-radius: 10px; display: none; }}
130
  .place-card.visited .visited-badge {{ display: block; }}
131
-
132
  .pc-name {{ font-size: 18px; font-weight: bold; color: white; }}
133
  .place-card.visited .pc-name {{ color: var(--gold); }}
134
  .pc-cat {{ font-size: 11px; color: var(--gold); font-weight: bold; text-transform: uppercase; margin-bottom: 4px; }}
135
  .pc-desc {{ font-size: 13px; color: #bbb; line-height: 1.5; margin-top: 5px; }}
136
-
137
  .card-actions {{ display: flex; gap: 10px; margin-top: 15px; }}
138
  .btn-action {{ flex: 1; padding: 12px; border-radius: 8px; font-size: 12px; font-weight: bold; border: none; cursor: pointer; }}
139
  .btn-ai {{ background: var(--gold); color: black; }}
140
  .btn-map {{ background: #333; color: white; border: 1px solid #555; }}
141
-
142
  #chat-page {{ background: var(--dark); z-index: 1000; transform: translateY(100%); display: flex; flex-direction: column; }}
143
  #chat-messages {{ flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 15px; }}
144
  .msg {{ max-width: 85%; padding: 12px 18px; border-radius: 15px; font-size: 14px; line-height: 1.6; position: relative; }}
@@ -147,13 +132,11 @@ html_code = f"""
147
  .reco-btn {{ display: inline-block; margin-top: 10px; padding: 12px 20px; background: var(--gold); color: black !important; border-radius: 20px; font-weight: 900; text-decoration: none; font-size: 13px; text-align: center; border: none; cursor: pointer; }}
148
  .chat-input-area {{ padding: 15px; background: #111; border-top: 1px solid #333; display: flex; gap: 10px; }}
149
  .chat-input {{ flex: 1; background: #222; border: 1px solid #444; color: white; padding: 14px; border-radius: 10px; outline: none; }}
150
-
151
  #detail-page {{ background: #111; z-index: 300; transform: translateX(100%); display: flex; flex-direction: column; }}
152
  .nav-bar {{ height: 60px; background: rgba(0,0,0,0.9); border-bottom: 1px solid #333; display: flex; align-items: center; padding: 0 20px; justify-content: space-between; }}
153
  #mini-map {{ width: 100%; height: 35%; border-bottom: 2px solid var(--gold); }}
154
  .stamp-btn {{ width: 100%; background: #4a1c1c; color: #ffcc00; border: 2px dashed #ffcc00; padding: 15px; margin-top: 20px; border-radius: 10px; font-weight: bold; cursor: pointer; text-transform: uppercase; letter-spacing: 2px; }}
155
  .stamp-btn:hover {{ background: #ffcc00; color: #4a1c1c; border-style: solid; }}
156
-
157
  #passport-page {{ background: #1a1a1a; z-index: 400; transform: translateY(100%); display: flex; flex-direction: column; }}
158
  .passport-cover {{ padding: 20px; border-bottom: 1px solid #333; display: flex; justify-content: space-between; align-items: center; background: #2c0b0e; }}
159
  .passport-grid {{ display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 20px; padding: 25px; overflow-y: auto; flex: 1; background-image: radial-gradient(#333 15%, transparent 16%), radial-gradient(#333 15%, transparent 16%); background-size: 20px 20px; }}
@@ -164,7 +147,6 @@ html_code = f"""
164
  .stamp-date {{ font-size: 10px; font-family: monospace; }}
165
  .data-controls {{ padding: 15px; background: #111; border-top: 1px solid #333; display: flex; gap: 10px; justify-content: center; }}
166
  .data-btn {{ background: #333; border: 1px solid #555; color: #ccc; padding: 10px 15px; border-radius: 5px; cursor: pointer; font-size: 12px; }}
167
-
168
  #world-map-page {{ background: #111; z-index: 200; transform: translateY(100%); }}
169
  #full-map {{ width: 100%; height: 100%; }}
170
  .map-search-bar {{ position: absolute; top: 80px; left: 50%; transform: translateX(-50%); z-index: 2000; display: flex; gap: 10px; background: rgba(0,0,0,0.85); padding: 10px 20px; border-radius: 30px; border: 1px solid var(--gold); box-shadow: 0 4px 15px rgba(0,0,0,0.5); }}
@@ -258,11 +240,11 @@ html_code = f"""
258
  let miniMap = null, worldMap = null, worldMarkers = L.layerGroup();
259
  let chatHistory = [];
260
  let currentDetailPlace = null;
 
261
 
262
- // [ํ•ต์‹ฌ ๋ณ€๊ฒฝ 1] ๋ฆฌ์ŠคํŠธ์™€ ๋งต ๋ฐ์ดํ„ฐ "๊ธˆ๊ณ " (Global Cache)
263
- // HTML์— ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ๋„ฃ์ง€ ์•Š๊ณ , ์ด๊ณณ์— ์ €์žฅ ํ›„ "๋ฒˆํ˜ธํ‘œ(Key)"๋กœ๋งŒ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.
264
- let LIST_CACHE = [];
265
  let MAP_CACHE = {{}};
 
266
 
267
  const TRANSLATIONS = {{
268
  "KO": {{ "sub": "๊ธ€๋กœ๋ฒŒ ์ฃผ๋ฅ˜ ํ—ค๋ฆฌํ‹ฐ์ง€ ํˆฌ์–ด", "ai": "Korean", "mapBtn": "๐Ÿ—บ๏ธ ์›”๋“œ๋งต ํƒํ—˜", "passBtn": "๐Ÿ“˜ ๋‚ด ์—ฌ๊ถŒ", "filters": ["๐ŸŒ ์ „์ฒด", "๐Ÿบ ์ „ํ†ต์ฃผ", "๐Ÿฅƒ ์œ„์Šคํ‚ค", "๐Ÿบ ๋งฅ์ฃผ", "๐Ÿท ์™€์ธ", "๐Ÿน ๋ฐ”"], "chatPlace": "์—๋ฐ€๋ฆฌ์—๊ฒŒ ๋งํ•ด๋ด, ์ž๊ธฐ์•ผ...", "send": "์ „์†ก", "home": "๐Ÿ  ํ™ˆ", "searchPlace": "์žฅ์†Œ ๊ฒ€์ƒ‰ (์˜ˆ: ๊ฐ•๋‚จ)", "emilyName": "์—๋ฐ€๋ฆฌ (Emily)", "reportHeader": "๋ฆฌํฌํŠธ", "aiBtn": "AI ๋ฆฌ๋ทฐ", "mapLinkBtn": "์ง€๋„" }},
@@ -293,9 +275,15 @@ html_code = f"""
293
  document.querySelectorAll('.lang-btn').forEach(b => b.classList.remove('active'));
294
  el.classList.add('active');
295
  const t = TRANSLATIONS[currentLang];
296
- document.getElementById('app-subtitle').innerText = t.sub; document.getElementById('world-map-btn').innerText = t.mapBtn; document.getElementById('passport-btn').innerText = t.passBtn;
297
- document.getElementById('chat-input').placeholder = t.chatPlace; document.getElementById('chat-send-btn').innerText = t.send; document.getElementById('nav-home-btn').innerText = t.home;
298
- document.getElementById('map-search-input').placeholder = t.searchPlace; document.getElementById('emily-name').innerText = t.emilyName; document.getElementById('repo-header').innerText = t.reportHeader;
 
 
 
 
 
 
299
  for(let i=0; i<6; i++) {{ document.getElementById('f-btn-' + i).innerText = t.filters[i]; }}
300
  renderList();
301
  }}
@@ -308,52 +296,38 @@ html_code = f"""
308
  else {{ return "https://www.google.com/maps/search/" + encodeURIComponent(name) + "/@" + lat + "," + lon + ",18z"; }}
309
  }}
310
 
311
- // [ํ•ต์‹ฌ ๋ณ€๊ฒฝ 2] ๋ฆฌ์ŠคํŠธ ๋ Œ๋”๋ง ๋กœ์ง (์ธ๋ฑ์Šค ๊ธฐ๋ฐ˜)
 
312
  function renderList() {{
313
  const list = document.getElementById('place-list'); list.innerHTML = "";
314
  const stamps = getStamps();
315
  const visitedSet = new Set(stamps.map(s => s ? s.name + '|' + s.country : ''));
316
-
317
  const filtered = (PLACE_DB[selectedCountry] || []).filter(p => selectedType === "All" || p.category === selectedType);
318
- LIST_CACHE = filtered; // ํ˜„์žฌ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ „์—ญ ๋ณ€์ˆ˜์— ์ €์žฅ (์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•จ)
319
-
320
  const t = TRANSLATIONS[currentLang];
321
 
322
  filtered.forEach((p, idx) => {{
323
- const card = document.createElement('div');
324
  const isVisited = visitedSet.has(p.name + '|' + selectedCountry);
325
  card.className = isVisited ? 'place-card visited' : 'place-card';
326
  let desc = (currentLang === 'KO' ? p.desc_ko : (currentLang === 'EN' ? p.desc_en : (currentLang === 'JP' ? p.desc_jp : p.desc_cn))) || p.desc_en || "No description.";
327
 
328
  // HTML ํ‘œ์‹œ์šฉ (๋”ฐ์˜ดํ‘œ ์ œ๊ฑฐ)
329
- const displayDesc = String(desc).replace(/['"]/g, "");
330
- const displayName = String(p.name).replace(/['"]/g, "");
331
-
 
332
  let catIdx = CAT_MAP[p.category] || 0;
333
  let translatedCat = t.filters[catIdx] || p.category;
334
 
335
- // onclick์—๋Š” ์˜ค์ง 'idx'(์ˆซ์ž)๋งŒ ์ „๋‹ฌ -> ์—๋Ÿฌ ์›์ฒœ ์ฐจ๋‹จ
336
- card.innerHTML = "<div class='visited-badge'>VISITED ๐Ÿ’ฎ</div><b>" + displayName + "</b><div class='pc-cat'>" + translatedCat + "</div><div class='pc-desc'>" + displayDesc + "</div>" +
337
- "<div class='card-actions'><button class='btn-action btn-ai' onclick='clickListDetail(" + idx + ")'>" + t.aiBtn + "</button>" +
338
- "<button class='btn-action btn-map' onclick='clickListMap(" + idx + ")'>" + t.mapLinkBtn + "</button></div>";
339
  list.appendChild(card);
340
  }});
341
  }}
342
 
343
- // [ํ•ต์‹ฌ ๋ณ€๊ฒฝ 3] ๋ฆฌ์ŠคํŠธ ํด๋ฆญ ํ•ธ๋“ค๋Ÿฌ (์ธ๋ฑ์Šค๋กœ ๋ฐ์ดํ„ฐ ์ฐพ๊ธฐ)
344
- function clickListDetail(idx) {{
345
- const p = LIST_CACHE[idx];
346
- if(p) openDetail(p.name, selectedCountry);
347
- }}
348
-
349
- function clickListMap(idx) {{
350
- const p = LIST_CACHE[idx];
351
- if(p) {{
352
- const url = getMapUrl(p.name, selectedCountry, p.lat, p.lon, p.address || "");
353
- window.open(url, '_blank');
354
- }}
355
- }}
356
-
357
  function getStamps() {{
358
  try {{ return JSON.parse(localStorage.getItem('drunk_passport') || '[]'); }}
359
  catch(e) {{ localStorage.setItem('drunk_passport', '[]'); return []; }}
@@ -446,26 +420,27 @@ html_code = f"""
446
 
447
  function addChatMessage(t, s, html=false) {{ const box = document.getElementById('chat-messages'); const div = document.createElement('div'); div.className = "msg " + s; if(html) div.innerHTML = t.replace(/\\n/g, '<br>'); else div.innerText = t; box.appendChild(div); box.scrollTop = box.scrollHeight; return div; }}
448
 
449
- // [ํ•ต์‹ฌ ๋ณ€๊ฒฝ 4] ์›”๋“œ๋งต ํŒ์—… ๋กœ์ง (Key-Value ๋ฐฉ์‹)
450
  function openWorldMapPage() {{
451
  document.getElementById('world-map-page').style.transform = 'translateY(0%)';
452
  if(!worldMap) {{
453
  worldMap = L.map('full-map').setView([20, 0], 2); L.tileLayer('https://{{s}}.tile.openstreetmap.org/{{z}}/{{x}}/{{y}}.png').addTo(worldMap);
454
- MAP_CACHE = {{}}; // ์ดˆ๊ธฐํ™”
 
455
 
456
  Object.keys(PLACE_DB).forEach(c => PLACE_DB[c].forEach(p => {{
457
  if(p.lat && p.lon) {{
458
- // ์ขŒํ‘œ๋ฅผ ๊ณ ์œ  ID(Key)๋กœ ์‚ฌ์šฉ (๋ฌธ์ž์—ด ์•„๋‹˜)
459
- const key = p.lat + "_" + p.lon;
460
- MAP_CACHE[key] = {{ name: p.name, country: c, url: getMapUrl(p.name, c, p.lat, p.lon, "") }};
461
 
462
  const safeName = String(p.name).replace(/['"]/g, "");
463
 
464
- // onclick์—๋Š” ์˜ค์ง 'key'๋งŒ ์ „๋‹ฌ (ํŠน์ˆ˜๋ฌธ์ž ์—†์Œ)
465
  const popupContent = "<div style='text-align:center'><b>" + safeName + "</b><br>" +
466
  "<div style='display:flex; gap:5px; margin-top:5px; justify-content:center;'>" +
467
- "<button onclick=\"clickMapDetail('" + key + "')\" style='background:#d4af37; border:none; border-radius:4px; padding:4px 8px; font-size:11px; cursor:pointer;'>๐Ÿค– REVIEW</button>" +
468
- "<button onclick=\"clickMapLink('" + key + "')\" style='background:#333; color:white; border:1px solid #555; border-radius:4px; padding:4px 8px; font-size:11px; cursor:pointer;'>๐Ÿ—บ๏ธ MAP</button>" +
469
  "</div></div>";
470
  L.marker([p.lat, p.lon]).bindPopup(popupContent).addTo(worldMap);
471
  }}
@@ -474,14 +449,14 @@ html_code = f"""
474
  setTimeout(() => worldMap.invalidateSize(), 400);
475
  }}
476
 
477
- // ๋งต ํด๋ฆญ ํ•ธ๋“ค๋Ÿฌ (Key๋กœ ๋ฐ์ดํ„ฐ ์ฐพ๊ธฐ)
478
- function clickMapDetail(key) {{
479
- const data = MAP_CACHE[key];
480
  if(data) {{ closeWorldMapPage(); openDetail(data.name, data.country); }}
481
  }}
482
 
483
- function clickMapLink(key) {{
484
- const data = MAP_CACHE[key];
485
  if(data) window.open(data.url, '_blank');
486
  }}
487
 
@@ -495,7 +470,8 @@ html_code = f"""
495
  function flyToUserLocation() {{ if(userLat && userLon) {{ worldMap.flyTo([userLat, userLon], 13); L.marker([userLat, userLon]).addTo(worldMap).bindPopup("๐Ÿ“ You").openPopup(); }} else alert("GPS..."); }}
496
  function closeWorldMapPage() {{ document.getElementById('world-map-page').style.transform = 'translateY(100%)'; }}
497
 
498
- function closeDetail() {{ document.getElementById('detail-page').style.transform = 'translateX(100%)'; }}
 
499
  async function openDetail(n, c) {{
500
  const p = (PLACE_DB[c] || []).find(x => x.name === n); if(!p) return;
501
  currentDetailPlace = {{ name: n, country: c }};
@@ -508,6 +484,8 @@ html_code = f"""
508
  }}
509
 
510
  function closeChat() {{ document.getElementById('chat-page').style.transform = 'translateY(100%)'; }}
 
 
511
  function closePassportPage() {{ document.getElementById('passport-page').style.transform = 'translateY(100%)'; }}
512
  </script>
513
  </body>
 
8
  import io
9
 
10
  # ==========================================
11
+ # ๐Ÿ” 1. ์‹œํฌ๋ฆฟ ๋ฐ ๊ธฐ๋ณธ ์„ค์ •
12
  # ==========================================
13
  GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
14
  SHEET_URL = os.environ.get("PRIVATE_SHEET_URL")
 
24
  """, unsafe_allow_html=True)
25
 
26
  # ==========================================
27
+ # 2. ๋ฐ์ดํ„ฐ ๋กœ๋“œ ๋ฐ ๊ฐ•๋ ฅํ•œ ์ •์ œ (Sanitization)
28
  # ==========================================
29
  PLACE_DB = {}
30
 
31
  if not GROQ_API_KEY:
32
+ st.error("๐Ÿšจ 'GROQ_API_KEY'๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค! Settings > Secrets ์„ค์ •์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”.")
33
 
34
  try:
35
  if SHEET_URL:
36
  response = requests.get(SHEET_URL)
37
  response.raise_for_status()
38
 
 
39
  try:
40
  df = pd.read_csv(io.StringIO(response.text), on_bad_lines='skip')
41
  except:
42
  df = pd.read_csv(io.StringIO(response.text), engine='python', on_bad_lines='skip')
43
 
44
+ # [ํ•ต์‹ฌ] ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ๊นจ๋œจ๋ฆด ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ํŠน์ˆ˜๋ฌธ์ž ์ œ๊ฑฐ
45
+ # ๋”ฐ์˜ดํ‘œ, ์ค„๋ฐ”๊ฟˆ, ํƒญ์„ ๊ณต๋ฐฑ์ด๋‚˜ ๋นˆ ๋ฌธ์ž๋กœ ์น˜ํ™˜
46
+ df = df.replace({r'\r': ' ', r'\n': ' ', r'"': '', r"'": ''}, regex=True)
47
+
 
48
  df.columns = df.columns.str.strip()
49
  col_mapping = {col: 'Country' for col in df.columns if col.lower() == 'country'}
50
  df.rename(columns=col_mapping, inplace=True)
51
 
 
52
  if 'Country' in df.columns:
53
  for country, group in df.groupby('Country'):
 
54
  cleaned_group = group.fillna("").to_dict(orient='records')
55
  PLACE_DB[country] = cleaned_group
56
  else:
 
74
  emily_base64 = base64.b64encode(f.read()).decode()
75
  except: pass
76
 
 
77
  db_json_raw = json.dumps(PLACE_DB, ensure_ascii=False)
78
  db_base64 = base64.b64encode(db_json_raw.encode('utf-8')).decode('utf-8')
79
 
80
  # ==========================================
81
+ # 4. ํ”„๋ก ํŠธ์—”๋“œ (HTML/JS/CSS) - Integer ID System ์ ์šฉ
82
  # ==========================================
83
  html_code = f"""
84
  <!DOCTYPE html>
 
97
  body {{ margin: 0; padding: 0; background: var(--dark); color: var(--text); font-family: 'Segoe UI', sans-serif; overflow: hidden; }}
98
  .page {{ position: absolute; top: 0; left: 0; width: 100%; height: 100vh; transition: transform 0.5s ease; display: flex; flex-direction: column; }}
99
  #main-page {{ background: linear-gradient(rgba(0,0,0,0.7), rgba(0,0,0,0.95)), url('https://images.unsplash.com/photo-1516981879613-9f5da904015f?q=80&w=2560&auto=format&fit=crop'); background-size: cover; background-position: center; z-index: 100; align-items: center; justify-content: flex-start; padding-top: 20px; }}
 
100
  .bartender-btn {{ position: fixed; bottom: 30%; right: 20px; width: 85px; height: 85px; border-radius: 50%; background-image: url('data:image/jpeg;base64,{emily_base64}'); background-size: cover; background-position: center; background-color: #222; border: 3px solid var(--gold); box-shadow: 0 8px 30px rgba(212, 175, 55, 0.6); cursor: pointer; z-index: 999; animation: bounce-float 3s ease-in-out infinite; }}
101
  @keyframes bounce-float {{ 0%, 100% {{ transform: translateY(0); }} 50% {{ transform: translateY(-15px); }} }}
 
102
  .lang-switcher {{ position: absolute; top: 20px; right: 20px; z-index: 200; display: flex; gap: 8px; }}
103
  .lang-btn {{ background: rgba(0,0,0,0.5); border: 1px solid #555; color: #aaa; padding: 5px 10px; border-radius: 15px; cursor: pointer; font-size: 11px; }}
104
  .lang-btn.active {{ background: var(--gold); color: black; border-color: var(--gold); font-weight: bold; }}
 
105
  .brand-title {{ font-size: 26px; font-weight: 900; color: var(--gold); letter-spacing: 4px; margin-top: 10px; text-transform: uppercase; }}
106
  .brand-subtitle {{ font-size: 11px; color: #ccc; letter-spacing: 2px; margin-bottom: 15px; }}
 
107
  .world-map-btn {{ background: rgba(212, 175, 55, 0.2); border: 1px solid var(--gold); color: var(--gold); padding: 12px 25px; border-radius: 30px; font-weight: bold; cursor: pointer; margin-bottom: 20px; display: flex; align-items: center; gap: 8px; backdrop-filter: blur(5px); }}
108
  .passport-btn {{ background: rgba(0,0,0,0.6); border: 1px solid var(--gold); color: var(--gold); padding: 8px 15px; border-radius: 20px; font-size: 12px; cursor: pointer; margin-top: 10px; display: flex; align-items: center; gap: 5px; }}
 
109
  .country-select-wrapper {{ width: 85%; max-width: 400px; margin-bottom: 15px; }}
110
  .country-select {{ width: 100%; padding: 14px; background: rgba(30,30,30,0.9); border: 1px solid var(--gold); color: white; font-size: 16px; border-radius: 10px; outline: none; }}
 
111
  .type-filter-container {{ width: 100%; padding: 10px 20px; overflow-x: auto; display: flex; gap: 8px; scrollbar-width: none; }}
112
  .type-btn {{ padding: 8px 16px; border-radius: 20px; font-size: 12px; font-weight: bold; cursor: pointer; background: #222; color: #888; border: 1px solid #444; white-space: nowrap; }}
113
  .type-btn.selected {{ background: var(--gold); color: black; border-color: var(--gold); }}
 
114
  .place-list-container {{ width: 100%; max-width: 600px; flex: 1; overflow-y: auto; padding: 0 20px 120px 20px; box-sizing: border-box; }}
115
  .place-card {{ background: rgba(30,30,30,0.6); backdrop-filter: blur(10px); border: 1px solid rgba(255,255,255,0.1); border-radius: 15px; padding: 18px; margin-bottom: 15px; position: relative; transition: all 0.3s ease; }}
116
  .place-card.visited {{ background: linear-gradient(135deg, rgba(40,10,10,0.9), rgba(20,20,20,0.9)); border: 1px solid var(--gold); box-shadow: 0 0 15px rgba(212, 175, 55, 0.2); }}
117
  .visited-badge {{ position: absolute; top: 10px; right: 10px; background: var(--gold); color: black; font-size: 10px; font-weight: bold; padding: 3px 8px; border-radius: 10px; display: none; }}
118
  .place-card.visited .visited-badge {{ display: block; }}
 
119
  .pc-name {{ font-size: 18px; font-weight: bold; color: white; }}
120
  .place-card.visited .pc-name {{ color: var(--gold); }}
121
  .pc-cat {{ font-size: 11px; color: var(--gold); font-weight: bold; text-transform: uppercase; margin-bottom: 4px; }}
122
  .pc-desc {{ font-size: 13px; color: #bbb; line-height: 1.5; margin-top: 5px; }}
 
123
  .card-actions {{ display: flex; gap: 10px; margin-top: 15px; }}
124
  .btn-action {{ flex: 1; padding: 12px; border-radius: 8px; font-size: 12px; font-weight: bold; border: none; cursor: pointer; }}
125
  .btn-ai {{ background: var(--gold); color: black; }}
126
  .btn-map {{ background: #333; color: white; border: 1px solid #555; }}
 
127
  #chat-page {{ background: var(--dark); z-index: 1000; transform: translateY(100%); display: flex; flex-direction: column; }}
128
  #chat-messages {{ flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 15px; }}
129
  .msg {{ max-width: 85%; padding: 12px 18px; border-radius: 15px; font-size: 14px; line-height: 1.6; position: relative; }}
 
132
  .reco-btn {{ display: inline-block; margin-top: 10px; padding: 12px 20px; background: var(--gold); color: black !important; border-radius: 20px; font-weight: 900; text-decoration: none; font-size: 13px; text-align: center; border: none; cursor: pointer; }}
133
  .chat-input-area {{ padding: 15px; background: #111; border-top: 1px solid #333; display: flex; gap: 10px; }}
134
  .chat-input {{ flex: 1; background: #222; border: 1px solid #444; color: white; padding: 14px; border-radius: 10px; outline: none; }}
 
135
  #detail-page {{ background: #111; z-index: 300; transform: translateX(100%); display: flex; flex-direction: column; }}
136
  .nav-bar {{ height: 60px; background: rgba(0,0,0,0.9); border-bottom: 1px solid #333; display: flex; align-items: center; padding: 0 20px; justify-content: space-between; }}
137
  #mini-map {{ width: 100%; height: 35%; border-bottom: 2px solid var(--gold); }}
138
  .stamp-btn {{ width: 100%; background: #4a1c1c; color: #ffcc00; border: 2px dashed #ffcc00; padding: 15px; margin-top: 20px; border-radius: 10px; font-weight: bold; cursor: pointer; text-transform: uppercase; letter-spacing: 2px; }}
139
  .stamp-btn:hover {{ background: #ffcc00; color: #4a1c1c; border-style: solid; }}
 
140
  #passport-page {{ background: #1a1a1a; z-index: 400; transform: translateY(100%); display: flex; flex-direction: column; }}
141
  .passport-cover {{ padding: 20px; border-bottom: 1px solid #333; display: flex; justify-content: space-between; align-items: center; background: #2c0b0e; }}
142
  .passport-grid {{ display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 20px; padding: 25px; overflow-y: auto; flex: 1; background-image: radial-gradient(#333 15%, transparent 16%), radial-gradient(#333 15%, transparent 16%); background-size: 20px 20px; }}
 
147
  .stamp-date {{ font-size: 10px; font-family: monospace; }}
148
  .data-controls {{ padding: 15px; background: #111; border-top: 1px solid #333; display: flex; gap: 10px; justify-content: center; }}
149
  .data-btn {{ background: #333; border: 1px solid #555; color: #ccc; padding: 10px 15px; border-radius: 5px; cursor: pointer; font-size: 12px; }}
 
150
  #world-map-page {{ background: #111; z-index: 200; transform: translateY(100%); }}
151
  #full-map {{ width: 100%; height: 100%; }}
152
  .map-search-bar {{ position: absolute; top: 80px; left: 50%; transform: translateX(-50%); z-index: 2000; display: flex; gap: 10px; background: rgba(0,0,0,0.85); padding: 10px 20px; border-radius: 30px; border: 1px solid var(--gold); box-shadow: 0 4px 15px rgba(0,0,0,0.5); }}
 
240
  let miniMap = null, worldMap = null, worldMarkers = L.layerGroup();
241
  let chatHistory = [];
242
  let currentDetailPlace = null;
243
+ let currentFilteredList = [];
244
 
245
+ // [ํ•ต์‹ฌ ๋ณ€๊ฒฝ: ์ง€๋„ ๋ฐ์ดํ„ฐ ์ €์žฅ์†Œ (Key๋ฅผ ์ˆซ์ž๋กœ ๊ด€๋ฆฌ)]
 
 
246
  let MAP_CACHE = {{}};
247
+ let mapKeyCounter = 0; // ์œ ๋‹ˆํฌํ•œ ์ˆซ์ž ID ์ƒ์„ฑ์šฉ
248
 
249
  const TRANSLATIONS = {{
250
  "KO": {{ "sub": "๊ธ€๋กœ๋ฒŒ ์ฃผ๋ฅ˜ ํ—ค๋ฆฌํ‹ฐ์ง€ ํˆฌ์–ด", "ai": "Korean", "mapBtn": "๐Ÿ—บ๏ธ ์›”๋“œ๋งต ํƒํ—˜", "passBtn": "๐Ÿ“˜ ๋‚ด ์—ฌ๊ถŒ", "filters": ["๐ŸŒ ์ „์ฒด", "๐Ÿบ ์ „ํ†ต์ฃผ", "๐Ÿฅƒ ์œ„์Šคํ‚ค", "๐Ÿบ ๋งฅ์ฃผ", "๐Ÿท ์™€์ธ", "๐Ÿน ๋ฐ”"], "chatPlace": "์—๋ฐ€๋ฆฌ์—๊ฒŒ ๋งํ•ด๋ด, ์ž๊ธฐ์•ผ...", "send": "์ „์†ก", "home": "๐Ÿ  ํ™ˆ", "searchPlace": "์žฅ์†Œ ๊ฒ€์ƒ‰ (์˜ˆ: ๊ฐ•๋‚จ)", "emilyName": "์—๋ฐ€๋ฆฌ (Emily)", "reportHeader": "๋ฆฌํฌํŠธ", "aiBtn": "AI ๋ฆฌ๋ทฐ", "mapLinkBtn": "์ง€๋„" }},
 
275
  document.querySelectorAll('.lang-btn').forEach(b => b.classList.remove('active'));
276
  el.classList.add('active');
277
  const t = TRANSLATIONS[currentLang];
278
+ document.getElementById('app-subtitle').innerText = t.sub;
279
+ document.getElementById('world-map-btn').innerText = t.mapBtn;
280
+ document.getElementById('passport-btn').innerText = t.passBtn;
281
+ document.getElementById('chat-input').placeholder = t.chatPlace;
282
+ document.getElementById('chat-send-btn').innerText = t.send;
283
+ document.getElementById('nav-home-btn').innerText = t.home;
284
+ document.getElementById('map-search-input').placeholder = t.searchPlace;
285
+ document.getElementById('emily-name').innerText = t.emilyName;
286
+ document.getElementById('repo-header').innerText = t.reportHeader;
287
  for(let i=0; i<6; i++) {{ document.getElementById('f-btn-' + i).innerText = t.filters[i]; }}
288
  renderList();
289
  }}
 
296
  else {{ return "https://www.google.com/maps/search/" + encodeURIComponent(name) + "/@" + lat + "," + lon + ",18z"; }}
297
  }}
298
 
299
+ function openMapLink(url) {{ window.open(url, '_blank'); }}
300
+
301
  function renderList() {{
302
  const list = document.getElementById('place-list'); list.innerHTML = "";
303
  const stamps = getStamps();
304
  const visitedSet = new Set(stamps.map(s => s ? s.name + '|' + s.country : ''));
 
305
  const filtered = (PLACE_DB[selectedCountry] || []).filter(p => selectedType === "All" || p.category === selectedType);
306
+ currentFilteredList = filtered; // ๋ฆฌ์ŠคํŠธ ์ธ๋ฑ์Šค์šฉ
 
307
  const t = TRANSLATIONS[currentLang];
308
 
309
  filtered.forEach((p, idx) => {{
310
+ const card = document.createElement('div');
311
  const isVisited = visitedSet.has(p.name + '|' + selectedCountry);
312
  card.className = isVisited ? 'place-card visited' : 'place-card';
313
  let desc = (currentLang === 'KO' ? p.desc_ko : (currentLang === 'EN' ? p.desc_en : (currentLang === 'JP' ? p.desc_jp : p.desc_cn))) || p.desc_en || "No description.";
314
 
315
  // HTML ํ‘œ์‹œ์šฉ (๋”ฐ์˜ดํ‘œ ์ œ๊ฑฐ)
316
+ const safeName = String(p.name).replace(/['"]/g, "");
317
+ const safeDesc = String(desc).replace(/['"]/g, "");
318
+ const safeMapLink = getMapUrl(p.name, selectedCountry, p.lat, p.lon, p.address || "").replace(/['"]/g, "");
319
+
320
  let catIdx = CAT_MAP[p.category] || 0;
321
  let translatedCat = t.filters[catIdx] || p.category;
322
 
323
+ // [์ค‘์š”] onclick์—๋Š” ์˜ค์ง ์ˆซ์ž(idx)๋งŒ ๋“ค์–ด๊ฐ
324
+ card.innerHTML = "<div class='visited-badge'>VISITED ๐Ÿ’ฎ</div><b>" + safeName + "</b><div class='pc-cat'>" + translatedCat + "</div><div class='pc-desc'>" + safeDesc + "</div>" +
325
+ "<div class='card-actions'><button class='btn-action btn-ai' onclick='openDetailByIndex(" + idx + ")'>" + t.aiBtn + "</button>" +
326
+ "<button class='btn-action btn-map' onclick='openMapLink(\"" + safeMapLink + "\")'>" + t.mapLinkBtn + "</button></div>";
327
  list.appendChild(card);
328
  }});
329
  }}
330
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331
  function getStamps() {{
332
  try {{ return JSON.parse(localStorage.getItem('drunk_passport') || '[]'); }}
333
  catch(e) {{ localStorage.setItem('drunk_passport', '[]'); return []; }}
 
420
 
421
  function addChatMessage(t, s, html=false) {{ const box = document.getElementById('chat-messages'); const div = document.createElement('div'); div.className = "msg " + s; if(html) div.innerHTML = t.replace(/\\n/g, '<br>'); else div.innerText = t; box.appendChild(div); box.scrollTop = box.scrollHeight; return div; }}
422
 
423
+ // [ํ•ต์‹ฌ ๋ณ€๊ฒฝ 4] ์›”๋“œ๋งต ํŒ์—… - ์ˆซ์ž ID ์‚ฌ์šฉ (๋”ฐ์˜ดํ‘œ ์ง€์˜ฅ ํƒˆ์ถœ)
424
  function openWorldMapPage() {{
425
  document.getElementById('world-map-page').style.transform = 'translateY(0%)';
426
  if(!worldMap) {{
427
  worldMap = L.map('full-map').setView([20, 0], 2); L.tileLayer('https://{{s}}.tile.openstreetmap.org/{{z}}/{{x}}/{{y}}.png').addTo(worldMap);
428
+ MAP_CACHE = {{}};
429
+ mapKeyCounter = 0; // ์ดˆ๊ธฐํ™”
430
 
431
  Object.keys(PLACE_DB).forEach(c => PLACE_DB[c].forEach(p => {{
432
  if(p.lat && p.lon) {{
433
+ // ๋ฐ์ดํ„ฐ๋ฅผ ์บ์‹œ์— ์ €์žฅํ•˜๊ณ  ์ˆซ์ž(ID)๋ฅผ ๋ฐœ๊ธ‰
434
+ const currentId = mapKeyCounter++;
435
+ MAP_CACHE[currentId] = {{ name: p.name, country: c, url: getMapUrl(p.name, c, p.lat, p.lon, "") }};
436
 
437
  const safeName = String(p.name).replace(/['"]/g, "");
438
 
439
+ // onclick์—๋Š” ์˜ค์ง ์ˆซ์ž(currentId)๋งŒ ์ „๋‹ฌ -> ์ ˆ๋Œ€ ์—๋Ÿฌ ์•ˆ๋‚จ
440
  const popupContent = "<div style='text-align:center'><b>" + safeName + "</b><br>" +
441
  "<div style='display:flex; gap:5px; margin-top:5px; justify-content:center;'>" +
442
+ "<button onclick='clickMapDetail(" + currentId + ")' style='background:#d4af37; border:none; border-radius:4px; padding:4px 8px; font-size:11px; cursor:pointer;'>๐Ÿค– REVIEW</button>" +
443
+ "<button onclick='clickMapLink(" + currentId + ")' style='background:#333; color:white; border:1px solid #555; border-radius:4px; padding:4px 8px; font-size:11px; cursor:pointer;'>๐Ÿ—บ๏ธ MAP</button>" +
444
  "</div></div>";
445
  L.marker([p.lat, p.lon]).bindPopup(popupContent).addTo(worldMap);
446
  }}
 
449
  setTimeout(() => worldMap.invalidateSize(), 400);
450
  }}
451
 
452
+ // ๋งต ํด๋ฆญ ํ•ธ๋“ค๋Ÿฌ (์ˆซ์ž๋กœ ๋ฐ์ดํ„ฐ ์ฐพ๊ธฐ)
453
+ function clickMapDetail(id) {{
454
+ const data = MAP_CACHE[id];
455
  if(data) {{ closeWorldMapPage(); openDetail(data.name, data.country); }}
456
  }}
457
 
458
+ function clickMapLink(id) {{
459
+ const data = MAP_CACHE[id];
460
  if(data) window.open(data.url, '_blank');
461
  }}
462
 
 
470
  function flyToUserLocation() {{ if(userLat && userLon) {{ worldMap.flyTo([userLat, userLon], 13); L.marker([userLat, userLon]).addTo(worldMap).bindPopup("๐Ÿ“ You").openPopup(); }} else alert("GPS..."); }}
471
  function closeWorldMapPage() {{ document.getElementById('world-map-page').style.transform = 'translateY(100%)'; }}
472
 
473
+ function openDetailFromMap(n, c) {{ closeWorldMapPage(); openDetail(n, c); }}
474
+ function openDetailByIndex(idx) {{ const p = currentFilteredList[idx]; if(p) openDetail(p.name, selectedCountry); }}
475
  async function openDetail(n, c) {{
476
  const p = (PLACE_DB[c] || []).find(x => x.name === n); if(!p) return;
477
  currentDetailPlace = {{ name: n, country: c }};
 
484
  }}
485
 
486
  function closeChat() {{ document.getElementById('chat-page').style.transform = 'translateY(100%)'; }}
487
+ function closeDetail() {{ document.getElementById('detail-page').style.transform = 'translateX(100%)'; }}
488
+ function closeWorldMapPage() {{ document.getElementById('world-map-page').style.transform = 'translateY(100%)'; }}
489
  function closePassportPage() {{ document.getElementById('passport-page').style.transform = 'translateY(100%)'; }}
490
  </script>
491
  </body>