ginipick commited on
Commit
d134242
ยท
verified ยท
1 Parent(s): 383b3b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -30
app.py CHANGED
@@ -36,14 +36,13 @@ def get_trending_spaces(limit: int = 300) -> Union[List[Dict], str]:
36
  response = requests.get(url, params=params, headers=get_headers())
37
  response.raise_for_status()
38
  data = response.json()
39
- print(f"API Response: {json.dumps(data[:2], indent=2)}") # ์ฒ˜์Œ ๋‘ ํ•ญ๋ชฉ๋งŒ ์ถœ๋ ฅ
40
  return data
41
  except requests.RequestException as e:
42
  return f"API request error: {str(e)}"
43
  except ValueError as e:
44
  return f"JSON decoding error: {str(e)}"
45
 
46
-
47
 
48
  def get_most_liked_spaces(limit: int = 300) -> Union[List[Dict], str]:
49
  url = "https://huggingface.co/api/spaces"
@@ -64,11 +63,10 @@ def get_most_liked_spaces(limit: int = 300) -> Union[List[Dict], str]:
64
  return f"JSON decoding error: {str(e)}"
65
 
66
  def format_space(space: Dict) -> Dict:
67
- # ๋””๋ฒ„๊น…์„ ์œ„ํ•ด ์›๋ณธ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ
68
- print(f"Original space data: {json.dumps(space, indent=2)}")
69
 
70
  space_id = space.get('id', 'Unknown')
71
- space_name = space.get('title', space_id.split('/')[-1] if '/' in space_id else space_id)
72
 
73
  space_author = space.get('author', 'Unknown')
74
  if isinstance(space_author, dict):
@@ -77,16 +75,19 @@ def format_space(space: Dict) -> Dict:
77
  space_likes = space.get('likes', 'N/A')
78
  space_url = f"https://huggingface.co/spaces/{space_id}"
79
 
80
- return {
81
  "id": space_id,
82
  "name": space_name,
83
  "author": space_author,
84
  "likes": space_likes,
85
  "url": space_url,
86
  }
 
 
87
 
88
  def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
89
  if isinstance(spaces, str):
 
90
  return [{"error": spaces}]
91
 
92
  formatted = []
@@ -97,8 +98,31 @@ def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
97
  except Exception as e:
98
  print(f"Error formatting space: {str(e)}")
99
  print(f"Problematic space data: {json.dumps(space, indent=2)}")
 
100
  return formatted
101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
  def summarize_space(space: Dict) -> str:
104
  system_message = "๋‹น์‹ ์€ Hugging Face Space์˜ ๋‚ด์šฉ์„ ์š”์•ฝํ•˜๋Š” AI ์กฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๊ฐ„๊ฒฐํ•˜๊ณ  ๋ช…ํ™•ํ•œ ์š”์•ฝ์„ ์ œ๊ณตํ•ด์ฃผ์„ธ์š”."
@@ -180,28 +204,7 @@ def format_list_structure(tree_data: Dict) -> List[str]:
180
  formatted.extend(format_list_structure(item))
181
  return formatted
182
 
183
- def on_select(space):
184
- try:
185
- print(f"Selected space: {space['name']}")
186
- summary = summarize_space(space)
187
- app_content = get_app_py_content(space['id'])
188
- tree_structure = get_space_structure(space['id'])
189
-
190
- info = f"์„ ํƒ๋œ Space: {space['name']} (ID: {space['id']})\n"
191
- info += f"Author: {space['author']}\n"
192
- info += f"Likes: {space['likes']}\n"
193
- info += f"URL: {space['url']}\n\n"
194
- info += f"์š”์•ฝ:\n{summary}"
195
-
196
- tree_view = format_tree_structure(tree_structure)
197
- list_view = "\n".join(format_list_structure(tree_structure))
198
-
199
- print(f"Returning URL: {space['url']}")
200
- return info, app_content, space['url'], tree_view, list_view
201
- except Exception as e:
202
- print(f"Error in on_select: {str(e)}")
203
- print(traceback.format_exc())
204
- return f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}", "", "", "", ""
205
 
206
  def update_screenshot(url, last_url, force_update=False):
207
  print(f"Updating screenshot. Current URL: {url}, Last URL: {last_url}, Force update: {force_update}")
@@ -287,6 +290,7 @@ def create_ui():
287
  }
