Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import json
|
|
| 6 |
import re
|
| 7 |
import tempfile
|
| 8 |
import logging
|
| 9 |
-
from typing import List, Dict, Optional
|
| 10 |
import numpy as np
|
| 11 |
|
| 12 |
# Core ML/AI imports
|
|
@@ -19,10 +19,8 @@ from langgraph.graph import StateGraph, START, END
|
|
| 19 |
from langgraph.graph.message import add_messages
|
| 20 |
from langgraph.prebuilt import ToolNode, tools_condition
|
| 21 |
from langgraph.checkpoint.memory import MemorySaver
|
| 22 |
-
from typing import TypedDict, Annotated, List
|
| 23 |
|
| 24 |
# File processing
|
| 25 |
-
import pandas as pd
|
| 26 |
import wikipedia
|
| 27 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 28 |
import speech_recognition as sr
|
|
@@ -53,10 +51,6 @@ logging.getLogger("ultralytics").setLevel(logging.ERROR)
|
|
| 53 |
# --- Constants ---
|
| 54 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 55 |
|
| 56 |
-
# Agent State Definition
|
| 57 |
-
class AgentState(TypedDict):
|
| 58 |
-
messages: Annotated[List[AnyMessage], add_messages]
|
| 59 |
-
|
| 60 |
# System prompt for the agent
|
| 61 |
SYSTEM_PROMPT = """You are a precision research assistant for the GAIA benchmark. Your mission is EXTREME ACCURACY.
|
| 62 |
|
|
@@ -142,7 +136,9 @@ class GAIAAgent:
|
|
| 142 |
|
| 143 |
def _setup_tools(self):
|
| 144 |
"""Setup all the tools for the agent"""
|
| 145 |
-
|
|
|
|
|
|
|
| 146 |
|
| 147 |
# Wikipedia tool
|
| 148 |
@tool
|
|
@@ -164,10 +160,10 @@ class GAIAAgent:
|
|
| 164 |
@tool
|
| 165 |
def web_search_tool(query: str) -> str:
|
| 166 |
"""Web search for current information"""
|
| 167 |
-
if not
|
| 168 |
return "Tavily API key not available"
|
| 169 |
try:
|
| 170 |
-
tavily_search = TavilySearchResults(api_key=
|
| 171 |
results = tavily_search.invoke(query)
|
| 172 |
formatted_results = []
|
| 173 |
for i, res in enumerate(results, 1):
|
|
@@ -180,11 +176,11 @@ class GAIAAgent:
|
|
| 180 |
@tool
|
| 181 |
def wolfram_alpha_tool(query: str) -> str:
|
| 182 |
"""Use Wolfram Alpha for computational questions"""
|
| 183 |
-
if not
|
| 184 |
return "Wolfram API key not available"
|
| 185 |
|
| 186 |
params = {
|
| 187 |
-
'appid':
|
| 188 |
'input': query,
|
| 189 |
'format': 'plaintext',
|
| 190 |
'output': 'JSON'
|
|
@@ -260,18 +256,22 @@ class GAIAAgent:
|
|
| 260 |
# Python REPL tool
|
| 261 |
python_repl_tool = PythonREPLTool()
|
| 262 |
|
| 263 |
-
tools
|
| 264 |
wikipedia_tool,
|
| 265 |
web_search_tool,
|
| 266 |
wolfram_alpha_tool,
|
| 267 |
file_analyzer_tool,
|
| 268 |
python_repl_tool
|
| 269 |
-
]
|
| 270 |
|
| 271 |
return tools
|
| 272 |
|
| 273 |
def _create_agent_runner(self):
|
| 274 |
"""Create the LangGraph agent runner"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
model_with_tools = self.llm.bind_tools(self.tools)
|
| 276 |
|
| 277 |
def agent_node(state):
|
|
|
|
| 6 |
import re
|
| 7 |
import tempfile
|
| 8 |
import logging
|
| 9 |
+
from typing import List, Dict, Optional, TypedDict, Annotated
|
| 10 |
import numpy as np
|
| 11 |
|
| 12 |
# Core ML/AI imports
|
|
|
|
| 19 |
from langgraph.graph.message import add_messages
|
| 20 |
from langgraph.prebuilt import ToolNode, tools_condition
|
| 21 |
from langgraph.checkpoint.memory import MemorySaver
|
|
|
|
| 22 |
|
| 23 |
# File processing
|
|
|
|
| 24 |
import wikipedia
|
| 25 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 26 |
import speech_recognition as sr
|
|
|
|
| 51 |
# --- Constants ---
|
| 52 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# System prompt for the agent
|
| 55 |
SYSTEM_PROMPT = """You are a precision research assistant for the GAIA benchmark. Your mission is EXTREME ACCURACY.
|
| 56 |
|
|
|
|
| 136 |
|
| 137 |
def _setup_tools(self):
|
| 138 |
"""Setup all the tools for the agent"""
|
| 139 |
+
|
| 140 |
+
# Store reference to self for use in nested functions
|
| 141 |
+
agent_instance = self
|
| 142 |
|
| 143 |
# Wikipedia tool
|
| 144 |
@tool
|
|
|
|
| 160 |
@tool
|
| 161 |
def web_search_tool(query: str) -> str:
|
| 162 |
"""Web search for current information"""
|
| 163 |
+
if not agent_instance.tavily_api_key:
|
| 164 |
return "Tavily API key not available"
|
| 165 |
try:
|
| 166 |
+
tavily_search = TavilySearchResults(api_key=agent_instance.tavily_api_key, max_results=5)
|
| 167 |
results = tavily_search.invoke(query)
|
| 168 |
formatted_results = []
|
| 169 |
for i, res in enumerate(results, 1):
|
|
|
|
| 176 |
@tool
|
| 177 |
def wolfram_alpha_tool(query: str) -> str:
|
| 178 |
"""Use Wolfram Alpha for computational questions"""
|
| 179 |
+
if not agent_instance.wolfram_api_key:
|
| 180 |
return "Wolfram API key not available"
|
| 181 |
|
| 182 |
params = {
|
| 183 |
+
'appid': agent_instance.wolfram_api_key,
|
| 184 |
'input': query,
|
| 185 |
'format': 'plaintext',
|
| 186 |
'output': 'JSON'
|
|
|
|
| 256 |
# Python REPL tool
|
| 257 |
python_repl_tool = PythonREPLTool()
|
| 258 |
|
| 259 |
+
tools = [
|
| 260 |
wikipedia_tool,
|
| 261 |
web_search_tool,
|
| 262 |
wolfram_alpha_tool,
|
| 263 |
file_analyzer_tool,
|
| 264 |
python_repl_tool
|
| 265 |
+
]
|
| 266 |
|
| 267 |
return tools
|
| 268 |
|
| 269 |
def _create_agent_runner(self):
|
| 270 |
"""Create the LangGraph agent runner"""
|
| 271 |
+
# Define AgentState locally
|
| 272 |
+
class AgentState(TypedDict):
|
| 273 |
+
messages: Annotated[List[AnyMessage], add_messages]
|
| 274 |
+
|
| 275 |
model_with_tools = self.llm.bind_tools(self.tools)
|
| 276 |
|
| 277 |
def agent_node(state):
|