Spaces:
Sleeping
Sleeping
Update app.py
Browse files9 unmodified lines101112131415161718192021222315 unmodified lines394041424344454647484950515234 unmodified lines8788899091929394959642 unmodified lines1391401411421431449 unmodified lines# --- Basic Agent Definition ---# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------class BasicAgent:def __init__(self):print("BasicAgent initialized.")def __call__(self, question: str) -> str:print(f"Agent received question (first 50 chars): {question[:50]}...")fixed_answer = "This is a default answer."print(f"Agent returning fixed answer: {fixed_answer}")return fixed_answerdef run_and_submit_all( profile: gr.OAuthProfile | None):"""15 unmodified linessubmit_url = f"{api_url}/submit"# 1. Instantiate Agent ( modify this part to create your agent)try:agent = BasicAgent()except Exception as e:print(f"Error instantiating agent: {e}")return f"Error initializing agent: {e}", None# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"print(agent_code)# 2. Fetch Questionsprint(f"Fetching questions from: {questions_url}")34 unmodified linesprint(f"Error running agent on task {task_id}: {e}")results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})if not answers_payload:print("Agent did not produce any answers to submit.")return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)# 4. Prepare Submissionsubmission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."42 unmodified linesresults_df = pd.DataFrame(results_log)return status_message, results_df# --- Build Gradio Interface using Blocks ---with gr.Blocks() as demo:9 unmodified lines10111213141516171819202122232425262728293031323334353637383940414243444546474815 unmodified lines6465666768697071727374757677787980818283848534 unmodified lines12012112212312412512612712812913013113213313442 unmodified lines1771781791801811821831841851861871881891901911921931941951961979 unmodified lines# --- Basic Agent Definition ---# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------# --- GAIA Agent Definition ---class GAIAAgent:def __init__(self):print("Initializing GAIAAgent using build_graph()...")self.graph = build_graph()def __call__(self, question: str) -> str:print(f"Received question: {question[:60]}...")try:messages = [HumanMessage(content=question)]result = self.graph.invoke({"messages": messages})raw_output = result["messages"][-1].contentprint(f"Raw output: {raw_output}")# Extract FINAL ANSWERif "FINAL ANSWER:" in raw_output:return raw_output.split("FINAL ANSWER:")[-1].strip()else:print("⚠️ 'FINAL ANSWER:' not found — returning full output")return raw_output.strip()except Exception as e:print(f"Agent error: {e}")return f"AGENT ERROR: {e}"# class BasicAgent:# def __init__(self):# print("BasicAgent initialized.")# def __call__(self, question: str) -> str:# print(f"Agent received question (first 50 chars): {question[:50]}...")# fixed_answer = "This is a default answer."# print(f"Agent returning fixed answer: {fixed_answer}")# return fixed_answerdef run_and_submit_all( profile: gr.OAuthProfile | None):"""15 unmodified linessubmit_url = f"{api_url}/submit"# 1. Instantiate Agent ( modify this part to create your agent)try:agent = GAIAAgent()except Exception as e:return f"Error initializing agent: {e}", Noneagent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"print(f"Agent code URL: {agent_code}")# try:# agent = BasicAgent()# except Exception as e:# print(f"Error instantiating agent: {e}")# return f"Error initializing agent: {e}", None# In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)# agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"# print(agent_code)# 2. Fetch Questionsprint(f"Fetching questions from: {questions_url}")34 unmodified linesprint(f"Error running agent on task {task_id}: {e}")results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})# Save answers to filesave_answers_to_jsonl(answers_payload, filename="gaia_submission.jsonl")if not answers_payload:print("Agent did not produce any answers to submit.")return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)# 4. Prepare Submissionsubmission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."42 unmodified linesresults_df = pd.DataFrame(results_log)return status_message, results_dfdef save_answers_to_jsonl(answers_payload, filename="gaia_submission.jsonl"):"""Save the agent's answers to a .jsonl file for manual submission."""try:with open(filename, "w", encoding="utf-8") as f:for answer in answers_payload:f.write(json.dumps({"task_id": answer["task_id"],"model_answer": answer["submitted_answer"]}) + "\n")print(f" Saved {len(answers_payload)} answers to {filename}")except Exception as e:print(f" Error saving answers to file: {e}")# --- Build Gradio Interface using Blocks ---with gr.Blocks() as demo:
app.py+64-11