rahul7star commited on
Commit
f3c00ab
·
verified ·
1 Parent(s): fba00bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -32
app.py CHANGED
@@ -5,14 +5,15 @@ from huggingface_hub import HfApi
5
  HF_TOKEN = os.getenv("HF_TOKEN")
6
  api = HfApi(token=HF_TOKEN)
7
 
8
- def scan_spaces_detailed(username: str, filter_hw: str):
 
9
  """
10
- Scan all Spaces for a user, mark ZeroGPU, and filter by hardware.
11
  """
12
  try:
13
  spaces = api.list_spaces(author=username, token=HF_TOKEN)
14
  if not spaces:
15
- return []
16
 
17
  results = []
18
 
@@ -22,7 +23,7 @@ def scan_spaces_detailed(username: str, filter_hw: str):
22
  status = getattr(info, "status", "unknown")
23
  tags = getattr(info, "tags", [])
24
 
25
- # Check runtime requested_hardware
26
  runtime = getattr(info, "runtime", None)
27
  if runtime:
28
  requested_hw = getattr(runtime, "requested_hardware", None)
@@ -37,44 +38,69 @@ def scan_spaces_detailed(username: str, filter_hw: str):
37
  else:
38
  hardware = "ZeroGPU" if "cpu" in tags else "GPU"
39
 
40
- results.append({
41
- "Space ID": space.id,
42
- "Status": status,
43
- "Hardware": hardware,
44
- "Tags": ", ".join(tags)
45
- })
46
 
47
  except Exception as e:
48
- results.append({
49
- "Space ID": space.id,
50
- "Status": f"Error retrieving info: {e}",
51
- "Hardware": "Unknown",
52
- "Tags": ""
53
- })
54
-
55
- # Sort ZeroGPU spaces first
56
- results.sort(key=lambda x: 0 if x["Hardware"] == "ZeroGPU" else 1)
57
 
58
- # Apply hardware filter
59
- if filter_hw != "All":
60
- results = [r for r in results if r["Hardware"] == filter_hw]
61
 
62
- return results
 
 
63
 
64
  except Exception as e:
65
- return [{"Space ID": "Error", "Status": str(e), "Hardware": "", "Tags": ""}]
 
 
 
 
 
 
 
 
66
 
67
  # Gradio UI
68
  with gr.Blocks() as demo:
69
- gr.Markdown("## Hugging Face Spaces Scanner with Hardware Filter")
70
-
71
  with gr.Row():
72
  username_input = gr.Textbox(label="Hugging Face Username", value="rahul7star")
73
- filter_hw = gr.Dropdown(choices=["All", "ZeroGPU", "GPU"], value="All", label="Filter by Hardware")
74
-
75
- scan_button = gr.Button("Scan Spaces")
76
- output_table = gr.Dataframe(headers=["Space ID", "Status", "Hardware", "Tags"], datatype=["str","str","str","str"], interactive=False)
77
-
78
- scan_button.click(scan_spaces_detailed, inputs=[username_input, filter_hw], outputs=output_table)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
 
80
  demo.launch()
 
5
  HF_TOKEN = os.getenv("HF_TOKEN")
6
  api = HfApi(token=HF_TOKEN)
7
 
8
+
9
+ def scan_spaces_detailed(username: str):
10
  """
11
+ Scan all Spaces for a user and return full data in a table.
12
  """
13
  try:
14
  spaces = api.list_spaces(author=username, token=HF_TOKEN)
15
  if not spaces:
16
+ return [], []
17
 
18
  results = []
19
 
 
23
  status = getattr(info, "status", "unknown")
24
  tags = getattr(info, "tags", [])
25
 
26
+ # Detect hardware
27
  runtime = getattr(info, "runtime", None)
28
  if runtime:
29
  requested_hw = getattr(runtime, "requested_hardware", None)
 
38
  else:
39
  hardware = "ZeroGPU" if "cpu" in tags else "GPU"
40
 
41
+ results.append([
42
+ space.id,
43
+ status,
44
+ hardware,
45
+ ", ".join(tags)
46
+ ])
47
 
48
  except Exception as e:
49
+ results.append([
50
+ space.id,
51
+ f"Error retrieving info: {e}",
52
+ "Unknown",
53
+ ""
54
+ ])
 
 
 
55
 
56
+ # Sort ZeroGPU first
57
+ results.sort(key=lambda x: 0 if x[2] == "ZeroGPU" else 1)
 
58
 
59
+ # Return table and unique hardware values for filter
60
+ hardware_values = sorted(list({r[2] for r in results}))
61
+ return results, ["All"] + hardware_values
62
 
63
  except Exception as e:
64
+ return [[f"Error: {e}", "", "", ""]], ["All"]
65
+
66
+
67
+ def filter_table(rows, selected_hw):
68
+ """Filter the table by hardware locally (no API call)."""
69
+ if not rows or selected_hw == "All":
70
+ return rows
71
+ return [r for r in rows if r[2] == selected_hw]
72
+
73
 
74
  # Gradio UI
75
  with gr.Blocks() as demo:
76
+ gr.Markdown("## 🤖 Hugging Face Spaces ZeroGPU Scanner")
77
+
78
  with gr.Row():
79
  username_input = gr.Textbox(label="Hugging Face Username", value="rahul7star")
80
+ scan_button = gr.Button("Scan Spaces")
81
+
82
+ with gr.Row():
83
+ filter_hw = gr.Dropdown(label="Filter by Hardware", choices=["All"], value="All", interactive=True)
84
+
85
+ output_table = gr.Dataframe(
86
+ headers=["Space ID", "Status", "Hardware", "Tags"],
87
+ datatype=["str", "str", "str", "str"],
88
+ interactive=False,
89
+ wrap=True
90
+ )
91
+
92
+ # On scan: fetch all data and populate table + dropdown choices
93
+ scan_event = scan_button.click(
94
+ fn=scan_spaces_detailed,
95
+ inputs=username_input,
96
+ outputs=[output_table, filter_hw]
97
+ )
98
+
99
+ # When filter changes: re-filter table without re-calling API
100
+ filter_hw.change(
101
+ fn=filter_table,
102
+ inputs=[output_table, filter_hw],
103
+ outputs=output_table
104
+ )
105
 
106
  demo.launch()