openfree commited on
Commit
faeec80
โ€ข
1 Parent(s): 78f9683

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -147
app.py CHANGED
@@ -85,20 +85,72 @@ def take_screenshot(url):
85
  if 'driver' in locals():
86
  driver.quit()
87
 
88
- def get_space_card(space: dict, index: int) -> str:
89
- """์ŠคํŽ˜์ด์Šค ์นด๋“œ HTML ์ƒ์„ฑ"""
90
- space_id = space.get('id', '')
91
- author, title = space_id.split('/', 1)
92
- likes = format(space.get('likes', 0), ',')
93
- sdk = space.get('sdk', 'N/A')
94
- created = space.get('createdAt', '').split('T')[0]
95
- url = f"https://huggingface.co/spaces/{space_id}"
96
-
97
- screenshot = get_cached_screenshot(url)
98
- bg_color = f"rgba({random.randint(230,255)}, {random.randint(230,255)}, {random.randint(230,255)}, 0.8)"
99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  return f"""
101
- <div class="space-card" style='
102
  position: relative;
103
  border: none;
104
  padding: 0;
@@ -111,18 +163,15 @@ def get_space_card(space: dict, index: int) -> str:
111
  min-height: 400px;
112
  cursor: pointer;
113
  transform-origin: center;'
114
- onmouseover="this.style.transform='scale(1.02)'; this.style.boxShadow='0 15px 30px rgba(0,0,0,0.15)';"
115
- onmouseout="this.style.transform='scale(1)'; this.style.boxShadow='0 10px 20px rgba(0,0,0,0.1)';"
116
  onclick="window.open('{url}', '_blank')">
117
 
118
- <!-- ์Šคํฌ๋ฆฐ์ƒท ์˜์—ญ -->
119
  <div style='
120
  width: 100%;
121
  height: 200px;
122
- background-image: url(data:image/png;base64,{screenshot if screenshot else ''});
123
- background-size: cover;
124
- background-position: center;
125
- border-bottom: 3px solid {bg_color};
126
  position: relative;'>
127
 
128
  <!-- ์ˆœ์œ„ ๋ฑƒ์ง€ -->
@@ -140,27 +189,24 @@ def get_space_card(space: dict, index: int) -> str:
140
  #{index + 1}
141
  </div>
142
 
143
- <!-- SDK ๋ฑƒ์ง€ -->
144
  <div style='
145
  position: absolute;
146
  top: 10px;
147
  right: 10px;
148
- background: {bg_color};
149
  padding: 5px 15px;
150
  border-radius: 20px;
151
  font-weight: bold;
152
- font-size: 0.8em;
153
- backdrop-filter: blur(5px);'>
154
- ๐Ÿ› ๏ธ {sdk}
155
  </div>
 
 
156
  </div>
157
 
158
  <!-- ์ฝ˜ํ…์ธ  ๏ฟฝ๏ฟฝ๏ฟฝ์—ญ -->
159
- <div style='
160
- padding: 20px;
161
- background: white;'>
162
-
163
- <!-- ์ œ๋ชฉ -->
164
  <h3 style='
165
  margin: 0 0 15px 0;
166
  color: #333;
@@ -174,88 +220,27 @@ def get_space_card(space: dict, index: int) -> str:
174
  {title}
175
  </h3>
176
 
177
- <!-- ๋ฉ”ํƒ€ ์ •๋ณด ๊ทธ๋ฆฌ๋“œ -->
178
  <div style='
179
  display: grid;
180
  grid-template-columns: repeat(2, 1fr);
181
  gap: 10px;
182
  margin-bottom: 20px;
183
  font-size: 0.9em;'>
184
-
185
- <div style='
186
- display: flex;
187
- align-items: center;
188
- color: #666;'>
189
  <span style='margin-right: 5px;'>๐Ÿ‘ค</span> {author}
190
  </div>
191
-
192
- <div style='
193
- display: flex;
194
- align-items: center;
195
- color: #666;'>
196
  <span style='margin-right: 5px;'>โค๏ธ</span> {likes}
197
  </div>
198
-
199
- <div style='
200
- display: flex;
201
- align-items: center;
202
- color: #666;
203
- grid-column: span 2;'>
204
  <span style='margin-right: 5px;'>๐Ÿ“…</span> {created}
205
  </div>
206
  </div>
207
-
208
- <!-- ์•ก์…˜ ๋ฒ„ํŠผ -->
209
- <div style='
210
- display: flex;
211
- justify-content: space-between;
212
- align-items: center;'>
213
-
214
- <a href='{url}' target='_blank'
215
- style='
216
- display: inline-flex;
217
- align-items: center;
218
- padding: 8px 20px;
219
- background: linear-gradient(135deg, #6e8efb, #4a6cf7);
220
- color: white;
221
- text-decoration: none;
222
- border-radius: 25px;
223
- font-weight: 500;
224
- transition: all 0.3s ease;
225
- box-shadow: 0 4px 15px rgba(74,108,247,0.2);'
226
- onmouseover="this.style.transform='translateY(-2px)'; this.style.boxShadow='0 6px 20px rgba(74,108,247,0.3)';"
227
- onmouseout="this.style.transform='translateY(0)'; this.style.boxShadow='0 4px 15px rgba(74,108,247,0.2)';">
228
- View Space <span style='margin-left: 8px;'>โ†’</span>
229
- </a>
230
-
231
- <div style='
232
- display: flex;
233
- gap: 10px;'>
234
- <div style='
235
- padding: 8px;
236
- background: #f5f5f5;
237
- border-radius: 50%;
238
- cursor: pointer;
239
- transition: all 0.3s ease;'
240
- onmouseover="this.style.background='#ebebeb';"
241
- onmouseout="this.style.background='#f5f5f5';">
242
- โญ
243
- </div>
244
- <div style='
245
- padding: 8px;
246
- background: #f5f5f5;
247
- border-radius: 50%;
248
- cursor: pointer;
249
- transition: all 0.3s ease;'
250
- onmouseover="this.style.background='#ebebeb';"
251
- onmouseout="this.style.background='#f5f5f5';">
252
- ๐Ÿ”—
253
- </div>
254
- </div>
255
- </div>
256
  </div>
257
  </div>
258
  """
 
 
