ginipick commited on
Commit
35865e4
·
verified ·
1 Parent(s): 8fcba35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -4
app.py CHANGED
@@ -19,17 +19,27 @@ def get_most_liked_spaces(limit: int = 10) -> List[Dict]:
19
  def format_spaces(spaces: List[Dict]) -> str:
20
  output = ""
21
  for idx, space in enumerate(spaces, 1):
22
- output += f"{idx}. {space['name']} by {space['author']}\n"
23
- output += f" Likes: {space['likes']}\n"
24
- output += f" URL: https://huggingface.co/spaces/{space['id']}\n\n"
 
 
 
 
 
 
25
  return output
26
 
27
  def get_spaces_list(limit: int) -> str:
28
  try:
29
  spaces = get_most_liked_spaces(limit)
 
 
30
  return format_spaces(spaces)
 
 
31
  except Exception as e:
32
- return f"An error occurred: {str(e)}"
33
 
34
  # Gradio 인터페이스 정의
35
  iface = gr.Interface(
 
19
  def format_spaces(spaces: List[Dict]) -> str:
20
  output = ""
21
  for idx, space in enumerate(spaces, 1):
22
+ # API 응답 구조 변경을 고려한 안전한 데이터 접근
23
+ space_id = space.get('id', 'Unknown')
24
+ space_name = space.get('title', space.get('name', 'Unknown')) # 'title' 또는 'name' 사용
25
+ space_author = space.get('author', {}).get('name', 'Unknown') # 중첩된 구조 고려
26
+ space_likes = space.get('likes', 'N/A')
27
+
28
+ output += f"{idx}. {space_name} by {space_author}\n"
29
+ output += f" Likes: {space_likes}\n"
30
+ output += f" URL: https://huggingface.co/spaces/{space_id}\n\n"
31
  return output
32
 
33
  def get_spaces_list(limit: int) -> str:
34
  try:
35
  spaces = get_most_liked_spaces(limit)
36
+ if not spaces:
37
+ return "No spaces found or empty response from API."
38
  return format_spaces(spaces)
39
+ except requests.RequestException as e:
40
+ return f"An error occurred while fetching data: {str(e)}"
41
  except Exception as e:
42
+ return f"An unexpected error occurred: {str(e)}"
43
 
44
  # Gradio 인터페이스 정의
45
  iface = gr.Interface(