Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -42,25 +42,27 @@ target_spaces = {
|
|
42 |
"NCSOFT/VARCO_Arena": "https://huggingface.co/spaces/NCSOFT/VARCO_Arena"
|
43 |
}
|
44 |
|
45 |
-
def
|
46 |
-
"""
|
47 |
-
url = "https://huggingface.co/api/spaces"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
try:
|
50 |
-
progress(0, desc="Fetching spaces data...")
|
51 |
-
params = {
|
52 |
-
'full': 'true',
|
53 |
-
'limit': 300 # μμ 300κ° μ€νμ΄μ€ κ°μ Έμ€κΈ°
|
54 |
-
}
|
55 |
-
|
56 |
response = requests.get(url, params=params)
|
57 |
response.raise_for_status()
|
58 |
all_spaces = response.json()
|
59 |
|
60 |
-
#
|
61 |
space_ranks = {space['id']: idx + 1 for idx, space in enumerate(all_spaces)}
|
62 |
|
63 |
-
# target_spaces
|
64 |
spaces = []
|
65 |
for space in all_spaces:
|
66 |
if space.get('id', '') in target_spaces:
|
@@ -72,18 +74,21 @@ def get_trending_spaces(progress=gr.Progress()):
|
|
72 |
|
73 |
progress(0.3, desc="Creating visualization...")
|
74 |
|
75 |
-
#
|
76 |
fig = go.Figure()
|
77 |
|
78 |
-
#
|
79 |
ids = [space['id'] for space in spaces]
|
80 |
ranks = [space['rank'] for space in spaces]
|
81 |
likes = [space.get('likes', 0) for space in spaces]
|
82 |
|
|
|
|
|
|
|
83 |
# λ§λ κ·Έλν μμ±
|
84 |
fig.add_trace(go.Bar(
|
85 |
x=ids,
|
86 |
-
y=
|
87 |
text=[f"Rank: {r}<br>Likes: {l}" for r, l in zip(ranks, likes)],
|
88 |
textposition='auto',
|
89 |
marker_color='rgb(158,202,225)',
|
@@ -92,7 +97,7 @@ def get_trending_spaces(progress=gr.Progress()):
|
|
92 |
|
93 |
fig.update_layout(
|
94 |
title={
|
95 |
-
'text': 'Hugging Face Spaces Rankings (Top 300)',
|
96 |
'y':0.95,
|
97 |
'x':0.5,
|
98 |
'xanchor': 'center',
|
@@ -100,7 +105,11 @@ def get_trending_spaces(progress=gr.Progress()):
|
|
100 |
},
|
101 |
xaxis_title='Space ID',
|
102 |
yaxis_title='Rank',
|
103 |
-
|
|
|
|
|
|
|
|
|
104 |
height=800,
|
105 |
showlegend=False,
|
106 |
template='plotly_white',
|
@@ -109,9 +118,10 @@ def get_trending_spaces(progress=gr.Progress()):
|
|
109 |
|
110 |
progress(0.6, desc="Creating space cards...")
|
111 |
|
112 |
-
#
|
113 |
-
html_content = """
|
114 |
<div style='padding: 20px; background: #f5f5f5;'>
|
|
|
115 |
<div style='display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px;'>
|
116 |
"""
|
117 |
|
@@ -149,12 +159,10 @@ def get_trending_spaces(progress=gr.Progress()):
|
|
149 |
</a>
|
150 |
</div>
|
151 |
"""
|
152 |
-
progress((0.6 + 0.3 * spaces.index(space)/len(spaces)),
|
153 |
-
desc=f"Loading space {spaces.index(space)+1}/{len(spaces)}...")
|
154 |
|
155 |
html_content += "</div></div>"
|
156 |
|
157 |
-
#
|
158 |
df = pd.DataFrame([{
|
159 |
'Rank': space.get('rank', 'N/A'),
|
160 |
'Space ID': space.get('id', ''),
|
@@ -171,7 +179,8 @@ def get_trending_spaces(progress=gr.Progress()):
|
|
171 |
error_plot = create_error_plot()
|
172 |
return error_plot, error_html, pd.DataFrame()
|
173 |
|
174 |
-
|
|
|
175 |
|
176 |
def create_trend_visualization(spaces_data):
|
177 |
if not spaces_data:
|
@@ -379,35 +388,43 @@ def refresh_data():
|
|
379 |
else:
|
380 |
return create_error_plot(), "<div>API μΈμ¦μ΄ νμν©λλ€.</div>", pd.DataFrame()
|
381 |
|
382 |
-
# Gradio μΈν°νμ΄μ€ μμ±
|
383 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
384 |
gr.Markdown("""
|
385 |
-
# π€ HuggingFace Spaces
|
386 |
-
μ€μκ°μΌλ‘ Hugging Face Spacesμ
|
387 |
""")
|
388 |
|
389 |
-
with gr.Tab("Trending
|
390 |
-
|
391 |
-
|
|
|
392 |
|
393 |
-
with gr.Tab("
|
394 |
-
|
|
|
|
|
395 |
|
396 |
refresh_btn = gr.Button("π Refresh Data", variant="primary")
|
397 |
|
398 |
def refresh_data():
|
399 |
-
|
|
|
|
|
400 |
|
401 |
refresh_btn.click(
|
402 |
refresh_data,
|
403 |
-
outputs=[
|
|
|
|
|
|
|
404 |
)
|
405 |
|
406 |
# μ΄κΈ° λ°μ΄ν° λ‘λ
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
|
|
|
411 |
|
412 |
# Gradio μ± μ€ν
|
413 |
demo.launch(
|
|
|
42 |
"NCSOFT/VARCO_Arena": "https://huggingface.co/spaces/NCSOFT/VARCO_Arena"
|
43 |
}
|
44 |
|
45 |
+
def get_spaces_data(sort_type="trending", progress=gr.Progress()):
|
46 |
+
"""μ€νμ΄μ€ λ°μ΄ν° κ°μ Έμ€κΈ° (trending λλ modes)"""
|
47 |
+
url = f"https://huggingface.co/api/spaces"
|
48 |
+
params = {
|
49 |
+
'full': 'true',
|
50 |
+
'limit': 300
|
51 |
+
}
|
52 |
+
|
53 |
+
if sort_type == "modes":
|
54 |
+
params['sort'] = 'likes' # modesλ μ’μμ μμΌλ‘ μ λ ¬
|
55 |
|
56 |
try:
|
57 |
+
progress(0, desc=f"Fetching {sort_type} spaces data...")
|
|
|
|
|
|
|
|
|
|
|
58 |
response = requests.get(url, params=params)
|
59 |
response.raise_for_status()
|
60 |
all_spaces = response.json()
|
61 |
|
62 |
+
# μμ μ 보 μ μ₯
|
63 |
space_ranks = {space['id']: idx + 1 for idx, space in enumerate(all_spaces)}
|
64 |
|
65 |
+
# target_spaces νν°λ§ λ° μμ μ 보 ν¬ν¨
|
66 |
spaces = []
|
67 |
for space in all_spaces:
|
68 |
if space.get('id', '') in target_spaces:
|
|
|
74 |
|
75 |
progress(0.3, desc="Creating visualization...")
|
76 |
|
77 |
+
# μκ°ν μμ±
|
78 |
fig = go.Figure()
|
79 |
|
80 |
+
# λ°μ΄ν° μ€λΉ
|
81 |
ids = [space['id'] for space in spaces]
|
82 |
ranks = [space['rank'] for space in spaces]
|
83 |
likes = [space.get('likes', 0) for space in spaces]
|
84 |
|
85 |
+
# YμΆ κ°μ λ°μ (300 - rank + 1)
|
86 |
+
y_values = [301 - r for r in ranks] # μμλ₯Ό λ°μ λ κ°μΌλ‘ λ³ν
|
87 |
+
|
88 |
# λ§λ κ·Έλν μμ±
|
89 |
fig.add_trace(go.Bar(
|
90 |
x=ids,
|
91 |
+
y=y_values,
|
92 |
text=[f"Rank: {r}<br>Likes: {l}" for r, l in zip(ranks, likes)],
|
93 |
textposition='auto',
|
94 |
marker_color='rgb(158,202,225)',
|
|
|
97 |
|
98 |
fig.update_layout(
|
99 |
title={
|
100 |
+
'text': f'Hugging Face Spaces {sort_type.title()} Rankings (Top 300)',
|
101 |
'y':0.95,
|
102 |
'x':0.5,
|
103 |
'xanchor': 'center',
|
|
|
105 |
},
|
106 |
xaxis_title='Space ID',
|
107 |
yaxis_title='Rank',
|
108 |
+
yaxis=dict(
|
109 |
+
ticktext=[str(i) for i in range(1, 301, 20)], # 1λΆν° 300κΉμ§ 20 κ°κ²©
|
110 |
+
tickvals=[301 - i for i in range(1, 301, 20)], # λ°μ λ κ°
|
111 |
+
range=[0, 300] # yμΆ λ²μ μ€μ
|
112 |
+
),
|
113 |
height=800,
|
114 |
showlegend=False,
|
115 |
template='plotly_white',
|
|
|
118 |
|
119 |
progress(0.6, desc="Creating space cards...")
|
120 |
|
121 |
+
# HTML μΉ΄λ μμ±
|
122 |
+
html_content = f"""
|
123 |
<div style='padding: 20px; background: #f5f5f5;'>
|
124 |
+
<h2 style='color: #2c3e50;'>{sort_type.title()} Rankings</h2>
|
125 |
<div style='display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px;'>
|
126 |
"""
|
127 |
|
|
|
159 |
</a>
|
160 |
</div>
|
161 |
"""
|
|
|
|
|
162 |
|
163 |
html_content += "</div></div>"
|
164 |
|
165 |
+
# λ°μ΄ν°νλ μ μμ±
|
166 |
df = pd.DataFrame([{
|
167 |
'Rank': space.get('rank', 'N/A'),
|
168 |
'Space ID': space.get('id', ''),
|
|
|
179 |
error_plot = create_error_plot()
|
180 |
return error_plot, error_html, pd.DataFrame()
|
181 |
|
182 |
+
|
183 |
+
|
184 |
|
185 |
def create_trend_visualization(spaces_data):
|
186 |
if not spaces_data:
|
|
|
388 |
else:
|
389 |
return create_error_plot(), "<div>API μΈμ¦μ΄ νμν©λλ€.</div>", pd.DataFrame()
|
390 |
|
|
|
391 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
392 |
gr.Markdown("""
|
393 |
+
# π€ HuggingFace Spaces Rankings Analysis
|
394 |
+
μ€μκ°μΌλ‘ Hugging Face Spacesμ μμλ₯Ό λΆμν©λλ€.
|
395 |
""")
|
396 |
|
397 |
+
with gr.Tab("Trending Rankings"):
|
398 |
+
trending_plot = gr.Plot()
|
399 |
+
trending_info = gr.HTML()
|
400 |
+
trending_df = gr.DataFrame()
|
401 |
|
402 |
+
with gr.Tab("Most Liked Rankings"):
|
403 |
+
modes_plot = gr.Plot()
|
404 |
+
modes_info = gr.HTML()
|
405 |
+
modes_df = gr.DataFrame()
|
406 |
|
407 |
refresh_btn = gr.Button("π Refresh Data", variant="primary")
|
408 |
|
409 |
def refresh_data():
|
410 |
+
trending_results = get_spaces_data("trending")
|
411 |
+
modes_results = get_spaces_data("modes")
|
412 |
+
return [*trending_results, *modes_results]
|
413 |
|
414 |
refresh_btn.click(
|
415 |
refresh_data,
|
416 |
+
outputs=[
|
417 |
+
trending_plot, trending_info, trending_df,
|
418 |
+
modes_plot, modes_info, modes_df
|
419 |
+
]
|
420 |
)
|
421 |
|
422 |
# μ΄κΈ° λ°μ΄ν° λ‘λ
|
423 |
+
trending_results = get_spaces_data("trending")
|
424 |
+
modes_results = get_spaces_data("modes")
|
425 |
+
|
426 |
+
trending_plot.value, trending_info.value, trending_df.value = trending_results
|
427 |
+
modes_plot.value, modes_info.value, modes_df.value = modes_results
|
428 |
|
429 |
# Gradio μ± μ€ν
|
430 |
demo.launch(
|