openfree commited on
Commit
e53076b
ยท
verified ยท
1 Parent(s): c386b43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -92
app.py CHANGED
@@ -298,68 +298,59 @@ def get_models_data(progress=gr.Progress()):
298
  """๋ชจ๋ธ ID๋ฅผ ์ •๊ทœํ™”"""
299
  return model_id.strip().lower()
300
 
301
- url = "https://huggingface.co/api/models" # ์ผ๋ฐ˜ API ์‚ฌ์šฉ
302
-
303
  try:
304
  progress(0, desc="Fetching models data...")
305
- params = {
306
- 'full': 'true',
307
- 'limit': 3000,
308
- 'sort': 'lastModified', # ์ตœ์‹  ์ˆ˜์ •์ˆœ์œผ๋กœ ์ •๋ ฌ
309
- 'direction': -1
310
- }
311
-
312
- headers = {'Accept': 'application/json'}
313
-
314
- response = requests.get(url, params=params, headers=headers)
315
- if response.status_code != 200:
316
- print(f"API ์š”์ฒญ ์‹คํŒจ: {response.status_code}")
317
- print(f"Response: {response.text}")
318
- return create_error_plot(), "<div>๋ชจ๋ธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.</div>", pd.DataFrame()
319
-
320
- models = response.json()
321
 
322
- # ์ „์ฒด ์ˆœ์œ„ ์ •๋ณด ์ €์žฅ
323
- model_ranks = {}
324
- model_data = {} # ๋ชจ๋“  ๋ชจ๋ธ์˜ ์ƒ์„ธ ๋ฐ์ดํ„ฐ ์ €์žฅ
325
-
326
- for idx, model in enumerate(models, 1):
327
- model_id = normalize_model_id(model.get('id', ''))
328
- model_data[model_id] = {
329
- 'rank': idx,
330
- 'downloads': model.get('downloads', 0),
331
- 'likes': model.get('likes', 0),
332
- 'title': model.get('title', 'No Title')
333
- }
334
-
335
- # target_models ์ค‘ ์ˆœ์œ„๊ถŒ ๋‚ด ๋ชจ๋ธ ํ•„ํ„ฐ๋ง
336
  filtered_models = []
337
- for target_id in target_models.keys():
338
- normalized_target_id = normalize_model_id(target_id)
339
- if normalized_target_id in model_data:
340
- model_info = {
341
- 'id': target_id,
342
- 'rank': model_data[normalized_target_id]['rank'],
343
- 'downloads': model_data[normalized_target_id]['downloads'],
344
- 'likes': model_data[normalized_target_id]['likes'],
345
- 'title': model_data[normalized_target_id]['title']
346
- }
347
- else:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
348
  model_info = {
349
- 'id': target_id,
350
- 'rank': 'Not in top 3000',
351
- 'downloads': 'N/A',
352
- 'likes': 'N/A',
353
  'title': 'No Title'
354
  }
355
- filtered_models.append(model_info)
356
 
357
- # ์ˆœ์œ„๋กœ ์ •๋ ฌ
358
- filtered_models.sort(key=lambda x: float('inf') if x['rank'] == 'Not in top 3000' else x['rank'])
359
-
 
 
 
360
 
361
  if not filtered_models:
362
- return create_error_plot(), "<div>์„ ํƒ๋œ ๋ชจ๋ธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.</div>", pd.DataFrame()
363
 
364
  progress(0.3, desc="Creating visualization...")
365
 
@@ -372,14 +363,11 @@ def get_models_data(progress=gr.Progress()):
372
  likes = [model['likes'] for model in filtered_models]
373
  downloads = [model['downloads'] for model in filtered_models]
374
 
375
- # Y์ถ• ๊ฐ’์„ ๋ฐ˜์ „ (์ˆซ์ž ์ˆœ์œ„๋งŒ)
376
- y_values = [3001 - r if isinstance(r, int) else 0 for r in ranks]
377
-
378
  # ๋ง‰๋Œ€ ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ
