Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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,7 +24,7 @@ st.markdown("""
|
|
| 24 |
""", unsafe_allow_html=True)
|
| 25 |
|
| 26 |
# ==========================================
|
| 27 |
-
# 2. λ°μ΄ν° λ‘λ (Google Sheet μ°λ)
|
| 28 |
# ==========================================
|
| 29 |
PLACE_DB = {}
|
| 30 |
try:
|
|
@@ -34,6 +34,7 @@ try:
|
|
| 34 |
response.raise_for_status()
|
| 35 |
df = pd.read_csv(io.StringIO(response.text), on_bad_lines='skip')
|
| 36 |
|
|
|
|
| 37 |
for col in df.select_dtypes(include=['object']).columns:
|
| 38 |
df[col] = df[col].astype(str).str.replace(r'[\r\n\t]+', ' ', regex=True).str.replace('"', '\\"').str.replace("'", "\\'")
|
| 39 |
|
|
@@ -58,7 +59,7 @@ db_json_str = json.dumps(PLACE_DB, ensure_ascii=False)
|
|
| 58 |
db_base64 = base64.b64encode(db_json_str.encode('utf-8')).decode('utf-8')
|
| 59 |
|
| 60 |
# ==========================================
|
| 61 |
-
# 3. ν΅ν© νλ‘ νΈμλ (μ€κ΄νΈ μ
|
| 62 |
# ==========================================
|
| 63 |
html_code = f"""
|
| 64 |
<!DOCTYPE html>
|
|
@@ -175,15 +176,20 @@ html_code = f"""
|
|
| 175 |
window.onload = function() {{
|
| 176 |
const keys = Object.keys(PLACE_DB).sort();
|
| 177 |
const select = document.getElementById('country-select');
|
| 178 |
-
keys.forEach(k
|
|
|
|
|
|
|
|
|
|
| 179 |
if(keys.length > 0) selectCountry(keys[0]);
|
| 180 |
-
navigator.geolocation.getCurrentPosition(p
|
|
|
|
|
|
|
| 181 |
}};
|
| 182 |
|
| 183 |
function selectCountry(c) {{ selectedCountry = c; renderList(); }}
|
| 184 |
function setLanguage(l, el) {{
|
| 185 |
currentLang = l;
|
| 186 |
-
document.querySelectorAll('.lang-btn').forEach(b
|
| 187 |
el.classList.add('active');
|
| 188 |
renderList();
|
| 189 |
}}
|
|
@@ -202,19 +208,19 @@ html_code = f"""
|
|
| 202 |
list.innerHTML = "";
|
| 203 |
const countryPlaces = PLACE_DB[selectedCountry] || [];
|
| 204 |
|
| 205 |
-
countryPlaces.forEach(p
|
| 206 |
const card = document.createElement('div');
|
| 207 |
card.className = 'place-card';
|
| 208 |
const descKey = 'desc_' + currentLang.toLowerCase();
|
| 209 |
let desc = p[descKey] || p.desc_en || p.desc_ko || "Exploring the taste of heritage...";
|
| 210 |
const mapUrl = getMapUrl(p, selectedCountry);
|
| 211 |
|
| 212 |
-
card.innerHTML =
|
| 213 |
-
<div class=
|
| 214 |
-
<div style=
|
| 215 |
-
<button class=
|
| 216 |
-
<button class=
|
| 217 |
-
</div>
|
| 218 |
list.appendChild(card);
|
| 219 |
}});
|
| 220 |
}}
|
|
@@ -238,7 +244,7 @@ html_code = f"""
|
|
| 238 |
const match = res.match(/\\[(.*?)\\]/);
|
| 239 |
if(match) {{
|
| 240 |
const pName = match[1];
|
| 241 |
-
botMsgDiv.innerHTML +=
|
| 242 |
}}
|
| 243 |
chatHistory.push({{ role: "assistant", content: res }});
|
| 244 |
}}
|
|
@@ -251,7 +257,7 @@ html_code = f"""
|
|
| 251 |
}}
|
| 252 |
|
| 253 |
async function openReview(name, country) {{
|
| 254 |
-
const p = PLACE_DB[country].find(x
|
| 255 |
document.getElementById('repo-title').innerText = name;
|
| 256 |
document.getElementById('detail-page').style.transform = 'translateX(0%)';
|
| 257 |
document.getElementById('loader').style.display = 'flex';
|
|
@@ -259,7 +265,7 @@ html_code = f"""
|
|
| 259 |
miniMap = L.map('mini-map');
|
| 260 |
L.tileLayer('https://{{s}}.tile.openstreetmap.org/{{z}}/{{x}}/{{y}}.png').addTo(miniMap);
|
| 261 |
}}
|
| 262 |
-
setTimeout(()
|
| 263 |
const prompt = [
|
| 264 |
{{ role: "system", content: "Act as Emily. Write review in " + TRANSLATIONS[currentLang].ai + ". Sections: Vibe, Must Try, Secret Tip." }},
|
| 265 |
{{ role: "user", content: "Write review for " + name + " in " + country }}
|
|
@@ -289,28 +295,32 @@ html_code = f"""
|
|
| 289 |
if(!worldMap) {{
|
| 290 |
worldMap = L.map('full-map').setView([20, 0], 2);
|
| 291 |
L.tileLayer('https://{{s}}.tile.openstreetmap.org/{{z}}/{{x}}/{{y}}.png').addTo(worldMap);
|
| 292 |
-
Object.keys(PLACE_DB).forEach(c
|
| 293 |
-
|
| 294 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
}}
|
| 296 |
-
setTimeout(()
|
| 297 |
}}
|
| 298 |
|
| 299 |
function flyToUser() {{
|
| 300 |
if(userLat && userLon) {{
|
| 301 |
worldMap.flyTo([userLat, userLon], 15);
|
| 302 |
L.marker([userLat, userLon]).addTo(worldMap).bindPopup("π My Spot").openPopup();
|
| 303 |
-
}} else alert("GPS off, honey!");
|
| 304 |
}}
|
| 305 |
|
| 306 |
function searchAndOpenMap(pName) {{
|
| 307 |
let found = null; let fCountry = "";
|
| 308 |
-
Object.keys(PLACE_DB).some(c
|
| 309 |
-
found = PLACE_DB[c].find(p
|
| 310 |
if(found) {{ fCountry = c; return true; }}
|
| 311 |
}});
|
| 312 |
-
if(found) window.open(getMapUrl(found, fCountry), '_blank');
|
| 313 |
-
else window.open("https://www.google.com/maps/search/" + encodeURIComponent(pName), '_blank');
|
| 314 |
}}
|
| 315 |
|
| 316 |
function closeMap() {{ document.getElementById('world-map-page').style.transform = 'translateY(100%)'; }}
|
|
|
|
| 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. λ°μ΄ν° λ‘λ (Google Sheet μ°λ λ° μ μ )
|
| 28 |
# ==========================================
|
| 29 |
PLACE_DB = {}
|
| 30 |
try:
|
|
|
|
| 34 |
response.raise_for_status()
|
| 35 |
df = pd.read_csv(io.StringIO(response.text), on_bad_lines='skip')
|
| 36 |
|
| 37 |
+
# λ¬Έμμ΄ μ μ (JS λ¬Έλ² μΆ©λ λ°©μ§)
|
| 38 |
for col in df.select_dtypes(include=['object']).columns:
|
| 39 |
df[col] = df[col].astype(str).str.replace(r'[\r\n\t]+', ' ', regex=True).str.replace('"', '\\"').str.replace("'", "\\'")
|
| 40 |
|
|
|
|
| 59 |
db_base64 = base64.b64encode(db_json_str.encode('utf-8')).decode('utf-8')
|
| 60 |
|
| 61 |
# ==========================================
|
| 62 |
+
# 3. ν΅ν© νλ‘ νΈμλ (μ€κ΄νΈ μμ μ΄μ€μΌμ΄ν μλ£)
|
| 63 |
# ==========================================
|
| 64 |
html_code = f"""
|
| 65 |
<!DOCTYPE html>
|
|
|
|
| 176 |
window.onload = function() {{
|
| 177 |
const keys = Object.keys(PLACE_DB).sort();
|
| 178 |
const select = document.getElementById('country-select');
|
| 179 |
+
keys.forEach(function(k) {{
|
| 180 |
+
const opt = document.createElement('option');
|
| 181 |
+
opt.value = k; opt.text = k; select.appendChild(opt);
|
| 182 |
+
}});
|
| 183 |
if(keys.length > 0) selectCountry(keys[0]);
|
| 184 |
+
navigator.geolocation.getCurrentPosition(function(p) {{
|
| 185 |
+
userLat = p.coords.latitude; userLon = p.coords.longitude;
|
| 186 |
+
}}, function(e) {{ console.log("GPS Denied"); }});
|
| 187 |
}};
|
| 188 |
|
| 189 |
function selectCountry(c) {{ selectedCountry = c; renderList(); }}
|
| 190 |
function setLanguage(l, el) {{
|
| 191 |
currentLang = l;
|
| 192 |
+
document.querySelectorAll('.lang-btn').forEach(function(b) {{ b.classList.remove('active'); }});
|
| 193 |
el.classList.add('active');
|
| 194 |
renderList();
|
| 195 |
}}
|
|
|
|
| 208 |
list.innerHTML = "";
|
| 209 |
const countryPlaces = PLACE_DB[selectedCountry] || [];
|
| 210 |
|
| 211 |
+
countryPlaces.forEach(function(p) {{
|
| 212 |
const card = document.createElement('div');
|
| 213 |
card.className = 'place-card';
|
| 214 |
const descKey = 'desc_' + currentLang.toLowerCase();
|
| 215 |
let desc = p[descKey] || p.desc_en || p.desc_ko || "Exploring the taste of heritage...";
|
| 216 |
const mapUrl = getMapUrl(p, selectedCountry);
|
| 217 |
|
| 218 |
+
card.innerHTML = "<div class='pc-name'>" + p.name + "</div>" +
|
| 219 |
+
"<div class='pc-desc'>" + desc + "</div>" +
|
| 220 |
+
"<div style='margin-top:15px; display:flex; gap:10px;'>" +
|
| 221 |
+
"<button class='btn-action' style='background:var(--gold); color:black;' onclick=\\"openReview('" + p.name.replace(/'/g, "\\\\'") + "', '" + selectedCountry.replace(/'/g, "\\\\'") + "')\\">AI REVIEW</button>" +
|
| 222 |
+
"<button class='btn-action' style='background:#333; color:white; border:1px solid #555;' onclick=\\"window.open('" + mapUrl + "', '_blank')\\">MAP</button>" +
|
| 223 |
+
"</div>";
|
| 224 |
list.appendChild(card);
|
| 225 |
}});
|
| 226 |
}}
|
|
|
|
| 244 |
const match = res.match(/\\[(.*?)\\]/);
|
| 245 |
if(match) {{
|
| 246 |
const pName = match[1];
|
| 247 |
+
botMsgDiv.innerHTML += "<br><button class='reco-btn' onclick=\\"searchAndOpenMap('" + pName.replace(/'/g, "\\\\'") + "')\\">π View on Map</button>";
|
| 248 |
}}
|
| 249 |
chatHistory.push({{ role: "assistant", content: res }});
|
| 250 |
}}
|
|
|
|
| 257 |
}}
|
| 258 |
|
| 259 |
async function openReview(name, country) {{
|
| 260 |
+
const p = PLACE_DB[country].find(function(x) {{ return x.name === name; }}); if(!p) return;
|
| 261 |
document.getElementById('repo-title').innerText = name;
|
| 262 |
document.getElementById('detail-page').style.transform = 'translateX(0%)';
|
| 263 |
document.getElementById('loader').style.display = 'flex';
|
|
|
|
| 265 |
miniMap = L.map('mini-map');
|
| 266 |
L.tileLayer('https://{{s}}.tile.openstreetmap.org/{{z}}/{{x}}/{{y}}.png').addTo(miniMap);
|
| 267 |
}}
|
| 268 |
+
setTimeout(function() {{ miniMap.invalidateSize(); miniMap.setView([p.lat, p.lon], 15); L.marker([p.lat, p.lon]).addTo(miniMap); }}, 400);
|
| 269 |
const prompt = [
|
| 270 |
{{ role: "system", content: "Act as Emily. Write review in " + TRANSLATIONS[currentLang].ai + ". Sections: Vibe, Must Try, Secret Tip." }},
|
| 271 |
{{ role: "user", content: "Write review for " + name + " in " + country }}
|
|
|
|
| 295 |
if(!worldMap) {{
|
| 296 |
worldMap = L.map('full-map').setView([20, 0], 2);
|
| 297 |
L.tileLayer('https://{{s}}.tile.openstreetmap.org/{{z}}/{{x}}/{{y}}.png').addTo(worldMap);
|
| 298 |
+
Object.keys(PLACE_DB).forEach(function(c) {{
|
| 299 |
+
PLACE_DB[c].forEach(function(p) {{
|
| 300 |
+
if(p.lat && p.lon) {{
|
| 301 |
+
L.marker([p.lat, p.lon]).addTo(worldMap).bindPopup("<b>" + p.name + "</b><br><button onclick=\\"openReview('" + p.name.replace(/'/g, "\\\\'") + "', '" + c.replace(/'/g, "\\\\'") + "')\\" style='background:var(--gold); border:none; padding:5px 10px; border-radius:5px; font-weight:bold; cursor:pointer; margin-top:8px;'>AI REVIEW</button>");
|
| 302 |
+
}}
|
| 303 |
+
}});
|
| 304 |
+
}});
|
| 305 |
}}
|
| 306 |
+
setTimeout(function() {{ worldMap.invalidateSize(); }}, 400);
|
| 307 |
}}
|
| 308 |
|
| 309 |
function flyToUser() {{
|
| 310 |
if(userLat && userLon) {{
|
| 311 |
worldMap.flyTo([userLat, userLon], 15);
|
| 312 |
L.marker([userLat, userLon]).addTo(worldMap).bindPopup("π My Spot").openPopup();
|
| 313 |
+
}} else {{ alert("GPS off, honey!"); }}
|
| 314 |
}}
|
| 315 |
|
| 316 |
function searchAndOpenMap(pName) {{
|
| 317 |
let found = null; let fCountry = "";
|
| 318 |
+
Object.keys(PLACE_DB).some(function(c) {{
|
| 319 |
+
found = PLACE_DB[c].find(function(p) {{ return p.name.includes(pName); }});
|
| 320 |
if(found) {{ fCountry = c; return true; }}
|
| 321 |
}});
|
| 322 |
+
if(found) {{ window.open(getMapUrl(found, fCountry), '_blank'); }}
|
| 323 |
+
else {{ window.open("https://www.google.com/maps/search/" + encodeURIComponent(pName), '_blank'); }}
|
| 324 |
}}
|
| 325 |
|
| 326 |
function closeMap() {{ document.getElementById('world-map-page').style.transform = 'translateY(100%)'; }}
|