import requests import gradio as gr from datetime import datetime USERNAME = "openfree" 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"""

{space_name}

SDK: {sdk}

Created: {created_at}

Likes: {likes} ❤️

View Space ID: {space_id}
""" def get_user_spaces(): url = f"https://huggingface.co/api/spaces?author={USERNAME}&limit=500" # API 쿼리 수정 headers = { "Accept": "application/json", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" } try: response = requests.get(url, headers=headers) print(f"Status Code: {response.status_code}") print(f"Response length: {len(response.json()) if response.status_code == 200 else 'N/A'}") if response.status_code != 200: return f"Error: Failed to fetch spaces (Status Code: {response.status_code})" spaces_data = response.json() # Filter spaces for the specific user (although API should already filter) user_spaces = spaces_data if not user_spaces: return f"""

No public Spaces found for user: {USERNAME}

Try visiting: https://huggingface.co/{USERNAME}

""" # Sort spaces by likes (most liked first) user_spaces.sort(key=lambda x: x.get('likes', 0), reverse=True) # Create HTML grid layout html_content = f"""

Spaces by {USERNAME}

Found {len(user_spaces)} public spaces

{"".join(get_space_card(space) for space in user_spaces)}
""" return html_content except Exception as e: print(f"Error: {str(e)}") return f"""

Error occurred while fetching spaces

Error details: {str(e)}

Please try again later.

""" # Creating the Gradio interface with automatic refresh with gr.Blocks() as app: html_output = gr.HTML() # 시작할 때 자동으로 데이터 로드 gr.on( triggers=[], # 빈 triggers는 페이지 로드 시 실행을 의미 fn=get_user_spaces, outputs=[html_output], ) # Launch the Gradio app if __name__ == "__main__": app.launch()