openfree commited on
Commit
e44e7ed
Β·
verified Β·
1 Parent(s): fb8428f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -61
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" # κΈ°λ³Έ URL
49
 
50
  headers = {
51
  'Authorization': f'Bearer {HF_TOKEN}',
52
- 'Accept': 'application/json'
 
53
  }
54
 
 
55
  params = {
56
- 'sort': 'likes', # 'trending' λŒ€μ‹  'likes'둜 λ³€κ²½
57
- 'direction': -1, # λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬
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
- 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()
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
- 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',
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
- for idx, space in enumerate(spaces_data, 1):
197
- space_id = space.get('id', '')
198
- if space_id in target_spaces:
 
 
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;'>#{idx} - {space_id}</h3>
209
- <p style='color: #7f8c8d;'>πŸ‘ Likes: {space.get('likes', 'N/A')}</p>
210
- <p style='color: #2c3e50;'>{space.get('title', 'N/A')}</p>
211
- <p style='color: #7f8c8d; font-size: 0.9em;'>{space.get('description', 'N/A')[:100]}...</p>
 
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
- hover: background: #2980b9;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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>