259
 
260
  def get_trending_spaces(progress=gr.Progress()) -> Tuple[str, str]:
261
  """ํŠธ๋ Œ๋”ฉ ์ŠคํŽ˜์ด์Šค ๊ฐ€์ ธ์˜ค๊ธฐ"""
@@ -277,54 +262,7 @@ def get_trending_spaces(progress=gr.Progress()) -> Tuple[str, str]:
277
  """
278
 
279
  for idx, space in enumerate(top_spaces):
280
- author, title = space['id'].split('/', 1)
281
- space_url = f"https://huggingface.co/spaces/{space['id']}"
282
- likes = format(space.get('likes', 0), ',')
283
- created = space.get('createdAt', '').split('T')[0]
284
-
285
- screenshot = take_screenshot(space_url)
286
- bg_color = f"rgba({random.randint(230,255)}, {random.randint(230,255)}, {random.randint(230,255)}, 0.8)"
287
-
288
- html_content += f"""
289
- <div class="space-card" style='
290
- position: relative;
291
- border: none;
292
- padding: 20px;
293
- margin: 10px;
294
- border-radius: 15px;
295
- box-shadow: 0 4px 8px rgba(0,0,0,0.1);
296
- background-image: linear-gradient({bg_color}, {bg_color}),
297
- url(data:image/png;base64,{screenshot if screenshot else ''});
298
- background-size: cover;
299
- background-position: center;
300
- background-blend-mode: overlay;
301
- transition: transform 0.3s ease;
302
- min-height: 250px;
303
- cursor: pointer;'
304
- onclick="window.open('{space_url}', '_blank')">
305
-
306
- <div style='
307
- position: absolute;
308
- bottom: 0;
309
- left: 0;
310
- right: 0;
311
- background: rgba(255, 255, 255, 0.95);
312
- padding: 10px;
313
- border-radius: 0 0 15px 15px;
314
- font-size: 0.8em;'>
315
- <div style='font-size: 1.2em; font-weight: bold; color: #333; margin-bottom: 5px;'>
316
- #{idx + 1} {title}
317
- </div>
318
- <div style='display: grid; grid-template-columns: repeat(2, 1fr); gap: 5px;'>
319
- <div style='color: #666;'>๐Ÿ‘ค {author}</div>
320
- <div style='color: #666;'>๐Ÿ› ๏ธ {space.get('sdk', 'N/A')}</div>
321
- <div style='color: #666;'>โค๏ธ {likes}</div>
322
- <div style='color: #666;'>๐Ÿ“… {created}</div>
323
- </div>
324
- </div>
325
- </div>
326
- """
327
-
328
  progress((0.1 + 0.9 * idx/10), desc=f"Loading space {idx+1}/10...")
329
 
330
  html_content += "</div></div>"
@@ -350,6 +288,7 @@ def get_models(progress=gr.Progress()) -> Tuple[str, str]:
350
  top_models = models[:10]
351
 
352
  progress(0.1, desc="Creating gallery...")
 
353
  html_content = """
