Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -45,7 +45,7 @@ target_spaces = {
|
|
45 |
def get_trending_spaces():
|
46 |
try:
|
47 |
# API μλν¬μΈνΈ μμ
|
48 |
-
url = "https://huggingface.co/api/spaces
|
49 |
|
50 |
headers = {
|
51 |
'Authorization': f'Bearer {HF_TOKEN}',
|
@@ -53,7 +53,9 @@ def get_trending_spaces():
|
|
53 |
}
|
54 |
|
55 |
params = {
|
56 |
-
'
|
|
|
|
|
57 |
'full': 'true'
|
58 |
}
|
59 |
|
@@ -62,32 +64,45 @@ def get_trending_spaces():
|
|
62 |
if response.status_code == 200:
|
63 |
return response.json()
|
64 |
else:
|
|
|
65 |
print(f"API μμ² μ€ν¨: {response.status_code}")
|
66 |
print(f"Response: {response.text}")
|
67 |
-
print(f"URL: {url}")
|
68 |
print(f"Headers: {headers}")
|
|
|
69 |
return None
|
70 |
except Exception as e:
|
71 |
print(f"API νΈμΆ μ€ μλ¬ λ°μ: {str(e)}")
|
72 |
return None
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
|
|
91 |
def create_trend_visualization(spaces_data):
|
92 |
if not spaces_data:
|
93 |
return create_error_plot()
|
@@ -102,7 +117,8 @@ def create_trend_visualization(spaces_data):
|
|
102 |
ranks.append({
|
103 |
'id': space_id,
|
104 |
'rank': idx,
|
105 |
-
'likes': space.get('likes', 0)
|
|
|
106 |
})
|
107 |
|
108 |
if not ranks:
|
@@ -115,20 +131,22 @@ def create_trend_visualization(spaces_data):
|
|
115 |
ids = [r['id'] for r in ranks]
|
116 |
rank_values = [r['rank'] for r in ranks]
|
117 |
likes = [r['likes'] for r in ranks]
|
|
|
118 |
|
119 |
-
# λ§λ κ·Έλν μμ±
|
120 |
fig.add_trace(go.Bar(
|
121 |
x=ids,
|
122 |
y=rank_values,
|
123 |
-
text=[f"Rank: {r}<br>Likes: {l}" for r, l in zip(rank_values, likes)],
|
124 |
textposition='auto',
|
125 |
marker_color='rgb(158,202,225)',
|
126 |
-
opacity=0.8
|
|
|
127 |
))
|
128 |
|
129 |
fig.update_layout(
|
130 |
title={
|
131 |
-
'text': '
|
132 |
'y':0.95,
|
133 |
'x':0.5,
|
134 |
'xanchor': 'center',
|
@@ -137,13 +155,34 @@ def create_trend_visualization(spaces_data):
|
|
137 |
xaxis_title='Space ID',
|
138 |
yaxis_title='Rank',
|
139 |
yaxis_autorange='reversed',
|
140 |
-
height=
|
141 |
showlegend=False,
|
142 |
-
template='plotly_white'
|
|
|
|
|
143 |
)
|
144 |
|
145 |
return fig
|
146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
def create_space_info_html(spaces_data):
|
148 |
if not spaces_data:
|
149 |
return "<div style='padding: 20px;'><h2>λ°μ΄ν°λ₯Ό λΆλ¬μ€λλ° μ€ν¨νμ΅λλ€.</h2></div>"
|
|
|
45 |
def get_trending_spaces():
|
46 |
try:
|
47 |
# API μλν¬μΈνΈ μμ
|
48 |
+
url = "https://huggingface.co/api/spaces" # κΈ°λ³Έ URL
|
49 |
|
50 |
headers = {
|
51 |
'Authorization': f'Bearer {HF_TOKEN}',
|
|
|
53 |
}
|
54 |
|
55 |
params = {
|
56 |
+
'sort': 'likes', # 'trending' λμ 'likes'λ‘ λ³κ²½
|
57 |
+
'direction': -1, # λ΄λ¦Όμ°¨μ μ λ ¬
|
58 |
+
'limit': 1000,
|
59 |
'full': 'true'
|
60 |
}
|
61 |
|
|
|
64 |
if response.status_code == 200:
|
65 |
return response.json()
|
66 |
else:
|
67 |
+
# μμΈν λλ²κΉ
μ 보 μΆλ ₯
|
68 |
print(f"API μμ² μ€ν¨: {response.status_code}")
|
69 |
print(f"Response: {response.text}")
|
70 |
+
print(f"URL: {response.url}") # μ€μ μμ²λ URL μΆλ ₯
|
71 |
print(f"Headers: {headers}")
|
72 |
+
print(f"Params: {params}")
|
73 |
return None
|
74 |
except Exception as e:
|
75 |
print(f"API νΈμΆ μ€ μλ¬ λ°μ: {str(e)}")
|
76 |
return None
|
77 |
|
78 |
+
# ν ν°μ΄ μλ κ²½μ°λ₯Ό μν λ체 ν¨μ
|
79 |
+
def get_trending_spaces_without_token():
|
80 |
+
try:
|
81 |
+
url = "https://huggingface.co/api/spaces"
|
82 |
+
params = {
|
83 |
+
'sort': 'likes',
|
84 |
+
'direction': -1,
|
85 |
+
'limit': 1000,
|
86 |
+
'full': 'true'
|
87 |
+
}
|
88 |
+
|
89 |
+
response = requests.get(url, params=params)
|
90 |
+
|
91 |
+
if response.status_code == 200:
|
92 |
+
return response.json()
|
93 |
+
else:
|
94 |
+
print(f"API μμ² μ€ν¨ (ν ν° μμ): {response.status_code}")
|
95 |
+
print(f"Response: {response.text}")
|
96 |
+
return None
|
97 |
+
except Exception as e:
|
98 |
+
print(f"API νΈμΆ μ€ μλ¬ λ°μ (ν ν° μμ): {str(e)}")
|
99 |
+
return None
|
100 |
+
|
101 |
+
# API ν ν° μ€μ λ° ν¨μ μ ν
|
102 |
+
if not HF_TOKEN:
|
103 |
+
get_trending_spaces = get_trending_spaces_without_token
|
104 |
|
105 |
+
# create_trend_visualization ν¨μ μμ
|
106 |
def create_trend_visualization(spaces_data):
|
107 |
if not spaces_data:
|
108 |
return create_error_plot()
|
|
|
117 |
ranks.append({
|
118 |
'id': space_id,
|
119 |
'rank': idx,
|
120 |
+
'likes': space.get('likes', 0),
|
121 |
+
'title': space.get('title', 'N/A')
|
122 |
})
|
123 |
|
124 |
if not ranks:
|
|
|
131 |
ids = [r['id'] for r in ranks]
|
132 |
rank_values = [r['rank'] for r in ranks]
|
133 |
likes = [r['likes'] for r in ranks]
|
134 |
+
titles = [r['title'] for r in ranks]
|
135 |
|
136 |
+
# λ§λ κ·Έλν μμ± (κ°μ λ μ€νμΌ)
|
137 |
fig.add_trace(go.Bar(
|
138 |
x=ids,
|
139 |
y=rank_values,
|
140 |
+
text=[f"Rank: {r}<br>Likes: {l}<br>{t}" for r, l, t in zip(rank_values, likes, titles)],
|
141 |
textposition='auto',
|
142 |
marker_color='rgb(158,202,225)',
|
143 |
+
opacity=0.8,
|
144 |
+
hoverinfo='text'
|
145 |
))
|
146 |
|
147 |
fig.update_layout(
|
148 |
title={
|
149 |
+
'text': 'Hugging Face Spaces Rankings',
|
150 |
'y':0.95,
|
151 |
'x':0.5,
|
152 |
'xanchor': 'center',
|
|
|
155 |
xaxis_title='Space ID',
|
156 |
yaxis_title='Rank',
|
157 |
yaxis_autorange='reversed',
|
158 |
+
height=800,
|
159 |
showlegend=False,
|
160 |
+
template='plotly_white',
|
161 |
+
margin=dict(l=50, r=50, t=100, b=100),
|
162 |
+
xaxis_tickangle=-45
|
163 |
)
|
164 |
|
165 |
return fig
|
166 |
|
167 |
+
def create_error_plot():
|
168 |
+
fig = go.Figure()
|
169 |
+
fig.add_annotation(
|
170 |
+
text="λ°μ΄ν°λ₯Ό λΆλ¬μ¬ μ μμ΅λλ€.\n(API μΈμ¦μ΄ νμν©λλ€)",
|
171 |
+
xref="paper",
|
172 |
+
yref="paper",
|
173 |
+
x=0.5,
|
174 |
+
y=0.5,
|
175 |
+
showarrow=False,
|
176 |
+
font=dict(size=20)
|
177 |
+
)
|
178 |
+
fig.update_layout(
|
179 |
+
title="Error Loading Data",
|
180 |
+
height=400
|
181 |
+
)
|
182 |
+
return fig
|
183 |
+
|
184 |
+
|
185 |
+
|
186 |
def create_space_info_html(spaces_data):
|
187 |
if not spaces_data:
|
188 |
return "<div style='padding: 20px;'><h2>λ°μ΄ν°λ₯Ό λΆλ¬μ€λλ° μ€ν¨νμ΅λλ€.</h2></div>"
|