import requests import gradio as gr from datetime import datetime USERNAME = "openfree" LIMIT = 100 # 한 번에 가져올 최대 spaces 수 def format_timestamp(timestamp): if timestamp: dt = datetime.fromisoformat(timestamp.replace('Z', '+00:00')) return dt.strftime('%Y-%m-%d %H:%M') return 'N/A' def get_space_card(space): """Generate HTML card for a space""" space_id = space.get('id', '') space_name = space_id.split('/')[-1] likes = space.get('likes', 0) created_at = format_timestamp(space.get('createdAt')) sdk = space.get('sdk', 'N/A') return f"""
""" def get_all_user_spaces(): """Get all spaces for a user with pagination""" all_spaces = [] offset = 0 while True: url = f"https://huggingface.co/api/spaces" params = { "limit": LIMIT, "offset": offset, "full": "true" } headers = { "Accept": "application/json", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" } try: response = requests.get(url, params=params, headers=headers) if response.status_code != 200: print(f"Error: Status Code {response.status_code}") break spaces_data = response.json() if not spaces_data: break # Filter spaces for the specific user user_spaces = [space for space in spaces_data if space.get('id', '').startswith(f"{USERNAME}/")] all_spaces.extend(user_spaces) # If we got less than the limit, we've reached the end if len(spaces_data) < LIMIT: break offset += LIMIT except Exception as e: print(f"Error fetching spaces: {e}") break return all_spaces def get_user_spaces(): try: # Get all spaces for the user user_spaces = get_all_user_spaces() if not user_spaces: return f"""Try visiting: https://huggingface.co/{USERNAME}
Found {len(user_spaces)} public spaces
Error details: {str(e)}
Please try again later.