Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,157 +1,180 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
-
import json
|
| 6 |
-
import traceback
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
-
from typing import List, Dict, Any, Optional
|
|
|
|
| 9 |
|
| 10 |
-
# LangChain imports
|
| 11 |
-
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage, BaseMessage
|
| 12 |
-
from langchain_openai import ChatOpenAI
|
| 13 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 14 |
from langchain_community.tools.wikipedia.tool import WikipediaQueryRun
|
| 15 |
from langchain_community.utilities.wikipedia import WikipediaAPIWrapper
|
| 16 |
from langchain_community.tools.arxiv.tool import ArxivQueryRun
|
|
|
|
|
|
|
| 17 |
from langgraph.graph import StateGraph, END
|
| 18 |
-
from langgraph.prebuilt import
|
| 19 |
-
from
|
|
|
|
| 20 |
|
|
|
|
| 21 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
system_prompt = (
|
| 32 |
-
"You are a helpful AI assistant that uses tools to find information and answer questions.\n"
|
| 33 |
-
"When you don't know something, use the available tools to look up information. Be concise, direct, and provide accurate responses.\n"
|
| 34 |
-
"Always cite your sources when using information from searches or reference materials."
|
| 35 |
-
)
|
| 36 |
-
print("System prompt file not found, using default prompt")
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
def __init__(self):
|
| 40 |
-
print("
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
try:
|
| 49 |
-
self.graph = self.build_graph()
|
| 50 |
-
print("Graph successfully built")
|
| 51 |
-
except Exception as e:
|
| 52 |
-
print(f"Error building agent graph: {e}")
|
| 53 |
-
traceback.print_exc()
|
| 54 |
-
raise
|
| 55 |
-
|
| 56 |
-
def build_graph(self):
|
| 57 |
-
# Add consistent headers for OpenRouter
|
| 58 |
-
headers = {
|
| 59 |
-
"HTTP-Referer": "https://huggingface.co/",
|
| 60 |
-
"X-Title": "HF Agent"
|
| 61 |
-
}
|
| 62 |
-
|
| 63 |
-
try:
|
| 64 |
-
# Try alternative OpenAI model if Gemini is causing issues
|
| 65 |
-
model_name = "google/gemini-2.0-flash-001"
|
| 66 |
-
# Fallback to OpenAI if needed
|
| 67 |
-
# model_name = "gpt-3.5-turbo"
|
| 68 |
-
|
| 69 |
-
llm = ChatOpenAI(
|
| 70 |
-
model=model_name,
|
| 71 |
-
temperature=0,
|
| 72 |
-
openai_api_key=os.getenv("OPENROUTER_API_KEY"),
|
| 73 |
-
openai_api_base="https://openrouter.ai/api/v1",
|
| 74 |
-
headers=headers
|
| 75 |
-
)
|
| 76 |
-
print(f"LLM initialized: {model_name} via OpenRouter")
|
| 77 |
-
|
| 78 |
-
wikipedia_tool = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
|
| 79 |
-
arxiv_tool = ArxivQueryRun()
|
| 80 |
-
tavily_search = TavilySearchResults(max_results=5)
|
| 81 |
-
tools = [wikipedia_tool, arxiv_tool, tavily_search]
|
| 82 |
-
print(f"Initialized {len(tools)} tools: Wikipedia, Arxiv, Tavily Search")
|
| 83 |
-
|
| 84 |
-
llm_with_tools = llm.bind_tools(tools)
|
| 85 |
-
|
| 86 |
-
def assistant(state: MessagesState):
|
| 87 |
-
messages = state["messages"]
|
| 88 |
-
response = llm_with_tools.invoke(messages)
|
| 89 |
-
return {"messages": messages + [response]} # Always return dict
|
| 90 |
-
|
| 91 |
-
tools_node = ToolNode(tools)
|
| 92 |
-
|
| 93 |
-
builder = StateGraph(MessagesState)
|
| 94 |
-
builder.add_node("assistant", assistant)
|
| 95 |
-
builder.add_node("tools", tools_node)
|
| 96 |
-
builder.add_edge("tools", "assistant")
|
| 97 |
-
builder.add_edge("assistant", "tools")
|
| 98 |
-
builder.set_entry_point("assistant")
|
| 99 |
-
builder.add_conditional_edges(
|
| 100 |
-
"tools",
|
| 101 |
-
lambda x: "assistant",
|
| 102 |
-
{"assistant": "assistant"}
|
| 103 |
)
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
)
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
def __call__(self, question: str) -> str:
|
| 112 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 113 |
-
|
| 114 |
-
SystemMessage(content=system_prompt),
|
| 115 |
-
HumanMessage(content=question)
|
| 116 |
-
]
|
| 117 |
-
|
| 118 |
-
# Print message details for debugging
|
| 119 |
-
print(f"Sending {len(messages)} messages to the LLM")
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
try:
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
if
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
else:
|
| 145 |
-
print("
|
| 146 |
-
return "
|
| 147 |
-
|
| 148 |
except Exception as e:
|
| 149 |
-
print(f"Error
|
|
|
|
| 150 |
traceback.print_exc()
|
| 151 |
-
return f"
|
| 152 |
|
|
|
|
| 153 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
if profile:
|
| 156 |
username = f"{profile.username}"
|
| 157 |
print(f"User logged in: {username}")
|
|
@@ -159,41 +182,28 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 159 |
print("User not logged in.")
|
| 160 |
return "Please Login to Hugging Face with the button.", None
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
api_url = DEFAULT_API_URL
|
| 163 |
questions_url = f"{api_url}/questions"
|
| 164 |
submit_url = f"{api_url}/submit"
|
| 165 |
-
|
| 166 |
-
print(f"Checking OpenRouter API key...")
|
| 167 |
-
if not os.getenv("OPENROUTER_API_KEY"):
|
| 168 |
-
return "Error: OPENROUTER_API_KEY not set. Please add this to your Space secrets.", None
|
| 169 |
|
| 170 |
try:
|
| 171 |
-
agent =
|
| 172 |
-
except Exception as e:
|
| 173 |
-
error_msg = f"Error instantiating agent: {e}"
|
| 174 |
-
print(error_msg)
|
| 175 |
-
traceback.print_exc()
|
| 176 |
-
return error_msg, None
|
| 177 |
-
|
| 178 |
-
# Generate a simple test question to verify the agent works
|
| 179 |
-
test_question = "What is the capital of France?"
|
| 180 |
-
print(f"Testing agent with a simple question: '{test_question}'")
|
| 181 |
-
try:
|
| 182 |
-
test_response = agent(test_question)
|
| 183 |
-
print(f"Test response: {test_response[:100]}...")
|
| 184 |
-
if "I wasn't able to generate" in test_response or "error" in test_response.lower():
|
| 185 |
-
print("WARNING: Agent test response indicates potential issues")
|
| 186 |
except Exception as e:
|
| 187 |
-
print(f"
|
| 188 |
-
|
| 189 |
-
return f"Agent test failed: {e}", None
|
| 190 |
|
| 191 |
-
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 192 |
-
print(agent_code)
|
| 193 |
|
| 194 |
print(f"Fetching questions from: {questions_url}")
|
| 195 |
try:
|
| 196 |
-
response = requests.get(questions_url, timeout=
|
| 197 |
response.raise_for_status()
|
| 198 |
questions_data = response.json()
|
| 199 |
if not questions_data:
|
|
@@ -207,9 +217,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 207 |
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 208 |
print(f"Response text: {response.text[:500]}")
|
| 209 |
return f"Error decoding server response for questions: {e}", None
|
| 210 |
-
except Exception as e:
|
| 211 |
-
print(f"An unexpected error occurred fetching questions: {e}")
|
| 212 |
-
return f"An unexpected error occurred fetching questions: {e}", None
|
| 213 |
|
| 214 |
results_log = []
|
| 215 |
answers_payload = []
|
|
@@ -221,6 +228,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 221 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 222 |
continue
|
| 223 |
try:
|
|
|
|
| 224 |
submitted_answer = agent(question_text)
|
| 225 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 226 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
@@ -233,7 +241,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 233 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 234 |
|
| 235 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 236 |
-
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user
|
| 237 |
print(status_update)
|
| 238 |
|
| 239 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
|
@@ -278,22 +286,32 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 278 |
results_df = pd.DataFrame(results_log)
|
| 279 |
return status_message, results_df
|
| 280 |
|
|
|
|
| 281 |
with gr.Blocks() as demo:
|
| 282 |
-
gr.Markdown("#
|
| 283 |
gr.Markdown(
|
| 284 |
"""
|
| 285 |
**Instructions:**
|
| 286 |
-
1.
|
| 287 |
-
2.
|
| 288 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
---
|
| 290 |
-
**
|
| 291 |
-
|
|
|
|
|
|
|
| 292 |
"""
|
| 293 |
)
|
| 294 |
|
| 295 |
gr.LoginButton()
|
|
|
|
| 296 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
|
|
|
| 297 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 298 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 299 |
|
|
@@ -306,17 +324,22 @@ if __name__ == "__main__":
|
|
| 306 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 307 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 308 |
space_id_startup = os.getenv("SPACE_ID")
|
|
|
|
| 309 |
if space_host_startup:
|
| 310 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 311 |
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 312 |
else:
|
| 313 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
|
|
|
| 314 |
if space_id_startup:
|
| 315 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 316 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 317 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 318 |
else:
|
| 319 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
|
|
|
| 320 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 321 |
-
|
| 322 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
+
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
+
from typing import TypedDict, Annotated, Sequence, List, Dict, Any, Optional
|
| 8 |
+
import operator
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 11 |
from langchain_community.tools.wikipedia.tool import WikipediaQueryRun
|
| 12 |
from langchain_community.utilities.wikipedia import WikipediaAPIWrapper
|
| 13 |
from langchain_community.tools.arxiv.tool import ArxivQueryRun
|
| 14 |
+
from langchain_community.utilities.arxiv import ArxivAPIWrapper
|
| 15 |
+
|
| 16 |
from langgraph.graph import StateGraph, END
|
| 17 |
+
from langgraph.prebuilt import ToolInvocation
|
| 18 |
+
from langchain_core.messages import BaseMessage, FunctionMessage, HumanMessage, AIMessage
|
| 19 |
+
from langchain_openai import ChatOpenAI
|
| 20 |
|
| 21 |
+
# --- Constants ---
|
| 22 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 23 |
|
| 24 |
+
# --- Environment Setup ---
|
| 25 |
+
load_dotenv()
|
| 26 |
+
|
| 27 |
+
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
|
| 28 |
+
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY") # Assuming Tavily might also need an API key
|
| 29 |
+
|
| 30 |
+
if not OPENROUTER_API_KEY:
|
| 31 |
+
print("Warning: OPENROUTER_API_KEY not found in .env file. The LLM will not function.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# --- Tool Setup ---
|
| 34 |
+
tavily_tool = TavilySearchResults(max_results=3, api_key=TAVILY_API_KEY if TAVILY_API_KEY else "placeholder_tavily_key") # Add placeholder if not found
|
| 35 |
+
wikipedia_tool = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper(top_k_results=2, doc_content_chars_max=2000))
|
| 36 |
+
arxiv_tool = ArxivQueryRun(api_wrapper=ArxivAPIWrapper(top_k_results=2, doc_content_chars_max=2000))
|
| 37 |
+
|
| 38 |
+
tools = [tavily_tool, wikipedia_tool, arxiv_tool]
|
| 39 |
+
|
| 40 |
+
# --- LangGraph Agent Definition ---
|
| 41 |
+
class AgentState(TypedDict):
|
| 42 |
+
messages: Annotated[Sequence[BaseMessage], operator.add]
|
| 43 |
+
next_action: Optional[str] # To decide if we need to call tools or respond
|
| 44 |
+
|
| 45 |
+
class LangGraphAgent:
|
| 46 |
def __init__(self):
|
| 47 |
+
print("LangGraphAgent initializing...")
|
| 48 |
+
if not OPENROUTER_API_KEY:
|
| 49 |
+
raise ValueError("OPENROUTER_API_KEY is not set. Cannot initialize LLM.")
|
| 50 |
+
|
| 51 |
+
self.llm = ChatOpenAI(
|
| 52 |
+
model="google/gemini-2.0-flash-001",
|
| 53 |
+
api_key=OPENROUTER_API_KEY,
|
| 54 |
+
base_url="https://openrouter.ai/api/v1"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
)
|
| 56 |
+
self.tools_map = {tool.name: tool for tool in tools}
|
| 57 |
+
self.graph = self._build_graph()
|
| 58 |
+
print("LangGraphAgent initialized.")
|
| 59 |
+
|
| 60 |
+
def _build_graph(self):
|
| 61 |
+
workflow = StateGraph(AgentState)
|
| 62 |
+
|
| 63 |
+
workflow.add_node("llm", self._call_llm)
|
| 64 |
+
workflow.add_node("tools", self._tool_node)
|
| 65 |
+
|
| 66 |
+
workflow.set_entry_point("llm")
|
| 67 |
+
|
| 68 |
+
workflow.add_conditional_edges(
|
| 69 |
+
"llm",
|
| 70 |
+
self._should_call_tools,
|
| 71 |
+
{
|
| 72 |
+
"continue": "tools",
|
| 73 |
+
"end": END
|
| 74 |
+
}
|
| 75 |
)
|
| 76 |
+
workflow.add_edge("tools", "llm")
|
| 77 |
+
return workflow.compile()
|
| 78 |
+
|
| 79 |
+
def _should_call_tools(self, state: AgentState) -> str:
|
| 80 |
+
print("LLM deciding next step...")
|
| 81 |
+
last_message = state["messages"][-1]
|
| 82 |
+
if hasattr(last_message, "tool_calls") and last_message.tool_calls:
|
| 83 |
+
print(f"LLM decided to call tools: {last_message.tool_calls}")
|
| 84 |
+
return "continue"
|
| 85 |
+
print("LLM decided to end.")
|
| 86 |
+
return "end"
|
| 87 |
+
|
| 88 |
+
def _call_llm(self, state: AgentState) -> Dict[str, Any]:
|
| 89 |
+
print("Calling LLM...")
|
| 90 |
+
# Bind tools to LLM for function calling
|
| 91 |
+
llm_with_tools = self.llm.bind_tools(tools)
|
| 92 |
+
response = llm_with_tools.invoke(state["messages"])
|
| 93 |
+
print(f"LLM response: {response.content[:100]}...")
|
| 94 |
+
return {"messages": [response]}
|
| 95 |
+
|
| 96 |
+
def _tool_node(self, state: AgentState) -> Dict[str, Any]:
|
| 97 |
+
print("Executing tools...")
|
| 98 |
+
tool_messages = []
|
| 99 |
+
last_message = state["messages"][-1]
|
| 100 |
+
|
| 101 |
+
if not hasattr(last_message, "tool_calls") or not last_message.tool_calls:
|
| 102 |
+
print("No tool calls found in the last message.")
|
| 103 |
+
# This case should ideally be handled by the conditional edge, but as a fallback:
|
| 104 |
+
return {"messages": [AIMessage(content="No tools to call, proceeding.")]}
|
| 105 |
+
|
| 106 |
+
for tool_call in last_message.tool_calls:
|
| 107 |
+
tool_name = tool_call["name"]
|
| 108 |
+
tool_args = tool_call["args"]
|
| 109 |
+
print(f"Calling tool: {tool_name} with args: {tool_args}")
|
| 110 |
+
if tool_name in self.tools_map:
|
| 111 |
+
try:
|
| 112 |
+
tool_result = self.tools_map[tool_name].invoke(tool_args)
|
| 113 |
+
print(f"Tool {tool_name} result (first 100 chars): {str(tool_result)[:100]}...")
|
| 114 |
+
tool_messages.append(FunctionMessage(content=str(tool_result), name=tool_name, tool_call_id=tool_call["id"]))
|
| 115 |
+
except Exception as e:
|
| 116 |
+
print(f"Error executing tool {tool_name}: {e}")
|
| 117 |
+
tool_messages.append(FunctionMessage(content=f"Error executing tool {tool_name}: {e}", name=tool_name, tool_call_id=tool_call["id"]))
|
| 118 |
+
else:
|
| 119 |
+
print(f"Tool {tool_name} not found.")
|
| 120 |
+
tool_messages.append(FunctionMessage(content=f"Tool {tool_name} not found.", name=tool_name, tool_call_id=tool_call["id"]))
|
| 121 |
+
return {"messages": tool_messages}
|
| 122 |
+
|
| 123 |
def __call__(self, question: str) -> str:
|
| 124 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 125 |
+
initial_state = {"messages": [HumanMessage(content=question)]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
# The GAIA prompt example suggests not including "FINAL ANSWER" and just replying with the answer.
|
| 128 |
+
# We need to ensure the LLM is prompted to provide a direct answer after tool use.
|
| 129 |
+
# For simplicity in this template, we will take the last AI message content as the answer.
|
| 130 |
+
# A more robust solution might involve a specific "final answer" node or prompt engineering.
|
| 131 |
+
|
| 132 |
+
final_graph_state = None
|
| 133 |
try:
|
| 134 |
+
for event in self.graph.stream(initial_state, {"recursion_limit": 10}): # Added recursion limit
|
| 135 |
+
# print(f"Graph event: {event}") # For debugging stream
|
| 136 |
+
if END in event:
|
| 137 |
+
final_graph_state = event[END]
|
| 138 |
+
break
|
| 139 |
+
# Update final_graph_state with the latest state from any node
|
| 140 |
+
# This ensures we have the latest messages even if END is not directly reached by llm
|
| 141 |
+
# (e.g. if recursion limit is hit)
|
| 142 |
+
for key in event:
|
| 143 |
+
if key != END:
|
| 144 |
+
final_graph_state = event[key]
|
| 145 |
+
|
| 146 |
+
if final_graph_state and final_graph_state["messages"]:
|
| 147 |
+
# Get the last AI message as the answer
|
| 148 |
+
for msg in reversed(final_graph_state["messages"]):
|
| 149 |
+
if isinstance(msg, AIMessage) and not msg.tool_calls:
|
| 150 |
+
answer = msg.content.strip()
|
| 151 |
+
# Ensure no "FINAL ANSWER:" prefix as per GAIA instructions
|
| 152 |
+
if answer.upper().startswith("FINAL ANSWER:"):
|
| 153 |
+
answer = answer[len("FINAL ANSWER:"):].strip()
|
| 154 |
+
print(f"Agent returning answer: {answer}")
|
| 155 |
+
return answer
|
| 156 |
+
# Fallback if no suitable AI message is found
|
| 157 |
+
print("No suitable AI message found for final answer. Returning last message content.")
|
| 158 |
+
# This might be a tool call or an intermediate thought, not ideal.
|
| 159 |
+
return str(final_graph_state["messages"][-1].content) if final_graph_state["messages"] else "Error: No messages in final state."
|
| 160 |
else:
|
| 161 |
+
print("Error: Agent did not reach a final state or no messages found.")
|
| 162 |
+
return "Error: Agent did not produce a conclusive answer."
|
| 163 |
+
|
| 164 |
except Exception as e:
|
| 165 |
+
print(f"Error during agent execution: {e}")
|
| 166 |
+
import traceback
|
| 167 |
traceback.print_exc()
|
| 168 |
+
return f"Error during agent execution: {e}"
|
| 169 |
|
| 170 |
+
# --- Main Evaluation Logic (Modified from starter) ---
|
| 171 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 172 |
+
"""
|
| 173 |
+
Fetches all questions, runs the LangGraphAgent on them, submits all answers,
|
| 174 |
+
and displays the results.
|
| 175 |
+
"""
|
| 176 |
+
space_id = os.getenv("SPACE_ID")
|
| 177 |
+
|
| 178 |
if profile:
|
| 179 |
username = f"{profile.username}"
|
| 180 |
print(f"User logged in: {username}")
|
|
|
|
| 182 |
print("User not logged in.")
|
| 183 |
return "Please Login to Hugging Face with the button.", None
|
| 184 |
|
| 185 |
+
if not OPENROUTER_API_KEY:
|
| 186 |
+
return "Error: OPENROUTER_API_KEY not found. Please set it in your .env file.", None
|
| 187 |
+
if not TAVILY_API_KEY:
|
| 188 |
+
print("Warning: TAVILY_API_KEY not found. Tavily search might not work as expected.")
|
| 189 |
+
# return "Error: TAVILY_API_KEY not found. Please set it in your .env file.", None
|
| 190 |
+
|
| 191 |
api_url = DEFAULT_API_URL
|
| 192 |
questions_url = f"{api_url}/questions"
|
| 193 |
submit_url = f"{api_url}/submit"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
try:
|
| 196 |
+
agent = LangGraphAgent()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
except Exception as e:
|
| 198 |
+
print(f"Error instantiating agent: {e}")
|
| 199 |
+
return f"Error initializing agent: {e}", None
|
|
|
|
| 200 |
|
| 201 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main" if space_id else "local_run_no_space_id"
|
| 202 |
+
print(f"Agent code link: {agent_code}")
|
| 203 |
|
| 204 |
print(f"Fetching questions from: {questions_url}")
|
| 205 |
try:
|
| 206 |
+
response = requests.get(questions_url, timeout=20)
|
| 207 |
response.raise_for_status()
|
| 208 |
questions_data = response.json()
|
| 209 |
if not questions_data:
|
|
|
|
| 217 |
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 218 |
print(f"Response text: {response.text[:500]}")
|
| 219 |
return f"Error decoding server response for questions: {e}", None
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
results_log = []
|
| 222 |
answers_payload = []
|
|
|
|
| 228 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 229 |
continue
|
| 230 |
try:
|
| 231 |
+
print(f"\n--- Processing Task ID: {task_id} ---")
|
| 232 |
submitted_answer = agent(question_text)
|
| 233 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 234 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
| 241 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 242 |
|
| 243 |
submission_data = {"username": username.strip(), "agent_code": agent_code, "answers": answers_payload}
|
| 244 |
+
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username} '..."
|
| 245 |
print(status_update)
|
| 246 |
|
| 247 |
print(f"Submitting {len(answers_payload)} answers to: {submit_url}")
|
|
|
|
| 286 |
results_df = pd.DataFrame(results_log)
|
| 287 |
return status_message, results_df
|
| 288 |
|
| 289 |
+
# --- Gradio Interface (Mostly as provided) ---
|
| 290 |
with gr.Blocks() as demo:
|
| 291 |
+
gr.Markdown("# LangGraph GAIA Agent Evaluation Runner")
|
| 292 |
gr.Markdown(
|
| 293 |
"""
|
| 294 |
**Instructions:**
|
| 295 |
+
1. **Clone this space** if you haven't already.
|
| 296 |
+
2. **Create a `.env` file** in the root of your space with your API keys:
|
| 297 |
+
```
|
| 298 |
+
OPENROUTER_API_KEY="your_openrouter_api_key"
|
| 299 |
+
TAVILY_API_KEY="your_tavily_api_key" # Optional, but recommended for TavilySearch tool
|
| 300 |
+
```
|
| 301 |
+
3. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 302 |
+
4. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 303 |
---
|
| 304 |
+
**Disclaimers:**
|
| 305 |
+
- Ensure your Hugging Face Space is public for the `agent_code` link to be verifiable.
|
| 306 |
+
- Submitting all answers can take some time as the agent processes each question.
|
| 307 |
+
- This agent uses LangGraph with TavilySearch, Wikipedia, Arxiv, and Google Gemini via OpenRouter.
|
| 308 |
"""
|
| 309 |
)
|
| 310 |
|
| 311 |
gr.LoginButton()
|
| 312 |
+
|
| 313 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 314 |
+
|
| 315 |
status_output = gr.Textbox(label="Run Status / Submission Result", lines=5, interactive=False)
|
| 316 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 317 |
|
|
|
|
| 324 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 325 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 326 |
space_id_startup = os.getenv("SPACE_ID")
|
| 327 |
+
|
| 328 |
if space_host_startup:
|
| 329 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
| 330 |
print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
|
| 331 |
else:
|
| 332 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 333 |
+
|
| 334 |
if space_id_startup:
|
| 335 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 336 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 337 |
print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
| 338 |
else:
|
| 339 |
print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
|
| 340 |
+
|
| 341 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 342 |
+
|
| 343 |
+
print("Launching Gradio Interface for LangGraph GAIA Agent Evaluation...")
|
| 344 |
+
demo.launch(debug=True, share=False)
|
| 345 |
+
|