288
  """
289
 
 
290
  with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
291
  gr.Markdown("# 300: HuggingFace Trending Spaces")
292
 
@@ -301,7 +305,6 @@ def create_ui():
301
  button = gr.Button("ํด๋ฆญ", elem_classes="minimal-button")
302
  space_rows.append((space_row, button, space))
303
 
304
-
305
  with gr.Column(scale=2):
306
  with gr.Tabs():
307
  with gr.TabItem("๊ธฐ๋ณธ ์ •๋ณด"):
@@ -330,7 +333,6 @@ def create_ui():
330
 
331
  update_trigger = gr.Button("Update Screenshot", visible=False)
332
 
333
-
334
  def on_select_with_link(space):
335
  info, app_content, url, tree, list_structure = on_select(space)
336
  info += f"\n\n์„ ํƒํ•œ Space URL: {url}"
@@ -347,6 +349,7 @@ def create_ui():
347
  outputs=[screenshot_output, last_url_state]
348
  )
349
 
 
350
  def open_space_in_browser(url):
351
  if url:
352
  import webbrowser
 
36
  response = requests.get(url, params=params, headers=get_headers())
37
  response.raise_for_status()
38
  data = response.json()
39
+ print(f"API Response (first 2 items): {json.dumps(data[:2], indent=2)}")
40
  return data
41
  except requests.RequestException as e:
42
  return f"API request error: {str(e)}"
43
  except ValueError as e:
44
  return f"JSON decoding error: {str(e)}"
45
 
 
46
 
47
  def get_most_liked_spaces(limit: int = 300) -> Union[List[Dict], str]:
48
  url = "https://huggingface.co/api/spaces"
 
63
  return f"JSON decoding error: {str(e)}"
64
 
65
  def format_space(space: Dict) -> Dict:
66
+ print(f"Formatting space: {json.dumps(space, indent=2)}")
 
67
 
68
  space_id = space.get('id', 'Unknown')
69
+ space_name = space.get('title', space.get('name', space_id.split('/')[-1] if '/' in space_id else space_id))
70
 
71
  space_author = space.get('author', 'Unknown')
72
  if isinstance(space_author, dict):
 
75
  space_likes = space.get('likes', 'N/A')
76
  space_url = f"https://huggingface.co/spaces/{space_id}"
77
 
78
+ formatted_space = {
79
  "id": space_id,
80
  "name": space_name,
81
  "author": space_author,
82
  "likes": space_likes,
83
  "url": space_url,
84
  }
85
+ print(f"Formatted space: {json.dumps(formatted_space, indent=2)}")
86
+ return formatted_space
87
 
88
  def format_spaces(spaces: Union[List[Dict], str]) -> List[Dict]:
89
  if isinstance(spaces, str):
90
+ print(f"Error in spaces data: {spaces}")
91
  return [{"error": spaces}]
92
 
93
  formatted = []
 
98
  except Exception as e:
99
  print(f"Error formatting space: {str(e)}")
100
  print(f"Problematic space data: {json.dumps(space, indent=2)}")
101
+ print(f"Total formatted spaces: {len(formatted)}")
102
  return formatted
103
 
104
+ def on_select(space):
105
+ try:
106
+ print(f"Selected space: {json.dumps(space, indent=2)}")
107
+ summary = summarize_space(space)
108
+ app_content = get_app_py_content(space['id'])
109
+ tree_structure = get_space_structure(space['id'])
110
+
111
+ info = f"์„ ํƒ๋œ Space: {space.get('name', 'Unknown')} (ID: {space.get('id', 'Unknown')})\n"
112
+ info += f"Author: {space.get('author', 'Unknown')}\n"
113
+ info += f"Likes: {space.get('likes', 'N/A')}\n"
114
+ info += f"URL: {space.get('url', 'Unknown')}\n\n"
115
+ info += f"์š”์•ฝ:\n{summary}"
116
+
117
+ tree_view = format_tree_structure(tree_structure)
118
+ list_view = "\n".join(format_list_structure(tree_structure))
119
+
120
+ print(f"Returning URL: {space.get('url', 'Unknown')}")
121
+ return info, app_content, space.get('url', ''), tree_view, list_view
122
+ except Exception as e:
123
+ print(f"Error in on_select: {str(e)}")
124
+ print(traceback.format_exc())
125
+ return f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}", "", "", "", ""
126
 
127
  def summarize_space(space: Dict) -> str:
128
  system_message = "๋‹น์‹ ์€ Hugging Face Space์˜ ๋‚ด์šฉ์„ ์š”์•ฝํ•˜๋Š” AI ์กฐ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ๊ฐ„๊ฒฐํ•˜๊ณ  ๋ช…ํ™•ํ•œ ์š”์•ฝ์„ ์ œ๊ณตํ•ด์ฃผ์„ธ์š”."
 
204
  formatted.extend(format_list_structure(item))
205
  return formatted
206
 
207
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
 
209
  def update_screenshot(url, last_url, force_update=False):
210
  print(f"Updating screenshot. Current URL: {url}, Last URL: {last_url}, Force update: {force_update}")
 
290
  }
291
  """
292
 
293
+
294
  with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
295
  gr.Markdown("# 300: HuggingFace Trending Spaces")
296
 
 
305
  button = gr.Button("ํด๋ฆญ", elem_classes="minimal-button")
306
  space_rows.append((space_row, button, space))
307
 
 
308
  with gr.Column(scale=2):
309
  with gr.Tabs():
310
  with gr.TabItem("๊ธฐ๋ณธ ์ •๋ณด"):
 
333
 
334
  update_trigger = gr.Button("Update Screenshot", visible=False)
335
 
 
336
  def on_select_with_link(space):
337
  info, app_content, url, tree, list_structure = on_select(space)
338
  info += f"\n\n์„ ํƒํ•œ Space URL: {url}"
 
349
  outputs=[screenshot_output, last_url_state]
350
  )
351
 
352
+
353
  def open_space_in_browser(url):
354
  if url:
355
  import webbrowser