379
  fig.add_trace(go.Bar(
380
  x=ids,
381
- y=y_values,
382
- text=[f"Rank: {r}<br>Likes: {format(l, ',') if isinstance(l, (int, float)) else l}<br>Downloads: {format(d, ',') if isinstance(d, (int, float)) else d}"
383
  for r, l, d in zip(ranks, likes, downloads)],
384
  textposition='auto',
385
  marker_color='rgb(158,202,225)',
@@ -388,19 +376,15 @@ def get_models_data(progress=gr.Progress()):
388
 
389
  fig.update_layout(
390
  title={
391
- 'text': 'Hugging Face Models Global Download Rankings (Top 3000)',
392
  'y':0.95,
393
  'x':0.5,
394
  'xanchor': 'center',
395
  'yanchor': 'top'
396
  },
397
  xaxis_title='Model ID',
398
- yaxis_title='Global Rank',
399
- yaxis=dict(
400
- ticktext=[str(i) for i in range(1, 3001, 150)],
401
- tickvals=[3001 - i for i in range(1, 3001, 150)],
402
- range=[0, 3000]
403
- ),
404
  height=800,
405
  showlegend=False,
406
  template='plotly_white',
@@ -412,22 +396,17 @@ def get_models_data(progress=gr.Progress()):
412
  # HTML ์นด๋“œ ์ƒ์„ฑ
413
  html_content = """
414
  <div style='padding: 20px; background: #f5f5f5;'>
415
- <h2 style='color: #2c3e50;'>Models ๊ธ€๋กœ๋ฒŒ ์ธ๊ธฐ ์ˆœ์œ„ (Top 3000)</h2>
416
  <div style='display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px;'>
417
  """
418
 
419
- # ์ˆœ์œ„๊ถŒ ๋‚ด ๋ชจ๋ธ ์นด๋“œ ์ƒ์„ฑ
420
  for model in filtered_models:
421
  model_id = model['id']
422
  rank = model['rank']
423
- likes = model.get('likes', 0)
424
- downloads = model.get('downloads', 0)
425
  title = model.get('title', 'No Title')
426
 
427
- # ์ˆซ์ž ํฌ๋งทํŒ… ์ฒ˜๋ฆฌ
428
- likes_display = format(likes, ',') if isinstance(likes, (int, float)) else likes
429
- downloads_display = format(downloads, ',') if isinstance(downloads, (int, float)) else downloads
430
-
431
  html_content += f"""
432
  <div style='
433
  background: white;
@@ -438,8 +417,8 @@ def get_models_data(progress=gr.Progress()):
438
  '>
439
  <h3 style='color: #34495e;'>Rank #{rank} - {model_id}</h3>
440
  <p style='color: #2c3e50;'>{title}</p>
441
- <p style='color: #7f8c8d;'>๐Ÿ‘ Likes: {likes_display}</p>
442
- <p style='color: #7f8c8d;'>โฌ‡๏ธ Downloads: {downloads_display}</p>
443
  <a href='{target_models[model_id]}'
444
  target='_blank'
445
  style='
@@ -459,24 +438,14 @@ def get_models_data(progress=gr.Progress()):
459
  html_content += "</div></div>"
460
 
461
  # ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„ ์ƒ์„ฑ
462
- df_data = []
463
- # ๋ชจ๋“  ๋ชจ๋ธ ์ •๋ณด๋ฅผ ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์— ์ถ”๊ฐ€
464
- for model in filtered_models:
465
- likes = model.get('likes', 0)
466
- downloads = model.get('downloads', 0)
467
-
468
- # ์ˆซ์ž ํฌ๋งทํŒ… ์ฒ˜๋ฆฌ
469
- likes_display = format(likes, ',') if isinstance(likes, (int, float)) else likes
470
- downloads_display = format(downloads, ',') if isinstance(downloads, (int, float)) else downloads
471
-
472
- df_data.append({
473
- 'Global Rank': model['rank'],
474
- 'Model ID': model['id'],
475
- 'Title': model.get('title', 'No Title'),
476
- 'Likes': likes_display,
477
- 'Downloads': downloads_display,
478
- 'URL': target_models[model['id']]
479
- })
480
 
481
  df = pd.DataFrame(df_data)
482
 
 
298
  """๋ชจ๋ธ ID๋ฅผ ์ •๊ทœํ™”"""
299
  return model_id.strip().lower()
300
 
 
 
301
  try:
302
  progress(0, desc="Fetching models data...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303
 
304
+ # ๊ฐ ๋ชจ๋ธ์˜ ์ƒ์„ธ ์ •๋ณด๋ฅผ ๊ฐœ๋ณ„์ ์œผ๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ
 
 
 
 
 
 
 
 
 
 
 
 
 
305
  filtered_models = []
306
+ total_models = len(target_models)
307
+
308
+ for idx, (model_id, model_url) in enumerate(target_models.items()):
309
+ progress((idx + 1) / total_models, desc=f"Fetching model {idx + 1}/{total_models}...")
310
+
311
+ try:
312
+ # ๊ฐœ๋ณ„ ๋ชจ๋ธ API ํ˜ธ์ถœ
313
+ model_url_api = f"https://huggingface.co/api/models/{model_id}"
314
+ response = requests.get(model_url_api, headers={'Accept': 'application/json'})
315
+
316
+ if response.status_code == 200:
317
+ model_data = response.json()
318
+ model_info = {
319
+ 'id': model_id,
320
+ 'downloads': model_data.get('downloads', 0),
321
+ 'likes': model_data.get('likes', 0),
322
+ 'title': model_data.get('title', 'No Title')
323
+ }
324
+ filtered_models.append(model_info)
325
+ print(f"Model {model_id}: Downloads={model_info['downloads']}, Likes={model_info['likes']}")
326
+ else:
327
+ print(f"Failed to fetch data for {model_id}: {response.status_code}")
328
+ model_info = {
329
+ 'id': model_id,
330
+ 'downloads': 0,
331
+ 'likes': 0,
332
+ 'title': 'No Title'
333
+ }
334
+ filtered_models.append(model_info)
335
+ except Exception as e:
336
+ print(f"Error fetching data for {model_id}: {str(e)}")
337
  model_info = {
338
+ 'id': model_id,
339
+ 'downloads': 0,
340
+ 'likes': 0,
 
341
  'title': 'No Title'
342
  }
343
+ filtered_models.append(model_info)
344
 
345
+ # ๋‹ค์šด๋กœ๋“œ ์ˆ˜๋กœ ์ •๋ ฌ
346
+ filtered_models.sort(key=lambda x: x['downloads'], reverse=True)
347
+
348
+ # ์ˆœ์œ„ ํ• ๋‹น
349
+ for idx, model in enumerate(filtered_models, 1):
350
+ model['rank'] = idx
351
 
352
  if not filtered_models:
353
+ return create_error_plot(), "<div>๋ชจ๋ธ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.</div>", pd.DataFrame()
354
 
355
  progress(0.3, desc="Creating visualization...")
356
 
 
363
  likes = [model['likes'] for model in filtered_models]
364
  downloads = [model['downloads'] for model in filtered_models]
365
 
 
 
 
366
  # ๋ง‰๋Œ€ ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ
367
  fig.add_trace(go.Bar(
368
  x=ids,
369
+ y=ranks,
370
+ text=[f"Rank: {r}<br>Likes: {format(l, ',')}<br>Downloads: {format(d, ',')}"
371
  for r, l, d in zip(ranks, likes, downloads)],
372
  textposition='auto',
373
  marker_color='rgb(158,202,225)',
 
376
 
377
  fig.update_layout(
378
  title={
379
+ 'text': 'Hugging Face Models Rankings by Downloads',
380
  'y':0.95,
381
  'x':0.5,
382
  'xanchor': 'center',
383
  'yanchor': 'top'
384
  },
385
  xaxis_title='Model ID',
386
+ yaxis_title='Rank',
387
+ yaxis_autorange='reversed', # ์ˆœ์œ„๋ฅผ ์œ„์—์„œ ์•„๋ž˜๋กœ
 
 
 
 
388
  height=800,
389
  showlegend=False,
390
  template='plotly_white',
 
396
  # HTML ์นด๋“œ ์ƒ์„ฑ
397
  html_content = """
398
  <div style='padding: 20px; background: #f5f5f5;'>
399
+ <h2 style='color: #2c3e50;'>Models Rankings by Downloads</h2>
400
  <div style='display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px;'>
401
  """
402
 
 
403
  for model in filtered_models:
404
  model_id = model['id']
405
  rank = model['rank']
406
+ likes = model['likes']
407
+ downloads = model['downloads']
408
  title = model.get('title', 'No Title')
409
 
 
 
 
 
410
  html_content += f"""
411
  <div style='
412
  background: white;
 
417
  '>
418
  <h3 style='color: #34495e;'>Rank #{rank} - {model_id}</h3>
419
  <p style='color: #2c3e50;'>{title}</p>
420
+ <p style='color: #7f8c8d;'>๐Ÿ‘ Likes: {format(likes, ',')}</p>
421
+ <p style='color: #7f8c8d;'>โฌ‡๏ธ Downloads: {format(downloads, ',')}</p>
422
  <a href='{target_models[model_id]}'
423
  target='_blank'
424
  style='
 
438
  html_content += "</div></div>"
439
 
440
  # ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„ ์ƒ์„ฑ
441
+ df_data = [{
442
+ 'Rank': model['rank'],
443
+ 'Model ID': model['id'],
444
+ 'Title': model.get('title', 'No Title'),
445
+ 'Likes': format(model['likes'], ','),
446
+ 'Downloads': format(model['downloads'], ','),
447
+ 'URL': target_models[model['id']]
448
+ } for model in filtered_models]
 
 
 
 
 
 
 
 
 
 
449
 
450
  df = pd.DataFrame(df_data)
451