rahul7star commited on
Commit
6714a02
·
verified ·
1 Parent(s): 3229259

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -32
app.py CHANGED
@@ -1,34 +1,11 @@
1
  import os
2
  import gradio as gr
3
- import requests
4
  from huggingface_hub import HfApi
5
 
6
  HF_TOKEN = os.getenv("HF_TOKEN")
7
  api = HfApi(token=HF_TOKEN)
8
 
9
- def check_space_live(space_id: str):
10
- """
11
- Query the live HF Space to see if it's running on CPU/ZeroGPU.
12
- Returns: 'ZeroGPU' or 'GPU' or 'Offline'
13
- """
14
- try:
15
- # Most HF Spaces expose this endpoint
16
- url = f"https://hf.space/embed/{space_id}/+/api/status"
17
- headers = {"Authorization": f"Bearer {HF_TOKEN}"} if HF_TOKEN else {}
18
- resp = requests.get(url, headers=headers, timeout=5)
19
- data = resp.json()
20
- # 'cpu' or 'gpu' in hardware field
21
- if "gpu" in str(data.get("hardware", "")).lower():
22
- return "GPU"
23
- else:
24
- return "ZeroGPU"
25
- except Exception:
26
- return "Offline"
27
-
28
- def scan_spaces_live(username: str):
29
- """
30
- Scan all Spaces of a user and return live ZeroGPU status.
31
- """
32
  output = ""
33
  try:
34
  spaces = api.list_spaces(author=username, token=HF_TOKEN)
@@ -36,21 +13,25 @@ def scan_spaces_live(username: str):
36
  return f"No Spaces found for user '{username}'"
37
 
38
  for space in spaces:
39
- live_status = check_space_live(space.id)
40
- output += f"{space.id} | Live Status: {live_status}\n"
41
-
 
 
 
 
 
42
  except Exception as e:
43
  return f"Error: {e}"
44
-
45
  return output
46
 
47
  # Gradio UI
48
  with gr.Blocks() as demo:
49
- gr.Markdown("## Hugging Face Spaces Live ZeroGPU Scanner")
50
  username_input = gr.Textbox(label="Hugging Face Username", value="rahul7star")
51
- scan_button = gr.Button("Scan Live Spaces")
52
- output_box = gr.Textbox(label="Live ZeroGPU Status", lines=25)
53
 
54
- scan_button.click(scan_spaces_live, inputs=username_input, outputs=output_box)
55
 
56
  demo.launch()
 
1
  import os
2
  import gradio as gr
 
3
  from huggingface_hub import HfApi
4
 
5
  HF_TOKEN = os.getenv("HF_TOKEN")
6
  api = HfApi(token=HF_TOKEN)
7
 
8
+ def scan_spaces(username: str):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  output = ""
10
  try:
11
  spaces = api.list_spaces(author=username, token=HF_TOKEN)
 
13
  return f"No Spaces found for user '{username}'"
14
 
15
  for space in spaces:
16
+ try:
17
+ info = api.space_info(space_id=space.id, token=HF_TOKEN)
18
+ status = getattr(info, "status", "unknown")
19
+ hardware = getattr(info, "hardware", [])
20
+ zero_gpu = "Yes" if (not hardware or all("cpu" in h.lower() for h in hardware)) else "No"
21
+ output += f"{space.id} | Status: {status} | ZeroGPU: {zero_gpu}\n"
22
+ except Exception as e:
23
+ output += f"{space.id} | Error retrieving info: {e}\n"
24
  except Exception as e:
25
  return f"Error: {e}"
 
26
  return output
27
 
28
  # Gradio UI
29
  with gr.Blocks() as demo:
30
+ gr.Markdown("## Hugging Face Spaces ZeroGPU Status Scanner")
31
  username_input = gr.Textbox(label="Hugging Face Username", value="rahul7star")
32
+ scan_button = gr.Button("Scan Spaces")
33
+ output_box = gr.Textbox(label="ZeroGPU Status", lines=25)
34
 
35
+ scan_button.click(scan_spaces, inputs=username_input, outputs=output_box)
36
 
37
  demo.launch()