import json import os import gradio as gr import requests from agent import API, fetch, make_agent, solve def owner(profile): if not profile or profile.username != os.getenv("OWNER_USERNAME", "bestdive"): raise gr.Error("Please sign in as the Space owner.") def smoke(profile: gr.OAuthProfile | None): owner(profile) try: answer = make_agent().run("Use calculate to compute (19 * 7) + 4. Return only the number.") return "Smoke test: " + str(answer) except Exception as exc: return "Model connection failed (" + type(exc).__name__ + "). Check Space Secrets, model availability and provider quota." def evaluate(profile: gr.OAuthProfile | None): owner(profile) make_agent() # Fail before running if credentials are absent. questions = json.loads(fetch(API + "/questions")[0]) rows = [] yield "Starting evaluation...", rows, None for i, question in enumerate(questions): try: answer = solve(question) status = "generated" except Exception as exc: answer, status = "", "error:" + type(exc).__name__ rows.append({"task_id": question["task_id"], "question": question["question"], "submitted_answer": answer, "status": status}) yield f"Completed {i + 1}/{len(questions)}", rows, None yield "Evaluation complete. Review answers before submission.", rows, rows def submit(rows, profile: gr.OAuthProfile | None): owner(profile) if not rows: raise gr.Error("Run an evaluation first.") space = os.getenv("SPACE_ID", "") if not space.startswith(profile.username + "/"): raise gr.Error("Publish this app in your own public Space before submission.") payload = {"username": profile.username, "agent_code": f"https://huggingface.co/spaces/{space}/tree/main", "answers": [{"task_id": r["task_id"], "submitted_answer": r["submitted_answer"]} for r in rows]} response = requests.post(API + "/submit", json=payload, timeout=90) response.raise_for_status() return json.dumps(response.json(), ensure_ascii=False, indent=2) with gr.Blocks() as demo: gr.Markdown("# Bestdive Research Agent\nSign in, test the model connection, run the official questions, then submit.") gr.LoginButton() with gr.Row(): test_button = gr.Button("1. Test model connection") run_button = gr.Button("2. Run official evaluation") submit_button = gr.Button("3. Submit generated answers") status = gr.Textbox(label="Status", lines=5) results = gr.JSON(label="Generated answers") state = gr.State(None) test_button.click(smoke, outputs=status, concurrency_limit=1) run_button.click(evaluate, outputs=[status, results, state], concurrency_limit=1) submit_button.click(submit, inputs=state, outputs=status, concurrency_limit=1) if __name__ == "__main__": demo.queue(default_concurrency_limit=1).launch()