openfree's picture
Update app.py
3a6600d verified
raw
history blame
4.88 kB
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()