openfree commited on
Commit
14aed2b
ยท
verified ยท
1 Parent(s): 3a6600d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -40
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
- space_name = space_id.split('/')[-1]
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;'>{space_name}</h3>
106
- <p style='margin: 10px 0; color: #666;'>SDK: {sdk}</p>
107
- <p style='margin: 10px 0; color: #666;'>โค๏ธ {likes}</p>
 
 
 
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
- if response.status_code == 200:
118
- spaces_data = response.json()
119
- html_content = """
 
 
 
 
 
120
  <div style='
121
- padding: 20px;
122
- background: #f5f5f5;'>
123
- <div style='
124
- display: grid;
125
- grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
126
- gap: 20px;'>
127
- """
128
- html_content += "".join(get_space_card(space, idx) for idx, space in enumerate(spaces_data))
129
- html_content += "</div></div>"
130
- return html_content
131
- return "<p>Failed to fetch spaces</p>"
 
 
 
132
  except Exception as e:
133
- return f"<p>Error: {str(e)}</p>"
 
134
 
135
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
136
- demo = gr.Interface(
137
- fn=lambda: get_user_spaces(),
138
- inputs=None,
139
- outputs=gr.HTML(),
140
- title="Hugging Face Spaces Gallery",
141
- css="""
142
- .container { max-width: 1200px; margin: 0 auto; }
143
- .gradio-container { font-family: 'Arial', sans-serif; }
144
- """
145
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
 
147
  if __name__ == "__main__":
148
- demo.launch()
 
 
 
 
 
 
 
 
 
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}")