openfree commited on
Commit
059f849
1 Parent(s): b0355f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -16
app.py CHANGED
@@ -1,15 +1,19 @@
1
  import os
2
  import requests
3
  import gradio as gr
4
- import pandas as pd
5
  from datetime import datetime
6
 
7
- # Hugging Face API URL
8
- HF_API_URL = "https://huggingface.co/api/spaces"
9
-
10
  # Hugging Face Token
11
  HF_TOKEN = os.getenv("HF_TOKEN")
12
 
 
 
 
 
 
 
 
 
13
  def format_timestamp(timestamp):
14
  if timestamp:
15
  dt = datetime.fromisoformat(timestamp.replace('Z', '+00:00'))
@@ -17,26 +21,43 @@ def format_timestamp(timestamp):
17
  return 'N/A'
18
 
19
  def get_space_card(space):
 
20
  return f"""
21
  <div style='border: 1px solid #ddd; padding: 15px; margin: 10px; border-radius: 8px;
22
  background-color: white; box-shadow: 2px 2px 5px rgba(0,0,0,0.1);'>
23
- <h3 style='color: #2d2d2d; margin: 0 0 10px 0;'>{space.get('name', 'N/A')}</h3>
24
- <p style='margin: 5px 0;'><strong>Author:</strong> {space.get('author', 'N/A')}</p>
25
- <p style='margin: 5px 0;'><strong>SDK:</strong> {space.get('sdk', 'N/A')}</p>
 
 
 
 
 
 
26
  <p style='margin: 5px 0;'><strong>Status:</strong>
27
- <span style='color: {"green" if space.get("status") == "running" else "red"}'>
28
- {space.get('status', 'N/A')}</span></p>
29
- <p style='margin: 5px 0;'><strong>Created:</strong> {format_timestamp(space.get('createdAt'))}</p>
30
- <p style='margin: 5px 0;'><strong>Updated:</strong> {format_timestamp(space.get('updatedAt'))}</p>
 
31
  </div>
32
  """
33
 
34
- def get_all_spaces():
35
  if not HF_TOKEN:
36
  return "Error: Hugging Face token not found."
37
 
 
 
 
 
38
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
39
- response = requests.get(HF_API_URL, headers=headers)
 
 
 
 
 
40
 
41
  if response.status_code != 200:
42
  return f"Error: Failed to fetch spaces (Status Code: {response.status_code})"
@@ -63,11 +84,11 @@ def get_all_spaces():
63
 
64
  # Creating the Gradio interface
65
  app = gr.Interface(
66
- fn=get_all_spaces,
67
  inputs=None,
68
  outputs=gr.HTML(),
69
- title="Hugging Face Spaces Dashboard",
70
- description="Displays all available Spaces in a grid layout",
71
  theme=gr.themes.Soft(),
72
  css="""
73
  .gradio-container {
 
1
  import os
2
  import requests
3
  import gradio as gr
 
4
  from datetime import datetime
5
 
 
 
 
6
  # Hugging Face Token
7
  HF_TOKEN = os.getenv("HF_TOKEN")
8
 
9
+ def get_user_info():
10
+ """Get username from token"""
11
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
12
+ response = requests.get("https://huggingface.co/api/whoami", headers=headers)
13
+ if response.status_code == 200:
14
+ return response.json().get('name')
15
+ return None
16
+
17
  def format_timestamp(timestamp):
18
  if timestamp:
19
  dt = datetime.fromisoformat(timestamp.replace('Z', '+00:00'))
 
21
  return 'N/A'
22
 
23
  def get_space_card(space):
24
+ """Generate HTML card for a space"""
25
  return f"""
26
  <div style='border: 1px solid #ddd; padding: 15px; margin: 10px; border-radius: 8px;
27
  background-color: white; box-shadow: 2px 2px 5px rgba(0,0,0,0.1);'>
28
+ <h3 style='color: #2d2d2d; margin: 0 0 10px 0;'>
29
+ <a href='https://huggingface.co/spaces/{space["id"]}' target='_blank'
30
+ style='text-decoration: none; color: #2d2d2d;'>
31
+ {space["id"].split("/")[-1]}
32
+ </a>
33
+ </h3>
34
+ <p style='margin: 5px 0;'><strong>Space ID:</strong> {space["id"]}</p>
35
+ <p style='margin: 5px 0;'><strong>Runtime:</strong> {space.get("runtime", {}).get("stage", "N/A")}</p>
36
+ <p style='margin: 5px 0;'><strong>Hardware:</strong> {space.get("runtime", {}).get("hardware", "N/A")}</p>
37
  <p style='margin: 5px 0;'><strong>Status:</strong>
38
+ <span style='color: {"green" if space.get("runtime", {}).get("stage") == "RUNNING" else "red"}'>
39
+ {space.get("runtime", {}).get("stage", "N/A")}</span>
40
+ </p>
41
+ <p style='margin: 5px 0;'><strong>Created:</strong> {format_timestamp(space.get("createdAt"))}</p>
42
+ <p style='margin: 5px 0;'><strong>Updated:</strong> {format_timestamp(space.get("lastModified"))}</p>
43
  </div>
44
  """
45
 
46
+ def get_user_spaces():
47
  if not HF_TOKEN:
48
  return "Error: Hugging Face token not found."
49
 
50
+ username = get_user_info()
51
+ if not username:
52
+ return "Error: Could not get user information."
53
+
54
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
55
+
56
+ # Get user's spaces
57
+ response = requests.get(
58
+ f"https://huggingface.co/api/spaces/{username}",
59
+ headers=headers
60
+ )
61
 
62
  if response.status_code != 200:
63
  return f"Error: Failed to fetch spaces (Status Code: {response.status_code})"
 
84
 
85
  # Creating the Gradio interface
86
  app = gr.Interface(
87
+ fn=get_user_spaces,
88
  inputs=None,
89
  outputs=gr.HTML(),
90
+ title="My Hugging Face Spaces Dashboard",
91
+ description="Displays your Hugging Face Spaces in a grid layout",
92
  theme=gr.themes.Soft(),
93
  css="""
94
  .gradio-container {