Spaces:
Running
Running
import os | |
import random | |
import base64 | |
import requests | |
from selenium import webdriver | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.common.by import By | |
from selenium.common.exceptions import WebDriverException, TimeoutException | |
from PIL import Image | |
from io import BytesIO | |
from datetime import datetime | |
import gradio as gr | |
USERNAME = "openfree" | |
# ์คํฌ๋ฆฐ์ท ์บ์ ์ ์ฅ์ | |
SCREENSHOT_CACHE = {} | |
def take_screenshot(url): | |
"""์น์ฌ์ดํธ ์คํฌ๋ฆฐ์ท ์ดฌ์""" | |
if url in SCREENSHOT_CACHE: | |
return SCREENSHOT_CACHE[url] | |
if not url.startswith('http'): | |
url = f"https://{url}" | |
options = webdriver.ChromeOptions() | |
options.add_argument('--headless') | |
options.add_argument('--no-sandbox') | |
options.add_argument('--disable-dev-shm-usage') | |
options.add_argument('--window-size=1080,720') | |
try: | |
driver = webdriver.Chrome(options=options) | |
driver.get(url) | |
WebDriverWait(driver, 10).until( | |
EC.presence_of_element_located((By.TAG_NAME, "body")) | |
) | |
screenshot = driver.get_screenshot_as_png() | |
img = Image.open(BytesIO(screenshot)) | |
buffered = BytesIO() | |
img.save(buffered, format="PNG") | |
base64_image = base64.b64encode(buffered.getvalue()).decode() | |
SCREENSHOT_CACHE[url] = base64_image | |
return base64_image | |
except Exception as e: | |
print(f"Screenshot error for {url}: {str(e)}") | |
return None | |
finally: | |
if 'driver' in locals(): | |
driver.quit() | |
def get_pastel_color(index): | |
"""์ธ๋ฑ์ค ๊ธฐ๋ฐ ํ์คํ ์์ ์์ฑ""" | |
pastel_colors = [ | |
'rgba(255, 230, 230, 0.8)', 'rgba(255, 230, 255, 0.8)', | |
'rgba(230, 230, 255, 0.8)', 'rgba(230, 255, 255, 0.8)', | |
'rgba(230, 255, 230, 0.8)' | |
] | |
return pastel_colors[index % len(pastel_colors)] | |
def get_space_card(space, index): | |
"""์คํ์ด์ค ์นด๋ HTML ์์ฑ""" | |
space_id = space.get('id', '') | |
space_name = space_id.split('/')[-1] | |
likes = space.get('likes', 0) | |
sdk = space.get('sdk', 'N/A') | |
url = f"https://huggingface.co/spaces/{space_id}" | |
# ์คํฌ๋ฆฐ์ท ๊ฐ์ ธ์ค๊ธฐ | |
screenshot = take_screenshot(url) | |
bg_style = f""" | |
background-image: linear-gradient( | |
{get_pastel_color(index)}, | |
{get_pastel_color(index)} | |
), url(data:image/png;base64,{screenshot}); | |
background-size: cover; | |
background-position: center; | |
background-blend-mode: overlay; | |
""" if screenshot else f"background-color: {get_pastel_color(index)};" | |
return f""" | |
<div style=' | |
border: none; | |
padding: 20px; | |
margin: 10px; | |
border-radius: 15px; | |
box-shadow: 0 4px 8px rgba(0,0,0,0.1); | |
{bg_style} | |
transition: transform 0.3s ease; | |
cursor: pointer; | |
position: relative; | |
overflow: hidden; | |
min-height: 200px;' | |
onclick="window.open('{url}', '_blank')" | |
onmouseover="this.style.transform='scale(1.02)'" | |
onmouseout="this.style.transform='scale(1)'"> | |
<div style=' | |
background: rgba(255, 255, 255, 0.9); | |
padding: 15px; | |
border-radius: 10px; | |
backdrop-filter: blur(5px);'> | |
<h3 style='margin: 0; color: #333;'>{space_name}</h3> | |
<p style='margin: 10px 0; color: #666;'>SDK: {sdk}</p> | |
<p style='margin: 10px 0; color: #666;'>โค๏ธ {likes}</p> | |
</div> | |
</div> | |
""" | |
def get_user_spaces(): | |
"""ํ๊น ํ์ด์ค ์คํ์ด์ค ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ""" | |
url = f"https://huggingface.co/api/spaces?author={USERNAME}&limit=500" | |
try: | |
response = requests.get(url) | |
if response.status_code == 200: | |
spaces_data = response.json() | |
html_content = """ | |
<div style=' | |
padding: 20px; | |
background: #f5f5f5;'> | |
<div style=' | |
display: grid; | |
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); | |
gap: 20px;'> | |
""" | |
html_content += "".join(get_space_card(space, idx) for idx, space in enumerate(spaces_data)) | |
html_content += "</div></div>" | |
return html_content | |
return "<p>Failed to fetch spaces</p>" | |
except Exception as e: | |
return f"<p>Error: {str(e)}</p>" | |
# Gradio ์ธํฐํ์ด์ค ์์ฑ | |
demo = gr.Interface( | |
fn=lambda: get_user_spaces(), | |
inputs=None, | |
outputs=gr.HTML(), | |
title="Hugging Face Spaces Gallery", | |
css=""" | |
.container { max-width: 1200px; margin: 0 auto; } | |
.gradio-container { font-family: 'Arial', sans-serif; } | |
""" | |
) | |
if __name__ == "__main__": | |
demo.launch() |