ginipick commited on
Commit
47e0641
ยท
verified ยท
1 Parent(s): 0841e14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -69
app.py CHANGED
@@ -21,10 +21,10 @@ def get_headers():
21
  raise ValueError("Hugging Face token not found in environment variables")
22
  return {"Authorization": f"Bearer {HF_TOKEN}"}
23
 
24
- def get_trending_spaces(limit: int = 300) -> Union[List[Dict], str]:
25
  url = "https://huggingface.co/api/spaces"
26
  params = {
27
- "sort": "trending",
28
  "direction": -1,
29
  "limit": limit,
30
  "full": "true"
@@ -103,7 +103,6 @@ def on_select(space):
103
  print(f"Selected space: {json.dumps(space, indent=2)}")
104
  summary = summarize_space(space)
105
  app_content = get_app_py_content(space['id'])
106
- tree_structure = get_space_structure(space['id'])
107
 
108
  info = f"์„ ํƒ๋œ Space: {space.get('name', 'Unknown')} (ID: {space.get('id', 'Unknown')})\n"
109
  info += f"Author: {space.get('author', 'Unknown')}\n"
@@ -111,11 +110,8 @@ def on_select(space):
111
  info += f"URL: {space.get('url', 'Unknown')}\n\n"
112
  info += f"์š”์•ฝ:\n{summary}"
113
 
114
- tree_view = format_tree_structure(tree_structure)
115
- list_view = "\n".join(format_list_structure(tree_structure))
116
-
117
  print(f"Returning URL: {space.get('url', 'Unknown')}")
118
- return info, app_content, space.get('url', ''), tree_view, list_view
119
  except Exception as e:
120
  print(f"Error in on_select: {str(e)}")
121
  print(traceback.format_exc())
@@ -141,65 +137,12 @@ def get_app_py_content(space_id: str) -> str:
141
  try:
142
  response = requests.get(app_py_url, headers=get_headers())
143
  if response.status_code == 200:
144
- return response.text # ์ „์ฒด ๋‚ด์šฉ์„ ๋ฐ˜ํ™˜
145
  else:
146
  return f"app.py file not found or inaccessible for space: {space_id}"
147
  except requests.RequestException:
148
  return f"Error fetching app.py content for space: {space_id}"
149
 
150
- def get_space_structure(space_id: str) -> Dict:
151
- try:
152
- # space_id์—์„œ owner์™€ repo_name์„ ๋ถ„๋ฆฌ
153
- owner, repo_name = space_id.split('/')
154
-
155
- # HfApi๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ผ ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜ด
156
- files = hf_api.list_repo_files(repo_id=space_id, repo_type="space")
157
-
158
- # ํŒŒ์ผ ๋ชฉ๋ก์„ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋กœ ๋ณ€ํ™˜
159
- tree = {"type": "directory", "path": "", "tree": []}
160
- for file in files:
161
- path_parts = file.split('/')
162
- current = tree
163
- for i, part in enumerate(path_parts):
164
- if i == len(path_parts) - 1: # ํŒŒ์ผ
165
- current["tree"].append({"type": "file", "path": part})
166
- else: # ๋””๋ ‰ํ† ๋ฆฌ
167
- found = False
168
- for item in current["tree"]:
169
- if item["type"] == "directory" and item["path"] == part:
170
- current = item
171
- found = True
172
- break
173
- if not found:
174
- new_dir = {"type": "directory", "path": part, "tree": []}
175
- current["tree"].append(new_dir)
176
- current = new_dir
177
-
178
- return tree
179
- except Exception as e:
180
- print(f"Error in get_space_structure: {str(e)}")
181
- return {"error": f"API request error: {str(e)}"}
182
-
183
- def format_tree_structure(tree_data: Dict, indent: str = "") -> str:
184
- formatted = ""
185
- for item in tree_data.get("tree", []):
186
- if item["type"] == "file":
187
- formatted += f"{indent}โ”œโ”€โ”€ {item['path']}\n"
188
- elif item["type"] == "directory":
189
- formatted += f"{indent}โ”œโ”€โ”€ {item['path']}/\n"
190
- formatted += format_tree_structure(item, indent + "โ”‚ ")
191
- return formatted
192
-
193
- def format_list_structure(tree_data: Dict) -> List[str]:
194
- formatted = []
195
- for item in tree_data.get("tree", []):
196
- if item["type"] == "file":
197
- formatted.append(item["path"])
198
- elif item["type"] == "directory":
199
- formatted.append(f"{item['path']}/")
200
- formatted.extend(format_list_structure(item))
201
- return formatted
202
-
203
  def update_screenshot(url, last_url, force_update=False):
204
  print(f"Updating screenshot. Current URL: {url}, Last URL: {last_url}, Force update: {force_update}")
205
  if url and (url != last_url or force_update):
@@ -246,9 +189,9 @@ def generate_usage_guide(app_content):
246
 
247
  def create_ui():
248
  try:
249
- spaces_list = get_trending_spaces()
250
  formatted_spaces = format_spaces(spaces_list)
251
- print(f"Total trending spaces loaded: {len(formatted_spaces)}")
252
 
253
  css = """
254
  footer {visibility: hidden;}
@@ -285,7 +228,7 @@ def create_ui():
285
  """
286
 
287
  with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
288
- gr.Markdown("# 300: HuggingFace Trending Spaces")
289
 
290
  with gr.Row():
291
  with gr.Column(scale=1):
@@ -305,10 +248,6 @@ def create_ui():
305
  info_output = gr.Textbox(label="Space ์ •๋ณด ๋ฐ ์š”์•ฝ", elem_id="info-output", lines=20, max_lines=30)
306
  url_state = gr.State("")
307
  last_url_state = gr.State("")
308
-
309
-
310
-
311
-
312
 
313
  screenshot_output = gr.Image(type="pil", label="Live ํ™”๋ฉด", height=360, width=600)
314
  refresh_button = gr.Button("๐Ÿ”„ ์„œ๋น„์Šค ํ™”๋ฉด", elem_id="refresh-button")
@@ -348,6 +287,7 @@ def create_ui():
348
 
349
  def open_space_in_browser(url):
350
  if url:
 
351
  import webbrowser
352
  webbrowser.open(url)
353
  return f"'{url}' ์ฃผ์†Œ๊ฐ€ ์ƒˆ ํƒญ์—์„œ ์—ด๋ ธ์Šต๋‹ˆ๋‹ค."
@@ -402,4 +342,4 @@ if __name__ == "__main__":
402
  demo.launch()
403
  except Exception as e:
404
  print(f"Error in main: {str(e)}")
405
- print(traceback.format_exc())
 
21
  raise ValueError("Hugging Face token not found in environment variables")
22
  return {"Authorization": f"Bearer {HF_TOKEN}"}
23
 
24
+ def get_popular_spaces(limit: int = 300) -> Union[List[Dict], str]:
25
  url = "https://huggingface.co/api/spaces"
26
  params = {
27
+ "sort": "likes", # Changed from "trending" to "likes"
28
  "direction": -1,
29
  "limit": limit,
30
  "full": "true"
 
103
  print(f"Selected space: {json.dumps(space, indent=2)}")
104
  summary = summarize_space(space)
105
  app_content = get_app_py_content(space['id'])
 
106
 
107
  info = f"์„ ํƒ๋œ Space: {space.get('name', 'Unknown')} (ID: {space.get('id', 'Unknown')})\n"
108
  info += f"Author: {space.get('author', 'Unknown')}\n"
 
110
  info += f"URL: {space.get('url', 'Unknown')}\n\n"
111
  info += f"์š”์•ฝ:\n{summary}"
112
 
 
 
 
113
  print(f"Returning URL: {space.get('url', 'Unknown')}")
114
+ return info, app_content, space.get('url', ''), "", "" # Returning empty strings for tree and list views
115
  except Exception as e:
116
  print(f"Error in on_select: {str(e)}")
117
  print(traceback.format_exc())
 
137
  try:
138
  response = requests.get(app_py_url, headers=get_headers())
139
  if response.status_code == 200:
140
+ return response.text
141
  else:
142
  return f"app.py file not found or inaccessible for space: {space_id}"
143
  except requests.RequestException:
144
  return f"Error fetching app.py content for space: {space_id}"
145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  def update_screenshot(url, last_url, force_update=False):
147
  print(f"Updating screenshot. Current URL: {url}, Last URL: {last_url}, Force update: {force_update}")
148
  if url and (url != last_url or force_update):
 
189
 
190
  def create_ui():
191
  try:
192
+ spaces_list = get_popular_spaces() # Changed from get_trending_spaces
193
  formatted_spaces = format_spaces(spaces_list)
194
+ print(f"Total popular spaces loaded: {len(formatted_spaces)}") # Changed from "trending" to "popular"
195
 
196
  css = """
197
  footer {visibility: hidden;}
 
228
  """
229
 
230
  with gr.Blocks(css=css, theme="Nymbo/Nymbo_Theme") as demo:
231
+ gr.Markdown("# 300: HuggingFace Popular Spaces") # Changed from "Trending" to "Popular"
232
 
233
  with gr.Row():
234
  with gr.Column(scale=1):
 
248
  info_output = gr.Textbox(label="Space ์ •๋ณด ๋ฐ ์š”์•ฝ", elem_id="info-output", lines=20, max_lines=30)
249
  url_state = gr.State("")
250
  last_url_state = gr.State("")
 
 
 
 
251
 
252
  screenshot_output = gr.Image(type="pil", label="Live ํ™”๋ฉด", height=360, width=600)
253
  refresh_button = gr.Button("๐Ÿ”„ ์„œ๋น„์Šค ํ™”๋ฉด", elem_id="refresh-button")
 
287
 
288
  def open_space_in_browser(url):
289
  if url:
290
+
291
  import webbrowser
292
  webbrowser.open(url)
293
  return f"'{url}' ์ฃผ์†Œ๊ฐ€ ์ƒˆ ํƒญ์—์„œ ์—ด๋ ธ์Šต๋‹ˆ๋‹ค."
 
342
  demo.launch()
343
  except Exception as e:
344
  print(f"Error in main: {str(e)}")
345
+ print(traceback.format_exc())