openfree commited on
Commit
e195ac6
β€’
1 Parent(s): 62fc2dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -21
app.py CHANGED
@@ -2,8 +2,10 @@ import gradio as gr
2
  import requests
3
  import pandas as pd
4
  import plotly.graph_objects as go
5
- from datetime import datetime, timedelta
6
- import time
 
 
7
 
8
  # 관심 슀페이슀 URL λ¦¬μŠ€νŠΈμ™€ 정보
9
  target_spaces = {
@@ -44,8 +46,8 @@ def get_trending_spaces():
44
  try:
45
  url = "https://huggingface.co/api/spaces/sort/trending"
46
  headers = {
47
- 'Accept': 'application/json',
48
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
49
  }
50
  response = requests.get(url, headers=headers)
51
 
@@ -62,7 +64,7 @@ def get_trending_spaces():
62
  def create_error_plot():
63
  fig = go.Figure()
64
  fig.add_annotation(
65
- text="데이터λ₯Ό 뢈러올 수 μ—†μŠ΅λ‹ˆλ‹€.\nμž μ‹œ ν›„ λ‹€μ‹œ μ‹œλ„ν•΄μ£Όμ„Έμš”.",
66
  xref="paper",
67
  yref="paper",
68
  x=0.5,
@@ -93,6 +95,9 @@ def create_trend_visualization(spaces_data):
93
  'likes': space.get('likes', 0)
94
  })
95
 
 
 
 
96
  # μˆœμœ„λ³„λ‘œ μ •λ ¬
97
  ranks.sort(key=lambda x: x['rank'])
98
 
@@ -101,21 +106,30 @@ def create_trend_visualization(spaces_data):
101
  rank_values = [r['rank'] for r in ranks]
102
  likes = [r['likes'] for r in ranks]
103
 
 
104
  fig.add_trace(go.Bar(
105
  x=ids,
106
  y=rank_values,
107
- text=likes,
108
  textposition='auto',
109
- name='Rank'
 
110
  ))
111
 
112
  fig.update_layout(
113
- title='Current Trending Ranks',
 
 
 
 
 
 
114
  xaxis_title='Space ID',
115
  yaxis_title='Rank',
116
  yaxis_autorange='reversed',
117
  height=600,
118
- showlegend=False
 
119
  )
120
 
121
  return fig
@@ -124,23 +138,46 @@ def create_space_info_html(spaces_data):
124
  if not spaces_data:
125
  return "<div style='padding: 20px;'><h2>데이터λ₯Ό λΆˆλŸ¬μ˜€λŠ”λ° μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€.</h2></div>"
126
 
127
- html_content = "<div style='padding: 20px;'>"
128
- html_content += "<h2>Current Rankings</h2>"
 
 
 
129
 
130
  for idx, space in enumerate(spaces_data, 1):
131
  space_id = space.get('id', '')
132
  if space_id in target_spaces:
133
  html_content += f"""
134
- <div style='margin: 20px 0; padding: 15px; border: 1px solid #ddd; border-radius: 8px;'>
135
- <h3>#{idx} - {space_id}</h3>
136
- <p>πŸ‘ Likes: {space.get('likes', 'N/A')}</p>
137
- <p>πŸ“ {space.get('title', 'N/A')}</p>
138
- <p>{space.get('description', 'N/A')[:100]}...</p>
139
- <a href='{target_spaces[space_id]}' target='_blank' style='color: blue;'>Visit Space πŸ”—</a>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  </div>
141
  """
142
 
143
- html_content += "</div>"
144
  return html_content
145
 
146
  def create_data_table(spaces_data):
@@ -169,11 +206,14 @@ def refresh_data():
169
  df = create_data_table(spaces_data)
170
  return plot, info, df
171
  else:
172
- return create_error_plot(), "<div>데이터λ₯Ό λΆˆλŸ¬μ˜€λŠ”λ° μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€.</div>", pd.DataFrame()
173
 
174
  # Gradio μΈν„°νŽ˜μ΄μŠ€ 생성
175
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
176
- gr.Markdown("# πŸ€— HuggingFace Spaces Trending Analysis")
 
 
 
177
 
178
  with gr.Tab("Trending Analysis"):
179
  plot_output = gr.Plot()
@@ -182,7 +222,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
182
  with gr.Tab("Export Data"):
183
  df_output = gr.DataFrame()
184
 
