ginipick commited on
Commit
0041e1e
ยท
verified ยท
1 Parent(s): bc636ee

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
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()