Update app.py
Browse files
app.py
CHANGED
|
@@ -44,72 +44,47 @@ target_spaces = {
|
|
| 44 |
|
| 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}',
|
| 52 |
-
'Accept': 'application/json'
|
|
|
|
| 53 |
}
|
| 54 |
|
|
|
|
| 55 |
params = {
|
| 56 |
-
'
|
| 57 |
-
'
|
| 58 |
-
'limit': 1000,
|
| 59 |
-
'full': 'true'
|
| 60 |
}
|
| 61 |
|
| 62 |
response = requests.get(url, headers=headers, params=params)
|
| 63 |
|
| 64 |
if response.status_code == 200:
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|
| 109 |
|
| 110 |
fig = go.Figure()
|
| 111 |
|
| 112 |
-
#
|
| 113 |
ranks = []
|
| 114 |
for idx, space in enumerate(spaces_data, 1):
|
| 115 |
space_id = space.get('id', '')
|
|
@@ -118,7 +93,8 @@ def create_trend_visualization(spaces_data):
|
|
| 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,39 +107,66 @@ def create_trend_visualization(spaces_data):
|
|
| 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 |
-
|
| 135 |
|
| 136 |
-
# λ§λ κ·Έλν μμ±
|
| 137 |
fig.add_trace(go.Bar(
|
| 138 |
x=ids,
|
| 139 |
y=rank_values,
|
| 140 |
-
text=[f"Rank: {r}<br>Likes: {l}<br>{
|
| 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': '
|
| 150 |
'y':0.95,
|
| 151 |
'x':0.5,
|
| 152 |
'xanchor': 'center',
|
| 153 |
'yanchor': 'top'
|
| 154 |
},
|
| 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(
|
|
@@ -182,20 +185,21 @@ def create_error_plot():
|
|
| 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>"
|
| 189 |
|
| 190 |
html_content = """
|
| 191 |
<div style='padding: 20px;'>
|
| 192 |
-
<h2 style='color: #2c3e50;'>Current Rankings</h2>
|
| 193 |
<div style='display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px;'>
|
| 194 |
"""
|
| 195 |
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
if space_id
|
|
|
|
|
|
|
| 199 |
html_content += f"""
|
| 200 |
<div style='
|
| 201 |
background: white;
|
|
@@ -203,12 +207,12 @@ def create_space_info_html(spaces_data):
|
|
| 203 |
border-radius: 10px;
|
| 204 |
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 205 |
transition: transform 0.2s;
|
| 206 |
-
hover: transform: translateY(-5px);
|
| 207 |
'>
|
| 208 |
-
<h3 style='color: #34495e;'>#{
|
| 209 |
-
<p style='color: #7f8c8d;'>π Likes: {
|
| 210 |
-
<p style='color: #
|
| 211 |
-
<p style='color: #
|
|
|
|
| 212 |
<a href='{target_spaces[space_id]}'
|
| 213 |
target='_blank'
|
| 214 |
style='
|
|
@@ -219,7 +223,30 @@ def create_space_info_html(spaces_data):
|
|
| 219 |
text-decoration: none;
|
| 220 |
border-radius: 5px;
|
| 221 |
transition: background 0.3s;
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
'>
|
| 224 |
Visit Space π
|
| 225 |
</a>
|
|
|
|
| 44 |
|
| 45 |
def get_trending_spaces():
|
| 46 |
try:
|
| 47 |
+
# νΈλ λ© API μλν¬μΈνΈ μμ
|
| 48 |
+
url = "https://huggingface.co/api/spaces/trending"
|
| 49 |
|
| 50 |
headers = {
|
| 51 |
'Authorization': f'Bearer {HF_TOKEN}',
|
| 52 |
+
'Accept': 'application/json',
|
| 53 |
+
'User-Agent': 'Mozilla/5.0'
|
| 54 |
}
|
| 55 |
|
| 56 |
+
# λͺ¨λ κ²°κ³Όλ₯Ό κ°μ Έμ€κΈ° μν΄ limit μ¦κ°
|
| 57 |
params = {
|
| 58 |
+
'limit': 5000, # μ΅λν λ§μ κ²°κ³Όλ₯Ό κ°μ Έμ€κΈ°
|
| 59 |
+
'interval': 'day' # μΌκ° νΈλ λ©
|
|
|
|
|
|
|
| 60 |
}
|
| 61 |
|
| 62 |
response = requests.get(url, headers=headers, params=params)
|
| 63 |
|
| 64 |
if response.status_code == 200:
|
| 65 |
+
data = response.json()
|
| 66 |
+
# target_spacesμ μλ μ€νμ΄μ€λ§ νν°λ§
|
| 67 |
+
filtered_data = []
|
| 68 |
+
for space in data:
|
| 69 |
+
space_id = space.get('id', '')
|
| 70 |
+
if space_id in target_spaces:
|
| 71 |
+
filtered_data.append(space)
|
| 72 |
+
return filtered_data
|
| 73 |
else:
|
|
|
|
| 74 |
print(f"API μμ² μ€ν¨: {response.status_code}")
|
| 75 |
print(f"Response: {response.text}")
|
|
|
|
|
|
|
|
|
|
| 76 |
return None
|
| 77 |
except Exception as e:
|
| 78 |
print(f"API νΈμΆ μ€ μλ¬ λ°μ: {str(e)}")
|
| 79 |
return None
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
def create_trend_visualization(spaces_data):
|
| 82 |
if not spaces_data:
|
| 83 |
return create_error_plot()
|
| 84 |
|
| 85 |
fig = go.Figure()
|
| 86 |
|
| 87 |
+
# μμ λ°μ΄ν° μ€λΉ
|
| 88 |
ranks = []
|
| 89 |
for idx, space in enumerate(spaces_data, 1):
|
| 90 |
space_id = space.get('id', '')
|
|
|
|
| 93 |
'id': space_id,
|
| 94 |
'rank': idx,
|
| 95 |
'likes': space.get('likes', 0),
|
| 96 |
+
'title': space.get('title', 'N/A'),
|
| 97 |
+
'views': space.get('views', 0)
|
| 98 |
})
|
| 99 |
|
| 100 |
if not ranks:
|
|
|
|
| 107 |
ids = [r['id'] for r in ranks]
|
| 108 |
rank_values = [r['rank'] for r in ranks]
|
| 109 |
likes = [r['likes'] for r in ranks]
|
| 110 |
+
views = [r['views'] for r in ranks]
|
| 111 |
|
| 112 |
+
# λ§λ κ·Έλν μμ±
|
| 113 |
fig.add_trace(go.Bar(
|
| 114 |
x=ids,
|
| 115 |
y=rank_values,
|
| 116 |
+
text=[f"Rank: {r}<br>Likes: {l}<br>Views: {v}" for r, l, v in zip(rank_values, likes, views)],
|
| 117 |
textposition='auto',
|
| 118 |
marker_color='rgb(158,202,225)',
|
| 119 |
+
opacity=0.8
|
|
|
|
| 120 |
))
|
| 121 |
|
| 122 |
fig.update_layout(
|
| 123 |
title={
|
| 124 |
+
'text': 'Current Trending Ranks (All Target Spaces)',
|
| 125 |
'y':0.95,
|
| 126 |
'x':0.5,
|
| 127 |
'xanchor': 'center',
|
| 128 |
'yanchor': 'top'
|
| 129 |
},
|
| 130 |
xaxis_title='Space ID',
|
| 131 |
+
yaxis_title='Trending Rank',
|
| 132 |
yaxis_autorange='reversed',
|
| 133 |
height=800,
|
| 134 |
showlegend=False,
|
| 135 |
template='plotly_white',
|
|
|
|
| 136 |
xaxis_tickangle=-45
|
| 137 |
)
|
| 138 |
|
| 139 |
return fig
|
| 140 |
|
| 141 |
+
# ν ν°μ΄ μλ κ²½μ°λ₯Ό μν λ체 ν¨μ
|
| 142 |
+
def get_trending_spaces_without_token():
|
| 143 |
+
try:
|
| 144 |
+
url = "https://huggingface.co/api/spaces"
|
| 145 |
+
params = {
|
| 146 |
+
'sort': 'likes',
|
| 147 |
+
'direction': -1,
|
| 148 |
+
'limit': 1000,
|
| 149 |
+
'full': 'true'
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
response = requests.get(url, params=params)
|
| 153 |
+
|
| 154 |
+
if response.status_code == 200:
|
| 155 |
+
return response.json()
|
| 156 |
+
else:
|
| 157 |
+
print(f"API μμ² μ€ν¨ (ν ν° μμ): {response.status_code}")
|
| 158 |
+
print(f"Response: {response.text}")
|
| 159 |
+
return None
|
| 160 |
+
except Exception as e:
|
| 161 |
+
print(f"API νΈμΆ μ€ μλ¬ λ°μ (ν ν° μμ): {str(e)}")
|
| 162 |
+
return None
|
| 163 |
+
|
| 164 |
+
# API ν ν° μ€μ λ° ν¨μ μ ν
|
| 165 |
+
if not HF_TOKEN:
|
| 166 |
+
get_trending_spaces = get_trending_spaces_without_token
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
|
| 170 |
def create_error_plot():
|
| 171 |
fig = go.Figure()
|
| 172 |
fig.add_annotation(
|
|
|
|
| 185 |
return fig
|
| 186 |
|
| 187 |
|
|
|
|
| 188 |
def create_space_info_html(spaces_data):
|
| 189 |
if not spaces_data:
|
| 190 |
return "<div style='padding: 20px;'><h2>λ°μ΄ν°λ₯Ό λΆλ¬μ€λλ° μ€ν¨νμ΅λλ€.</h2></div>"
|
| 191 |
|
| 192 |
html_content = """
|
| 193 |
<div style='padding: 20px;'>
|
| 194 |
+
<h2 style='color: #2c3e50;'>Current Trending Rankings</h2>
|
| 195 |
<div style='display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px;'>
|
| 196 |
"""
|
| 197 |
|
| 198 |
+
# λͺ¨λ target spacesλ₯Ό ν¬ν¨νλλ‘ μμ
|
| 199 |
+
for space_id in target_spaces.keys():
|
| 200 |
+
space_info = next((s for s in spaces_data if s.get('id') == space_id), None)
|
| 201 |
+
if space_info:
|
| 202 |
+
rank = next((idx for idx, s in enumerate(spaces_data, 1) if s.get('id') == space_id), 'N/A')
|
| 203 |
html_content += f"""
|
| 204 |
<div style='
|
| 205 |
background: white;
|
|
|
|
| 207 |
border-radius: 10px;
|
| 208 |
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 209 |
transition: transform 0.2s;
|
|
|
|
| 210 |
'>
|
| 211 |
+
<h3 style='color: #34495e;'>#{rank} - {space_id}</h3>
|
| 212 |
+
<p style='color: #7f8c8d;'>π Likes: {space_info.get('likes', 'N/A')}</p>
|
| 213 |
+
<p style='color: #7f8c8d;'>π Views: {space_info.get('views', 'N/A')}</p>
|
| 214 |
+
<p style='color: #2c3e50;'>{space_info.get('title', 'N/A')}</p>
|
| 215 |
+
<p style='color: #7f8c8d; font-size: 0.9em;'>{space_info.get('description', 'N/A')[:100]}...</p>
|
| 216 |
<a href='{target_spaces[space_id]}'
|
| 217 |
target='_blank'
|
| 218 |
style='
|
|
|
|
| 223 |
text-decoration: none;
|
| 224 |
border-radius: 5px;
|
| 225 |
transition: background 0.3s;
|
| 226 |
+
'>
|
| 227 |
+
Visit Space π
|
| 228 |
+
</a>
|
| 229 |
+
</div>
|
| 230 |
+
"""
|
| 231 |
+
else:
|
| 232 |
+
html_content += f"""
|
| 233 |
+
<div style='
|
| 234 |
+
background: #f8f9fa;
|
| 235 |
+
padding: 20px;
|
| 236 |
+
border-radius: 10px;
|
| 237 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 238 |
+
'>
|
| 239 |
+
<h3 style='color: #34495e;'>{space_id}</h3>
|
| 240 |
+
<p style='color: #7f8c8d;'>Not in trending</p>
|
| 241 |
+
<a href='{target_spaces[space_id]}'
|
| 242 |
+
target='_blank'
|
| 243 |
+
style='
|
| 244 |
+
display: inline-block;
|
| 245 |
+
padding: 8px 16px;
|
| 246 |
+
background: #95a5a6;
|
| 247 |
+
color: white;
|
| 248 |
+
text-decoration: none;
|
| 249 |
+
border-radius: 5px;
|
| 250 |
'>
|
| 251 |
Visit Space π
|
| 252 |
</a>
|