openfree's picture
Update app.py
6cb3060 verified
raw
history blame
3.06 kB
import os
import requests
import gradio as gr
from datetime import datetime
# Hugging Face Token
HF_TOKEN = os.getenv("HF_TOKEN")
USERNAME = "openfree" # 특정 사용자 ID 설정
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', '').split('/')[-1]
return f"""
<div style='border: 1px solid #ddd; padding: 15px; margin: 10px; border-radius: 8px;
background-color: white; box-shadow: 2px 2px 5px rgba(0,0,0,0.1);'>
<h3 style='color: #2d2d2d; margin: 0 0 10px 0;'>
<a href='https://huggingface.co/spaces/{USERNAME}/{space_id}' target='_blank'
style='text-decoration: none; color: #2d2d2d;'>
{space_id}
</a>
</h3>
<p style='margin: 5px 0;'><strong>Space ID:</strong> {space_id}</p>
<p style='margin: 5px 0;'><strong>SDK:</strong> {space.get('sdk', 'N/A')}</p>
<p style='margin: 5px 0;'><strong>Status:</strong>
<span style='color: {"green" if space.get("status") == "running" else "red"}'>
{space.get('status', 'N/A')}</span>
</p>
<p style='margin: 5px 0;'><strong>Created:</strong> {format_timestamp(space.get("createdAt"))}</p>
<p style='margin: 5px 0;'><strong>Updated:</strong> {format_timestamp(space.get("updatedAt"))}</p>
</div>
"""
def get_user_spaces():
if not HF_TOKEN:
return "Error: Hugging Face token not found."
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
# Get all spaces and filter by username
response = requests.get(
"https://huggingface.co/api/spaces",
headers=headers,
params={"author": USERNAME} # Filter by author
)
if response.status_code != 200:
return f"Error: Failed to fetch spaces (Status Code: {response.status_code})"
spaces = response.json()
# Filter spaces for the specific user
user_spaces = [space for space in spaces if space.get('author', '') == USERNAME]
if not user_spaces:
return "No Spaces found for this user."
# Create HTML grid layout
html_content = f"""
<div style='
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 20px;
padding: 20px;
background-color: #f5f5f5;
'>
{"".join(get_space_card(space) for space in user_spaces)}
</div>
"""
return html_content
# Creating the Gradio interface
app = gr.Interface(
fn=get_user_spaces,
inputs=None,
outputs=gr.HTML(),
title=f"Hugging Face Spaces Dashboard - {USERNAME}",
description=f"Displays {USERNAME}'s Hugging Face Spaces in a grid layout",
theme=gr.themes.Soft(),
css="""
.gradio-container {
max-width: 100% !important;
}
"""
)
# Launch the Gradio app
if __name__ == "__main__":
app.launch()