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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -36
app.py CHANGED
@@ -42,25 +42,27 @@ target_spaces = {
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
 
49
  try:
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:
@@ -72,18 +74,21 @@ def get_trending_spaces(progress=gr.Progress()):
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)',
@@ -92,7 +97,7 @@ def get_trending_spaces(progress=gr.Progress()):
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',
@@ -100,7 +105,11 @@ def get_trending_spaces(progress=gr.Progress()):
100
  },
101
  xaxis_title='Space ID',
102
  yaxis_title='Rank',
103
- yaxis_autorange='reversed', # μˆœμœ„κ°€ μœ„μ—μ„œ μ•„λž˜λ‘œ 내렀가도둝
 
 
 
 
104
  height=800,
105
  showlegend=False,
106
  template='plotly_white',
@@ -109,9 +118,10 @@ def get_trending_spaces(progress=gr.Progress()):
109
 
110
  progress(0.6, desc="Creating space cards...")
111
 
112
- # 슀페이슀 μΉ΄λ“œ HTML 생성
113
- html_content = """
114
  <div style='padding: 20px; background: #f5f5f5;'>
 
115
  <div style='display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px;'>
116
  """
117
 
@@ -149,12 +159,10 @@ def get_trending_spaces(progress=gr.Progress()):
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', ''),
@@ -171,7 +179,8 @@ def get_trending_spaces(progress=gr.Progress()):
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:
@@ -379,35 +388,43 @@ def refresh_data():
379
  else:
380
  return create_error_plot(), "<div>API 인증이 ν•„μš”ν•©λ‹ˆλ‹€.</div>", pd.DataFrame()
381
 
382
- # Gradio μΈν„°νŽ˜μ΄μŠ€ 생성
383
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
384
  gr.Markdown("""
385
- # πŸ€— HuggingFace Spaces Trending Analysis
386
- μ‹€μ‹œκ°„μœΌλ‘œ Hugging Face Spaces의 νŠΈλ Œλ”© μˆœμœ„λ₯Ό λΆ„μ„ν•©λ‹ˆλ‹€.
387
  """)
388
 
389
- with gr.Tab("Trending Analysis"):
390
- plot_output = gr.Plot()
391
- info_output = gr.HTML()
 
392
 
393
- with gr.Tab("Export Data"):
394
- df_output = gr.DataFrame()
 
 
395
 
396
  refresh_btn = gr.Button("πŸ”„ Refresh Data", variant="primary")
397
 
398
  def refresh_data():
399
- return get_trending_spaces()
 
 
400
 
401
  refresh_btn.click(
402
  refresh_data,
403
- outputs=[plot_output, info_output, df_output]
 
 
 
404
  )
405
 
406
  # 초기 데이터 λ‘œλ“œ
407
- initial_plot, initial_info, initial_df = get_trending_spaces()
408
- plot_output.value = initial_plot
409
- info_output.value = initial_info
410
- df_output.value = initial_df
 
411
 
412
  # Gradio μ•± μ‹€ν–‰
413
  demo.launch(
 
42
  "NCSOFT/VARCO_Arena": "https://huggingface.co/spaces/NCSOFT/VARCO_Arena"
43
  }
44
 
45
+ def get_spaces_data(sort_type="trending", progress=gr.Progress()):
46
+ """슀페이슀 데이터 κ°€μ Έμ˜€κΈ° (trending λ˜λŠ” modes)"""
47
+ url = f"https://huggingface.co/api/spaces"
48
+ params = {
49
+ 'full': 'true',
50
+ 'limit': 300
51
+ }
52
+
53
+ if sort_type == "modes":
54
+ params['sort'] = 'likes' # modesλŠ” μ’‹μ•„μš” 순으둜 μ •λ ¬
55
 
56
  try:
57
+ progress(0, desc=f"Fetching {sort_type} spaces data...")
 
 
 
 
 
58
  response = requests.get(url, params=params)
59
  response.raise_for_status()
60
  all_spaces = response.json()
61
 
62
+ # μˆœμœ„ 정보 μ €μž₯
63
  space_ranks = {space['id']: idx + 1 for idx, space in enumerate(all_spaces)}
64
 
65
+ # target_spaces 필터링 및 μˆœμœ„ 정보 포함
66
  spaces = []
67
  for space in all_spaces:
68
  if space.get('id', '') in target_spaces:
 
74
 
75
  progress(0.3, desc="Creating visualization...")
76
 
77
+ # μ‹œκ°ν™” 생성
78
  fig = go.Figure()
79
 
