ginipick commited on
Commit
06391aa
ยท
verified ยท
1 Parent(s): 3f3f818

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -30
app.py CHANGED
@@ -1,9 +1,7 @@
1
  import requests
2
- import gradio as gr
3
- import json
4
  from typing import List, Dict, Union
5
 
6
- def get_most_liked_spaces(limit: int = 10) -> Union[List[Dict], str]:
7
  url = "https://huggingface.co/api/spaces"
8
  params = {
9
  "sort": "likes",
@@ -17,10 +15,6 @@ def get_most_liked_spaces(limit: int = 10) -> Union[List[Dict], str]:
17
  response.raise_for_status()
18
  data = response.json()
19
 
20
- # ๋””๋ฒ„๊น…: ์ „์ฒด ์‘๋‹ต ๊ตฌ์กฐ ์ถœ๋ ฅ
21
- print("API Response Structure:")
22
- print(json.dumps(data[:2], indent=2)) # ์ฒ˜์Œ ๋‘ ๊ฐœ์˜ ํ•ญ๋ชฉ๋งŒ ์ถœ๋ ฅ
23
-
24
  if isinstance(data, list):
25
  return data
26
  else:
@@ -30,46 +24,36 @@ def get_most_liked_spaces(limit: int = 10) -> Union[List[Dict], str]:
30
  except ValueError as e:
31
  return f"JSON decoding error: {str(e)}"
32
 
33
- def format_spaces(spaces: Union[List[Dict], str]) -> str:
34
  if isinstance(spaces, str):
35
- return spaces # ์ด๋ฏธ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€์ธ ๊ฒฝ์šฐ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜
36
 
37
- output = ""
38
- for idx, space in enumerate(spaces, 1):
39
  if not isinstance(space, dict):
40
- output += f"{idx}. Unexpected space data format: {type(space)}\n"
41
- output += f" Content: {space}\n\n"
42
  continue
43
 
44
- # 'id' ํ•„๋“œ์—์„œ space ์ด๋ฆ„ ์ถ”์ถœ
45
  space_id = space.get('id', 'Unknown')
46
  space_name = space_id.split('/')[-1] if '/' in space_id else space_id
47
 
48
- # 'author' ํ•„๋“œ์—์„œ ์ž‘์„ฑ์ž ์ •๋ณด ์ถ”์ถœ
49
  space_author = space.get('author', 'Unknown')
50
  if isinstance(space_author, dict):
51
  space_author = space_author.get('user', space_author.get('name', 'Unknown'))
52
 
53
  space_likes = space.get('likes', 'N/A')
54
 
55
- output += f"{idx}. {space_name} by {space_author}\n"
56
- output += f" Likes: {space_likes}\n"
57
- output += f" URL: https://huggingface.co/spaces/{space_id}\n\n"
58
 
59
- return output if output else "No valid space data found."
60
 
61
- def get_spaces_list(limit: int) -> str:
62
  spaces = get_most_liked_spaces(limit)
63
  return format_spaces(spaces)
64
 
65
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
66
- iface = gr.Interface(
67
- fn=get_spaces_list,
68
- inputs=gr.Slider(minimum=1, maximum=50, step=1, label="Number of Spaces to Display", value=10),
69
- outputs="text",
70
- title="Hugging Face Most Liked Spaces",
71
- description="Display the most liked Hugging Face Spaces in descending order.",
72
- )
73
-
74
  if __name__ == "__main__":
75
- iface.launch()
 
 
 
 
1
  import requests
 
 
2
  from typing import List, Dict, Union
3
 
4
+ def get_most_liked_spaces(limit: int = 100) -> Union[List[Dict], str]:
5
  url = "https://huggingface.co/api/spaces"
6
  params = {
7
  "sort": "likes",
 
15
  response.raise_for_status()
16
  data = response.json()
17
 
 
 
 
 
18
  if isinstance(data, list):
19
  return data
20
  else:
 
24
  except ValueError as e:
25
  return f"JSON decoding error: {str(e)}"
26
 
27
+ def format_spaces(spaces: Union[List[Dict], str]) -> List[str]:
28
  if isinstance(spaces, str):
29
+ return [spaces] # ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜
30
 
31
+ formatted_spaces = []
32
+ for space in spaces:
33
  if not isinstance(space, dict):
34
+ formatted_spaces.append(f"Unexpected space data format: {type(space)}")
 
35
  continue
36
 
 
37
  space_id = space.get('id', 'Unknown')
38
  space_name = space_id.split('/')[-1] if '/' in space_id else space_id
39
 
 
40
  space_author = space.get('author', 'Unknown')
41
  if isinstance(space_author, dict):
42
  space_author = space_author.get('user', space_author.get('name', 'Unknown'))
43
 
44
  space_likes = space.get('likes', 'N/A')
45
 
46
+ formatted_space = f"{space_name} by {space_author} (Likes: {space_likes})"
47
+ formatted_spaces.append(formatted_space)
 
48
 
49
+ return formatted_spaces
50
 
51
+ def get_spaces_list(limit: int = 100) -> List[str]:
52
  spaces = get_most_liked_spaces(limit)
53
  return format_spaces(spaces)
54
 
 
 
 
 
 
 
 
 
 
55
  if __name__ == "__main__":
56
+ # ์ž๋™์œผ๋กœ ์‹คํ–‰ํ•˜๊ณ  ๊ฒฐ๊ณผ ์ถœ๋ ฅ
57
+ result = get_spaces_list()
58
+ for idx, space in enumerate(result, 1):
59
+ print(f"{idx}. {space}")