Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,24 +2,17 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
-
from typing import TypedDict, Annotated
|
| 6 |
-
import
|
| 7 |
-
from
|
| 8 |
-
from langchain.agents import AgentExecutor, create_react_agent
|
| 9 |
from langchain import hub
|
| 10 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 11 |
from langchain_huggingface import HuggingFaceEndpoint
|
| 12 |
-
from langgraph.graph import StateGraph, END
|
| 13 |
-
from langgraph.prebuilt import ToolNode
|
| 14 |
|
| 15 |
# --- Main Application Logic ---
|
| 16 |
|
| 17 |
-
# This
|
| 18 |
-
|
| 19 |
-
messages: Annotated[Sequence[BaseMessage], operator.add]
|
| 20 |
-
|
| 21 |
-
# This function builds our agent using the standard ReAct framework
|
| 22 |
-
def create_agent():
|
| 23 |
print("Initializing ReAct Agent...")
|
| 24 |
|
| 25 |
# 1. Set up the LLM (The "Brain")
|
|
@@ -33,45 +26,41 @@ def create_agent():
|
|
| 33 |
|
| 34 |
# 2. Define the Tools
|
| 35 |
tools = [DuckDuckGoSearchRun()]
|
| 36 |
-
tool_node = ToolNode(tools)
|
| 37 |
print("Tools initialized.")
|
| 38 |
|
| 39 |
# 3. Get the ReAct Prompt Template
|
| 40 |
-
# This prompt is designed to
|
| 41 |
prompt = hub.pull("hwchase17/react-chat")
|
| 42 |
-
print("Prompt template pulled
|
| 43 |
|
| 44 |
-
# 4. Create the
|
| 45 |
agent_runnable = create_react_agent(llm, tools, prompt)
|
| 46 |
print("Agent logic created.")
|
| 47 |
|
| 48 |
-
# 5.
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
graph.add_conditional_edges("agent", should_continue)
|
| 62 |
-
graph.add_edge("tools", "agent")
|
| 63 |
-
|
| 64 |
-
# 6. Compile the graph into a runnable app
|
| 65 |
-
app = graph.compile()
|
| 66 |
-
print("LangGraph agent compiled and ready.")
|
| 67 |
-
return app
|
| 68 |
|
| 69 |
# This function runs the agent for a single question.
|
| 70 |
def run_agent_for_task(agent_executor, question: str) -> str:
|
| 71 |
print(f"Agent received question: {question}")
|
| 72 |
try:
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
except Exception as e:
|
| 76 |
print(f"Error during agent execution: {e}")
|
| 77 |
final_answer = f"Error: Agent failed to execute. {e}"
|
|
@@ -79,7 +68,7 @@ def run_agent_for_task(agent_executor, question: str) -> str:
|
|
| 79 |
print(f"Agent returning answer: {final_answer}")
|
| 80 |
return str(final_answer)
|
| 81 |
|
| 82 |
-
# The rest of the file
|
| 83 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 84 |
space_id = os.getenv("SPACE_ID")
|
| 85 |
if not profile:
|
|
@@ -87,7 +76,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 87 |
username = f"{profile.username}"
|
| 88 |
|
| 89 |
try:
|
| 90 |
-
agent_executor =
|
| 91 |
except Exception as e:
|
| 92 |
return f"Error initializing agent: {e}", None
|
| 93 |
|
|
@@ -125,8 +114,9 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 125 |
except Exception as e:
|
| 126 |
return f"Error during submission: {e}", pd.DataFrame(answers_payload)
|
| 127 |
|
|
|
|
| 128 |
with gr.Blocks() as demo:
|
| 129 |
-
gr.Markdown("# Agent Evaluation Runner (Final
|
| 130 |
gr.LoginButton()
|
| 131 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 132 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
+
from typing import TypedDict, Annotated
|
| 6 |
+
from langchain_core.messages import BaseMessage
|
| 7 |
+
from langchain.agents import create_react_agent, AgentExecutor
|
|
|
|
| 8 |
from langchain import hub
|
| 9 |
from langchain_community.tools import DuckDuckGoSearchRun
|
| 10 |
from langchain_huggingface import HuggingFaceEndpoint
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# --- Main Application Logic ---
|
| 13 |
|
| 14 |
+
# This function builds our agent and the executor that runs it.
|
| 15 |
+
def create_agent_executor():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
print("Initializing ReAct Agent...")
|
| 17 |
|
| 18 |
# 1. Set up the LLM (The "Brain")
|
|
|
|
| 26 |
|
| 27 |
# 2. Define the Tools
|
| 28 |
tools = [DuckDuckGoSearchRun()]
|
|
|
|
| 29 |
print("Tools initialized.")
|
| 30 |
|
| 31 |
# 3. Get the ReAct Prompt Template
|
| 32 |
+
# This prompt is specifically designed to make models think step-by-step
|
| 33 |
prompt = hub.pull("hwchase17/react-chat")
|
| 34 |
+
print("Prompt template pulled.")
|
| 35 |
|
| 36 |
+
# 4. Create the Agent's logic using the ReAct framework
|
| 37 |
agent_runnable = create_react_agent(llm, tools, prompt)
|
| 38 |
print("Agent logic created.")
|
| 39 |
|
| 40 |
+
# 5. Create the Agent Executor
|
| 41 |
+
# This is the runtime that will loop through the agent's thoughts and tool uses.
|
| 42 |
+
# We are going back to the standard AgentExecutor, as the LangGraph build was overly complex
|
| 43 |
+
# and causing the new errors.
|
| 44 |
+
agent_executor = AgentExecutor(
|
| 45 |
+
agent=agent_runnable,
|
| 46 |
+
tools=tools,
|
| 47 |
+
verbose=True,
|
| 48 |
+
handle_parsing_errors=True,
|
| 49 |
+
max_iterations=5,
|
| 50 |
+
)
|
| 51 |
+
print("Agent Executor created. Initialization complete.")
|
| 52 |
+
return agent_executor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
# This function runs the agent for a single question.
|
| 55 |
def run_agent_for_task(agent_executor, question: str) -> str:
|
| 56 |
print(f"Agent received question: {question}")
|
| 57 |
try:
|
| 58 |
+
# For this agent, the input is a dictionary with "input" and an empty "chat_history"
|
| 59 |
+
response = agent_executor.invoke({
|
| 60 |
+
"input": question,
|
| 61 |
+
"chat_history": []
|
| 62 |
+
})
|
| 63 |
+
final_answer = response.get("output", "Error: Could not parse final answer.")
|
| 64 |
except Exception as e:
|
| 65 |
print(f"Error during agent execution: {e}")
|
| 66 |
final_answer = f"Error: Agent failed to execute. {e}"
|
|
|
|
| 68 |
print(f"Agent returning answer: {final_answer}")
|
| 69 |
return str(final_answer)
|
| 70 |
|
| 71 |
+
# The rest of the file runs the evaluation and is mostly unchanged from the template.
|
| 72 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 73 |
space_id = os.getenv("SPACE_ID")
|
| 74 |
if not profile:
|
|
|
|
| 76 |
username = f"{profile.username}"
|
| 77 |
|
| 78 |
try:
|
| 79 |
+
agent_executor = create_agent_executor()
|
| 80 |
except Exception as e:
|
| 81 |
return f"Error initializing agent: {e}", None
|
| 82 |
|
|
|
|
| 114 |
except Exception as e:
|
| 115 |
return f"Error during submission: {e}", pd.DataFrame(answers_payload)
|
| 116 |
|
| 117 |
+
# --- Gradio Interface ---
|
| 118 |
with gr.Blocks() as demo:
|
| 119 |
+
gr.Markdown("# Agent Evaluation Runner (Final ReAct Version)")
|
| 120 |
gr.LoginButton()
|
| 121 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 122 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|