openfree commited on
Commit
514cd38
1 Parent(s): dffb689

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -9
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import os
2
  import requests
3
  import gradio as gr
 
 
4
 
5
  # Hugging Face API URL
6
  HF_API_URL = "https://huggingface.co/api/spaces"
@@ -8,7 +10,27 @@ HF_API_URL = "https://huggingface.co/api/spaces"
8
  # Hugging Face Token
9
  HF_TOKEN = os.getenv("HF_TOKEN")
10
 
11
- # Function to get all Spaces
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  def get_all_spaces():
13
  if not HF_TOKEN:
14
  return "Error: Hugging Face token not found."
@@ -24,15 +46,36 @@ def get_all_spaces():
24
  if not spaces:
25
  return "No Spaces found."
26
 
27
- # Formatting Spaces into a grid, handling missing keys with .get()
28
- space_info = [
29
- f"Name: {space.get('name', 'N/A')}\nSDK: {space.get('sdk', 'N/A')}\nStatus: {space.get('status', 'N/A')}"
30
- for space in spaces
31
- ]
32
- return "\n\n".join(space_info)
 
 
 
 
 
 
 
 
33
 
34
  # Creating the Gradio interface
35
- app = gr.Interface(fn=get_all_spaces, inputs=None, outputs="text")
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  # Launch the Gradio app
38
- app.launch()
 
 
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"
 
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'))
16
+ return dt.strftime('%Y-%m-%d %H:%M')
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."
 
46
  if not spaces:
47
  return "No Spaces found."
48
 
49
+ # Create HTML grid layout
50
+ html_content = f"""
51
+ <div style='
52
+ display: grid;
53
+ grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
54
+ gap: 20px;
55
+ padding: 20px;
56
+ background-color: #f5f5f5;
57
+ '>
58
+ {"".join(get_space_card(space) for space in spaces)}
59
+ </div>
60
+ """
61
+
62
+ return html_content
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 {
74
+ max-width: 100% !important;
75
+ }
76
+ """
77
+ )
78
 
79
  # Launch the Gradio app
80
+ if __name__ == "__main__":
81
+ app.launch()