Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update app.py
Browse files
app.py
CHANGED
@@ -11,14 +11,12 @@ from PIL import Image
|
|
11 |
from io import BytesIO
|
12 |
from datetime import datetime
|
13 |
import gradio as gr
|
|
|
14 |
|
15 |
USERNAME = "openfree"
|
16 |
-
|
17 |
-
# ์คํฌ๋ฆฐ์ท ์บ์ ์ ์ฅ์
|
18 |
SCREENSHOT_CACHE = {}
|
19 |
|
20 |
def take_screenshot(url):
|
21 |
-
"""์น์ฌ์ดํธ ์คํฌ๋ฆฐ์ท ์ดฌ์"""
|
22 |
if url in SCREENSHOT_CACHE:
|
23 |
return SCREENSHOT_CACHE[url]
|
24 |
|
@@ -52,7 +50,6 @@ def take_screenshot(url):
|
|
52 |
driver.quit()
|
53 |
|
54 |
def get_pastel_color(index):
|
55 |
-
"""์ธ๋ฑ์ค ๊ธฐ๋ฐ ํ์คํ
์์ ์์ฑ"""
|
56 |
pastel_colors = [
|
57 |
'rgba(255, 230, 230, 0.8)', 'rgba(255, 230, 255, 0.8)',
|
58 |
'rgba(230, 230, 255, 0.8)', 'rgba(230, 255, 255, 0.8)',
|
@@ -61,14 +58,13 @@ def get_pastel_color(index):
|
|
61 |
return pastel_colors[index % len(pastel_colors)]
|
62 |
|
63 |
def get_space_card(space, index):
|
64 |
-
"""์คํ์ด์ค ์นด๋ HTML ์์ฑ"""
|
65 |
space_id = space.get('id', '')
|
66 |
-
|
67 |
-
likes = space.get('likes', 0)
|
68 |
sdk = space.get('sdk', 'N/A')
|
|
|
69 |
url = f"https://huggingface.co/spaces/{space_id}"
|
70 |
|
71 |
-
# ์คํฌ๋ฆฐ์ท ๊ฐ์ ธ์ค๊ธฐ
|
72 |
screenshot = take_screenshot(url)
|
73 |
bg_style = f"""
|
74 |
background-image: linear-gradient(
|
@@ -102,47 +98,83 @@ def get_space_card(space, index):
|
|
102 |
padding: 15px;
|
103 |
border-radius: 10px;
|
104 |
backdrop-filter: blur(5px);'>
|
105 |
-
<h3 style='margin: 0; color: #333;'>{
|
106 |
-
<p style='margin:
|
107 |
-
<p style='margin:
|
|
|
|
|
|
|
108 |
</div>
|
109 |
</div>
|
110 |
"""
|
111 |
|
112 |
-
def get_user_spaces():
|
113 |
-
"""ํ๊น
ํ์ด์ค ์คํ์ด์ค ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ"""
|
114 |
url = f"https://huggingface.co/api/spaces?author={USERNAME}&limit=500"
|
115 |
try:
|
|
|
116 |
response = requests.get(url)
|
117 |
-
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
120 |
<div style='
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
html_content +=
|
130 |
-
|
131 |
-
|
|
|
|
|
|
|
132 |
except Exception as e:
|
133 |
-
|
|
|
134 |
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
if __name__ == "__main__":
|
148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
from io import BytesIO
|
12 |
from datetime import datetime
|
13 |
import gradio as gr
|
14 |
+
from typing import Tuple
|
15 |
|
16 |
USERNAME = "openfree"
|
|
|
|
|
17 |
SCREENSHOT_CACHE = {}
|
18 |
|
19 |
def take_screenshot(url):
|
|
|
20 |
if url in SCREENSHOT_CACHE:
|
21 |
return SCREENSHOT_CACHE[url]
|
22 |
|
|
|
50 |
driver.quit()
|
51 |
|
52 |
def get_pastel_color(index):
|
|
|
53 |
pastel_colors = [
|
54 |
'rgba(255, 230, 230, 0.8)', 'rgba(255, 230, 255, 0.8)',
|
55 |
'rgba(230, 230, 255, 0.8)', 'rgba(230, 255, 255, 0.8)',
|
|
|
58 |
return pastel_colors[index % len(pastel_colors)]
|
59 |
|
60 |
def get_space_card(space, index):
|
|
|
61 |
space_id = space.get('id', '')
|
62 |
+
author, title = space_id.split('/', 1)
|
63 |
+
likes = format(space.get('likes', 0), ',')
|
64 |
sdk = space.get('sdk', 'N/A')
|
65 |
+
created = space.get('createdAt', '').split('T')[0]
|
66 |
url = f"https://huggingface.co/spaces/{space_id}"
|
67 |
|
|
|
68 |
screenshot = take_screenshot(url)
|
69 |
bg_style = f"""
|
70 |
background-image: linear-gradient(
|
|
|
98 |
padding: 15px;
|
99 |
border-radius: 10px;
|
100 |
backdrop-filter: blur(5px);'>
|
101 |
+
<h3 style='margin: 0; color: #333;'>{title}</h3>
|
102 |
+
<p style='margin: 5px 0; color: #666;'>Author: {author}</p>
|
103 |
+
<p style='margin: 5px 0; color: #666;'>SDK: {sdk}</p>
|
104 |
+
<p style='margin: 5px 0; color: #666;'>โค๏ธ {likes}</p>
|
105 |
+
<p style='margin: 5px 0; color: #666;'>Created: {created}</p>
|
106 |
+
<a href='{url}' target='_blank' style='color: blue; text-decoration: none;'>๐ View</a>
|
107 |
</div>
|
108 |
</div>
|
109 |
"""
|
110 |
|
111 |
+
def get_user_spaces(progress=gr.Progress()) -> Tuple[str, str]:
|
|
|
112 |
url = f"https://huggingface.co/api/spaces?author={USERNAME}&limit=500"
|
113 |
try:
|
114 |
+
progress(0, desc="Fetching spaces data...")
|
115 |
response = requests.get(url)
|
116 |
+
response.raise_for_status()
|
117 |
+
spaces_data = response.json()
|
118 |
+
|
119 |
+
progress(0.1, desc="Creating gallery...")
|
120 |
+
html_content = """
|
121 |
+
<div style='
|
122 |
+
padding: 20px;
|
123 |
+
background: #f5f5f5;'>
|
124 |
<div style='
|
125 |
+
display: grid;
|
126 |
+
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
|
127 |
+
gap: 20px;'>
|
128 |
+
"""
|
129 |
+
|
130 |
+
total = len(spaces_data)
|
131 |
+
for idx, space in enumerate(spaces_data):
|
132 |
+
progress((0.1 + 0.9 * idx/total), desc=f"Loading space {idx+1}/{total}...")
|
133 |
+
html_content += get_space_card(space, idx)
|
134 |
+
|
135 |
+
html_content += "</div></div>"
|
136 |
+
|
137 |
+
progress(1.0, desc="Complete!")
|
138 |
+
return html_content, "Gallery refresh complete!"
|
139 |
except Exception as e:
|
140 |
+
error_html = f'<div style="color: red; padding: 20px;">Error: {str(e)}</div>'
|
141 |
+
return error_html, f"Error: {str(e)}"
|
142 |
|
143 |
+
def create_interface():
|
144 |
+
with gr.Blocks(title="Hugging Face Space Gallery") as interface:
|
145 |
+
gr.Markdown("# ๐ค Hugging Face Space Gallery")
|
146 |
+
gr.Markdown(f"Shows spaces by user: {USERNAME}")
|
147 |
+
|
148 |
+
with gr.Row():
|
149 |
+
refresh_btn = gr.Button("Refresh Gallery", variant="primary")
|
150 |
+
|
151 |
+
gallery_html = gr.HTML()
|
152 |
+
status = gr.Markdown("Ready")
|
153 |
+
|
154 |
+
def update():
|
155 |
+
html, status_text = get_user_spaces()
|
156 |
+
return [html, status_text]
|
157 |
+
|
158 |
+
refresh_btn.click(
|
159 |
+
fn=update,
|
160 |
+
outputs=[gallery_html, status],
|
161 |
+
show_progress=True
|
162 |
+
)
|
163 |
+
|
164 |
+
interface.load(
|
165 |
+
fn=update,
|
166 |
+
outputs=[gallery_html, status]
|
167 |
+
)
|
168 |
+
|
169 |
+
return interface
|
170 |
|
171 |
if __name__ == "__main__":
|
172 |
+
try:
|
173 |
+
demo = create_interface()
|
174 |
+
demo.launch(
|
175 |
+
share=True,
|
176 |
+
inbrowser=True,
|
177 |
+
show_api=False
|
178 |
+
)
|
179 |
+
except Exception as e:
|
180 |
+
print(f"Error launching app: {e}")
|