File size: 3,265 Bytes
3b2e7c4
8c9d551
3b2e7c4
 
f79dac7
3a0fa58
 
 
 
f79dac7
 
3b2e7c4
8849302
3b2e7c4
8849302
3b2e7c4
f79dac7
8849302
9c9698b
 
 
f79dac7
 
 
 
9c9698b
f79dac7
 
 
 
8849302
 
 
f79dac7
9c9698b
 
 
 
3a0fa58
8849302
9c9698b
f79dac7
9c9698b
f79dac7
 
8c9d551
8849302
8c9d551
8849302
 
8c9d551
 
 
 
8849302
c5437a5
 
 
 
8849302
3e40f7e
514cd38
8849302
 
 
 
 
 
514cd38
 
 
6d77ca6
8849302
49340e2
6d77ca6
8849302
 
 
 
 
 
 
 
bf508de
8849302
189b3c1
8849302
 
 
 
 
 
 
189b3c1
57572ee
8849302
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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()