benjamin5607 commited on
Commit
7b11e9f
ยท
verified ยท
1 Parent(s): 3d3f989

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -37
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,19 +24,21 @@ st.markdown("""
24
  """, unsafe_allow_html=True)
25
 
26
  # ==========================================
27
- # 2. ๋ฐ์ดํ„ฐ ๋กœ๋“œ ๋ฐ ์ •์ œ (JS ์—๋Ÿฌ ์›์ฒœ ๋ด‰์‡„)
28
  # ==========================================
29
  PLACE_DB = {}
30
 
31
  try:
32
  if SHEET_URL:
33
  response = requests.get(SHEET_URL)
 
34
  response.raise_for_status()
 
35
  df = pd.read_csv(io.StringIO(response.text), on_bad_lines='skip')
36
 
37
- # [๋ฐฉ์–ด๋ง‰] ๋ชจ๋“  ๋ฌธ์ž์—ด์—์„œ ๋”ฐ์˜ดํ‘œ, ์—ญ์Šฌ๋ž˜์‹œ, ์ค„๋ฐ”๊ฟˆ ์ œ๊ฑฐ (์—๋Ÿฌ์˜ ์ฃผ๋ฒ”๋“ค)
38
  for col in df.select_dtypes(include=['object']).columns:
39
- df[col] = df[col].astype(str).replace({r'["\\'']': '', r'[\r\n\t]+': ' '}, regex=True)
40
 
41
  df.columns = df.columns.str.strip()
42
  col_mapping = {col: 'Country' for col in df.columns if col.lower() == 'country'}
@@ -48,7 +50,7 @@ try:
48
  except Exception as e:
49
  st.error(f"๋ฐ์ดํ„ฐ ๋กœ๋“œ ์‹คํŒจ: {e}")
50
 
51
- # ์—๋ฐ€๋ฆฌ ์ด๋ฏธ์ง€
52
  emily_base64 = ""
53
  if os.path.exists('emily.jpg'):
54
  try:
@@ -56,11 +58,12 @@ if os.path.exists('emily.jpg'):
56
  emily_base64 = base64.b64encode(f.read()).decode()
57
  except: pass
58
 
59
- # JS์— ๋„˜๊ธธ ๋ฐ์ดํ„ฐ ์ธ์ฝ”๋”ฉ
60
- db_json_encoded = base64.b64encode(json.dumps(PLACE_DB, ensure_ascii=False).encode('utf-8')).decode('utf-8')
 
61
 
62
  # ==========================================
63
- # 3. ํ”„๋ก ํŠธ์—”๋“œ (f-string ์ค‘๊ด„ํ˜ธ ์™„๋ฒฝ ๋ณดํ˜ธ)
64
  # ==========================================
65
  html_code = f"""
66
  <!DOCTYPE html>
@@ -74,7 +77,7 @@ html_code = f"""
74
  <link href="https://fonts.googleapis.com/css2?family=Special+Elite&display=swap" rel="stylesheet">
75
  <style>
76
  :root {{ --gold: #d4af37; --dark: #0a0a0a; --text: #e0e0e0; }}
77
- body {{ margin: 0; padding: 0; background: var(--dark); color: var(--text); font-family: 'Segoe UI', sans-serif; overflow: hidden; }}
78
  .page {{ position: absolute; top: 0; left: 0; width: 100%; height: 100vh; transition: transform 0.5s ease; display: flex; flex-direction: column; }}
79
  #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; }}
80
  .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; }}
@@ -153,19 +156,17 @@ html_code = f"""
153
  <div id="chat-page" class="page">
154
  <div class="nav-bar" style="background:#111; padding:15px; display:flex; align-items:center;">
155
  <button onclick="closeChat()" style="background:none; border:none; color:var(--gold); font-size:24px;">โ†</button>
156
- <div id="emily-name" style="color:var(--gold); font-weight:bold; margin-left:15px;">์—๋ฐ€๋ฆฌ</div>
157
  </div>
158
  <div id="chat-messages"></div>
159
  <div class="chat-input-area">
160
- <input type="text" id="chat-input" class="chat-input" placeholder="..." onkeydown="if(event.key === 'Enter') askBartender()">
161
  <button onclick="askBartender()" style="background:var(--gold); border:none; padding:10px 20px; border-radius:8px; font-weight:bold;">SEND</button>
162
  </div>
163
  </div>
164
 
165
  <div id="detail-page" class="page">
166
- <div class="nav-bar" style="background:#000; padding:15px;">
167
- <button onclick="closeDetail()" style="background:none; border:none; color:var(--gold); font-size:24px;">โ†</button>
168
- </div>
169
  <div id="mini-map"></div>
170
  <div style="flex:1; overflow-y:auto; padding:20px;">
171
  <div id="repo-title" style="font-size:24px; color:var(--gold); font-weight:bold;"></div>
@@ -175,18 +176,14 @@ html_code = f"""
175
  </div>
