import os import gradio as gr import requests import inspect import pandas as pd from langgraph.prebuilt import ToolNode # from typing import Any, Dict # from typing import TypedDict, Annotated from langchain_openai import ChatOpenAI from langgraph.graph import StateGraph, START, END from langgraph.graph.message import add_messages from langchain.schema import HumanMessage, SystemMessage, AIMessage # Create a ToolNode that knows about your web_search function import json from old2state import AgentState # --- Constants --- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space" from old2tools import ocr_image_tool, parse_excel_tool, web_search_tool, run_tools, audio_transcriber_tool, wikipedia_search_tool llm = ChatOpenAI(model_name="gpt-4.1") # ─── 1) plan_node ─── # ─── 1) plan_node ─── tool_counter = 0 # ─── 1) plan_node ─── def plan_node(state: AgentState) -> AgentState: """ Step 1: Ask GPT to draft a concise direct answer (INTERIM_ANSWER), then decide if it's confident enough to stop or if it needs one tool. If confident: return {"final_answer":""} Otherwise: return exactly one of: {"wiki_query":"..."}, {"ocr_path":"..."}, {"excel_path":"...","excel_sheet_name":"..."}, {"audio_path":"..."} """ prior_msgs = state.get("messages", []) user_input = "" for msg in reversed(prior_msgs): if isinstance(msg, HumanMessage): user_input = msg.content break system_msg = SystemMessage( content=( "You are an agent that must do two things in one JSON output:\n\n" " 1) Provide a concise, direct answer to the user's question (no explanation).\n" " 2) Judge whether that answer is reliable:\n" " • If you are fully confident, return exactly:\n" " {\"final_answer\":\"\"}\n" " and nothing else.\n" " • Otherwise, return exactly one of:\n" " {\"wiki_query\":\"\"}\n" " {\"ocr_path\":\"\"}\n" " {\"excel_path\":\"\", \"excel_sheet_name\":\"\"}\n" " {\"audio_path\":\"