rahul7star commited on
Commit
b53506b
·
verified ·
1 Parent(s): 362cd4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -19
app.py CHANGED
@@ -7,61 +7,75 @@ api = HfApi(token=HF_TOKEN)
7
 
8
  def scan_spaces_detailed(username: str):
9
  """
10
- Scan all Spaces for a user and log all metadata.
11
- Mark Hardware: ZeroGPU if CPU only.
12
  """
13
- output = ""
14
  try:
15
  spaces = api.list_spaces(author=username, token=HF_TOKEN)
16
  if not spaces:
17
  return f"No Spaces found for user '{username}'"
18
 
 
 
19
  for space in spaces:
20
  try:
21
  info = api.space_info(space.id, token=HF_TOKEN)
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)
29
- # Consider ZeroGPU if requested hardware is "zero-*" (CPU-only)
30
  if requested_hw and requested_hw.lower().startswith("zero"):
31
  hardware = "ZeroGPU"
32
  else:
33
  hardware = "GPU"
34
  else:
35
- # fallback to old method
36
  hardware_field = getattr(info, "hardware", None)
37
  if hardware_field:
38
  hardware = "ZeroGPU" if all("cpu" in h.lower() for h in hardware_field) else "GPU"
39
  else:
40
  hardware = "ZeroGPU" if "cpu" in tags else "GPU"
41
 
42
- # Log all metadata
43
- output += f"Space: {space.id}\n"
44
- output += f" Status: {status}\n"
45
- output += f" Hardware: {hardware}\n"
46
- output += f" Tags: {tags}\n"
47
- output += f" Raw metadata: {info}\n"
48
- output += "-"*50 + "\n"
49
 
50
  except Exception as e:
51
- output += f"{space.id} | Error retrieving info: {e}\n"
52
- output += "-"*50 + "\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  except Exception as e:
55
  return f"Error: {e}"
56
 
57
- return output
58
-
59
  # Gradio UI
60
  with gr.Blocks() as demo:
61
- gr.Markdown("## Hugging Face Spaces Detailed ZeroGPU Scanner")
62
  username_input = gr.Textbox(label="Hugging Face Username", value="rahul7star")
63
  scan_button = gr.Button("Scan Spaces")
64
- output_box = gr.Textbox(label="Detailed Space Metadata", lines=30)
65
 
66
  scan_button.click(scan_spaces_detailed, inputs=username_input, outputs=output_box)
67
 
 
7
 
8
  def scan_spaces_detailed(username: str):
9
  """
10
+ Scan all Spaces for a user, mark ZeroGPU, and sort ZeroGPU spaces first.
 
11
  """
 
12
  try:
13
  spaces = api.list_spaces(author=username, token=HF_TOKEN)
14
  if not spaces:
15
  return f"No Spaces found for user '{username}'"
16
 
17
+ results = []
18
+
19
  for space in spaces:
20
  try:
21
  info = api.space_info(space.id, token=HF_TOKEN)
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)
 
29
  if requested_hw and requested_hw.lower().startswith("zero"):
30
  hardware = "ZeroGPU"
31
  else:
32
  hardware = "GPU"
33
  else:
 
34
  hardware_field = getattr(info, "hardware", None)
35
  if hardware_field:
36
  hardware = "ZeroGPU" if all("cpu" in h.lower() for h in hardware_field) else "GPU"
37
  else:
38
  hardware = "ZeroGPU" if "cpu" in tags else "GPU"
39
 
40
+ results.append({
41
+ "id": space.id,
42
+ "status": status,
43
+ "hardware": hardware,
44
+ "tags": tags
45
+ })
 
46
 
47
  except Exception as e:
48
+ results.append({
49
+ "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
+ # Format output for Gradio (hide raw metadata)
59
+ output = ""
60
+ for r in results:
61
+ line = f"Space: {r['id']}\n"
62
+ line += f" Status: {r['status']}\n"
63
+ line += f" Hardware: {r['hardware']}\n"
64
+ line += f" Tags: {r['tags']}\n"
65
+ line += "-"*50 + "\n"
66
+ output += line
67
+
68
+ return output
69
 
70
  except Exception as e:
71
  return f"Error: {e}"
72
 
 
 
73
  # Gradio UI
74
  with gr.Blocks() as demo:
75
+ gr.Markdown("## Hugging Face Spaces ZeroGPU Scanner (Sorted)")
76
  username_input = gr.Textbox(label="Hugging Face Username", value="rahul7star")
77
  scan_button = gr.Button("Scan Spaces")
78
+ output_box = gr.Textbox(label="Spaces Summary", lines=30)
79
 
80
  scan_button.click(scan_spaces_detailed, inputs=username_input, outputs=output_box)
81