Spaces:
Sleeping
Sleeping
Update app.py
Browse files9 unmodified lines10111213141315161517181920212223242526272829303132333435363738394041171819204243444546474815 unmodified lines64656642436768694570477172737475767778798081498283848534 unmodified lines12012112212312412512612712812913013113213313442 unmodified lines1771781791801811821831841851861871881891901911921931941951961979 unmodified lines
# --- Basic Agent Definition ---# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
# --- GAIA Agent Definition ---class BasicAgent:class GAIAAgent: def __init__(self): print("BasicAgent initialized.") 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].content print(f"Raw output: {raw_output}")
# Extract FINAL ANSWER if "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_answer# 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_answer
def run_and_submit_all( profile: gr.OAuthProfile | None): """15 unmodified lines submit_url = f"{api_url}/submit"
# 1. Instantiate Agent ( modify this part to create your agent) try: agent = BasicAgent() try: agent = GAIAAgent() 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(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) # print(agent_code)
# 2. Fetch Questions print(f"Fetching questions from: {questions_url}")34 unmodified lines print(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 file save_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 Submission submission_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 lines results_df = pd.DataFrame(results_log) return status_message, results_df
def 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