File size: 2,607 Bytes
3198da0
 
a90af14
3198da0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a90af14
 
 
 
3198da0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a90af14
 
3198da0
 
3f3f818
3198da0
3f3f818
a90af14
3f3f818
 
 
 
a90af14
3198da0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import requests
import gradio as gr
import json
from typing import List, Dict, Union

def get_most_liked_spaces(limit: int = 10) -> Union[List[Dict], str]:
    url = "https://huggingface.co/api/spaces"
    params = {
        "sort": "likes",
        "direction": -1,
        "limit": limit,
        "full": "true"
    }
    
    try:
        response = requests.get(url, params=params)
        response.raise_for_status()
        data = response.json()
        
        # ๋””๋ฒ„๊น…: ์ „์ฒด ์‘๋‹ต ๊ตฌ์กฐ ์ถœ๋ ฅ
        print("API Response Structure:")
        print(json.dumps(data[:2], indent=2))  # ์ฒ˜์Œ ๋‘ ๊ฐœ์˜ ํ•ญ๋ชฉ๋งŒ ์ถœ๋ ฅ
        
        if isinstance(data, list):
            return data
        else:
            return f"Unexpected API response format: {type(data)}"
    except requests.RequestException as e:
        return f"API request error: {str(e)}"
    except ValueError as e:
        return f"JSON decoding error: {str(e)}"

def format_spaces(spaces: Union[List[Dict], str]) -> str:
    if isinstance(spaces, str):
        return spaces  # ์ด๋ฏธ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€์ธ ๊ฒฝ์šฐ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜
    
    output = ""
    for idx, space in enumerate(spaces, 1):
        if not isinstance(space, dict):
            output += f"{idx}. Unexpected space data format: {type(space)}\n"
            output += f"   Content: {space}\n\n"
            continue
        
        # 'id' ํ•„๋“œ์—์„œ space ์ด๋ฆ„ ์ถ”์ถœ
        space_id = space.get('id', 'Unknown')
        space_name = space_id.split('/')[-1] if '/' in space_id else space_id
        
        # 'author' ํ•„๋“œ์—์„œ ์ž‘์„ฑ์ž ์ •๋ณด ์ถ”์ถœ
        space_author = space.get('author', 'Unknown')
        if isinstance(space_author, dict):
            space_author = space_author.get('user', space_author.get('name', 'Unknown'))
        
        space_likes = space.get('likes', 'N/A')
        
        output += f"{idx}. {space_name} by {space_author}\n"
        output += f"   Likes: {space_likes}\n"
        output += f"   URL: https://huggingface.co/spaces/{space_id}\n\n"
    
    return output if output else "No valid space data found."

def get_spaces_list(limit: int) -> str:
    spaces = get_most_liked_spaces(limit)
    return format_spaces(spaces)

# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
iface = gr.Interface(
    fn=get_spaces_list,
    inputs=gr.Slider(minimum=1, maximum=50, step=1, label="Number of Spaces to Display", value=10),
    outputs="text",
    title="Hugging Face Most Liked Spaces",
    description="Display the most liked Hugging Face Spaces in descending order.",
)

if __name__ == "__main__":
    iface.launch()