Update veryfinal.py
Browse files- veryfinal.py +320 -422
veryfinal.py
CHANGED
@@ -1,462 +1,360 @@
|
|
1 |
-
"""Enhanced
|
2 |
-
import os
|
3 |
-
|
4 |
-
|
5 |
-
import
|
|
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
from langgraph.prebuilt import tools_condition
|
10 |
-
from langgraph.prebuilt import ToolNode
|
11 |
-
from langgraph.checkpoint.memory import MemorySaver
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
from langchain_community.tools.tavily_search import TavilySearchResults
|
20 |
-
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
|
21 |
-
from langchain_community.vectorstores import FAISS
|
22 |
-
from langchain_nvidia_ai_endpoints import NVIDIAEmbeddings
|
23 |
-
from langchain.tools.retriever import create_retriever_tool
|
24 |
-
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
25 |
-
from langchain_community.document_loaders import JSONLoader
|
26 |
-
|
27 |
-
# Agno imports
|
28 |
-
from agno.agent import Agent
|
29 |
-
from agno.models.groq import GroqChat
|
30 |
-
from agno.models.google import GeminiChat
|
31 |
-
from agno.tools.duckduckgo import DuckDuckGoTools
|
32 |
-
from agno.memory.agent import AgentMemory
|
33 |
-
from agno.storage.agent import AgentStorage
|
34 |
-
|
35 |
-
load_dotenv()
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
def __init__(self, requests_per_minute: int, provider_name: str):
|
40 |
-
self.requests_per_minute = requests_per_minute
|
41 |
-
self.provider_name = provider_name
|
42 |
-
self.request_times = []
|
43 |
-
self.consecutive_failures = 0
|
44 |
-
self.performance_cache = {} # Cache for repeated queries
|
45 |
-
|
46 |
-
def wait_if_needed(self):
|
47 |
-
current_time = time.time()
|
48 |
-
self.request_times = [t for t in self.request_times if current_time - t < 60]
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
# Storage for persistent memory
|
76 |
-
storage = AgentStorage(
|
77 |
-
table_name="agent_sessions",
|
78 |
-
db_file="tmp/agent_storage.db"
|
79 |
-
)
|
80 |
-
|
81 |
-
# Math specialist using Groq (fastest)
|
82 |
-
math_agent = Agent(
|
83 |
-
name="MathSpecialist",
|
84 |
-
model=GroqChat(
|
85 |
-
model="llama-3.3-70b-versatile",
|
86 |
-
api_key=os.getenv("GROQ_API_KEY"),
|
87 |
-
temperature=0
|
88 |
-
),
|
89 |
-
description="Expert mathematical problem solver",
|
90 |
-
instructions=[
|
91 |
-
"Solve mathematical problems with precision",
|
92 |
-
"Show step-by-step calculations",
|
93 |
-
"Use tools for complex computations",
|
94 |
-
"Always provide numerical answers"
|
95 |
-
],
|
96 |
-
memory=AgentMemory(
|
97 |
-
db=storage,
|
98 |
-
create_user_memories=True,
|
99 |
-
create_session_summary=True
|
100 |
-
),
|
101 |
-
show_tool_calls=False,
|
102 |
-
markdown=False
|
103 |
-
)
|
104 |
-
|
105 |
-
# Research specialist using Gemini (most capable)
|
106 |
-
research_agent = Agent(
|
107 |
-
name="ResearchSpecialist",
|
108 |
-
model=GeminiChat(
|
109 |
-
model="gemini-2.0-flash-lite",
|
110 |
-
api_key=os.getenv("GOOGLE_API_KEY"),
|
111 |
-
temperature=0
|
112 |
-
),
|
113 |
-
description="Expert research and information gathering specialist",
|
114 |
-
instructions=[
|
115 |
-
"Conduct thorough research using available tools",
|
116 |
-
"Synthesize information from multiple sources",
|
117 |
-
"Provide comprehensive, well-cited answers",
|
118 |
-
"Focus on accuracy and relevance"
|
119 |
-
],
|
120 |
-
tools=[DuckDuckGoTools()],
|
121 |
-
memory=AgentMemory(
|
122 |
-
db=storage,
|
123 |
-
create_user_memories=True,
|
124 |
-
create_session_summary=True
|
125 |
-
),
|
126 |
-
show_tool_calls=False,
|
127 |
-
markdown=False
|
128 |
-
)
|
129 |
-
|
130 |
-
return {
|
131 |
-
"math": math_agent,
|
132 |
-
"research": research_agent
|
133 |
-
}
|
134 |
-
|
135 |
-
# LangGraph Tools (optimized)
|
136 |
-
@tool
|
137 |
-
def multiply(a: int, b: int) -> int:
|
138 |
-
"""Multiply two numbers."""
|
139 |
-
return a * b
|
140 |
-
|
141 |
-
@tool
|
142 |
-
def add(a: int, b: int) -> int:
|
143 |
-
"""Add two numbers."""
|
144 |
-
return a + b
|
145 |
-
|
146 |
-
@tool
|
147 |
-
def subtract(a: int, b: int) -> int:
|
148 |
-
"""Subtract two numbers."""
|
149 |
-
return a - b
|
150 |
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
|
|
157 |
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
|
|
|
|
162 |
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
try:
|
167 |
-
time.sleep(random.uniform(1, 2)) # Reduced wait time
|
168 |
-
search_docs = TavilySearchResults(max_results=2).invoke(query=query) # Reduced results for speed
|
169 |
-
formatted_search_docs = "\n\n---\n\n".join([
|
170 |
-
f'<Document source="{doc.get("url", "")}" />\n{doc.get("content", "")[:500]}\n</Document>' # Truncated for speed
|
171 |
-
for doc in search_docs
|
172 |
-
])
|
173 |
-
return formatted_search_docs
|
174 |
-
except Exception as e:
|
175 |
-
return f"Web search failed: {str(e)}"
|
176 |
|
177 |
-
|
178 |
-
def optimized_wiki_search(query: str) -> str:
|
179 |
-
"""Optimized Wikipedia search."""
|
180 |
try:
|
181 |
-
|
182 |
-
|
183 |
-
formatted_search_docs = "\n\n---\n\n".join([
|
184 |
-
f'<Document source="{doc.metadata["source"]}" />\n{doc.page_content[:800]}\n</Document>' # Truncated for speed
|
185 |
-
for doc in search_docs
|
186 |
-
])
|
187 |
-
return formatted_search_docs
|
188 |
except Exception as e:
|
189 |
-
|
|
|
|
|
|
|
|
|
190 |
|
191 |
-
#
|
192 |
-
|
193 |
-
"""Setup optimized FAISS vector store"""
|
194 |
try:
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
""
|
204 |
-
|
205 |
-
json_loader = JSONLoader(file_path="metadata.jsonl", jq_schema=jq_schema, json_lines=True, text_content=False)
|
206 |
-
json_docs = json_loader.load()
|
207 |
-
|
208 |
-
# Smaller chunks for faster processing
|
209 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=256, chunk_overlap=50)
|
210 |
-
json_chunks = text_splitter.split_documents(json_docs)
|
211 |
-
|
212 |
-
embeddings = NVIDIAEmbeddings(
|
213 |
-
model="nvidia/nv-embedqa-e5-v5",
|
214 |
-
api_key=os.getenv("NVIDIA_API_KEY")
|
215 |
-
)
|
216 |
-
vector_store = FAISS.from_documents(json_chunks, embeddings)
|
217 |
-
|
218 |
-
return vector_store
|
219 |
except Exception as e:
|
220 |
-
print(f"
|
221 |
-
return None
|
222 |
|
223 |
-
# Enhanced
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
def __init__(self):
|
235 |
-
self.agno_agents = create_agno_agents()
|
236 |
-
self.vector_store = setup_optimized_faiss()
|
237 |
-
self.langgraph_tools = [multiply, add, subtract, divide, modulus, optimized_web_search, optimized_wiki_search]
|
238 |
-
|
239 |
-
if self.vector_store:
|
240 |
-
retriever = self.vector_store.as_retriever(search_type="similarity", search_kwargs={"k": 2})
|
241 |
-
retriever_tool = create_retriever_tool(
|
242 |
-
retriever=retriever,
|
243 |
-
name="Question_Search",
|
244 |
-
description="Retrieve similar questions from knowledge base."
|
245 |
-
)
|
246 |
-
self.langgraph_tools.append(retriever_tool)
|
247 |
-
|
248 |
-
self.graph = self._build_hybrid_graph()
|
249 |
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
gemini_llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash-lite", temperature=0)
|
256 |
|
257 |
-
|
258 |
-
"
|
259 |
-
|
260 |
-
|
261 |
-
# Route math to LangGraph (faster for calculations)
|
262 |
-
if any(word in query for word in ['calculate', 'math', 'multiply', 'add', 'subtract', 'divide']):
|
263 |
-
agent_type = "langgraph_math"
|
264 |
-
# Route complex research to Agno (better reasoning)
|
265 |
-
elif any(word in query for word in ['research', 'analyze', 'explain', 'compare']):
|
266 |
-
agent_type = "agno_research"
|
267 |
-
# Route factual queries to LangGraph (faster retrieval)
|
268 |
-
elif any(word in query for word in ['what is', 'who is', 'when', 'where']):
|
269 |
-
agent_type = "langgraph_retrieval"
|
270 |
-
else:
|
271 |
-
agent_type = "agno_general"
|
272 |
|
273 |
-
|
274 |
|
275 |
-
|
276 |
-
|
277 |
-
|
|
|
278 |
|
279 |
-
|
280 |
-
|
|
|
|
|
|
|
281 |
|
282 |
-
|
283 |
-
|
|
|
284 |
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
"
|
294 |
-
|
295 |
-
|
296 |
-
return {**state, "final_answer": f"Math processing error: {str(e)}"}
|
297 |
-
|
298 |
-
def agno_research_node(state: EnhancedAgentState) -> EnhancedAgentState:
|
299 |
-
"""Agno research processing (optimized for quality)"""
|
300 |
-
gemini_limiter.wait_if_needed()
|
301 |
|
302 |
-
|
303 |
-
try:
|
304 |
-
# Use Agno's research agent for complex reasoning
|
305 |
-
response = self.agno_agents["research"].run(state["query"], stream=False)
|
306 |
-
processing_time = time.time() - start_time
|
307 |
-
|
308 |
-
return {
|
309 |
-
**state,
|
310 |
-
"agno_response": response,
|
311 |
-
"final_answer": response,
|
312 |
-
"performance_metrics": {"processing_time": processing_time, "provider": "Agno-Gemini"}
|
313 |
-
}
|
314 |
-
except Exception as e:
|
315 |
-
return {**state, "final_answer": f"Research processing error: {str(e)}"}
|
316 |
-
|
317 |
-
def langgraph_retrieval_node(state: EnhancedAgentState) -> EnhancedAgentState:
|
318 |
-
"""LangGraph retrieval processing (optimized for speed)"""
|
319 |
-
groq_limiter.wait_if_needed()
|
320 |
|
321 |
-
|
322 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
323 |
|
324 |
-
|
325 |
-
messages = [system_msg, HumanMessage(content=state["query"])]
|
326 |
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
339 |
|
340 |
-
|
341 |
-
"
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
processing_time = time.time() - start_time
|
353 |
-
|
354 |
-
return {
|
355 |
-
**state,
|
356 |
-
"agno_response": response,
|
357 |
-
"final_answer": response,
|
358 |
-
"performance_metrics": {"processing_time": processing_time, "provider": "Agno-General"}
|
359 |
-
}
|
360 |
-
except Exception as e:
|
361 |
-
return {**state, "final_answer": f"General processing error: {str(e)}"}
|
362 |
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
367 |
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
builder.add_node("langgraph_retrieval", langgraph_retrieval_node)
|
374 |
-
builder.add_node("agno_general", agno_general_node)
|
375 |
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
"langgraph_math": "langgraph_math",
|
382 |
-
"agno_research": "agno_research",
|
383 |
-
"langgraph_retrieval": "langgraph_retrieval",
|
384 |
-
"agno_general": "agno_general"
|
385 |
-
}
|
386 |
-
)
|
387 |
|
388 |
-
|
389 |
-
|
390 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
391 |
|
392 |
-
|
393 |
-
return builder.compile(checkpointer=memory)
|
394 |
-
|
395 |
-
def process_query(self, query: str) -> Dict[str, Any]:
|
396 |
-
"""Process query with performance optimization"""
|
397 |
-
start_time = time.time()
|
398 |
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
"performance_metrics": {},
|
405 |
-
"agno_response": ""
|
406 |
-
}
|
407 |
|
408 |
-
|
|
|
|
|
|
|
|
|
409 |
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
}
|
428 |
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
438 |
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
443 |
|
444 |
-
# Main execution
|
445 |
if __name__ == "__main__":
|
446 |
-
|
447 |
-
|
|
|
448 |
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
455 |
|
456 |
-
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
|
461 |
-
print(f"Processing Time: {result['performance_metrics'].get('total_time', 0):.2f}s")
|
462 |
-
print("-" * 80)
|
|
|
1 |
+
""" Enhanced Hybrid Agent Evaluation Runner"""
|
2 |
+
import os
|
3 |
+
import inspect
|
4 |
+
import gradio as gr
|
5 |
+
import requests
|
6 |
+
import pandas as pd
|
7 |
+
from langchain_core.messages import HumanMessage
|
8 |
+
from agent import HybridLangGraphAgnoSystem
|
9 |
|
10 |
+
# --- Constants ---
|
11 |
+
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
|
|
|
12 |
|
13 |
+
# --- Enhanced Basic Agent Definition ---
|
14 |
+
class BasicAgent:
|
15 |
+
"""A hybrid LangGraph + Agno agent with performance optimization."""
|
16 |
+
def __init__(self):
|
17 |
+
print("BasicAgent initialized with Hybrid LangGraph + Agno System.")
|
18 |
+
self.hybrid_system = HybridLangGraphAgnoSystem()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
def __call__(self, question: str) -> str:
|
21 |
+
print(f"Agent received question: {question}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
try:
|
24 |
+
# Process query using hybrid system
|
25 |
+
result = self.hybrid_system.process_query(question)
|
26 |
+
|
27 |
+
# Extract final answer
|
28 |
+
answer = result.get("answer", "No response generated")
|
29 |
+
|
30 |
+
# Clean up the answer - extract only final answer if present
|
31 |
+
if "FINAL ANSWER:" in answer:
|
32 |
+
final_answer = answer.split("FINAL ANSWER:")[-1].strip()
|
33 |
+
else:
|
34 |
+
final_answer = answer.strip()
|
35 |
+
|
36 |
+
# Log performance metrics for debugging
|
37 |
+
metrics = result.get("performance_metrics", {})
|
38 |
+
provider = result.get("provider_used", "Unknown")
|
39 |
+
processing_time = metrics.get("total_time", 0)
|
40 |
+
|
41 |
+
print(f"Provider used: {provider}, Processing time: {processing_time:.2f}s")
|
42 |
+
|
43 |
+
return final_answer
|
44 |
+
|
45 |
+
except Exception as e:
|
46 |
+
print(f"Error in agent processing: {e}")
|
47 |
+
return f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
50 |
+
"""
|
51 |
+
Fetches all questions, runs the Enhanced Hybrid Agent on them, submits all answers,
|
52 |
+
and displays the results with performance metrics.
|
53 |
+
"""
|
54 |
+
# --- Determine HF Space Runtime URL and Repo URL ---
|
55 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
56 |
|
57 |
+
if profile:
|
58 |
+
username= f"{profile.username}"
|
59 |
+
print(f"User logged in: {username}")
|
60 |
+
else:
|
61 |
+
print("User not logged in.")
|
62 |
+
return "Please Login to Hugging Face with the button.", None
|
63 |
|
64 |
+
api_url = DEFAULT_API_URL
|
65 |
+
questions_url = f"{api_url}/questions"
|
66 |
+
submit_url = f"{api_url}/submit"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
# 1. Instantiate Enhanced Hybrid Agent
|
|
|
|
|
69 |
try:
|
70 |
+
agent = BasicAgent()
|
71 |
+
print("β
Hybrid LangGraph + Agno Agent initialized successfully")
|
|
|
|
|
|
|
|
|
|
|
72 |
except Exception as e:
|
73 |
+
print(f"β Error instantiating hybrid agent: {e}")
|
74 |
+
return f"Error initializing hybrid agent: {e}", None
|
75 |
+
|
76 |
+
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
77 |
+
print(f"π Agent code repository: {agent_code}")
|
78 |
|
79 |
+
# 2. Fetch Questions
|
80 |
+
print(f"π₯ Fetching questions from: {questions_url}")
|
|
|
81 |
try:
|
82 |
+
response = requests.get(questions_url, timeout=15)
|
83 |
+
response.raise_for_status()
|
84 |
+
questions_data = response.json()
|
85 |
+
if not questions_data:
|
86 |
+
print("β Fetched questions list is empty.")
|
87 |
+
return "Fetched questions list is empty or invalid format.", None
|
88 |
+
print(f"β
Fetched {len(questions_data)} questions successfully.")
|
89 |
+
except requests.exceptions.RequestException as e:
|
90 |
+
print(f"β Error fetching questions: {e}")
|
91 |
+
return f"Error fetching questions: {e}", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
except Exception as e:
|
93 |
+
print(f"β An unexpected error occurred fetching questions: {e}")
|
94 |
+
return f"An unexpected error occurred fetching questions: {e}", None
|
95 |
|
96 |
+
# 3. Run Enhanced Hybrid Agent with Performance Tracking
|
97 |
+
results_log = []
|
98 |
+
answers_payload = []
|
99 |
+
performance_stats = {
|
100 |
+
"langgraph_math": 0,
|
101 |
+
"agno_research": 0,
|
102 |
+
"langgraph_retrieval": 0,
|
103 |
+
"agno_general": 0,
|
104 |
+
"errors": 0,
|
105 |
+
"total_processing_time": 0
|
106 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
+
print(f"π Running Enhanced Hybrid Agent on {len(questions_data)} questions...")
|
109 |
+
|
110 |
+
for i, item in enumerate(questions_data, 1):
|
111 |
+
task_id = item.get("task_id")
|
112 |
+
question_text = item.get("question")
|
|
|
113 |
|
114 |
+
if not task_id or question_text is None:
|
115 |
+
print(f"β οΈ Skipping item {i} with missing task_id or question: {item}")
|
116 |
+
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
+
print(f"π Processing question {i}/{len(questions_data)}: {task_id}")
|
119 |
|
120 |
+
try:
|
121 |
+
# Get detailed result from hybrid system
|
122 |
+
detailed_result = agent.hybrid_system.process_query(question_text)
|
123 |
+
submitted_answer = detailed_result.get("answer", "No response")
|
124 |
|
125 |
+
# Extract final answer
|
126 |
+
if "FINAL ANSWER:" in submitted_answer:
|
127 |
+
clean_answer = submitted_answer.split("FINAL ANSWER:")[-1].strip()
|
128 |
+
else:
|
129 |
+
clean_answer = submitted_answer.strip()
|
130 |
|
131 |
+
# Track performance metrics
|
132 |
+
provider = detailed_result.get("provider_used", "Unknown")
|
133 |
+
processing_time = detailed_result.get("performance_metrics", {}).get("total_time", 0)
|
134 |
|
135 |
+
# Update performance stats
|
136 |
+
if "LangGraph" in provider:
|
137 |
+
if "Math" in provider:
|
138 |
+
performance_stats["langgraph_math"] += 1
|
139 |
+
else:
|
140 |
+
performance_stats["langgraph_retrieval"] += 1
|
141 |
+
elif "Agno" in provider:
|
142 |
+
if "Research" in provider:
|
143 |
+
performance_stats["agno_research"] += 1
|
144 |
+
else:
|
145 |
+
performance_stats["agno_general"] += 1
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
+
performance_stats["total_processing_time"] += processing_time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
|
149 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": clean_answer})
|
150 |
+
results_log.append({
|
151 |
+
"Task ID": task_id,
|
152 |
+
"Question": question_text[:100] + "..." if len(question_text) > 100 else question_text,
|
153 |
+
"Submitted Answer": clean_answer,
|
154 |
+
"Provider": provider,
|
155 |
+
"Processing Time (s)": f"{processing_time:.2f}"
|
156 |
+
})
|
157 |
|
158 |
+
print(f"β
Question {i} processed successfully using {provider}")
|
|
|
159 |
|
160 |
+
except Exception as e:
|
161 |
+
print(f"β Error running agent on task {task_id}: {e}")
|
162 |
+
performance_stats["errors"] += 1
|
163 |
+
results_log.append({
|
164 |
+
"Task ID": task_id,
|
165 |
+
"Question": question_text[:100] + "..." if len(question_text) > 100 else question_text,
|
166 |
+
"Submitted Answer": f"AGENT ERROR: {e}",
|
167 |
+
"Provider": "Error",
|
168 |
+
"Processing Time (s)": "0.00"
|
169 |
+
})
|
170 |
+
|
171 |
+
if not answers_payload:
|
172 |
+
print("β Agent did not produce any answers to submit.")
|
173 |
+
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
174 |
+
|
175 |
+
# 4. Performance Summary
|
176 |
+
avg_processing_time = performance_stats["total_processing_time"] / len(answers_payload) if answers_payload else 0
|
177 |
+
performance_summary = f"""
|
178 |
+
π Performance Summary:
|
179 |
+
β’ LangGraph Math: {performance_stats['langgraph_math']} queries
|
180 |
+
β’ Agno Research: {performance_stats['agno_research']} queries
|
181 |
+
β’ LangGraph Retrieval: {performance_stats['langgraph_retrieval']} queries
|
182 |
+
β’ Agno General: {performance_stats['agno_general']} queries
|
183 |
+
β’ Errors: {performance_stats['errors']} queries
|
184 |
+
β’ Average Processing Time: {avg_processing_time:.2f}s
|
185 |
+
β’ Total Processing Time: {performance_stats['total_processing_time']:.2f}s
|
186 |
+
"""
|
187 |
+
print(performance_summary)
|
188 |
+
|
189 |
+
# 5. Prepare Submission
|
190 |
+
submission_data = {
|
191 |
+
"username": username.strip(),
|
192 |
+
"agent_code": agent_code,
|
193 |
+
"answers": answers_payload
|
194 |
+
}
|
195 |
+
status_update = f"π― Hybrid Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
196 |
+
print(status_update)
|
197 |
+
|
198 |
+
# 6. Submit Results
|
199 |
+
print(f"π€ Submitting {len(answers_payload)} answers to: {submit_url}")
|
200 |
+
try:
|
201 |
+
response = requests.post(submit_url, json=submission_data, timeout=120) # Increased timeout
|
202 |
+
response.raise_for_status()
|
203 |
+
result_data = response.json()
|
204 |
|
205 |
+
final_status = (
|
206 |
+
f"π Submission Successful!\n"
|
207 |
+
f"π€ User: {result_data.get('username')}\n"
|
208 |
+
f"π Overall Score: {result_data.get('score', 'N/A')}% "
|
209 |
+
f"({result_data.get('correct_count', '?')}/{result_data.get('total_attempted', '?')} correct)\n"
|
210 |
+
f"π¬ Message: {result_data.get('message', 'No message received.')}\n"
|
211 |
+
f"{performance_summary}"
|
212 |
+
)
|
213 |
+
print("β
Submission successful.")
|
214 |
+
results_df = pd.DataFrame(results_log)
|
215 |
+
return final_status, results_df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
|
217 |
+
except requests.exceptions.HTTPError as e:
|
218 |
+
error_detail = f"Server responded with status {e.response.status_code}."
|
219 |
+
try:
|
220 |
+
error_json = e.response.json()
|
221 |
+
error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
|
222 |
+
except requests.exceptions.JSONDecodeError:
|
223 |
+
error_detail += f" Response: {e.response.text[:500]}"
|
224 |
+
status_message = f"β Submission Failed: {error_detail}"
|
225 |
+
print(status_message)
|
226 |
+
results_df = pd.DataFrame(results_log)
|
227 |
+
return status_message, results_df
|
228 |
|
229 |
+
except requests.exceptions.Timeout:
|
230 |
+
status_message = "β Submission Failed: The request timed out."
|
231 |
+
print(status_message)
|
232 |
+
results_df = pd.DataFrame(results_log)
|
233 |
+
return status_message, results_df
|
|
|
|
|
234 |
|
235 |
+
except requests.exceptions.RequestException as e:
|
236 |
+
status_message = f"β Submission Failed: Network error - {e}"
|
237 |
+
print(status_message)
|
238 |
+
results_df = pd.DataFrame(results_log)
|
239 |
+
return status_message, results_df
|
|
|
|
|
|
|
|
|
|
|
|
|
240 |
|
241 |
+
except Exception as e:
|
242 |
+
status_message = f"β An unexpected error occurred during submission: {e}"
|
243 |
+
print(status_message)
|
244 |
+
results_df = pd.DataFrame(results_log)
|
245 |
+
return status_message, results_df
|
246 |
+
|
247 |
+
# --- Enhanced Gradio Interface ---
|
248 |
+
with gr.Blocks(title="Enhanced Hybrid Agent Evaluation") as demo:
|
249 |
+
gr.Markdown("# π Enhanced Hybrid LangGraph + Agno Agent Evaluation Runner")
|
250 |
+
gr.Markdown(
|
251 |
+
"""
|
252 |
+
## π― **Advanced AI Agent System**
|
253 |
|
254 |
+
This evaluation runner uses a **Hybrid LangGraph + Agno Agent System** that combines the best of both frameworks:
|
|
|
|
|
|
|
|
|
|
|
255 |
|
256 |
+
### π§ **Intelligent Routing System**
|
257 |
+
- **π’ Mathematical Queries** β LangGraph (Groq Llama 3.3 70B) - *Optimized for speed*
|
258 |
+
- **π Complex Research** β Agno (Gemini 2.0 Flash-Lite) - *Optimized for reasoning*
|
259 |
+
- **π Factual Retrieval** β LangGraph + FAISS Vector Store - *Optimized for accuracy*
|
260 |
+
- **π General Queries** β Agno Multi-Agent System - *Optimized for comprehensiveness*
|
|
|
|
|
|
|
261 |
|
262 |
+
### β‘ **Performance Features**
|
263 |
+
- **Rate Limiting**: Intelligent rate management for free tier models
|
264 |
+
- **Caching**: Performance optimization with query caching
|
265 |
+
- **Fallback Systems**: Automatic provider switching on failures
|
266 |
+
- **Performance Tracking**: Real-time metrics and provider usage stats
|
267 |
|
268 |
+
### π **Tools & Capabilities**
|
269 |
+
- Mathematical calculations (add, subtract, multiply, divide, modulus)
|
270 |
+
- Web search (Tavily, Wikipedia, ArXiv)
|
271 |
+
- FAISS vector database for similar question retrieval
|
272 |
+
- Memory persistence across sessions
|
273 |
+
|
274 |
+
---
|
275 |
+
|
276 |
+
**Instructions:**
|
277 |
+
1. π Log in to your Hugging Face account using the button below
|
278 |
+
2. π Click 'Run Evaluation & Submit All Answers' to start the evaluation
|
279 |
+
3. π Monitor real-time performance metrics and provider usage
|
280 |
+
4. π View your final score and detailed results
|
281 |
+
|
282 |
+
**Note:** The hybrid system automatically selects the optimal AI provider for each question type to maximize both speed and accuracy.
|
283 |
+
"""
|
284 |
+
)
|
|
|
285 |
|
286 |
+
gr.LoginButton()
|
287 |
+
|
288 |
+
with gr.Row():
|
289 |
+
run_button = gr.Button(
|
290 |
+
"π Run Evaluation & Submit All Answers",
|
291 |
+
variant="primary",
|
292 |
+
size="lg"
|
293 |
+
)
|
294 |
+
|
295 |
+
status_output = gr.Textbox(
|
296 |
+
label="π Run Status / Submission Result",
|
297 |
+
lines=10,
|
298 |
+
interactive=False,
|
299 |
+
placeholder="Status updates will appear here..."
|
300 |
+
)
|
301 |
+
|
302 |
+
results_table = gr.DataFrame(
|
303 |
+
label="π Questions, Answers & Performance Metrics",
|
304 |
+
wrap=True,
|
305 |
+
height=400
|
306 |
+
)
|
307 |
+
|
308 |
+
run_button.click(
|
309 |
+
fn=run_and_submit_all,
|
310 |
+
outputs=[status_output, results_table]
|
311 |
+
)
|
312 |
|
313 |
+
# Add footer with system info
|
314 |
+
gr.Markdown(
|
315 |
+
"""
|
316 |
+
---
|
317 |
+
### π§ **System Information**
|
318 |
+
- **Primary Models**: Groq Llama 3.3 70B, Gemini 2.0 Flash-Lite, NVIDIA Llama 3.1 70B
|
319 |
+
- **Frameworks**: LangGraph + Agno Hybrid Architecture
|
320 |
+
- **Vector Store**: FAISS with NVIDIA Embeddings
|
321 |
+
- **Rate Limiting**: Advanced rate management with exponential backoff
|
322 |
+
- **Memory**: Persistent agent memory with session summaries
|
323 |
+
"""
|
324 |
+
)
|
325 |
|
|
|
326 |
if __name__ == "__main__":
|
327 |
+
print("\n" + "="*80)
|
328 |
+
print("π ENHANCED HYBRID AGENT EVALUATION RUNNER")
|
329 |
+
print("="*80)
|
330 |
|
331 |
+
# Check for environment variables
|
332 |
+
space_host_startup = os.getenv("SPACE_HOST")
|
333 |
+
space_id_startup = os.getenv("SPACE_ID")
|
334 |
+
|
335 |
+
if space_host_startup:
|
336 |
+
print(f"β
SPACE_HOST found: {space_host_startup}")
|
337 |
+
print(f" π Runtime URL: https://{space_host_startup}.hf.space")
|
338 |
+
else:
|
339 |
+
print("βΉοΈ SPACE_HOST environment variable not found (running locally?).")
|
340 |
+
|
341 |
+
if space_id_startup:
|
342 |
+
print(f"β
SPACE_ID found: {space_id_startup}")
|
343 |
+
print(f" π Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
344 |
+
print(f" π³ Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
|
345 |
+
else:
|
346 |
+
print("βΉοΈ SPACE_ID environment variable not found (running locally?).")
|
347 |
+
|
348 |
+
print("\nπ― System Features:")
|
349 |
+
print(" β’ Hybrid LangGraph + Agno Architecture")
|
350 |
+
print(" β’ Intelligent Query Routing")
|
351 |
+
print(" β’ Performance Optimization")
|
352 |
+
print(" β’ Advanced Rate Limiting")
|
353 |
+
print(" β’ FAISS Vector Database")
|
354 |
+
print(" β’ Multi-Provider Fallbacks")
|
355 |
|
356 |
+
print("\n" + "="*80)
|
357 |
+
print("π Launching Enhanced Gradio Interface...")
|
358 |
+
print("="*80 + "\n")
|
359 |
+
|
360 |
+
demo.launch(debug=True, share=False)
|
|
|
|