354
  <div style='padding: 20px; background: #f5f5f5;'>
355
  <div style='display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px;'>
@@ -365,8 +304,9 @@ def get_models(progress=gr.Progress()) -> Tuple[str, str]:
365
  url = f"https://huggingface.co/{model_id}"
366
 
367
  screenshot = get_cached_screenshot(url)
368
- html_content += get_space_card({"id": model_id, "likes": model.get('likes', 0),
369
- "sdk": "model", "createdAt": created}, idx)
 
370
 
371
  progress((0.1 + 0.9 * idx/10), desc=f"Loading model {idx+1}/10...")
372
 
@@ -408,8 +348,9 @@ def get_datasets(progress=gr.Progress()) -> Tuple[str, str]:
408
  url = f"https://huggingface.co/datasets/{dataset_id}"
409
 
410
  screenshot = get_cached_screenshot(url)
411
- html_content += get_space_card({"id": dataset_id, "likes": dataset.get('likes', 0),
412
- "sdk": "dataset", "createdAt": created}, idx)
 
413
 
414
  progress((0.1 + 0.9 * idx/10), desc=f"Loading dataset {idx+1}/10...")
415
 
 
85
  if 'driver' in locals():
86
  driver.quit()
87
 
88
+ def get_card(item: dict, index: int, card_type: str = "space") -> str:
89
+ """ํ†ตํ•ฉ ์นด๋“œ HTML ์ƒ์„ฑ"""
90
+ item_id = item.get('id', '')
91
+ author, title = item_id.split('/', 1)
92
+ likes = format(item.get('likes', 0), ',')
93
+ created = item.get('createdAt', '').split('T')[0]
 
 
 
 
 
94
 
