Update app.py
Browse files
app.py
CHANGED
|
@@ -299,7 +299,27 @@ def get_models_data(progress=gr.Progress()):
|
|
| 299 |
return model_id.strip().lower()
|
| 300 |
|
| 301 |
try:
|
| 302 |
-
progress(0, desc="Fetching
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
|
| 304 |
# ๊ฐ ๋ชจ๋ธ์ ์์ธ ์ ๋ณด๋ฅผ ๊ฐ๋ณ์ ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ
|
| 305 |
filtered_models = []
|
|
@@ -315,21 +335,26 @@ def get_models_data(progress=gr.Progress()):
|
|
| 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:
|
|
@@ -338,16 +363,13 @@ def get_models_data(progress=gr.Progress()):
|
|
| 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['
|
| 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()
|
|
@@ -359,54 +381,65 @@ def get_models_data(progress=gr.Progress()):
|
|
| 359 |
|
| 360 |
# ๋ฐ์ดํฐ ์ค๋น
|
| 361 |
ids = [model['id'] for model in filtered_models]
|
| 362 |
-
ranks = [model['
|
| 363 |
likes = [model['likes'] for model in filtered_models]
|
| 364 |
downloads = [model['downloads'] for model in filtered_models]
|
| 365 |
|
| 366 |
-
#
|
| 367 |
-
|
| 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)',
|
| 374 |
-
opacity=0.8
|
| 375 |
-
))
|
| 376 |
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
|
| 394 |
progress(0.6, desc="Creating model cards...")
|
| 395 |
|
| 396 |
# HTML ์นด๋ ์์ฑ
|
| 397 |
html_content = """
|
| 398 |
<div style='padding: 20px; background: #f5f5f5;'>
|
| 399 |
-
<h2 style='color: #2c3e50;'>Models Rankings
|
| 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 |
-
|
| 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;
|
|
@@ -415,7 +448,7 @@ def get_models_data(progress=gr.Progress()):
|
|
| 415 |
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 416 |
transition: transform 0.2s;
|
| 417 |
'>
|
| 418 |
-
<h3 style='color: #34495e;'>
|
| 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>
|
|
@@ -439,7 +472,7 @@ def get_models_data(progress=gr.Progress()):
|
|
| 439 |
|
| 440 |
# ๋ฐ์ดํฐํ๋ ์ ์์ฑ
|
| 441 |
df_data = [{
|
| 442 |
-
'Rank': model['
|
| 443 |
'Model ID': model['id'],
|
| 444 |
'Title': model.get('title', 'No Title'),
|
| 445 |
'Likes': format(model['likes'], ','),
|
|
|
|
| 299 |
return model_id.strip().lower()
|
| 300 |
|
| 301 |
try:
|
| 302 |
+
progress(0, desc="Fetching global rankings...")
|
| 303 |
+
|
| 304 |
+
# ์ ์ฒด ๋ชจ๋ธ ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ (๋ค์ด๋ก๋ ์์ผ๋ก ์ ๋ ฌ)
|
| 305 |
+
global_url = "https://huggingface.co/api/models"
|
| 306 |
+
global_params = {
|
| 307 |
+
'full': 'true',
|
| 308 |
+
'limit': 10000, # ์ถฉ๋ถํ ํฐ ์๋ก ์ค์
|
| 309 |
+
'sort': 'downloads',
|
| 310 |
+
'direction': -1
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
global_response = requests.get(global_url, headers={'Accept': 'application/json'}, params=global_params)
|
| 314 |
+
if global_response.status_code != 200:
|
| 315 |
+
print(f"Failed to fetch global rankings: {global_response.status_code}")
|
| 316 |
+
return create_error_plot(), "<div>์ ์ฒด ์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋๋ฐ ์คํจํ์ต๋๋ค.</div>", pd.DataFrame()
|
| 317 |
+
|
| 318 |
+
global_models = global_response.json()
|
| 319 |
+
|
| 320 |
+
# ์ ์ฒด ์์ ๋งต ์์ฑ
|
| 321 |
+
global_ranks = {normalize_model_id(model['id']): idx + 1
|
| 322 |
+
for idx, model in enumerate(global_models)}
|
| 323 |
|
| 324 |
# ๊ฐ ๋ชจ๋ธ์ ์์ธ ์ ๋ณด๋ฅผ ๊ฐ๋ณ์ ์ผ๋ก ๊ฐ์ ธ์ค๊ธฐ
|
| 325 |
filtered_models = []
|
|
|
|
| 335 |
|
| 336 |
if response.status_code == 200:
|
| 337 |
model_data = response.json()
|
| 338 |
+
normalized_id = normalize_model_id(model_id)
|
| 339 |
+
global_rank = global_ranks.get(normalized_id, 'Not in top 10000')
|
| 340 |
+
|
| 341 |
model_info = {
|
| 342 |
'id': model_id,
|
| 343 |
'downloads': model_data.get('downloads', 0),
|
| 344 |
'likes': model_data.get('likes', 0),
|
| 345 |
+
'title': model_data.get('title', 'No Title'),
|
| 346 |
+
'global_rank': global_rank
|
| 347 |
}
|
| 348 |
filtered_models.append(model_info)
|
| 349 |
+
print(f"Model {model_id}: Global Rank={global_rank}, Downloads={model_info['downloads']}, Likes={model_info['likes']}")
|
| 350 |
else:
|
| 351 |
print(f"Failed to fetch data for {model_id}: {response.status_code}")
|
| 352 |
model_info = {
|
| 353 |
'id': model_id,
|
| 354 |
'downloads': 0,
|
| 355 |
'likes': 0,
|
| 356 |
+
'title': 'No Title',
|
| 357 |
+
'global_rank': 'Not in top 10000'
|
| 358 |
}
|
| 359 |
filtered_models.append(model_info)
|
| 360 |
except Exception as e:
|
|
|
|
| 363 |
'id': model_id,
|
| 364 |
'downloads': 0,
|
| 365 |
'likes': 0,
|
| 366 |
+
'title': 'No Title',
|
| 367 |
+
'global_rank': 'Not in top 10000'
|
| 368 |
}
|
| 369 |
filtered_models.append(model_info)
|
| 370 |
|
| 371 |
+
# ์ ์ฒด ์์๋ก ์ ๋ ฌ
|
| 372 |
+
filtered_models.sort(key=lambda x: float('inf') if isinstance(x['global_rank'], str) else x['global_rank'])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
|
| 374 |
if not filtered_models:
|
| 375 |
return create_error_plot(), "<div>๋ชจ๋ธ ๋ฐ์ดํฐ๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.</div>", pd.DataFrame()
|
|
|
|
| 381 |
|
| 382 |
# ๋ฐ์ดํฐ ์ค๋น
|
| 383 |
ids = [model['id'] for model in filtered_models]
|
| 384 |
+
ranks = [model['global_rank'] for model in filtered_models]
|
| 385 |
likes = [model['likes'] for model in filtered_models]
|
| 386 |
downloads = [model['downloads'] for model in filtered_models]
|
| 387 |
|
| 388 |
+
# ์์๊ถ ๋ด ๋ชจ๋ธ๋ง ํํฐ๋ง
|
| 389 |
+
valid_indices = [i for i, rank in enumerate(ranks) if isinstance(rank, (int, float))]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 390 |
|
| 391 |
+
if valid_indices: # ์์๊ถ ๋ด ๋ชจ๋ธ์ด ์๋ ๊ฒฝ์ฐ๋ง ๊ทธ๋ํ ์์ฑ
|
| 392 |
+
valid_ids = [ids[i] for i in valid_indices]
|
| 393 |
+
valid_ranks = [ranks[i] for i in valid_indices]
|
| 394 |
+
valid_likes = [likes[i] for i in valid_indices]
|
| 395 |
+
valid_downloads = [downloads[i] for i in valid_indices]
|
| 396 |
+
|
| 397 |
+
# ๋ง๋ ๊ทธ๋ํ ์์ฑ
|
| 398 |
+
fig.add_trace(go.Bar(
|
| 399 |
+
x=valid_ids,
|
| 400 |
+
y=valid_ranks,
|
| 401 |
+
text=[f"Global Rank: {r}<br>Likes: {format(l, ',')}<br>Downloads: {format(d, ',')}"
|
| 402 |
+
for r, l, d in zip(valid_ranks, valid_likes, valid_downloads)],
|
| 403 |
+
textposition='auto',
|
| 404 |
+
marker_color='rgb(158,202,225)',
|
| 405 |
+
opacity=0.8
|
| 406 |
+
))
|
| 407 |
+
|
| 408 |
+
fig.update_layout(
|
| 409 |
+
title={
|
| 410 |
+
'text': 'Hugging Face Models Global Rankings',
|
| 411 |
+
'y':0.95,
|
| 412 |
+
'x':0.5,
|
| 413 |
+
'xanchor': 'center',
|
| 414 |
+
'yanchor': 'top'
|
| 415 |
+
},
|
| 416 |
+
xaxis_title='Model ID',
|
| 417 |
+
yaxis_title='Global Rank',
|
| 418 |
+
yaxis_autorange='reversed', # ์์๋ฅผ ์์์ ์๋๋ก
|
| 419 |
+
height=800,
|
| 420 |
+
showlegend=False,
|
| 421 |
+
template='plotly_white',
|
| 422 |
+
xaxis_tickangle=-45
|
| 423 |
+
)
|
| 424 |
|
| 425 |
progress(0.6, desc="Creating model cards...")
|
| 426 |
|
| 427 |
# HTML ์นด๋ ์์ฑ
|
| 428 |
html_content = """
|
| 429 |
<div style='padding: 20px; background: #f5f5f5;'>
|
| 430 |
+
<h2 style='color: #2c3e50;'>Models Global Rankings</h2>
|
| 431 |
<div style='display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px;'>
|
| 432 |
"""
|
| 433 |
|
| 434 |
for model in filtered_models:
|
| 435 |
model_id = model['id']
|
| 436 |
+
global_rank = model['global_rank']
|
| 437 |
likes = model['likes']
|
| 438 |
downloads = model['downloads']
|
| 439 |
title = model.get('title', 'No Title')
|
| 440 |
|
| 441 |
+
rank_display = f"Global Rank #{global_rank}" if isinstance(global_rank, (int, float)) else global_rank
|
| 442 |
+
|
| 443 |
html_content += f"""
|
| 444 |
<div style='
|
| 445 |
background: white;
|
|
|
|
| 448 |
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 449 |
transition: transform 0.2s;
|
| 450 |
'>
|
| 451 |
+
<h3 style='color: #34495e;'>{rank_display} - {model_id}</h3>
|
| 452 |
<p style='color: #2c3e50;'>{title}</p>
|
| 453 |
<p style='color: #7f8c8d;'>๐ Likes: {format(likes, ',')}</p>
|
| 454 |
<p style='color: #7f8c8d;'>โฌ๏ธ Downloads: {format(downloads, ',')}</p>
|
|
|
|
| 472 |
|
| 473 |
# ๋ฐ์ดํฐํ๋ ์ ์์ฑ
|
| 474 |
df_data = [{
|
| 475 |
+
'Global Rank': model['global_rank'],
|
| 476 |
'Model ID': model['id'],
|
| 477 |
'Title': model.get('title', 'No Title'),
|
| 478 |
'Likes': format(model['likes'], ','),
|