File size: 2,461 Bytes
0041e1e
9665223
0041e1e
 
9665223
 
06391aa
0041e1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06391aa
0041e1e
06391aa
0041e1e
06391aa
 
0041e1e
06391aa
0041e1e
 
 
 
 
 
 
 
 
 
 
06391aa
 
0041e1e
06391aa
0041e1e
9665223
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0041e1e
 
9665223
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
76
77
78
79
80
81
import requests
from flask import Flask, render_template_string
from typing import List, Dict, Union

app = Flask(__name__)

def get_most_liked_spaces(limit: int = 100) -> 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()
        
        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]) -> List[str]:
    if isinstance(spaces, str):
        return [spaces]  # 오류 메시지를 리스트로 반환
    
    formatted_spaces = []
    for space in spaces:
        if not isinstance(space, dict):
            formatted_spaces.append(f"Unexpected space data format: {type(space)}")
            continue
        
        space_id = space.get('id', 'Unknown')
        space_name = space_id.split('/')[-1] if '/' in space_id else space_id
        
        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')
        
        formatted_space = f"{space_name} by {space_author} (Likes: {space_likes})"
        formatted_spaces.append(formatted_space)
    
    return formatted_spaces

@app.route('/')
def index():
    spaces_list = get_most_liked_spaces()
    formatted_spaces = format_spaces(spaces_list)
    
    html_template = """
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Hugging Face Most Liked Spaces</title>
    </head>
    <body>
        <h1>Hugging Face Most Liked Spaces</h1>
        <ol>
        {% for space in spaces %}
            <li>{{ space }}</li>
        {% endfor %}
        </ol>
    </body>
    </html>
    """
    
    return render_template_string(html_template, spaces=formatted_spaces)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=7860)