Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -75,33 +75,56 @@ def process_url_for_iframe(url):
|
|
75 |
"""URL์ iframe์ ํ์ํ๊ธฐ ์ ํฉํ ํํ๋ก ๋ณํํฉ๋๋ค."""
|
76 |
# ํ๊น
ํ์ด์ค URL ํจํด ๊ฐ์ง
|
77 |
is_huggingface = False
|
|
|
78 |
|
79 |
-
# huggingface.co/spaces ํจํด ์ฒ๋ฆฌ
|
80 |
if "huggingface.co/spaces" in url:
|
81 |
is_huggingface = True
|
|
|
|
|
|
|
|
|
82 |
try:
|
83 |
-
#
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
-
#
|
99 |
elif ".hf.space" in url:
|
100 |
is_huggingface = True
|
101 |
-
|
|
|
|
|
|
|
|
|
102 |
|
103 |
-
#
|
104 |
-
|
|
|
105 |
|
106 |
# โโโโโโโโโโโโโโโโโโโโโ 6. HTML ๊ทธ๋ฆฌ๋ โโโโโโโโโโโโโโโโโโโ
|
107 |
def html(cards, pg, total):
|
@@ -248,28 +271,163 @@ def html(cards, pg, total):
|
|
248 |
height: 350px; /* Adjusted for small screens */
|
249 |
}
|
250 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
251 |
</style>"""
|
252 |
|
253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
<div class="container">
|
255 |
<div class="grid">
|
256 |
"""
|
257 |
|
258 |
-
for c in cards:
|
259 |
date = datetime.datetime.fromtimestamp(int(c["ts"])).strftime("%Y-%m-%d")
|
260 |
|
261 |
# URL ์ฒ๋ฆฌ: ํ๊น
ํ์ด์ค URL์ธ ๊ฒฝ์ฐ ํน๋ณ ์ฒ๋ฆฌ
|
262 |
url = c['url']
|
263 |
-
iframe_url, is_huggingface = process_url_for_iframe(url)
|
264 |
|
265 |
# ํ๊น
ํ์ด์ค URL์ ํน๋ณ ํด๋์ค ์ถ๊ฐ
|
266 |
frame_class = "frame huggingface" if is_huggingface else "frame"
|
267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
h += f"""
|
269 |
<div class="card">
|
270 |
<div class="hdr"><p class="ttl">{c['title']}</p><p class="date">{date}</p></div>
|
271 |
<div class="{frame_class}">
|
272 |
<iframe
|
|
|
273 |
src="{iframe_url}"
|
274 |
loading="lazy"
|
275 |
frameborder="0"
|
@@ -277,7 +435,9 @@ def html(cards, pg, total):
|
|
277 |
height="100%"
|
278 |
allowfullscreen="true"
|
279 |
allow="accelerometer; camera; encrypted-media; gyroscope; geolocation;"
|
280 |
-
sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"
|
|
|
|
|
281 |
</iframe>
|
282 |
</div>
|
283 |
<div class="foot"><a class="link" href="{url}" target="_blank">์๋ณธโ</a></div>
|
|
|
75 |
"""URL์ iframe์ ํ์ํ๊ธฐ ์ ํฉํ ํํ๋ก ๋ณํํฉ๋๋ค."""
|
76 |
# ํ๊น
ํ์ด์ค URL ํจํด ๊ฐ์ง
|
77 |
is_huggingface = False
|
78 |
+
embed_urls = []
|
79 |
|
80 |
+
# 1. huggingface.co/spaces ํจํด ์ฒ๋ฆฌ
|
81 |
if "huggingface.co/spaces" in url:
|
82 |
is_huggingface = True
|
83 |
+
|
84 |
+
# ๊ธฐ๋ณธ URL ์ ๊ทํ
|
85 |
+
base_url = url.rstrip("/")
|
86 |
+
|
87 |
try:
|
88 |
+
# /spaces/ ์ดํ์ ๊ฒฝ๋ก ์ถ์ถ
|
89 |
+
if "/spaces/" in base_url:
|
90 |
+
path = base_url.split("/spaces/")[1]
|
91 |
+
parts = path.split("/")
|
92 |
+
owner = parts[0]
|
93 |
+
|
94 |
+
# name ๋ถ๋ถ ์ถ์ถ
|
95 |
+
if len(parts) > 1:
|
96 |
+
name = parts[1]
|
97 |
+
|
98 |
+
# ํน์ ๋ฌธ์ ๋ณํ
|
99 |
+
clean_name = name.replace('.', '-').replace('_', '-').lower()
|
100 |
+
clean_owner = owner.lower()
|
101 |
+
|
102 |
+
# ์ฌ๋ฌ ํฌ๋งท์ URL์ ์๋ํ๊ธฐ ์ํด ๋ชฉ๋ก์ ์ถ๊ฐ
|
103 |
+
embed_urls.append(f"https://huggingface.co/spaces/{owner}/{name}/embed") # ๊ณต์ embed URL
|
104 |
+
embed_urls.append(f"https://{clean_owner}-{clean_name}.hf.space") # ์ง์ ๋๋ฉ์ธ ์ ๊ทผ
|
105 |
+
else:
|
106 |
+
# owner๋ง ์๋ ๊ฒฝ์ฐ ๊ณต์ URL ์ฌ์ฉ
|
107 |
+
embed_urls.append(f"https://huggingface.co/spaces/{owner}/embed")
|
108 |
+
except Exception as e:
|
109 |
+
print(f"ํ๊น
ํ์ด์ค URL ์ฒ๋ฆฌ ์ค ์ค๋ฅ: {e}")
|
110 |
+
# ๊ธฐ๋ณธ embed URL ์๋
|
111 |
+
if not base_url.endswith("/embed"):
|
112 |
+
embed_urls.append(f"{base_url}/embed")
|
113 |
+
else:
|
114 |
+
embed_urls.append(base_url)
|
115 |
|
116 |
+
# 2. .hf.space ๋๋ฉ์ธ ์ฒ๋ฆฌ
|
117 |
elif ".hf.space" in url:
|
118 |
is_huggingface = True
|
119 |
+
embed_urls.append(url) # ํ์ฌ URL ๊ทธ๋๋ก ์๋
|
120 |
+
|
121 |
+
# 3. ์ผ๋ฐ URL์ ๊ทธ๋๋ก ๋ฐํ
|
122 |
+
else:
|
123 |
+
return url, is_huggingface, []
|
124 |
|
125 |
+
# ์ต์ข
URL๊ณผ ํจ๊ป ์๋ํ ๋์ฒด URL ๋ชฉ๋ก ๋ฐํ
|
126 |
+
primary_url = embed_urls[0] if embed_urls else url
|
127 |
+
return primary_url, is_huggingface, embed_urls[1:] if len(embed_urls) > 1 else []
|
128 |
|
129 |
# โโโโโโโโโโโโโโโโโโโโโ 6. HTML ๊ทธ๋ฆฌ๋ โโโโโโโโโโโโโโโโโโโ
|
130 |
def html(cards, pg, total):
|
|
|
271 |
height: 350px; /* Adjusted for small screens */
|
272 |
}
|
273 |
}
|
274 |
+
|
275 |
+
/* ์ค๋ฅ ๋ฉ์์ง ๊ด๋ จ CSS */
|
276 |
+
.error-message {
|
277 |
+
position: absolute;
|
278 |
+
top: 0;
|
279 |
+
left: 0;
|
280 |
+
width: 100%;
|
281 |
+
height: 100%;
|
282 |
+
display: flex;
|
283 |
+
flex-direction: column;
|
284 |
+
justify-content: center;
|
285 |
+
align-items: center;
|
286 |
+
background-color: rgba(255, 255, 255, 0.9);
|
287 |
+
z-index: 20;
|
288 |
+
padding: 20px;
|
289 |
+
text-align: center;
|
290 |
+
}
|
291 |
+
.error-icon {
|
292 |
+
font-size: 48px;
|
293 |
+
margin-bottom: 15px;
|
294 |
+
}
|
295 |
+
.error-text {
|
296 |
+
font-weight: bold;
|
297 |
+
margin-bottom: 15px;
|
298 |
+
}
|
299 |
+
.error-link {
|
300 |
+
display: inline-block;
|
301 |
+
padding: 8px 16px;
|
302 |
+
background-color: #f0f0f7;
|
303 |
+
border-radius: 4px;
|
304 |
+
color: #4a6dd8;
|
305 |
+
font-weight: bold;
|
306 |
+
text-decoration: none;
|
307 |
+
transition: background-color 0.2s;
|
308 |
+
}
|
309 |
+
.error-link:hover {
|
310 |
+
background-color: #e0e0f0;
|
311 |
+
}
|
312 |
</style>"""
|
313 |
|
314 |
+
js = """
|
315 |
+
<script>
|
316 |
+
// ํ๊น
ํ์ด์ค iframe ๋ก๋ฉ ์ค๋ฅ ์ฒ๋ฆฌ
|
317 |
+
function handleIframeError(iframeId, alternateUrls, originalUrl) {
|
318 |
+
const iframe = document.getElementById(iframeId);
|
319 |
+
if (!iframe) return;
|
320 |
+
|
321 |
+
// iframe ๋ก๋ ์คํจ ์ฒ๋ฆฌ
|
322 |
+
iframe.onerror = function() {
|
323 |
+
tryNextUrl(iframeId, alternateUrls, originalUrl);
|
324 |
+
};
|
325 |
+
|
326 |
+
// onload ์ด๋ฒคํธ์์ ๋ก๋ ์คํจ ํ์ธ
|
327 |
+
iframe.onload = function() {
|
328 |
+
try {
|
329 |
+
// iframe ๋ด์ฉ์ ์ ๊ทผ ์๋ (cross-origin ์ ํ์ผ๋ก ์คํจํ ์ ์์)
|
330 |
+
const iframeContent = iframe.contentWindow.document;
|
331 |
+
console.log("iframe loaded successfully: " + iframeId);
|
332 |
+
} catch (e) {
|
333 |
+
// cross-origin ์ค๋ฅ๋ ๋ฌด์ (์ ์์ ์ธ ์ํฉ์ผ ์ ์์)
|
334 |
+
console.log("Cross-origin iframe loaded (expected): " + iframeId);
|
335 |
+
}
|
336 |
+
|
337 |
+
// iframe์ด ์ ๋๋ก ํ์๋๋์ง 10์ด ํ ํ์ธ
|
338 |
+
setTimeout(function() {
|
339 |
+
if (iframe.offsetWidth === 0 || iframe.offsetHeight === 0) {
|
340 |
+
console.log("iframe not visible, trying alternate URL: " + iframeId);
|
341 |
+
tryNextUrl(iframeId, alternateUrls, originalUrl);
|
342 |
+
} else {
|
343 |
+
console.log("iframe appears to be visible: " + iframeId);
|
344 |
+
}
|
345 |
+
}, 5000);
|
346 |
+
};
|
347 |
+
}
|
348 |
+
|
349 |
+
// ๋์ฒด URL ์๋
|
350 |
+
function tryNextUrl(iframeId, alternateUrls, originalUrl) {
|
351 |
+
const iframe = document.getElementById(iframeId);
|
352 |
+
if (!iframe) return;
|
353 |
+
|
354 |
+
// ๋์ฒด URL์ด ์์ผ๋ฉด ์๋
|
355 |
+
if (alternateUrls && alternateUrls.length > 0) {
|
356 |
+
const nextUrl = alternateUrls.shift();
|
357 |
+
console.log("Trying alternate URL: " + nextUrl);
|
358 |
+
iframe.src = nextUrl;
|
359 |
+
|
360 |
+
// ๋์ฒด URL๋ก ๋ค์ ์ค๋ฅ ์ฒ๋ฆฌ ์ค์
|
361 |
+
handleIframeError(iframeId, alternateUrls, originalUrl);
|
362 |
+
} else {
|
363 |
+
// ๋ชจ๋ URL ์๋ ์คํจ ์ ์ค๋ฅ ๋ฉ์์ง ํ์
|
364 |
+
console.log("All URLs failed, showing error for: " + iframeId);
|
365 |
+
const container = iframe.parentNode;
|
366 |
+
|
367 |
+
// ์ค๋ฅ ๋ฉ์์ง ํ์
|
368 |
+
const errorDiv = document.createElement('div');
|
369 |
+
errorDiv.className = 'error-message';
|
370 |
+
|
371 |
+
errorDiv.innerHTML = `
|
372 |
+
<div class="error-icon">โ ๏ธ</div>
|
373 |
+
<p class="error-text">์ฝํ
์ธ ๋ฅผ ๋ก๋ํ ์ ์์ต๋๋ค</p>
|
374 |
+
<a href="${originalUrl}" target="_blank" class="error-link">
|
375 |
+
์๋ณธ ํ์ด์ง์์ ์ด๊ธฐ
|
376 |
+
</a>
|
377 |
+
`;
|
378 |
+
|
379 |
+
// iframe ์จ๊ธฐ๊ณ ์ค๋ฅ ๋ฉ์์ง ํ์
|
380 |
+
iframe.style.display = 'none';
|
381 |
+
container.appendChild(errorDiv);
|
382 |
+
}
|
383 |
+
}
|
384 |
+
|
385 |
+
// ํ์ด์ง ๋ก๋ ์ ๋ชจ๋ ํ๊น
ํ์ด์ค iframe ์ฒ๋ฆฌ
|
386 |
+
window.addEventListener('load', function() {
|
387 |
+
const iframes = document.querySelectorAll('.huggingface iframe');
|
388 |
+
iframes.forEach(function(iframe) {
|
389 |
+
const id = iframe.id;
|
390 |
+
const alternateUrlsStr = iframe.getAttribute('data-alternate-urls');
|
391 |
+
const alternateUrls = alternateUrlsStr ? alternateUrlsStr.split(',').filter(url => url) : [];
|
392 |
+
const originalUrl = iframe.getAttribute('data-original-url') || iframe.src;
|
393 |
+
|
394 |
+
if (id && alternateUrls.length > 0) {
|
395 |
+
handleIframeError(id, alternateUrls, originalUrl);
|
396 |
+
}
|
397 |
+
});
|
398 |
+
});
|
399 |
+
</script>
|
400 |
+
"""
|
401 |
+
|
402 |
+
h = css + js + """
|
403 |
<div class="container">
|
404 |
<div class="grid">
|
405 |
"""
|
406 |
|
407 |
+
for idx, c in enumerate(cards):
|
408 |
date = datetime.datetime.fromtimestamp(int(c["ts"])).strftime("%Y-%m-%d")
|
409 |
|
410 |
# URL ์ฒ๋ฆฌ: ํ๊น
ํ์ด์ค URL์ธ ๊ฒฝ์ฐ ํน๋ณ ์ฒ๋ฆฌ
|
411 |
url = c['url']
|
412 |
+
iframe_url, is_huggingface, alt_urls = process_url_for_iframe(url)
|
413 |
|
414 |
# ํ๊น
ํ์ด์ค URL์ ํน๋ณ ํด๋์ค ์ถ๊ฐ
|
415 |
frame_class = "frame huggingface" if is_huggingface else "frame"
|
416 |
|
417 |
+
# ๊ณ ์ ID ์์ฑ
|
418 |
+
iframe_id = f"iframe-{idx}-{hash(url) % 10000}"
|
419 |
+
|
420 |
+
# ๋์ฒด URL์ ๋ฐ์ดํฐ ์์ฑ์ผ๋ก ์ถ๊ฐ
|
421 |
+
alternate_urls_attr = ""
|
422 |
+
if alt_urls:
|
423 |
+
alternate_urls_attr = f'data-alternate-urls="{",".join(alt_urls)}"'
|
424 |
+
|
425 |
h += f"""
|
426 |
<div class="card">
|
427 |
<div class="hdr"><p class="ttl">{c['title']}</p><p class="date">{date}</p></div>
|
428 |
<div class="{frame_class}">
|
429 |
<iframe
|
430 |
+
id="{iframe_id}"
|
431 |
src="{iframe_url}"
|
432 |
loading="lazy"
|
433 |
frameborder="0"
|
|
|
435 |
height="100%"
|
436 |
allowfullscreen="true"
|
437 |
allow="accelerometer; camera; encrypted-media; gyroscope; geolocation;"
|
438 |
+
sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"
|
439 |
+
data-original-url="{url}"
|
440 |
+
{alternate_urls_attr}>
|
441 |
</iframe>
|
442 |
</div>
|
443 |
<div class="foot"><a class="link" href="{url}" target="_blank">์๋ณธโ</a></div>
|