Spaces:
Sleeping
Sleeping
Update app.py
Browse files23 unmodified lines24252627282930313233344 unmodified lines39404142434445464748491 unmodified line5152535455561 unmodified line585960616263646554 unmodified lines12012112212312412512612712812913023 unmodified lineshuggingfacehub_api_token=os.environ.get("HUGGINGFACEHUB_API_TOKEN"),)chat = ChatHuggingFace(llm=llm, verbose=True)search_tool = DuckDuckGoSearchRun()tools = [search_tool]chat_with_tools = chat.bind_tools(tools)class AgentState(TypedDict):4 unmodified lines"messages": [chat_with_tools.invoke(state["messages"])]}# Graphbuilder = StateGraph(AgentState)# Graph Nodesbuilder.add_node("assistant", assistant)builder.add_node("tools", ToolNode(tools))#Graph Edgesbuilder.add_edge(START, "assistant")builder.add_conditional_edges(1 unmodified linetools_condition)builder.add_edge("tools", "assistant")my_agent = builder.compile()class BasicAgent:1 unmodified lineprint("BasicAgent initialized.")def __call__(self, question: str) -> str:print(f"Agent received question (first 50 chars): {question[:50]}...")messages = [HumanMessage(content=question)]response_state = my_agent.invoke({"messages": messages})final_answer = response_state["messages"][-1].contentreturn final_answer54 unmodified linesfor item in questions_data:task_id = item.get("task_id")question_text = item.get("question")if not task_id or question_text is None:print(f"Skipping item with missing task_id or question: {item}")continuetry:submitted_answer = agent(question_text)answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})except Exception as e:23 unmodified lines242526272829303132333435363738394041424344454647484950515253545556574 unmodified lines626364656667686970717273741 unmodified line76777879808182831 unmodified line85868788899091929394959697989910010110210310410510610710810954 unmodified lines16416516616716816917017117217317417517623 unmodified lineshuggingfacehub_api_token=os.environ.get("HUGGINGFACEHUB_API_TOKEN"),)system_prompt = """You are an AI assistant taking the GAIA benchmark. You must output ONLY the final answer. Do not include any explanations, conversational text or formatting. If the answer is a number, output just the number. If it is a list, output a comma-separated list."""chat = ChatHuggingFace(llm=llm, verbose=True)# Tool Definitionsearch_tool = DuckDuckGoSearchRun()@tooldef python_repl(code:str) -> str:"""Executes Python code and returns the standard output.Use this to read files (like CSVs or Excel), process data with pandas or do math.CRITICAL: You MUST use print() to output the final result so it can be captured."""old_stdout = sys.stdoutredirected_output = sys.stdout = StringIO()try:# Execute the code in the global namespaceexec(code, globals())sys.stdout = old_stdoutoutput = redirected_output.getvalue()return output if output else "Code executed successfully, but printed nothing. Use print() to see output."except Exception as e:sys.stdout = old_stdoutreturn f"Error executing code: {e}"# Tools Instantiationtools = [search_tool, python_repl]chat_with_tools = chat.bind_tools(tools)class AgentState(TypedDict):4 unmodified lines"messages": [chat_with_tools.invoke(state["messages"])]}# Graph Instantiationbuilder = StateGraph(AgentState)# Graph Nodesbuilder.add_node("assistant", assistant)builder.add_node("tools", ToolNode(tools))#Graph Edgesbuilder.add_edge(START, "assistant")builder.add_conditional_edges(1 unmodified linetools_condition)builder.add_edge("tools", "assistant")# Agent Compilemy_agent = builder.compile()class BasicAgent:1 unmodified lineprint("BasicAgent initialized.")def __call__(self, question: str) -> str:print(f"Agent received question (first 50 chars): {question[:50]}...")# 1. Construct the prompt with the file path if it existsif file_name:# Note: You may need to adjust the path depending on where your space saves downloaded files.# Usually, the grading space provides the file name, and it sits in the same directory.prompt = f"Question: {question}\nAttached File: {file_name}\n\nUse the python_repl tool to read and analyze this file using pandas."else:prompt = f"Question: {question}"# Inject the strict GAIA System Prompt and the Human Promptmessages = [SystemMessage(content=system_prompt),HumanMessage(content=question)]# Invoke Agentresponse_state = my_agent.invoke({"messages": messages})# Final answerfinal_answer = response_state["messages"][-1].contentreturn final_answer54 unmodified linesfor item in questions_data:task_id = item.get("task_id")question_text = item.get("question")file_name = item.get("file_name")if not task_id or question_text is None:print(f"Skipping item with missing task_id or question: {item}")continuetry:submitted_answer = agent(question_text, file_name)answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})except Exception as e:
app.py+50-4