Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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",
|
9 |
+
"direction": -1,
|
10 |
+
"limit": limit,
|
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 |
+
# 업데이트된 데이터 추출 로직
|
39 |
+
space_id = space.get('id', 'Unknown')
|
40 |
+
space_name = space.get('title', 'Unknown')
|
41 |
+
space_author = space.get('author', {}).get('user', '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(
|
56 |
+
fn=get_spaces_list,
|
57 |
+
inputs=gr.Slider(minimum=1, maximum=50, step=1, label="Number of Spaces to Display", value=10),
|
58 |
+
outputs="text",
|
59 |
+
title="Hugging Face Most Liked Spaces",
|
60 |
+
description="Display the most liked Hugging Face Spaces in descending order.",
|
61 |
+
)
|
62 |
+
|
63 |
+
if __name__ == "__main__":
|
64 |
+
iface.launch()
|