Spaces:
Running
Running
import os, re, time, json, datetime, requests, gradio as gr | |
# โโโโโโโโโโโโโโโโโโโโโ 1. ๊ธฐ๋ณธ ์ค์ โโโโโโโโโโโโโโโโโโโโโ | |
BEST_FILE, PER_PAGE = "best_games.json", 9 | |
# โโโโโโโโโโโโโโโโโโโโโ 2. BEST ๋ฐ์ดํฐ โโโโโโโโโโโโโโโโโโโโ | |
def _init_best(): | |
if not os.path.exists(BEST_FILE): | |
json.dump([], open(BEST_FILE, "w")) | |
def _load_best(): | |
try: | |
data = json.load(open(BEST_FILE)) | |
for it in data: | |
if "ts" not in it: | |
it["ts"] = int(it.get("timestamp", time.time())) | |
return data | |
except Exception as e: | |
print(f"BEST ๋ฐ์ดํฐ ๋ก๋ ์ค๋ฅ: {e}") | |
return [] | |
def _save_best(data): | |
try: | |
json.dump(data, open(BEST_FILE, "w")) | |
return True | |
except Exception as e: | |
print(f"BEST ๋ฐ์ดํฐ ์ ์ฅ ์ค๋ฅ: {e}") | |
return False | |
# โโโโโโโโโโโโโโโโโโโโโ 3. URL ์ถ๊ฐ ๊ธฐ๋ฅ โโโโโโโโโโโโโโโโโโโโโ | |
def add_url_to_best(title, url): | |
"""์ฌ์ฉ์๊ฐ ์ ๊ณตํ URL์ BEST ๋ชฉ๋ก์ ์ถ๊ฐํฉ๋๋ค.""" | |
try: | |
# ํ์ฌ BEST ๋ฐ์ดํฐ ๋ก๋ | |
data = _load_best() | |
# URL์ด ์ด๋ฏธ ์กด์ฌํ๋์ง ํ์ธ | |
for item in data: | |
if item.get("url") == url: | |
print(f"URL์ด ์ด๋ฏธ ์กด์ฌํฉ๋๋ค: {url}") | |
return False | |
# ์ ํญ๋ชฉ ์ถ๊ฐ | |
new_item = { | |
"title": title, | |
"url": url, | |
"ts": int(time.time()), | |
"projectId": "", # ์ฌ์ฉ์๊ฐ ์ง์ ์ถ๊ฐํ๋ฏ๋ก projectId ์์ | |
"deploymentId": "" # ์ฌ์ฉ์๊ฐ ์ง์ ์ถ๊ฐํ๋ฏ๋ก deploymentId ์์ | |
} | |
data.append(new_item) | |
# ์๊ฐ์์ผ๋ก ์ ๋ ฌ | |
data = sorted(data, key=lambda x: x["ts"], reverse=True) | |
# ์ ์ฅ | |
if _save_best(data): | |
print(f"URL์ด ์ฑ๊ณต์ ์ผ๋ก ์ถ๊ฐ๋์์ต๋๋ค: {url}") | |
return True | |
return False | |
except Exception as e: | |
print(f"URL ์ถ๊ฐ ์ค๋ฅ: {str(e)}") | |
return False | |
# โโโโโโโโโโโโโโโโโโโโโ 4. ํ์ด์ง๋ค์ด์ โโโโโโโโโโโโโโโโโโโ | |
def page(lst, pg): | |
s = (pg-1) * PER_PAGE | |
e = s + PER_PAGE | |
total = (len(lst) + PER_PAGE - 1) // PER_PAGE | |
return lst[s:e], total | |
# โโโโโโโโโโโโโโโโโโโโโ 5. URL ์ฒ๋ฆฌ ํจ์ โโโโโโโโโโโโโโโโโโโโโ | |
def process_url_for_iframe(url): | |
"""URL์ iframe์ ํ์ํ๊ธฐ ์ ํฉํ ํํ๋ก ๋ณํํฉ๋๋ค.""" | |
# ํ๊น ํ์ด์ค URL ํจํด ๊ฐ์ง | |
is_huggingface = False | |
embed_urls = [] | |
# 1. huggingface.co/spaces ํจํด ์ฒ๋ฆฌ | |
if "huggingface.co/spaces" in url: | |
is_huggingface = True | |
# ๊ธฐ๋ณธ URL ์ ๊ทํ | |
base_url = url.rstrip("/") | |
try: | |
# /spaces/ ์ดํ์ ๊ฒฝ๋ก ์ถ์ถ | |
if "/spaces/" in base_url: | |
path = base_url.split("/spaces/")[1] | |
parts = path.split("/") | |
owner = parts[0] | |
# name ๋ถ๋ถ ์ถ์ถ | |
if len(parts) > 1: | |
name = parts[1] | |
# ํน์ ๋ฌธ์ ๋ณํ | |
clean_name = name.replace('.', '-').replace('_', '-').lower() | |
clean_owner = owner.lower() | |
# ์ฌ๋ฌ ํฌ๋งท์ URL์ ์๋ํ๊ธฐ ์ํด ๋ชฉ๋ก์ ์ถ๊ฐ | |
embed_urls.append(f"https://huggingface.co/spaces/{owner}/{name}/embed") # ๊ณต์ embed URL | |
embed_urls.append(f"https://{clean_owner}-{clean_name}.hf.space") # ์ง์ ๋๋ฉ์ธ ์ ๊ทผ | |
else: | |
# owner๋ง ์๋ ๊ฒฝ์ฐ ๊ณต์ URL ์ฌ์ฉ | |
embed_urls.append(f"https://huggingface.co/spaces/{owner}/embed") | |
except Exception as e: | |
print(f"ํ๊น ํ์ด์ค URL ์ฒ๋ฆฌ ์ค ์ค๋ฅ: {e}") | |
# ๊ธฐ๋ณธ embed URL ์๋ | |
if not base_url.endswith("/embed"): | |
embed_urls.append(f"{base_url}/embed") | |
else: | |
embed_urls.append(base_url) | |
# 2. .hf.space ๋๋ฉ์ธ ์ฒ๋ฆฌ | |
elif ".hf.space" in url: | |
is_huggingface = True | |
embed_urls.append(url) # ํ์ฌ URL ๊ทธ๋๋ก ์๋ | |
# 3. ์ผ๋ฐ URL์ ๊ทธ๋๋ก ๋ฐํ | |
else: | |
return url, is_huggingface, [] | |
# ์ต์ข URL๊ณผ ํจ๊ป ์๋ํ ๋์ฒด URL ๋ชฉ๋ก ๋ฐํ | |
primary_url = embed_urls[0] if embed_urls else url | |
return primary_url, is_huggingface, embed_urls[1:] if len(embed_urls) > 1 else [] | |
# โโโโโโโโโโโโโโโโโโโโโ 6. HTML ๊ทธ๋ฆฌ๋ โโโโโโโโโโโโโโโโโโโ | |
def html(cards, pg, total): | |
if not cards: | |
return "<div style='text-align:center;padding:70px;color:#555;'>ํ์ํ ๋ฐฐํฌ๊ฐ ์์ต๋๋ค.</div>" | |
css = r""" | |
<style> | |
body { | |
margin: 0; | |
padding: 0; | |
font-family: Poppins, sans-serif; | |
background: #f0f0f0; | |
overflow-x: hidden; | |
overflow-y: auto; | |
} | |
.container { | |
position: relative; | |
width: 100%; | |
height: auto; | |
box-sizing: border-box; | |
padding: 10px; | |
padding-bottom: 70px; /* Space for buttons */ | |
overflow-y: auto; | |
} | |
.grid { | |
display: grid; | |
grid-template-columns: repeat(3, 1fr); | |
gap: 12px; | |
width: 100%; | |
margin-bottom: 60px; /* Space for buttons */ | |
} | |
.card { | |
background: #fff; | |
border-radius: 10px; | |
overflow: hidden; | |
box-shadow: 0 4px 8px rgba(0,0,0,0.1); | |
display: flex; | |
flex-direction: column; | |
/* Each card is 50% taller */ | |
height: 300px; /* Base height - will be 50% larger */ | |
} | |
.hdr { | |
padding: 8px 12px; | |
background: rgba(255,255,255,.95); | |
border-bottom: 1px solid #eee; | |
flex-shrink: 0; | |
z-index: 10; | |
} | |
.ttl { | |
margin: 0; | |
font-size: 0.95rem; | |
font-weight: 600; | |
color: #333; | |
white-space: nowrap; | |
overflow: hidden; | |
text-overflow: ellipsis; | |
} | |
.date { | |
margin-top: 2px; | |
font-size: 0.75rem; | |
color: #777; | |
} | |
.frame { | |
flex: 1; | |
position: relative; | |
overflow: hidden; | |
} | |
.frame iframe { | |
position: absolute; | |
top: 0; | |
left: 0; | |
width: 142.857%; | |
height: 142.857%; | |
transform: scale(0.7); | |
transform-origin: top left; | |
border: 0; | |
frameborder: 0; | |
} | |
/* ํ๊น ํ์ด์ค iframe ํน๋ณ ์คํ์ผ */ | |
.frame.huggingface iframe { | |
width: 100% !important; | |
height: 100% !important; | |
transform: none !important; | |
border: none !important; | |
} | |
.foot { | |
padding: 6px 12px; | |
background: rgba(255,255,255,.95); | |
text-align: right; | |
flex-shrink: 0; | |
border-top: 1px solid #f0f0f0; | |
z-index: 10; | |
} | |
.link { | |
font-size: 0.8rem; | |
font-weight: 600; | |
color: #4a6dd8; | |
text-decoration: none; | |
} | |
/* ๋ฒํผ ์ปจํ ์ด๋ */ | |
.button-area { | |
position: fixed; | |
bottom: 0; | |
left: 0; | |
right: 0; | |
height: 60px; | |
display: flex; | |
justify-content: center; | |
align-items: center; | |
background: #f0f0f0; | |
box-shadow: 0 -2px 10px rgba(0,0,0,0.05); | |
z-index: 100; | |
} | |
/* ํ์ด์ง ์ ๋ณด */ | |
.page-info { | |
position: fixed; | |
bottom: 5px; | |
left: 0; | |
right: 0; | |
text-align: center; | |
font-size: 0.8rem; | |
color: #777; | |
z-index: 101; | |
} | |
/* ๋ฏธ๋์ด ์ฟผ๋ฆฌ */ | |
@media (min-width: 1200px) { | |
.card { | |
height: 450px; /* 50% taller than 300px */ | |
} | |
} | |
@media (min-width: 768px) and (max-width: 1199px) { | |
.card { | |
height: 400px; /* Adjusted for medium screens */ | |
} | |
} | |
@media (max-width: 767px) { | |
.grid { | |
grid-template-columns: 1fr; | |
} | |
.card { | |
height: 350px; /* Adjusted for small screens */ | |
} | |
} | |
/* ์ค๋ฅ ๋ฉ์์ง ๊ด๋ จ CSS */ | |
.error-message { | |
position: absolute; | |
top: 0; | |
left: 0; | |
width: 100%; | |
height: 100%; | |
display: flex; | |
flex-direction: column; | |
justify-content: center; | |
align-items: center; | |
background-color: rgba(255, 255, 255, 0.9); | |
z-index: 20; | |
padding: 20px; | |
text-align: center; | |
} | |
.error-icon { | |
font-size: 48px; | |
margin-bottom: 15px; | |
} | |
.error-text { | |
font-weight: bold; | |
margin-bottom: 15px; | |
} | |
.error-link { | |
display: inline-block; | |
padding: 8px 16px; | |
background-color: #f0f0f7; | |
border-radius: 4px; | |
color: #4a6dd8; | |
font-weight: bold; | |
text-decoration: none; | |
transition: background-color 0.2s; | |
} | |
.error-link:hover { | |
background-color: #e0e0f0; | |
} | |
</style>""" | |
js = """ | |
<script> | |
// ํ๊น ํ์ด์ค iframe ๋ก๋ฉ ์ค๋ฅ ์ฒ๋ฆฌ | |
function handleIframeError(iframeId, alternateUrls, originalUrl) { | |
const iframe = document.getElementById(iframeId); | |
if (!iframe) return; | |
// iframe ๋ก๋ ์คํจ ์ฒ๋ฆฌ | |
iframe.onerror = function() { | |
tryNextUrl(iframeId, alternateUrls, originalUrl); | |
}; | |
// onload ์ด๋ฒคํธ์์ ๋ก๋ ์คํจ ํ์ธ | |
iframe.onload = function() { | |
try { | |
// iframe ๋ด์ฉ์ ์ ๊ทผ ์๋ (cross-origin ์ ํ์ผ๋ก ์คํจํ ์ ์์) | |
const iframeContent = iframe.contentWindow.document; | |
console.log("iframe loaded successfully: " + iframeId); | |
} catch (e) { | |
// cross-origin ์ค๋ฅ๋ ๋ฌด์ (์ ์์ ์ธ ์ํฉ์ผ ์ ์์) | |
console.log("Cross-origin iframe loaded (expected): " + iframeId); | |
} | |
// iframe์ด ์ ๋๋ก ํ์๋๋์ง 10์ด ํ ํ์ธ | |
setTimeout(function() { | |
if (iframe.offsetWidth === 0 || iframe.offsetHeight === 0) { | |
console.log("iframe not visible, trying alternate URL: " + iframeId); | |
tryNextUrl(iframeId, alternateUrls, originalUrl); | |
} else { | |
console.log("iframe appears to be visible: " + iframeId); | |
} | |
}, 5000); | |
}; | |
} | |
// ๋์ฒด URL ์๋ | |
function tryNextUrl(iframeId, alternateUrls, originalUrl) { | |
const iframe = document.getElementById(iframeId); | |
if (!iframe) return; | |
// ๋์ฒด URL์ด ์์ผ๋ฉด ์๋ | |
if (alternateUrls && alternateUrls.length > 0) { | |
const nextUrl = alternateUrls.shift(); | |
console.log("Trying alternate URL: " + nextUrl); | |
iframe.src = nextUrl; | |
// ๋์ฒด URL๋ก ๋ค์ ์ค๋ฅ ์ฒ๋ฆฌ ์ค์ | |
handleIframeError(iframeId, alternateUrls, originalUrl); | |
} else { | |
// ๋ชจ๋ URL ์๋ ์คํจ ์ ์ค๋ฅ ๋ฉ์์ง ํ์ | |
console.log("All URLs failed, showing error for: " + iframeId); | |
const container = iframe.parentNode; | |
// ์ค๋ฅ ๋ฉ์์ง ํ์ | |
const errorDiv = document.createElement('div'); | |
errorDiv.className = 'error-message'; | |
errorDiv.innerHTML = ` | |
<div class="error-icon">โ ๏ธ</div> | |
<p class="error-text">์ฝํ ์ธ ๋ฅผ ๋ก๋ํ ์ ์์ต๋๋ค</p> | |
<a href="${originalUrl}" target="_blank" class="error-link"> | |
์๋ณธ ํ์ด์ง์์ ์ด๊ธฐ | |
</a> | |
`; | |
// iframe ์จ๊ธฐ๊ณ ์ค๋ฅ ๋ฉ์์ง ํ์ | |
iframe.style.display = 'none'; | |
container.appendChild(errorDiv); | |
} | |
} | |
// ํ์ด์ง ๋ก๋ ์ ๋ชจ๋ ํ๊น ํ์ด์ค iframe ์ฒ๋ฆฌ | |
window.addEventListener('load', function() { | |
const iframes = document.querySelectorAll('.huggingface iframe'); | |
iframes.forEach(function(iframe) { | |
const id = iframe.id; | |
const alternateUrlsStr = iframe.getAttribute('data-alternate-urls'); | |
const alternateUrls = alternateUrlsStr ? alternateUrlsStr.split(',').filter(url => url) : []; | |
const originalUrl = iframe.getAttribute('data-original-url') || iframe.src; | |
if (id && alternateUrls.length > 0) { | |
handleIframeError(id, alternateUrls, originalUrl); | |
} | |
}); | |
}); | |
</script> | |
""" | |
h = css + js + """ | |
<div class="container"> | |
<div class="grid"> | |
""" | |
for idx, c in enumerate(cards): | |
date = datetime.datetime.fromtimestamp(int(c["ts"])).strftime("%Y-%m-%d") | |
# URL ์ฒ๋ฆฌ: ํ๊น ํ์ด์ค URL์ธ ๊ฒฝ์ฐ ํน๋ณ ์ฒ๋ฆฌ | |
url = c['url'] | |
iframe_url, is_huggingface, alt_urls = process_url_for_iframe(url) | |
# ํ๊น ํ์ด์ค URL์ ํน๋ณ ํด๋์ค ์ถ๊ฐ | |
frame_class = "frame huggingface" if is_huggingface else "frame" | |
# ๊ณ ์ ID ์์ฑ | |
iframe_id = f"iframe-{idx}-{hash(url) % 10000}" | |
# ๋์ฒด URL์ ๋ฐ์ดํฐ ์์ฑ์ผ๋ก ์ถ๊ฐ | |
alternate_urls_attr = "" | |
if alt_urls: | |
alternate_urls_attr = f'data-alternate-urls="{",".join(alt_urls)}"' | |
h += f""" | |
<div class="card"> | |
<div class="hdr"><p class="ttl">{c['title']}</p><p class="date">{date}</p></div> | |
<div class="{frame_class}"> | |
<iframe | |
id="{iframe_id}" | |
src="{iframe_url}" | |
loading="lazy" | |
frameborder="0" | |
width="100%" | |
height="100%" | |
allowfullscreen="true" | |
allow="accelerometer; camera; encrypted-media; gyroscope; geolocation;" | |
sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads" | |
data-original-url="{url}" | |
{alternate_urls_attr}> | |
</iframe> | |
</div> | |
<div class="foot"><a class="link" href="{url}" target="_blank">์๋ณธโ</a></div> | |
</div>""" | |
h += """ | |
</div> | |
</div> | |
""" | |
# ํ์ด์ง ์ ๋ณด | |
h += f'<div class="page-info">Page {pg} / {total}</div>' | |
return h | |
# โโโโโโโโโโโโโโโโโโโโโ 7. Gradio Blocks UI โโโโโโโโโโโโโโโโโโโโโ | |
# โโโโโโโโโโโโโโโโโโโโโ 7. Gradio Blocks UI โโโโโโโโโโโโโโโโโโโโโ | |
def build(): | |
_init_best() | |
# (1) โโ ํค๋ HTML (๋ณ๊ฒฝ ์์) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
header_html_snippet = """ | |
<style> | |
.app-header{ text-align:center; margin-bottom:24px; } | |
.badge-row{ | |
display:inline-flex; | |
gap:8px; | |
margin:8px 0; | |
} | |
</style> | |
<div class="app-header"> | |
<h1>๐ฎ Vibe Game Gallery</h1> | |
<div class="badge-row"> | |
<a href="https://huggingface.co/spaces/openfree/Vibe-Game" target="_blank"> | |
<img src="https://img.shields.io/static/v1?label=huggingface&message=Vibe%20Game%20Craft&color=%23800080&labelColor=%23ffa500&logo=huggingface&logoColor=%23ffff00&style=for-the-badge" alt="HF-Vibe"> | |
</a> | |
<a href="https://huggingface.co/spaces/openfree/Game-Gallery" target="_blank"> | |
<img src="https://img.shields.io/static/v1?label=huggingface&message=Game%20Gallery&color=%23800080&labelColor=%23ffa500&logo=huggingface&logoColor=%23ffff00&style=for-the-badge" alt="HF-Gallery"> | |
</a> | |
<a href="https://discord.gg/openfreeai" target="_blank"> | |
<img src="https://img.shields.io/static/v1?label=Discord&message=Openfree%20AI&color=%230000ff&labelColor=%23800080&logo=discord&logoColor=white&style=for-the-badge" alt="Discord"> | |
</a> | |
</div> | |
<p>ํ๋กฌํํธ ์ ๋ ฅ๋ง์ผ๋ก ์ต์ LLM๋ค๊ณผ Agent๊ฐ ํ์ ํ์ฌ ์น ๊ธฐ๋ฐ ๊ฒ์์ ์์ฑํ๊ณ ๋ฐฐํฌํฉ๋๋ค.</p> | |
</div> | |
""" | |
# (2) โโ ์ ์ญ CSS (ํค๋ sticky + ์คํฌ๋กค ์์ญ ์ฌ์กฐ์ ) โโโโโโโโโ | |
css_global = """ | |
footer{display:none !important;} | |
/* ์๋จ ํค๋๋ฅผ ํญ์ ๋ณด์ด๋๋ก ๊ณ ์ */ | |
.app-header{ | |
position:sticky; | |
top:0; | |
background:#fff; | |
z-index:1100; | |
padding:16px 0 8px; | |
border-bottom:1px solid #eee; | |
} | |
/* ํ๋จ ๊ณ ์ ๋ฒํผ ๋ฐ */ | |
.button-row{ | |
position:fixed !important; | |
bottom:0 !important; | |
left:0 !important; | |
right:0 !important; | |
height:60px !important; | |
background:#f0f0f0 !important; | |
padding:10px !important; | |
text-align:center !important; | |
box-shadow:0 -2px 10px rgba(0,0,0,0.05) !important; | |
margin:0 !important; | |
z-index:1000 !important; | |
} | |
.button-row button{ | |
margin:0 10px !important; | |
padding:10px 20px !important; | |
font-size:16px !important; | |
font-weight:bold !important; | |
border-radius:50px !important; | |
} | |
/* ์นด๋ ๊ทธ๋ฆฌ๋ ์คํฌ๋กค ์์ญ */ | |
#content-area{ | |
overflow-y:auto !important; | |
height:calc(100vh - 60px - 160px) !important; /* ์ ์ฒด-๋์ด - ํ๋จ๋ฐ - ํค๋ */ | |
box-sizing:border-box; | |
padding-top:10px; | |
} | |
""" | |
# (3) โโ Gradio Blocks โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
with gr.Blocks(title="Vibe Game Craft", css=css_global) as demo: | |
# โ ๊ณ ์ ํค๋ | |
gr.HTML(header_html_snippet) | |
# โก ๋ณธ๋ฌธ(์นด๋ ๊ทธ๋ฆฌ๋) โ ๊ณ ์ ID ๋ถ์ฌ | |
out = gr.HTML(elem_id="content-area") | |
# โข ํ๋จ ํ์ด์ง ๋ค๋น๊ฒ์ด์ ๋ฐ | |
with gr.Row(elem_classes="button-row"): | |
b_prev = gr.Button("โ ์ด์ ", size="lg") | |
b_next = gr.Button("๋ค์ โถ", size="lg") | |
# โโ ์ํ ๋ฐ ํฌํผ โโโโโโโโโโโโโโโโโโโโโโโโโโโ | |
bp = gr.State(1) | |
def show_best(p=1): | |
d, t = page(_load_best(), p) | |
return html(d, p, t), p | |
def prev(b): | |
b = max(1, b-1) | |
h, _ = show_best(b) | |
return h, b | |
def nxt(b): | |
maxp = (len(_load_best()) + PER_PAGE - 1) // PER_PAGE | |
b = min(maxp, b+1) | |
h, _ = show_best(b) | |
return h, b | |
# โโ ์ด๋ฒคํธ ๋ฐ์ธ๋ฉ โโโโโโโโโโโโโโโโโโโโโโโโโโ | |
b_prev.click(prev, inputs=[bp], outputs=[out, bp]) | |
b_next.click(nxt, inputs=[bp], outputs=[out, bp]) | |
# ์ต์ด ๋ก๋ | |
demo.load(show_best, outputs=[out, bp]) | |
return demo | |
# โโโโโโโโโโโโโโโโโโโโโ 8. ์ฑ ์์ฑ & ์คํ โโโโโโโโโโโโโโโโโโโโโ | |
app = build() # โ Blocks ์ธ์คํด์ค ์์ฑ | |
if __name__ == "__main__": | |
# Spaces๋ ๋ก์ปฌ์์ ์คํ๋ ๋ ์ง์ ์ | |
app.launch() # share=True ๋ฑ ์ต์ ์ด ํ์ํ๋ฉด ์ฌ๊ธฐ์ ์ง์ |