openfree commited on
Commit
d0bb13d
Β·
verified Β·
1 Parent(s): cce8fbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -13
app.py CHANGED
@@ -42,7 +42,7 @@ target_spaces = {
42
  "NCSOFT/VARCO_Arena": "https://huggingface.co/spaces/NCSOFT/VARCO_Arena"
43
  }
44
 
45
- def get_trending_spaces(search_query="", sort_by="rank", progress=gr.Progress()):
46
  """νŠΈλ Œλ”© 슀페이슀 κ°€μ Έμ˜€κΈ°"""
47
  url = "https://huggingface.co/api/spaces"
48
 
@@ -50,22 +50,64 @@ def get_trending_spaces(search_query="", sort_by="rank", progress=gr.Progress())
50
  progress(0, desc="Fetching spaces data...")
51
  params = {
52
  'full': 'true',
53
- 'limit': 1000 # μΆ©λΆ„ν•œ 수의 슀페이슀λ₯Ό κ°€μ Έμ˜€κΈ° μœ„ν•΄ 증가
54
  }
55
 
56
  response = requests.get(url, params=params)
57
  response.raise_for_status()
58
  all_spaces = response.json()
59
 
60
- # target_spaces에 μžˆλŠ” 슀페이슀만 필터링
61
- spaces = [space for space in all_spaces if space.get('id', '') in target_spaces]
62
 
63
- progress(0.1, desc="Creating visualization...")
 
 
 
 
 
 
 
 
 
 
64
 
65
  # νŠΈλ Œλ“œ μ‹œκ°ν™” 생성
