spaces-research-korea / app-backup1.py
ginipick's picture
Rename app.py to app-backup1.py
e70874f verified
raw
history blame
2.2 kB
import requests
import gradio as gr
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()
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\n"
continue
space_id = space.get('id', 'Unknown')
space_name = space.get('title', space.get('name', 'Unknown'))
author_data = space.get('author', {})
space_author = author_data.get('name', 'Unknown') if isinstance(author_data, dict) else '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()