File size: 3,486 Bytes
f61429a
 
 
514cd38
f61429a
 
 
629e9cd
 
 
 
 
 
 
 
 
 
 
 
8ccc387
 
 
629e9cd
 
 
 
 
 
 
 
 
059f849
514cd38
 
 
 
 
 
 
059f849
514cd38
 
 
059f849
2d1b715
059f849
2d1b715
059f849
 
2d1b715
 
 
 
514cd38
 
 
059f849
f61429a
 
 
2d1b715
 
 
 
059f849
2d1b715
059f849
2d1b715
 
059f849
f61429a
 
2d1b715
f61429a
 
 
2d1b715
6cb3060
f61429a
2d1b715
 
 
514cd38
 
 
 
 
 
 
 
 
2d1b715
514cd38
 
 
 
f61429a
 
514cd38
059f849
514cd38
 
7fad5b1
 
514cd38
 
 
 
 
 
 
f61429a
 
514cd38
629e9cd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import os
import requests
import gradio as gr
from datetime import datetime

# Hugging Face Token
HF_TOKEN = os.getenv("HF_TOKEN")

def get_username_from_token():
    """Retrieve the username using the Hugging Face token"""
    if not HF_TOKEN:
        return None

    headers = {
        "Authorization": f"Bearer {HF_TOKEN}",
        "Accept": "application/json"
    }

    response = requests.get("https://huggingface.co/api/whoami", headers=headers)
    print(f"Response status: {response.status_code}")
    print(f"Response content: {response.text}")

    if response.status_code == 200:
        return response.json().get("name")
    return None

# Retrieve the username
USERNAME = get_username_from_token()

if not USERNAME:
    raise ValueError("Error: Could not retrieve username. Check your Hugging Face token.")

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"""
    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/{space["repo_id"]}' target='_blank' 
               style='text-decoration: none; color: #2d2d2d;'>
                {space["repo_id"].split('/')[-1]}
            </a>
        </h3>
        <p style='margin: 5px 0;'><strong>Repository:</strong> {space["repo_id"]}</p>
        <p style='margin: 5px 0;'><strong>Private:</strong> {space.get("private", False)}</p>
        <p style='margin: 5px 0;'><strong>Last Modified:</strong> {format_timestamp(space.get("lastModified"))}</p>
        <p style='margin: 5px 0;'><strong>Space SDK:</strong> {space.get("sdk", "N/A")}</p>
    </div>
    """

def get_user_spaces():
    if not HF_TOKEN:
        return "Error: Hugging Face token not found."

    headers = {
        "Authorization": f"Bearer {HF_TOKEN}",
        "Accept": "application/json"
    }
    
    # Get user's repositories filtered by space type
    response = requests.get(
        f"https://huggingface.co/api/models?author={USERNAME}&filter=space",
        headers=headers
    )

    if response.status_code != 200:
        return f"Error: Failed to fetch spaces (Status Code: {response.status_code})\nResponse: {response.text}"

    spaces = response.json()

    if not spaces:
        return "No Spaces found for this user."

    # Print response for debugging
    print(f"Found {len(spaces)} spaces: {spaces}")

    # 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 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()