185
- refresh_btn = gr.Button("Refresh Data")
186
  refresh_btn.click(
187
  refresh_data,
188
  outputs=[plot_output, info_output, df_output]
 
2
  import requests
3
  import pandas as pd
4
  import plotly.graph_objects as go
5
+ from datetime import datetime
6
+ import os
7
+
8
+ HF_TOKEN = os.getenv('HF_TOKEN')
9
 
10
  # 관심 슀페이슀 URL λ¦¬μŠ€νŠΈμ™€ 정보
11
  target_spaces = {
 
46
  try:
47
  url = "https://huggingface.co/api/spaces/sort/trending"
48
  headers = {
49
+ 'Authorization': f'Bearer {HF_TOKEN}',
50
+ 'Accept': 'application/json'
51
  }
52
  response = requests.get(url, headers=headers)
53
 
 
64
  def create_error_plot():
65
  fig = go.Figure()
66
  fig.add_annotation(
67
+ text="데이터λ₯Ό 뢈러올 수 μ—†μŠ΅λ‹ˆλ‹€.\n(API 인증이 ν•„μš”ν•©λ‹ˆλ‹€)",
68
  xref="paper",
69
  yref="paper",
70
  x=0.5,
 
95
  'likes': space.get('likes', 0)
96
  })
97
 
98
+ if not ranks:
99
+ return create_error_plot()
100
+
101
  # μˆœμœ„λ³„λ‘œ μ •λ ¬
102
  ranks.sort(key=lambda x: x['rank'])
103
 
 
106
  rank_values = [r['rank'] for r in ranks]
107
  likes = [r['likes'] for r in ranks]
108
 
109
+ # λ§‰λŒ€ κ·Έλž˜ν”„ 생성
110
  fig.add_trace(go.Bar(
111
  x=ids,
112
  y=rank_values,
113
+ text=[f"Rank: {r}<br>Likes: {l}" for r, l in zip(rank_values, likes)],
114
  textposition='auto',
115
+ marker_color='rgb(158,202,225)',
116
+ opacity=0.8
117
  ))
118
 
119
  fig.update_layout(
120
+ title={
121
+ 'text': 'Current Trending Ranks',
122
+ 'y':0.95,
123
+ 'x':0.5,
124
+ 'xanchor': 'center',
125
+ 'yanchor': 'top'
126
+ },
127
  xaxis_title='Space ID',
128
  yaxis_title='Rank',
129
  yaxis_autorange='reversed',
130
  height=600,
131
+ showlegend=False,
132
+ template='plotly_white'
133
  )
134
 
135
  return fig
 
138
  if not spaces_data:
139
  return "<div style='padding: 20px;'><h2>데이터λ₯Ό λΆˆλŸ¬μ˜€λŠ”λ° μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€.</h2></div>"
140
 
141
+ html_content = """
142
+ <div style='padding: 20px;'>
143
+ <h2 style='color: #2c3e50;'>Current Rankings</h2>
144
+ <div style='display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px;'>
145
+ """
146
 
147
  for idx, space in enumerate(spaces_data, 1):
148
  space_id = space.get('id', '')
149
  if space_id in target_spaces:
150
  html_content += f"""
151
+ <div style='
152
+ background: white;
153
+ padding: 20px;
154
+ border-radius: 10px;
155
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
156
+ transition: transform 0.2s;
157
+ hover: transform: translateY(-5px);
158
+ '>
159
+ <h3 style='color: #34495e;'>#{idx} - {space_id}</h3>
160
+ <p style='color: #7f8c8d;'>πŸ‘ Likes: {space.get('likes', 'N/A')}</p>
161
+ <p style='color: #2c3e50;'>{space.get('title', 'N/A')}</p>
162
+ <p style='color: #7f8c8d; font-size: 0.9em;'>{space.get('description', 'N/A')[:100]}...</p>
163
+ <a href='{target_spaces[space_id]}'
164
+ target='_blank'
165
+ style='
166
+ display: inline-block;
167
+ padding: 8px 16px;
168
+ background: #3498db;
169
+ color: white;
170
+ text-decoration: none;
171
+ border-radius: 5px;
172
+ transition: background 0.3s;
173
+ hover: background: #2980b9;
174
+ '>
175
+ Visit Space πŸ”—
176
+ </a>
177
  </div>
178
  """
179
 
180
+ html_content += "</div></div>"
181
  return html_content
182
 
183
  def create_data_table(spaces_data):
 
206
  df = create_data_table(spaces_data)
207
  return plot, info, df
208
  else:
209
+ return create_error_plot(), "<div>API 인증이 ν•„μš”ν•©λ‹ˆλ‹€.</div>", pd.DataFrame()
210
 
211
  # Gradio μΈν„°νŽ˜μ΄μŠ€ 생성
212
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
213
+ gr.Markdown("""
214
+ # πŸ€— HuggingFace Spaces Trending Analysis
215
+ μ‹€μ‹œκ°„μœΌλ‘œ Hugging Face Spaces의 νŠΈλ Œλ”© μˆœμœ„λ₯Ό λΆ„μ„ν•©λ‹ˆλ‹€.
216
+ """)
217
 
218
  with gr.Tab("Trending Analysis"):
219
  plot_output = gr.Plot()
 
222
  with gr.Tab("Export Data"):
223
  df_output = gr.DataFrame()
224
 
225
+ refresh_btn = gr.Button("πŸ”„ Refresh Data", variant="primary")
226
  refresh_btn.click(
227
  refresh_data,
228
  outputs=[plot_output, info_output, df_output]