176
 
177
  <div id="passport-page" class="page">
178
- <div class="nav-bar" style="background:#2c0b0e; padding:15px;">
179
- <button onclick="closePassportPage()" style="background:none; border:none; color:var(--gold); font-size:24px;">โ†</button>
180
- </div>
181
  <div id="passport-grid" class="passport-grid"></div>
182
  </div>
183
 
184
  <div id="world-map-page" class="page">
185
- <div class="nav-bar" style="background:#000; padding:15px;">
186
- <button onclick="closeWorldMapPage()" style="background:none; border:none; color:var(--gold); font-size:18px;">๐Ÿ  HOME</button>
187
- </div>
188
  <div class="map-search-bar">
189
- <input type="text" id="map-search-input" class="search-input" placeholder="..." onkeydown="if(event.key === 'Enter') searchLocation()">
190
  <button onclick="searchLocation()" style="background:var(--gold); border:none; border-radius:50%; width:30px; height:30px;">๐Ÿ”</button>
191
  </div>
192
  <button class="map-gps-btn" onclick="flyToUserLocation()">๐ŸŽฏ</button>
@@ -196,17 +193,21 @@ html_code = f"""
196
  <div id="loader" class="loading-screen"><div class="loader-ring"></div><div id="loader-msg" style="color:var(--gold); margin-top:15px;"></div></div>
197
 
198
  <script>
199
- // [์•ˆ์ „์žฅ์น˜] Base64๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„ ํŒŒ์‹ฑ (๋ฌธ์ž์—ด ๊นจ์ง ๋ฐฉ์ง€)
200
- const RAW_DATA = "{db_json_encoded}";
201
- const PLACE_DB = JSON.parse(decodeURIComponent(escape(atob(RAW_DATA))));
 
 
 
202
 
 
203
  let userLat=null, userLon=null, selectedCountry="", selectedType="All", currentLang="KO", chatInitialized=false;
204
  let miniMap=null, worldMap=null, currentFilteredList=[], mapCache={{}}, mapIdCounter=0;
205
 
206
  const TRANSLATIONS = {{
207
  "KO": {{ "ai": "Korean", "aiBtn": "AI ๋ฆฌ๋ทฐ", "mapLinkBtn": "์ง€๋„" }},
208
  "EN": {{ "ai": "English", "aiBtn": "AI REVIEW", "mapLinkBtn": "MAP" }},
209
- "JP": {{ "ai": "Japanese", "aiBtn": "AI ใƒฌใƒ“ใƒฅใƒผ", "mapLinkBtn": "ๅœฐๅ›ณ" }},
210
  "CN": {{ "ai": "Chinese", "aiBtn": "AI ็‚น่ฏ„", "mapLinkBtn": "ๅœฐๅ›พ" }}
211
  }};
212
 
@@ -244,11 +245,9 @@ html_code = f"""
244
  const isVisited = visitedSet.has(p.name + '|' + selectedCountry);
245
  const card = document.createElement('div');
246
  card.className = isVisited ? 'place-card visited' : 'place-card';
247
-
248
  const t = TRANSLATIONS[currentLang];
249
  const desc = p['desc_' + currentLang.toLowerCase()] || p.desc_en || "...";
250
 
251
- // [์•ˆ์ „] ์ˆซ์ž๋กœ๋งŒ ๋ฒ„ํŠผ ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
252
  card.innerHTML = `
253
  <div class='visited-badge'>VISITED ๐Ÿ’ฎ</div>
254
  <b style='font-size:18px;'>${{p.name}}</b>
@@ -257,8 +256,7 @@ html_code = f"""
257
  <div class='card-actions'>
258
  <button class='btn-action btn-ai' onclick='clickAction(${{idx}}, "ai")'>${{t.aiBtn}}</button>
259
  <button class='btn-action btn-map' onclick='clickAction(${{idx}}, "map")'>${{t.mapLinkBtn}}</button>
260
- </div>
261
- `;
262
  list.appendChild(card);
