Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
import requests
|
2 |
import gradio as gr
|
3 |
-
from typing import List, Dict
|
4 |
|
5 |
-
def get_most_liked_spaces(limit: int = 10) -> List[Dict]:
|
6 |
url = "https://huggingface.co/api/spaces"
|
7 |
params = {
|
8 |
"sort": "likes",
|
@@ -11,35 +11,45 @@ def get_most_liked_spaces(limit: int = 10) -> List[Dict]:
|
|
11 |
"full": "true"
|
12 |
}
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
def format_spaces(spaces: List[Dict]) -> str:
|
|
|
|
|
|
|
20 |
output = ""
|
21 |
for idx, space in enumerate(spaces, 1):
|
22 |
-
|
|
|
|
|
|
|
23 |
space_id = space.get('id', 'Unknown')
|
24 |
-
space_name = space.get('title', space.get('name', 'Unknown'))
|
25 |
-
|
|
|
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 |
-
|
|
|
32 |
|
33 |
def get_spaces_list(limit: int) -> str:
|
34 |
-
|
35 |
-
|
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(
|
|
|
1 |
import requests
|
2 |
import gradio as gr
|
3 |
+
from typing import List, Dict, Union
|
4 |
|
5 |
+
def get_most_liked_spaces(limit: int = 10) -> Union[List[Dict], str]:
|
6 |
url = "https://huggingface.co/api/spaces"
|
7 |
params = {
|
8 |
"sort": "likes",
|
|
|
11 |
"full": "true"
|
12 |
}
|
13 |
|
14 |
+
try:
|
15 |
+
response = requests.get(url, params=params)
|
16 |
+
response.raise_for_status()
|
17 |
+
data = response.json()
|
18 |
+
|
19 |
+
if isinstance(data, list):
|
20 |
+
return data
|
21 |
+
else:
|
22 |
+
return f"Unexpected API response format: {type(data)}"
|
23 |
+
except requests.RequestException as e:
|
24 |
+
return f"API request error: {str(e)}"
|
25 |
+
except ValueError as e:
|
26 |
+
return f"JSON decoding error: {str(e)}"
|
27 |
|
28 |
+
def format_spaces(spaces: Union[List[Dict], str]) -> str:
|
29 |
+
if isinstance(spaces, str):
|
30 |
+
return spaces # 이미 오류 메시지인 경우 그대로 반환
|
31 |
+
|
32 |
output = ""
|
33 |
for idx, space in enumerate(spaces, 1):
|
34 |
+
if not isinstance(space, dict):
|
35 |
+
output += f"{idx}. Unexpected space data format: {type(space)}\n\n"
|
36 |
+
continue
|
37 |
+
|
38 |
space_id = space.get('id', 'Unknown')
|
39 |
+
space_name = space.get('title', space.get('name', 'Unknown'))
|
40 |
+
author_data = space.get('author', {})
|
41 |
+
space_author = author_data.get('name', 'Unknown') if isinstance(author_data, dict) else 'Unknown'
|
42 |
space_likes = space.get('likes', 'N/A')
|
43 |
|
44 |
output += f"{idx}. {space_name} by {space_author}\n"
|
45 |
output += f" Likes: {space_likes}\n"
|
46 |
output += f" URL: https://huggingface.co/spaces/{space_id}\n\n"
|
47 |
+
|
48 |
+
return output if output else "No valid space data found."
|
49 |
|
50 |
def get_spaces_list(limit: int) -> str:
|
51 |
+
spaces = get_most_liked_spaces(limit)
|
52 |
+
return format_spaces(spaces)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
# Gradio 인터페이스 정의
|
55 |
iface = gr.Interface(
|