File size: 4,882 Bytes
3b2e7c4
8c9d551
3b2e7c4
 
f79dac7
3a0fa58
 
 
 
f79dac7
 
3b2e7c4
8849302
3b2e7c4
8849302
3b2e7c4
3a6600d
 
 
f79dac7
8849302
3a6600d
 
 
9c9698b
 
 
f79dac7
 
 
 
9c9698b
f79dac7
 
 
 
8849302
 
 
f79dac7
9c9698b
 
 
3a6600d
 
 
3a0fa58
3a6600d
9c9698b
f79dac7
9c9698b
f79dac7
 
8c9d551
8849302
8c9d551
3a6600d
 
 
8c9d551
 
 
 
8849302
c5437a5
 
 
 
3a6600d
 
 
 
 
 
 
 
 
 
 
 
 
3e40f7e
514cd38
3a6600d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
514cd38
 
 
6d77ca6
8849302
49340e2
6d77ca6
8849302
 
 
3a6600d
 
 
 
 
 
 
 
 
8849302
3a6600d
8849302
 
bf508de
8849302
189b3c1
8849302
 
 
 
 
3a6600d
 
 
 
 
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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()