95
+ # ์นด๋“œ ํƒ€์ž…๋ณ„ ํŠน์ˆ˜ ์ฒ˜๋ฆฌ
96
+ if card_type == "space":
97
+ url = f"https://huggingface.co/spaces/{item_id}"
98
+ type_icon = "๐ŸŽฏ"
99
+ type_label = "SPACE"
100
+ bg_content = f"""
101
+ background-image: url(data:image/png;base64,{get_cached_screenshot(url) if get_cached_screenshot(url) else ''});
102
+ background-size: cover;
103
+ background-position: center;
104
+ """
105
+ elif card_type == "model":
106
+ url = f"https://huggingface.co/{item_id}"
107
+ type_icon = "๐Ÿค–"
108
+ type_label = "MODEL"
109
+ tags = item.get('tags', [])[:3] # ์ƒ์œ„ 3๊ฐœ ํƒœ๊ทธ๋งŒ ํ‘œ์‹œ
110
+ bg_content = f"""
111
+ background: linear-gradient(135deg, #6e8efb, #4a6cf7);
112
+ display: flex;
113
+ flex-wrap: wrap;
114
+ gap: 5px;
115
+ padding: 15px;
116
+ align-content: flex-start;
117
+ """
118
+ tag_html = ''.join([f"""
119
+ <span style='
120
+ background: rgba(255,255,255,0.2);
121
+ padding: 5px 10px;
122
+ border-radius: 15px;
123
+ color: white;
124
+ font-size: 0.8em;'>
125
+ #{tag}
126
+ </span>
127
+ """ for tag in tags])
128
+ else: # dataset
129
+ url = f"https://huggingface.co/datasets/{item_id}"
130
+ type_icon = "๐Ÿ“Š"
131
+ type_label = "DATASET"
132
+ tags = item.get('tags', [])[:3]
133
+ bg_content = f"""
134
+ background: linear-gradient(135deg, #ff6b6b, #ff8787);
135
+ display: flex;
136
+ flex-wrap: wrap;
137
+ gap: 5px;
138
+ padding: 15px;
139
+ align-content: flex-start;
140
+ """
141
+ tag_html = ''.join([f"""
142
+ <span style='
143
+ background: rgba(255,255,255,0.2);
144
+ padding: 5px 10px;
145
+ border-radius: 15px;
146
+ color: white;
147
+ font-size: 0.8em;'>
148
+ #{tag}
149
+ </span>
150
+ """ for tag in tags])
151
+
152
  return f"""
153
+ <div class="card" style='
154
  position: relative;
155
  border: none;
156
  padding: 0;
 
163
  min-height: 400px;
164
  cursor: pointer;
165
  transform-origin: center;'
166
+ onmouseover="this.style.transform='scale(0.98) translateY(5px)'; this.style.boxShadow='0 5px 15px rgba(0,0,0,0.2)';"
167
+ onmouseout="this.style.transform='scale(1) translateY(0)'; this.style.boxShadow='0 10px 20px rgba(0,0,0,0.1)';"
168
  onclick="window.open('{url}', '_blank')">
169
 
170
+ <!-- ์ƒ๋‹จ ์˜์—ญ -->
171
  <div style='
172
  width: 100%;
173
  height: 200px;
174
+ {bg_content}
 
 
 
175
  position: relative;'>
176
 
177
  <!-- ์ˆœ์œ„ ๋ฑƒ์ง€ -->
 
189
  #{index + 1}
190
  </div>
191
 
192
+ <!-- ํƒ€์ž… ๋ฑƒ์ง€ -->
193
  <div style='
194
  position: absolute;
195
  top: 10px;
196
  right: 10px;
197
+ background: rgba(255,255,255,0.9);
198
  padding: 5px 15px;
199
  border-radius: 20px;
200
  font-weight: bold;
201
+ font-size: 0.8em;'>
202
+ {type_icon} {type_label}
 
203
  </div>
204
+
205
+ {tag_html if card_type != "space" else ""}
206
  </div>
207
 
208
  <!-- ์ฝ˜ํ…์ธ  ๏ฟฝ๏ฟฝ๏ฟฝ์—ญ -->
209
+ <div style='padding: 20px;'>
 
 
 
 
210
  <h3 style='
211
  margin: 0 0 15px 0;
212
  color: #333;
 
220
  {title}
221
  </h3>
222
 
 
223
  <div style='
224
  display: grid;
225
  grid-template-columns: repeat(2, 1fr);
226
  gap: 10px;
227
  margin-bottom: 20px;
228
  font-size: 0.9em;'>
229
+ <div style='color: #666;'>
 
 
 
 
230
  <span style='margin-right: 5px;'>๐Ÿ‘ค</span> {author}
231
  </div>
232
+ <div style='color: #666;'>
 
 
 
 
233
  <span style='margin-right: 5px;'>โค๏ธ</span> {likes}
234
  </div>
235
+ <div style='color: #666; grid-column: span 2;'>
 
 
 
 
 
236
  <span style='margin-right: 5px;'>๐Ÿ“…</span> {created}
237
  </div>
238
  </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
239
  </div>
240
  </div>
241
  """
242
+
243
+
244
 
245
  def get_trending_spaces(progress=gr.Progress()) -> Tuple[str, str]:
246
  """ํŠธ๋ Œ๋”ฉ ์ŠคํŽ˜์ด์Šค ๊ฐ€์ ธ์˜ค๊ธฐ"""
 
262
  """
263
 
264
  for idx, space in enumerate(top_spaces):
265
+ html_content += get_card(space, idx, "space")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266
  progress((0.1 + 0.9 * idx/10), desc=f"Loading space {idx+1}/10...")
267
 
268
  html_content += "</div></div>"
 
288
  top_models = models[:10]
289
 
290
  progress(0.1, desc="Creating gallery...")
291
+
292
  html_content = """
293
  <div style='padding: 20px; background: #f5f5f5;'>
294
  <div style='display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px;'>
 
304
  url = f"https://huggingface.co/{model_id}"
305
 
306
  screenshot = get_cached_screenshot(url)
307
+ html_content += get_card(model, idx, "model")
308
+
309
+
310
 
311
  progress((0.1 + 0.9 * idx/10), desc=f"Loading model {idx+1}/10...")
312
 
 
348
  url = f"https://huggingface.co/datasets/{dataset_id}"
349
 
350
  screenshot = get_cached_screenshot(url)
351
+ html_content += get_card(dataset, idx, "dataset")
352
+
353
+
354
 
355
  progress((0.1 + 0.9 * idx/10), desc=f"Loading dataset {idx+1}/10...")
356