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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -26
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?sort=trending" # μˆ˜μ •λœ URL
49
 
50
  headers = {
51
  'Authorization': f'Bearer {HF_TOKEN}',
@@ -53,7 +53,9 @@ def get_trending_spaces():
53
  }
54
 
55
  params = {
56
- 'limit': 1000, # 더 λ§Žμ€ κ²°κ³Όλ₯Ό κ°€μ Έμ˜€κΈ° μœ„ν•΄ limit 증가
 
 
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
- def create_error_plot():
75
- fig = go.Figure()
76
- fig.add_annotation(
77
- text="데이터λ₯Ό 뢈러올 수 μ—†μŠ΅λ‹ˆλ‹€.\n(API 인증이 ν•„μš”ν•©λ‹ˆλ‹€)",
78
- xref="paper",
79
- yref="paper",
80
- x=0.5,
81
- y=0.5,
82
- showarrow=False,
83
- font=dict(size=20)
84
- )
85
- fig.update_layout(
86
- title="Error Loading Data",
87
- height=400
88
- )
89
- return fig
 
 
 
 
 
 
 
 
 
 
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': 'Current Trending Ranks',
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=600,
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>"