263
  }});
264
  }}
@@ -304,14 +302,9 @@ html_code = f"""
304
  document.getElementById('detail-page').style.transform = 'translateX(0%)';
305
  document.getElementById('repo-title').innerText = n;
306
  document.getElementById('loader').style.display='flex';
307
-
308
- if(!miniMap) {{
309
- miniMap = L.map('mini-map');
310
- L.tileLayer('https://{{s}}.tile.openstreetmap.org/{{z}}/{{x}}/{{y}}.png').addTo(miniMap);
311
- }}
312
  setTimeout(() => {{ miniMap.invalidateSize(); miniMap.setView([p.lat, p.lon], 15); L.marker([p.lat, p.lon]).addTo(miniMap); }}, 400);
313
-
314
- const res = await callGroq([{{ role: "system", content: "You are Emily. Write a report." }}, {{ role: "user", content: "Review " + n + " in " + c }}]);
315
  document.getElementById('loader').style.display='none';
316
  document.getElementById('repo-content').innerText = res;
317
  }}
@@ -325,7 +318,7 @@ html_code = f"""
325
  }});
326
  const data = await res.json();
327
  return data.choices[0].message.content;
328
- }} catch(e) {{ return "Error..."; }}
329
  }}
330
 
331
  function openChat() {{ document.getElementById('chat-page').style.transform = 'translateY(0%)'; }}
@@ -337,6 +330,7 @@ html_code = f"""
337
 
338
  function stampCurrentPlace() {{
339
  const stamps = JSON.parse(localStorage.getItem('drunk_passport') || '[]');
 
340
  stamps.push({{ name: currentDetailPlace.name, country: currentDetailPlace.country, date: new Date().toLocaleDateString() }});
341
  localStorage.setItem('drunk_passport', JSON.stringify(stamps));
342
  renderList(); alert("Stamped! ๐Ÿ’ฎ");
@@ -351,6 +345,13 @@ html_code = f"""
351
  grid.appendChild(el);
352
  }});
353
  }}
 
 
 
 
 
 
 
354
  </script>
355
  </body>
356
  </html>
 
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. ๋ฐ์ดํ„ฐ ๋กœ๋“œ ๋ฐ ์ •์ œ (์œ ๋‹ˆ์ฝ”๋“œ ๋ณด์กด ๋ฒ„์ „)
28
  # ==========================================
29
  PLACE_DB = {}
30
 
31
  try:
32
  if SHEET_URL:
33
  response = requests.get(SHEET_URL)
34
+ response.encoding = 'utf-8' # ํ•œ๊ธ€ ๊นจ์ง ๋ฐฉ์ง€ 1์ˆœ์œ„
35
  response.raise_for_status()
36
+
37
  df = pd.read_csv(io.StringIO(response.text), on_bad_lines='skip')
38
 
39
+ # [์ˆ˜์ •] ๋”ฐ์˜ดํ‘œ์™€ ์ค„๋ฐ”๊ฟˆ๋งŒ ์‚ด์ง ๋‹ค๋“ฌ๊ณ  ํ•œ๊ธ€์€ ๊ทธ๋Œ€๋กœ ๋‘ก๋‹ˆ๋‹ค.
40
  for col in df.select_dtypes(include=['object']).columns:
41
+ df[col] = df[col].astype(str).str.replace(r'[\r\n\t]+', ' ', regex=True).str.replace('"', '').str.replace("'", "")
42
 
43
  df.columns = df.columns.str.strip()
44
  col_mapping = {col: 'Country' for col in df.columns if col.lower() == 'country'}
 
50
  except Exception as e:
51
  st.error(f"๋ฐ์ดํ„ฐ ๋กœ๋“œ ์‹คํŒจ: {e}")
52
 
53
+ # ์—๋ฐ€๋ฆฌ ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ
54
  emily_base64 = ""
55
  if os.path.exists('emily.jpg'):
56
  try:
 
58
  emily_base64 = base64.b64encode(f.read()).decode()
59
  except: pass
60
 
61
+ # [ํ•ต์‹ฌ] ๋ฐ์ดํ„ฐ๋ฅผ Base64๋กœ ์ธ์ฝ”๋”ฉํ•˜๋˜ ์œ ๋‹ˆ์ฝ”๋“œ๋ฅผ ์ง€์›ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌ
62
+ db_json_str = json.dumps(PLACE_DB, ensure_ascii=False)
63
+ db_base64 = base64.b64encode(db_json_str.encode('utf-8')).decode('utf-8')
64
 
