Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
Update app.py
Browse files
app.py
CHANGED
@@ -2,8 +2,10 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
import pandas as pd
|
4 |
import plotly.graph_objects as go
|
5 |
-
from datetime import datetime
|
6 |
-
import
|
|
|
|
|
7 |
|
8 |
# κ΄μ¬ μ€νμ΄μ€ URL 리μ€νΈμ μ 보
|
9 |
target_spaces = {
|
@@ -44,8 +46,8 @@ def get_trending_spaces():
|
|
44 |
try:
|
45 |
url = "https://huggingface.co/api/spaces/sort/trending"
|
46 |
headers = {
|
47 |
-
'
|
48 |
-
'
|
49 |
}
|
50 |
response = requests.get(url, headers=headers)
|
51 |
|
@@ -62,7 +64,7 @@ def get_trending_spaces():
|
|
62 |
def create_error_plot():
|
63 |
fig = go.Figure()
|
64 |
fig.add_annotation(
|
65 |
-
text="λ°μ΄ν°λ₯Ό λΆλ¬μ¬ μ μμ΅λλ€.\n
|
66 |
xref="paper",
|
67 |
yref="paper",
|
68 |
x=0.5,
|
@@ -93,6 +95,9 @@ def create_trend_visualization(spaces_data):
|
|
93 |
'likes': space.get('likes', 0)
|
94 |
})
|
95 |
|
|
|
|
|
|
|
96 |
# μμλ³λ‘ μ λ ¬
|
97 |
ranks.sort(key=lambda x: x['rank'])
|
98 |
|
@@ -101,21 +106,30 @@ def create_trend_visualization(spaces_data):
|
|
101 |
rank_values = [r['rank'] for r in ranks]
|
102 |
likes = [r['likes'] for r in ranks]
|
103 |
|
|
|
104 |
fig.add_trace(go.Bar(
|
105 |
x=ids,
|
106 |
y=rank_values,
|
107 |
-
text=likes,
|
108 |
textposition='auto',
|
109 |
-
|
|
|
110 |
))
|
111 |
|
112 |
fig.update_layout(
|
113 |
-
title=
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
xaxis_title='Space ID',
|
115 |
yaxis_title='Rank',
|
116 |
yaxis_autorange='reversed',
|
117 |
height=600,
|
118 |
-
showlegend=False
|
|
|
119 |
)
|
120 |
|
121 |
return fig
|
@@ -124,23 +138,46 @@ def create_space_info_html(spaces_data):
|
|
124 |
if not spaces_data:
|
125 |
return "<div style='padding: 20px;'><h2>λ°μ΄ν°λ₯Ό λΆλ¬μ€λλ° μ€ν¨νμ΅λλ€.</h2></div>"
|
126 |
|
127 |
-
html_content = "
|
128 |
-
|
|
|
|
|
|
|
129 |
|
130 |
for idx, space in enumerate(spaces_data, 1):
|
131 |
space_id = space.get('id', '')
|
132 |
if space_id in target_spaces:
|
133 |
html_content += f"""
|
134 |
-
<div style='
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
</div>
|
141 |
"""
|
142 |
|
143 |
-
html_content += "</div>"
|
144 |
return html_content
|
145 |
|
146 |
def create_data_table(spaces_data):
|
@@ -169,11 +206,14 @@ def refresh_data():
|
|
169 |
df = create_data_table(spaces_data)
|
170 |
return plot, info, df
|
171 |
else:
|
172 |
-
return create_error_plot(), "<div
|
173 |
|
174 |
# Gradio μΈν°νμ΄μ€ μμ±
|
175 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
176 |
-
gr.Markdown("
|
|
|
|
|
|
|
177 |
|
178 |
with gr.Tab("Trending Analysis"):
|
179 |
plot_output = gr.Plot()
|
@@ -182,7 +222,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
182 |
with gr.Tab("Export Data"):
|
183 |
df_output = gr.DataFrame()
|
184 |
|
185 |
-
refresh_btn = gr.Button("Refresh Data")
|
186 |
refresh_btn.click(
|
187 |
refresh_data,
|
188 |
outputs=[plot_output, info_output, df_output]
|
|
|
2 |
import requests
|
3 |
import pandas as pd
|
4 |
import plotly.graph_objects as go
|
5 |
+
from datetime import datetime
|
6 |
+
import os
|
7 |
+
|
8 |
+
HF_TOKEN = os.getenv('HF_TOKEN')
|
9 |
|
10 |
# κ΄μ¬ μ€νμ΄μ€ URL 리μ€νΈμ μ 보
|
11 |
target_spaces = {
|
|
|
46 |
try:
|
47 |
url = "https://huggingface.co/api/spaces/sort/trending"
|
48 |
headers = {
|
49 |
+
'Authorization': f'Bearer {HF_TOKEN}',
|
50 |
+
'Accept': 'application/json'
|
51 |
}
|
52 |
response = requests.get(url, headers=headers)
|
53 |
|
|
|
64 |
def create_error_plot():
|
65 |
fig = go.Figure()
|
66 |
fig.add_annotation(
|
67 |
+
text="λ°μ΄ν°λ₯Ό λΆλ¬μ¬ μ μμ΅λλ€.\n(API μΈμ¦μ΄ νμν©λλ€)",
|
68 |
xref="paper",
|
69 |
yref="paper",
|
70 |
x=0.5,
|
|
|
95 |
'likes': space.get('likes', 0)
|
96 |
})
|
97 |
|
98 |
+
if not ranks:
|
99 |
+
return create_error_plot()
|
100 |
+
|
101 |
# μμλ³λ‘ μ λ ¬
|
102 |
ranks.sort(key=lambda x: x['rank'])
|
103 |
|
|
|
106 |
rank_values = [r['rank'] for r in ranks]
|
107 |
likes = [r['likes'] for r in ranks]
|
108 |
|
109 |
+
# λ§λ κ·Έλν μμ±
|
110 |
fig.add_trace(go.Bar(
|
111 |
x=ids,
|
112 |
y=rank_values,
|
113 |
+
text=[f"Rank: {r}<br>Likes: {l}" for r, l in zip(rank_values, likes)],
|
114 |
textposition='auto',
|
115 |
+
marker_color='rgb(158,202,225)',
|
116 |
+
opacity=0.8
|
117 |
))
|
118 |
|
119 |
fig.update_layout(
|
120 |
+
title={
|
121 |
+
'text': 'Current Trending Ranks',
|
122 |
+
'y':0.95,
|
123 |
+
'x':0.5,
|
124 |
+
'xanchor': 'center',
|
125 |
+
'yanchor': 'top'
|
126 |
+
},
|
127 |
xaxis_title='Space ID',
|
128 |
yaxis_title='Rank',
|
129 |
yaxis_autorange='reversed',
|
130 |
height=600,
|
131 |
+
showlegend=False,
|
132 |
+
template='plotly_white'
|
133 |
)
|
134 |
|
135 |
return fig
|
|
|
138 |
if not spaces_data:
|
139 |
return "<div style='padding: 20px;'><h2>λ°μ΄ν°λ₯Ό λΆλ¬μ€λλ° μ€ν¨νμ΅λλ€.</h2></div>"
|
140 |
|
141 |
+
html_content = """
|
142 |
+
<div style='padding: 20px;'>
|
143 |
+
<h2 style='color: #2c3e50;'>Current Rankings</h2>
|
144 |
+
<div style='display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px;'>
|
145 |
+
"""
|
146 |
|
147 |
for idx, space in enumerate(spaces_data, 1):
|
148 |
space_id = space.get('id', '')
|
149 |
if space_id in target_spaces:
|
150 |
html_content += f"""
|
151 |
+
<div style='
|
152 |
+
background: white;
|
153 |
+
padding: 20px;
|
154 |
+
border-radius: 10px;
|
155 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
156 |
+
transition: transform 0.2s;
|
157 |
+
hover: transform: translateY(-5px);
|
158 |
+
'>
|
159 |
+
<h3 style='color: #34495e;'>#{idx} - {space_id}</h3>
|
160 |
+
<p style='color: #7f8c8d;'>π Likes: {space.get('likes', 'N/A')}</p>
|
161 |
+
<p style='color: #2c3e50;'>{space.get('title', 'N/A')}</p>
|
162 |
+
<p style='color: #7f8c8d; font-size: 0.9em;'>{space.get('description', 'N/A')[:100]}...</p>
|
163 |
+
<a href='{target_spaces[space_id]}'
|
164 |
+
target='_blank'
|
165 |
+
style='
|
166 |
+
display: inline-block;
|
167 |
+
padding: 8px 16px;
|
168 |
+
background: #3498db;
|
169 |
+
color: white;
|
170 |
+
text-decoration: none;
|
171 |
+
border-radius: 5px;
|
172 |
+
transition: background 0.3s;
|
173 |
+
hover: background: #2980b9;
|
174 |
+
'>
|
175 |
+
Visit Space π
|
176 |
+
</a>
|
177 |
</div>
|
178 |
"""
|
179 |
|
180 |
+
html_content += "</div></div>"
|
181 |
return html_content
|
182 |
|
183 |
def create_data_table(spaces_data):
|
|
|
206 |
df = create_data_table(spaces_data)
|
207 |
return plot, info, df
|
208 |
else:
|
209 |
+
return create_error_plot(), "<div>API μΈμ¦μ΄ νμν©λλ€.</div>", pd.DataFrame()
|
210 |
|
211 |
# Gradio μΈν°νμ΄μ€ μμ±
|
212 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
213 |
+
gr.Markdown("""
|
214 |
+
# π€ HuggingFace Spaces Trending Analysis
|
215 |
+
μ€μκ°μΌλ‘ Hugging Face Spacesμ νΈλ λ© μμλ₯Ό λΆμν©λλ€.
|
216 |
+
""")
|
217 |
|
218 |
with gr.Tab("Trending Analysis"):
|
219 |
plot_output = gr.Plot()
|
|
|
222 |
with gr.Tab("Export Data"):
|
223 |
df_output = gr.DataFrame()
|
224 |
|
225 |
+
refresh_btn = gr.Button("π Refresh Data", variant="primary")
|
226 |
refresh_btn.click(
|
227 |
refresh_data,
|
228 |
outputs=[plot_output, info_output, df_output]
|