bgamazay commited on
Commit
eaa80ff
Β·
verified Β·
1 Parent(s): e139f4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -5
app.py CHANGED
@@ -12,6 +12,29 @@ TOKEN = os.environ.get("DEBUG") # keep your existing env var
12
 
13
  API = HfApi(token=TOKEN)
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  # ---------- Upload to HF dataset (kept from your original) ----------
16
  def add_docker_eval(zip_file):
17
  new_fid = os.path.basename(zip_file)
@@ -61,6 +84,7 @@ with gr.Blocks(title="AI Energy Score") as demo:
61
  """)
62
 
63
  gr.Markdown("<div style='text-align:center;'><h2>Submission Portal</h2></div>")
 
64
 
65
  with gr.Row():
66
  # -------- Open Models ----------
@@ -108,13 +132,24 @@ Run the benchmark **in your own environment** and upload the logs here.
108
  if not temp_path:
109
  gr.Warning("No file selected.")
110
  return "❌ No file uploaded."
111
- # Run your original uploader (pushes to HF dataset + toast)
 
 
 
 
 
 
 
 
 
112
  try:
113
- add_docker_eval(temp_path)
114
- return f"βœ… Received and submitted: **{os.path.basename(temp_path)}**"
 
 
115
  except Exception as e:
116
- gr.Warning(str(e))
117
- return f"❌ Upload failed: {e}"
118
 
119
  # IMPORTANT: bind inside Blocks context
120
  upload_button.upload(
 
12
 
13
  API = HfApi(token=TOKEN)
14
 
15
+ def preflight_status():
16
+ # 1) Check token presence
17
+ if not TOKEN:
18
+ return ("❌ No HF token found in env var 'DEBUG'. "
19
+ "Add a secret named DEBUG in the Space settings (a token with 'write' scope).")
20
+
21
+ # 2) Check identity
22
+ try:
23
+ me = API.whoami(token=TOKEN)
24
+ user_str = me.get("name") or me.get("username") or "unknown-user"
25
+ except Exception as e:
26
+ return f"❌ Token error: cannot authenticate ({e})."
27
+
28
+ # 3) Check dataset access
29
+ repo_id = "AIEnergyScore/tested_proprietary_models"
30
+ try:
31
+ info = API.repo_info(repo_id=repo_id, repo_type="dataset", token=TOKEN)
32
+ # If this succeeds, you at least have read access; write failure will surface during upload.
33
+ return f"βœ… Connected as **{user_str}**. Dataset **{repo_id}** reachable."
34
+ except Exception as e:
35
+ return (f"⚠️ Auth OK as **{user_str}**, but cannot access dataset "
36
+ f"**{repo_id}** ({e}). Make sure the token user has write access.")
37
+
38
  # ---------- Upload to HF dataset (kept from your original) ----------
39
  def add_docker_eval(zip_file):
40
  new_fid = os.path.basename(zip_file)
 
84
  """)
85
 
86
  gr.Markdown("<div style='text-align:center;'><h2>Submission Portal</h2></div>")
87
+ preflight_box = gr.Markdown(preflight_status())
88
 
89
  with gr.Row():
90
  # -------- Open Models ----------
 
132
  if not temp_path:
133
  gr.Warning("No file selected.")
134
  return "❌ No file uploaded."
135
+
136
+ if not TOKEN:
137
+ gr.Warning("Missing HF token in env var 'DEBUG'.")
138
+ return "❌ Upload blocked: missing token (DEBUG)."
139
+
140
+ # Enforce .zip
141
+ if not str(temp_path).lower().endswith(".zip"):
142
+ gr.Warning("Only .zip files are accepted.")
143
+ return "❌ Please upload a .zip file."
144
+
145
  try:
146
+ # Your existing uploader: pushes to AIEnergyScore/tested_proprietary_models/submitted_models/
147
+ add_docker_eval(temp_path) # shows a toast on success/failure internally
148
+ basename = os.path.basename(temp_path)
149
+ return f"βœ… Received and submitted: **{basename}**"
150
  except Exception as e:
151
+ gr.Warning(f"Upload error: {e}")
152
+ return f"❌ Upload failed β€” {e}"
153
 
154
  # IMPORTANT: bind inside Blocks context
155
  upload_button.upload(