import requests import gradio as gr from datetime import datetime # 테스트를 위해 Gradio 공식 계정으로 설정 USERNAME = "gradio" 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 search_spaces(): """Search for spaces using the Hugging Face search API""" url = "https://huggingface.co/api/spaces" headers = {"Accept": "application/json"} try: # First, try to get all spaces response = requests.get(url, headers=headers) if response.status_code == 200: all_spaces = response.json() # Filter spaces by username user_spaces = [space for space in all_spaces if space.get('author', '').lower() == USERNAME.lower()] return user_spaces except Exception as e: print(f"Error in search_spaces: {e}") return [] def get_space_card(space): """Generate HTML card for a space""" space_id = space.get('id', '') space_name = space_id.split('/')[-1] if space_id else 'Unknown' return f"""

{space_name}

Author: {space.get('author', 'Unknown')}

SDK: {space.get('sdk', 'N/A')}

Likes: {space.get('likes', 0)} ❤️

View Space
""" def get_user_spaces(): # Get spaces for the user spaces = search_spaces() if not spaces: # Try alternative search method try: response = requests.get( f"https://huggingface.co/search/full-text", params={"q": f"user:{USERNAME} type:space"}, headers={"Accept": "application/json"} ) if response.status_code == 200: search_results = response.json() if 'hits' in search_results: spaces = search_results['hits'] except Exception as e: print(f"Error in alternative search: {e}") if not spaces: return """

No public Spaces found for user: {USERNAME}

This could be because:

""" # Create HTML grid layout html_content = f"""

Found {len(spaces)} public spaces for {USERNAME}

{"".join(get_space_card(space) for space in spaces)}
""" return html_content # Creating the Gradio interface app = gr.Interface( fn=get_user_spaces, inputs=None, outputs=gr.HTML(), title=f"Hugging Face Public Spaces - {USERNAME}", description=f"Displays public Spaces from {USERNAME}", theme=gr.themes.Soft(), css=""" .gradio-container { max-width: 100% !important; } """ ) # Launch the Gradio app if __name__ == "__main__": app.launch()