LeafCat79 commited on
Commit
32aa7c0
·
verified ·
1 Parent(s): 27c8f1d

Add safe HF token validity check

Browse files
Files changed (1) hide show
  1. app.py +22 -0
app.py CHANGED
@@ -1357,6 +1357,25 @@ def generate_images_and_game(html_code: str, roles: str, style_hint: str):
1357
  return rewritten, status, gallery, build_prompt_preview(specs), build_model_report(model_rows), build_preview(rewritten)
1358
 
1359
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1360
  with gr.Blocks(title="Image Generator for HTML Games") as demo:
1361
  gr.Markdown(
1362
  "# Image Generator for HTML Games\n"
@@ -1379,6 +1398,8 @@ with gr.Blocks(title="Image Generator for HTML Games") as demo:
1379
  )
1380
  generate_btn = gr.Button("Generate Images + Embed Game", variant="primary")
1381
  status = gr.Markdown("Ready.")
 
 
1382
  gallery = gr.Gallery(label="Generated assets", columns=2, height=300)
1383
 
1384
  with gr.Column(scale=2):
@@ -1411,6 +1432,7 @@ with gr.Blocks(title="Image Generator for HTML Games") as demo:
1411
  inputs=[html_input, roles, style],
1412
  outputs=[output_code, status, gallery, prompt_preview, model_report, preview],
1413
  )
 
1414
 
1415
 
1416
  if __name__ == "__main__":
 
1357
  return rewritten, status, gallery, build_prompt_preview(specs), build_model_report(model_rows), build_preview(rewritten)
1358
 
1359
 
1360
+ def check_hf_token() -> str:
1361
+ if not HF_TOKEN:
1362
+ return "HF_TOKEN is missing or not visible to the Space runtime."
1363
+
1364
+ request = Request(
1365
+ "https://huggingface.co/api/whoami-v2",
1366
+ headers={"Authorization": f"Bearer {HF_TOKEN}"},
1367
+ method="GET",
1368
+ )
1369
+ try:
1370
+ with urlopen(request, timeout=30) as response:
1371
+ raw = response.read().decode("utf-8", errors="replace")
1372
+ data = json.loads(raw)
1373
+ name = data.get("name") or data.get("fullname") or "authenticated account"
1374
+ return f"HF_TOKEN is visible and valid for {name}."
1375
+ except Exception as exc:
1376
+ return f"HF_TOKEN check failed: {short_error(exc)}"
1377
+
1378
+
1379
  with gr.Blocks(title="Image Generator for HTML Games") as demo:
1380
  gr.Markdown(
1381
  "# Image Generator for HTML Games\n"
 
1398
  )
1399
  generate_btn = gr.Button("Generate Images + Embed Game", variant="primary")
1400
  status = gr.Markdown("Ready.")
1401
+ token_btn = gr.Button("Check HF Token")
1402
+ token_status = gr.Markdown("")
1403
  gallery = gr.Gallery(label="Generated assets", columns=2, height=300)
1404
 
1405
  with gr.Column(scale=2):
 
1432
  inputs=[html_input, roles, style],
1433
  outputs=[output_code, status, gallery, prompt_preview, model_report, preview],
1434
  )
1435
+ token_btn.click(fn=check_hf_token, inputs=None, outputs=token_status)
1436
 
1437
 
1438
  if __name__ == "__main__":