Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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",
|
10 |
+
"direction": -1,
|
11 |
+
"limit": limit,
|
12 |
+
"full": "true"
|
13 |
+
}
|
14 |
+
|
15 |
+
try:
|
16 |
+
response = requests.get(url, params=params)
|
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:
|
27 |
+
return f"Unexpected API response format: {type(data)}"
|
28 |
+
except requests.RequestException as e:
|
29 |
+
return f"API request error: {str(e)}"
|
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()
|