Spaces:
Running
Running
Update app.py
Browse files
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 =
|
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 |
-
|
38 |
-
for
|
39 |
if not isinstance(space, dict):
|
40 |
-
|
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 |
-
|
56 |
-
|
57 |
-
output += f" URL: https://huggingface.co/spaces/{space_id}\n\n"
|
58 |
|
59 |
-
return
|
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 |
-
|
|
|
|
|
|
|
|
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}")
|