openfree commited on
Commit
cce8fbb
ยท
verified ยท
1 Parent(s): e44e7ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -37
app.py CHANGED
@@ -42,41 +42,84 @@ target_spaces = {
42
  "NCSOFT/VARCO_Arena": "https://huggingface.co/spaces/NCSOFT/VARCO_Arena"
43
  }
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:
@@ -299,22 +342,24 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
299
  df_output = gr.DataFrame()
300
 
301
  refresh_btn = gr.Button("๐Ÿ”„ Refresh Data", variant="primary")
 
 
 
 
302
  refresh_btn.click(
303
  refresh_data,
304
  outputs=[plot_output, info_output, df_output]
305
  )
306
 
307
  # ์ดˆ๊ธฐ ๋ฐ์ดํ„ฐ ๋กœ๋“œ
308
- initial_plot, initial_info, initial_df = refresh_data()
309
  plot_output.value = initial_plot
310
  info_output.value = initial_info
311
  df_output.value = initial_df
312
 
313
  # Gradio ์•ฑ ์‹คํ–‰
314
- if __name__ == "__main__":
315
- demo.launch(
316
- server_name="0.0.0.0",
317
- server_port=7860,
318
- share=False, # True๋กœ ์„ค์ •ํ•˜๋ฉด ๊ณต๊ฐœ ๋งํฌ ์ƒ์„ฑ
319
- debug=True
320
- )
 
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
+
49
  try:
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 = """
72
+ <div style='padding: 20px; background: #f5f5f5;'>
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]
81
+
82
+ html_content += f"""
83
+ <div style='
84
+ background: white;
85
+ padding: 20px;
86
+ border-radius: 10px;
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>
94
+ <a href='{target_spaces[space_id]}'
95
+ target='_blank'
96
+ style='
97
+ display: inline-block;
98
+ padding: 8px 16px;
99
+ background: #3498db;
100
+ color: white;
101
+ text-decoration: none;
102
+ border-radius: 5px;
103
+ transition: background 0.3s;
104
+ '>
105
+ Visit Space ๐Ÿ”—
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:
 
342
  df_output = gr.DataFrame()
343
 
344
  refresh_btn = gr.Button("๐Ÿ”„ Refresh Data", variant="primary")
345
+
346
+ def refresh_data():
347
+ return get_trending_spaces()
348
+
349
  refresh_btn.click(
350
  refresh_data,
351
  outputs=[plot_output, info_output, df_output]
352
  )
353
 
354
  # ์ดˆ๊ธฐ ๋ฐ์ดํ„ฐ ๋กœ๋“œ
355
+ initial_plot, initial_info, initial_df = get_trending_spaces()
356
  plot_output.value = initial_plot
357
  info_output.value = initial_info
358
  df_output.value = initial_df
359
 
360
  # Gradio ์•ฑ ์‹คํ–‰
361
+ demo.launch(
362
+ server_name="0.0.0.0",
363
+ server_port=7860,
364
+ share=False
365
+ )