80
+ # 데이터 μ€€λΉ„
81
  ids = [space['id'] for space in spaces]
82
  ranks = [space['rank'] for space in spaces]
83
  likes = [space.get('likes', 0) for space in spaces]
84
 
85
+ # YμΆ• 값을 λ°˜μ „ (300 - rank + 1)
86
+ y_values = [301 - r for r in ranks] # μˆœμœ„λ₯Ό λ°˜μ „λœ κ°’μœΌλ‘œ λ³€ν™˜
87
+
88
  # λ§‰λŒ€ κ·Έλž˜ν”„ 생성
89
  fig.add_trace(go.Bar(
90
  x=ids,
91
+ y=y_values,
92
  text=[f"Rank: {r}<br>Likes: {l}" for r, l in zip(ranks, likes)],
93
  textposition='auto',
94
  marker_color='rgb(158,202,225)',
 
97
 
98
  fig.update_layout(
99
  title={
100
+ 'text': f'Hugging Face Spaces {sort_type.title()} Rankings (Top 300)',
101
  'y':0.95,
102
  'x':0.5,
103
  'xanchor': 'center',
 
105
  },
106
  xaxis_title='Space ID',
107
  yaxis_title='Rank',
108
+ yaxis=dict(
109
+ ticktext=[str(i) for i in range(1, 301, 20)], # 1λΆ€ν„° 300κΉŒμ§€ 20 간격
110
+ tickvals=[301 - i for i in range(1, 301, 20)], # λ°˜μ „λœ κ°’
111
+ range=[0, 300] # yμΆ• λ²”μœ„ μ„€μ •
112
+ ),
113
  height=800,
114
  showlegend=False,
115
  template='plotly_white',
 
118
 
119
  progress(0.6, desc="Creating space cards...")
120
 
121
+ # HTML μΉ΄λ“œ 생성
122
+ html_content = f"""
123
  <div style='padding: 20px; background: #f5f5f5;'>
124
+ <h2 style='color: #2c3e50;'>{sort_type.title()} Rankings</h2>
125
  <div style='display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px;'>
126
  """
127
 
 
159
  </a>
160
  </div>
161
  """
 
 
162
 
163
  html_content += "</div></div>"
164
 
165
+ # λ°μ΄ν„°ν”„λ ˆμž„ 생성
166
  df = pd.DataFrame([{
167
  'Rank': space.get('rank', 'N/A'),
168
  'Space ID': space.get('id', ''),
 
179
  error_plot = create_error_plot()
180
  return error_plot, error_html, pd.DataFrame()
181
 
182
+
183
+
184
 
185
  def create_trend_visualization(spaces_data):
186
  if not spaces_data:
 
388
  else:
389
  return create_error_plot(), "<div>API 인증이 ν•„μš”ν•©λ‹ˆλ‹€.</div>", pd.DataFrame()
390
 
 
391
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
392
  gr.Markdown("""
393
+ # πŸ€— HuggingFace Spaces Rankings Analysis
394
+ μ‹€μ‹œκ°„μœΌλ‘œ Hugging Face Spaces의 μˆœμœ„λ₯Ό λΆ„μ„ν•©λ‹ˆλ‹€.
395
  """)
396
 
397
+ with gr.Tab("Trending Rankings"):
398
+ trending_plot = gr.Plot()
399
+ trending_info = gr.HTML()
400
+ trending_df = gr.DataFrame()
401
 
402
+ with gr.Tab("Most Liked Rankings"):
403
+ modes_plot = gr.Plot()
404
+ modes_info = gr.HTML()
405
+ modes_df = gr.DataFrame()
406
 
407
  refresh_btn = gr.Button("πŸ”„ Refresh Data", variant="primary")
408
 
409
  def refresh_data():
410
+ trending_results = get_spaces_data("trending")
411
+ modes_results = get_spaces_data("modes")
412
+ return [*trending_results, *modes_results]
413
 
414
  refresh_btn.click(
415
  refresh_data,
416
+ outputs=[
417
+ trending_plot, trending_info, trending_df,
418
+ modes_plot, modes_info, modes_df
419
+ ]
420
  )
421
 
422
  # 초기 데이터 λ‘œλ“œ
423
+ trending_results = get_spaces_data("trending")
424
+ modes_results = get_spaces_data("modes")
425
+
426
+ trending_plot.value, trending_info.value, trending_df.value = trending_results
427
+ modes_plot.value, modes_info.value, modes_df.value = modes_results
428
 
429
  # Gradio μ•± μ‹€ν–‰
430
  demo.launch(