trending-board / app.py
openfree's picture
Update app.py
8849302 verified
raw
history blame
3.27 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"
def take_screenshot(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")
return base64.b64encode(buffered.getvalue()).decode()
except Exception as e:
print(f"Screenshot error: {str(e)}")
return None
finally:
if 'driver' in locals():
driver.quit()
def get_pastel_color(index):
"""์ธ๋ฑ์Šค ๊ธฐ๋ฐ˜ ํŒŒ์Šคํ…” ์ƒ‰์ƒ ์ƒ์„ฑ"""
pastel_colors = [
'#FFE6E6', '#FFE6FF', '#E6E6FF', '#E6FFFF', '#E6FFE6',
'#FFFFE6', '#FFF0E6', '#F0E6FF', '#FFE6F0', '#E6FFF0'
]
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')
bg_color = get_pastel_color(index)
return f"""
<div style='border: none; padding: 20px; margin: 10px; border-radius: 15px;
background-color: {bg_color}; box-shadow: 0 4px 8px rgba(0,0,0,0.1);'>
<h3>{space_name}</h3>
<p>SDK: {sdk}</p>
<p>Likes: {likes}</p>
<a href='https://huggingface.co/spaces/{space_id}' target='_blank'>Visit Space</a>
</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='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>"
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"
)
if __name__ == "__main__":
demo.launch()