66
- plot = create_trend_visualization(spaces)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
- progress(0.4, desc="Creating space cards...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
  # 슀페이슀 μΉ΄λ“œ HTML 생성
71
  html_content = """
@@ -73,8 +115,9 @@ def get_trending_spaces(search_query="", sort_by="rank", progress=gr.Progress())
73
  <div style='display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px;'>
74
  """
75
 
76
- for idx, space in enumerate(spaces):
77
  space_id = space.get('id', '')
 
78
  likes = space.get('likes', 0)
79
  title = space.get('title', 'No Title')
80
  description = space.get('description', 'No Description')[:100]
@@ -87,7 +130,7 @@ def get_trending_spaces(search_query="", sort_by="rank", progress=gr.Progress())
87
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
88
  transition: transform 0.2s;
89
  '>
90
- <h3 style='color: #34495e;'>#{idx + 1} - {space_id}</h3>
91
  <p style='color: #7f8c8d;'>πŸ‘ Likes: {likes}</p>
92
  <p style='color: #2c3e50;'>{title}</p>
93
  <p style='color: #7f8c8d; font-size: 0.9em;'>{description}...</p>
@@ -106,21 +149,30 @@ def get_trending_spaces(search_query="", sort_by="rank", progress=gr.Progress())
106
  </a>
107
  </div>
108
  """
109
- progress((0.4 + 0.5 * idx/len(spaces)), desc=f"Loading space {idx+1}/{len(spaces)}...")
110
-
 
111
  html_content += "</div></div>"
112
 
113
  # 데이터 ν…Œμ΄λΈ” 생성
114
- df = create_data_table(spaces)
 
 
 
 
 
 
115
 
116
  progress(1.0, desc="Complete!")
117
- return plot, html_content, df
118
 
119
  except Exception as e:
120
  error_html = f'<div style="color: red; padding: 20px;">Error: {str(e)}</div>'
121
  error_plot = create_error_plot()
122
  return error_plot, error_html, pd.DataFrame()
123
 
 
 
124
  def create_trend_visualization(spaces_data):
125
  if not spaces_data:
126
  return create_error_plot()
 
42
  "NCSOFT/VARCO_Arena": "https://huggingface.co/spaces/NCSOFT/VARCO_Arena"
43
  }
44
 
45
+ def get_trending_spaces(progress=gr.Progress()):
46
  """νŠΈλ Œλ”© 슀페이슀 κ°€μ Έμ˜€κΈ°"""
47
  url = "https://huggingface.co/api/spaces"
48
 
 
50
  progress(0, desc="Fetching spaces data...")
51
  params = {
52
  'full': 'true',
53
+ 'limit': 300 # μƒμœ„ 300개 슀페이슀 κ°€μ Έμ˜€κΈ°
54
  }
55
 
56
  response = requests.get(url, params=params)
57
  response.raise_for_status()
58
  all_spaces = response.json()
59
 
60
+ # λͺ¨λ“  슀페이슀의 μˆœμœ„ 정보 μ €μž₯
61
+ space_ranks = {space['id']: idx + 1 for idx, space in enumerate(all_spaces)}
62
 
63
+ # target_spaces에 μžˆλŠ” 슀페이슀만 ν•„ν„°λ§ν•˜κ³  μ‹€μ œ μˆœμœ„ 정보 포함
64
+ spaces = []
65
+ for space in all_spaces:
66
+ if space.get('id', '') in target_spaces:
67
+ space['rank'] = space_ranks.get(space['id'], 'N/A')
68
+ spaces.append(space)
69
+
70
+ # μˆœμœ„λ³„λ‘œ μ •λ ¬
71
+ spaces.sort(key=lambda x: x['rank'])
72
+
73
+ progress(0.3, desc="Creating visualization...")
74
 
75
  # νŠΈλ Œλ“œ μ‹œκ°ν™” 생성
76
+ fig = go.Figure()
77
+
78
+ # μˆœμœ„ 데이터 μ€€λΉ„
79
+ ids = [space['id'] for space in spaces]
80
+ ranks = [space['rank'] for space in spaces]
81
+ likes = [space.get('likes', 0) for space in spaces]
82
+
83
+ # λ§‰λŒ€ κ·Έλž˜ν”„ 생성
84
+ fig.add_trace(go.Bar(
85
+ x=ids,
86
+ y=ranks,
87
+ text=[f"Rank: {r}<br>Likes: {l}" for r, l in zip(ranks, likes)],
88
+ textposition='auto',
89
+ marker_color='rgb(158,202,225)',
90
+ opacity=0.8
91
+ ))
92
 
93
+ fig.update_layout(
94
+ title={
95
+ 'text': 'Hugging Face Spaces Rankings (Top 300)',
96
+ 'y':0.95,
97
+ 'x':0.5,
98
+ 'xanchor': 'center',
99
+ 'yanchor': 'top'
100
+ },
101
+ xaxis_title='Space ID',
102
+ yaxis_title='Rank',
103
+ yaxis_autorange='reversed', # μˆœμœ„κ°€ μœ„μ—μ„œ μ•„λž˜λ‘œ 내렀가도둝
104
+ height=800,
105
+ showlegend=False,
106
+ template='plotly_white',
107
+ xaxis_tickangle=-45
108
+ )
109
+
110
+ progress(0.6, desc="Creating space cards...")
111
 
112
  # 슀페이슀 μΉ΄λ“œ HTML 생성
113
  html_content = """
 
115
  <div style='display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px;'>
116
  """
117
 
118
+ for space in spaces:
119
  space_id = space.get('id', '')
120
+ rank = space.get('rank', 'N/A')
121
  likes = space.get('likes', 0)
122
  title = space.get('title', 'No Title')
123
  description = space.get('description', 'No Description')[:100]
 
130
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
131
  transition: transform 0.2s;
132
  '>
133
+ <h3 style='color: #34495e;'>Rank #{rank} - {space_id}</h3>
134
  <p style='color: #7f8c8d;'>πŸ‘ Likes: {likes}</p>
135
  <p style='color: #2c3e50;'>{title}</p>
136
  <p style='color: #7f8c8d; font-size: 0.9em;'>{description}...</p>
 
149
  </a>
150
  </div>
151
  """
152
+ progress((0.6 + 0.3 * spaces.index(space)/len(spaces)),
153
+ desc=f"Loading space {spaces.index(space)+1}/{len(spaces)}...")
154
+
155
  html_content += "</div></div>"
156
 
157
  # 데이터 ν…Œμ΄λΈ” 생성
158
+ df = pd.DataFrame([{
159
+ 'Rank': space.get('rank', 'N/A'),
160
+ 'Space ID': space.get('id', ''),
161
+ 'Likes': space.get('likes', 'N/A'),
162
+ 'Title': space.get('title', 'N/A'),
163
+ 'URL': target_spaces[space.get('id', '')]
164
+ } for space in spaces])
165
 
166
  progress(1.0, desc="Complete!")
167
+ return fig, html_content, df
168
 
169
  except Exception as e:
170
  error_html = f'<div style="color: red; padding: 20px;">Error: {str(e)}</div>'
171
  error_plot = create_error_plot()
172
  return error_plot, error_html, pd.DataFrame()
173
 
174
+ # Gradio μΈν„°νŽ˜μ΄μŠ€λŠ” 이전과 동일
175
+
176
  def create_trend_visualization(spaces_data):
177
  if not spaces_data:
178
  return create_error_plot()