65
  # ==========================================
66
+ # 3. ํ”„๋ก ํŠธ์—”๋“œ (ํ•œ๊ธ€ ๋ณต๊ตฌ ๋กœ์ง ํฌํ•จ)
67
  # ==========================================
68
  html_code = f"""
69
  <!DOCTYPE html>
 
77
  <link href="https://fonts.googleapis.com/css2?family=Special+Elite&display=swap" rel="stylesheet">
78
  <style>
79
  :root {{ --gold: #d4af37; --dark: #0a0a0a; --text: #e0e0e0; }}
80
+ body {{ margin: 0; padding: 0; background: var(--dark); color: var(--text); font-family: 'Segoe UI', 'Apple SD Gothic Neo', sans-serif; overflow: hidden; }}
81
  .page {{ position: absolute; top: 0; left: 0; width: 100%; height: 100vh; transition: transform 0.5s ease; display: flex; flex-direction: column; }}
82
  #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; }}
83
  .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; }}
 
156
  <div id="chat-page" class="page">
157
  <div class="nav-bar" style="background:#111; padding:15px; display:flex; align-items:center;">
158
  <button onclick="closeChat()" style="background:none; border:none; color:var(--gold); font-size:24px;">โ†</button>
159
+ <div style="color:var(--gold); font-weight:bold; margin-left:15px;">Emily the Bartender</div>
160
  </div>
161
  <div id="chat-messages"></div>
162
  <div class="chat-input-area">
163
+ <input type="text" id="chat-input" class="chat-input" placeholder="Talk to Emily..." onkeydown="if(event.key === 'Enter') askBartender()">
164
  <button onclick="askBartender()" style="background:var(--gold); border:none; padding:10px 20px; border-radius:8px; font-weight:bold;">SEND</button>
165
  </div>
166
  </div>
167
 
168
  <div id="detail-page" class="page">
169
+ <div class="nav-bar" style="background:#000; padding:15px;"><button onclick="closeDetail()" style="background:none; border:none; color:var(--gold); font-size:24px;">โ†</button></div>
 
 
170
  <div id="mini-map"></div>
171
  <div style="flex:1; overflow-y:auto; padding:20px;">
172
  <div id="repo-title" style="font-size:24px; color:var(--gold); font-weight:bold;"></div>
 
176
  </div>
177
 
178
  <div id="passport-page" class="page">
179
+ <div class="nav-bar" style="background:#2c0b0e; padding:15px;"><button onclick="closePassportPage()" style="background:none; border:none; color:var(--gold); font-size:24px;">โ†</button></div>
 
 
180
  <div id="passport-grid" class="passport-grid"></div>
181
  </div>
182
 
183
  <div id="world-map-page" class="page">
184
+ <div class="nav-bar" style="background:#000; padding:15px;"><button onclick="closeWorldMapPage()" style="background:none; border:none; color:var(--gold); font-size:18px;">๐Ÿ  HOME</button></div>
 
 
185
  <div class="map-search-bar">
186
+ <input type="text" id="map-search-input" class="search-input" placeholder="Search..." onkeydown="if(event.key === 'Enter') searchLocation()">
187
  <button onclick="searchLocation()" style="background:var(--gold); border:none; border-radius:50%; width:30px; height:30px;">๐Ÿ”</button>
188
  </div>
189
  <button class="map-gps-btn" onclick="flyToUserLocation()">๐ŸŽฏ</button>
 
193
  <div id="loader" class="loading-screen"><div class="loader-ring"></div><div id="loader-msg" style="color:var(--gold); margin-top:15px;"></div></div>
194
 
195
  <script>
196
+ // [์ˆ˜์ •] ํ•œ๊ธ€ ๊นจ์ง์„ ๋ฐฉ์ง€ํ•˜๋Š” ์œ ๋‹ˆ์ฝ”๋“œ ์•ˆ์ „ ๋””์ฝ”๋”ฉ ํ•จ์ˆ˜
197
+ function decodeUnicodeBase64(s) {{
198
+ return decodeURIComponent(Array.prototype.map.call(atob(s), function(c) {{
199
+ return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
200
+ }}).join(''));
201
+ }}
202
 
203
+ const PLACE_DB = JSON.parse(decodeUnicodeBase64("{db_base64}"));
204
  let userLat=null, userLon=null, selectedCountry="", selectedType="All", currentLang="KO", chatInitialized=false;
205
  let miniMap=null, worldMap=null, currentFilteredList=[], mapCache={{}}, mapIdCounter=0;
206
 
207
  const TRANSLATIONS = {{
208
  "KO": {{ "ai": "Korean", "aiBtn": "AI ๋ฆฌ๋ทฐ", "mapLinkBtn": "์ง€๋„" }},
209
  "EN": {{ "ai": "English", "aiBtn": "AI REVIEW", "mapLinkBtn": "MAP" }},
210
+ "JP": {{ "ai": "Japanese", "aiBtn": "AI ใƒฌ๋ทฐ", "mapLinkBtn": "ๅœฐๅ›ณ" }},
211
  "CN": {{ "ai": "Chinese", "aiBtn": "AI ็‚น่ฏ„", "mapLinkBtn": "ๅœฐๅ›พ" }}
212
  }};
213
 
 
245
  const isVisited = visitedSet.has(p.name + '|' + selectedCountry);
246
  const card = document.createElement('div');
247
  card.className = isVisited ? 'place-card visited' : 'place-card';
 
248
  const t = TRANSLATIONS[currentLang];
249
  const desc = p['desc_' + currentLang.toLowerCase()] || p.desc_en || "...";
250
 
 
251
  card.innerHTML = `
252
  <div class='visited-badge'>VISITED ๐Ÿ’ฎ</div>
253
  <b style='font-size:18px;'>${{p.name}}</b>
 
256
  <div class='card-actions'>
257
  <button class='btn-action btn-ai' onclick='clickAction(${{idx}}, "ai")'>${{t.aiBtn}}</button>
258
  <button class='btn-action btn-map' onclick='clickAction(${{idx}}, "map")'>${{t.mapLinkBtn}}</button>
259
+ </div>`;
 
260
  list.appendChild(card);
261
  }});
262
  }}
 
302
  document.getElementById('detail-page').style.transform = 'translateX(0%)';
303
  document.getElementById('repo-title').innerText = n;
304
  document.getElementById('loader').style.display='flex';
305
+ if(!miniMap) {{ miniMap = L.map('mini-map'); L.tileLayer('https://{{s}}.tile.openstreetmap.org/{{z}}/{{x}}/{{y}}.png').addTo(miniMap); }}
 
 
 
 
306
  setTimeout(() => {{ miniMap.invalidateSize(); miniMap.setView([p.lat, p.lon], 15); L.marker([p.lat, p.lon]).addTo(miniMap); }}, 400);
307
+ const res = await callGroq([{{ role: "system", content: "You are Emily. Write a travel report." }}, {{ role: "user", content: "Review " + n + " in " + c }}]);
 
308
  document.getElementById('loader').style.display='none';
309
  document.getElementById('repo-content').innerText = res;
310
  }}
 
318
  }});
319
  const data = await res.json();
320
  return data.choices[0].message.content;
321
+ }} catch(e) {{ return "Error connecting to AI..."; }}
322
  }}
323
 
324
  function openChat() {{ document.getElementById('chat-page').style.transform = 'translateY(0%)'; }}
 
330
 
331
  function stampCurrentPlace() {{
332
  const stamps = JSON.parse(localStorage.getItem('drunk_passport') || '[]');
333
+ if(stamps.find(s => s.name === currentDetailPlace.name)) return alert("Already stamped!");
334
  stamps.push({{ name: currentDetailPlace.name, country: currentDetailPlace.country, date: new Date().toLocaleDateString() }});
335
  localStorage.setItem('drunk_passport', JSON.stringify(stamps));
336
  renderList(); alert("Stamped! ๐Ÿ’ฎ");
 
345
  grid.appendChild(el);
346
  }});
347
  }}
348
+ async function searchLocation() {{
349
+ const q = document.getElementById('map-search-input').value; if(!q) return;
350
+ const res = await fetch(`https://nominatim.openstreetmap.org/search?format=json&q=${{encodeURIComponent(q)}}`);
351
+ const data = await res.json();
352
+ if(data.length > 0) worldMap.flyTo([data[0].lat, data[0].lon], 12);
353
+ }}
354
+ function flyToUserLocation() {{ if(userLat) worldMap.flyTo([userLat, userLon], 14); }}
355
  </script>
356
  </body>
357
  </html>