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 = """ Hugging Face Most Liked Spaces

Hugging Face Most Liked Spaces

    {% for space in spaces %}
  1. {{ space }}
  2. {% endfor %}
""" return render_template_string(html_template, spaces=formatted_spaces) if __name__ == "__main__": app.run(host='0.0.0.0', port=7860)