Commit
·
05ecbdd
1
Parent(s):
f844f16
updated app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,14 @@ import requests
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
# from agents import LlamaIndexAgent
|
| 7 |
-
from
|
|
|
|
| 8 |
import asyncio
|
| 9 |
import aiohttp
|
| 10 |
from langfuse.langchain import CallbackHandler
|
| 11 |
from langchain_core.messages import HumanMessage
|
| 12 |
import tempfile
|
|
|
|
| 13 |
|
| 14 |
# Initialize Langfuse CallbackHandler for LangGraph/Langchain (tracing)
|
| 15 |
langfuse_handler = CallbackHandler()
|
|
@@ -21,18 +23,25 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 21 |
# --- Basic Agent Definition ---
|
| 22 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 23 |
class BasicAgent:
|
| 24 |
-
"""Wrapper that executes the
|
| 25 |
|
| 26 |
def __init__(self):
|
| 27 |
-
print("BasicAgent (multi-agent) initialized.")
|
| 28 |
|
| 29 |
async def aquery(self, question: str) -> str:
|
| 30 |
-
"""Run the
|
| 31 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 32 |
-
loop = asyncio.get_event_loop()
|
| 33 |
try:
|
| 34 |
-
#
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
print(f"Agent returning answer: {answer}")
|
| 37 |
return answer
|
| 38 |
except Exception as e:
|
|
@@ -234,6 +243,15 @@ with gr.Blocks() as demo:
|
|
| 234 |
api_name="submit_answers",
|
| 235 |
)
|
| 236 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
if __name__ == "__main__":
|
| 238 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 239 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
|
@@ -255,5 +273,16 @@ if __name__ == "__main__":
|
|
| 255 |
|
| 256 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 257 |
|
| 258 |
-
print("Launching Gradio Interface
|
| 259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
# from agents import LlamaIndexAgent
|
| 7 |
+
from langgraph_agent_system import run_agent_system # Updated: use the latest multi-agent system
|
| 8 |
+
from observability import flush_traces, shutdown_observability # Add cleanup functions
|
| 9 |
import asyncio
|
| 10 |
import aiohttp
|
| 11 |
from langfuse.langchain import CallbackHandler
|
| 12 |
from langchain_core.messages import HumanMessage
|
| 13 |
import tempfile
|
| 14 |
+
import uuid
|
| 15 |
|
| 16 |
# Initialize Langfuse CallbackHandler for LangGraph/Langchain (tracing)
|
| 17 |
langfuse_handler = CallbackHandler()
|
|
|
|
| 23 |
# --- Basic Agent Definition ---
|
| 24 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 25 |
class BasicAgent:
|
| 26 |
+
"""Wrapper that executes the latest multi-agent LangGraph system."""
|
| 27 |
|
| 28 |
def __init__(self):
|
| 29 |
+
print("BasicAgent (latest multi-agent system) initialized.")
|
| 30 |
|
| 31 |
async def aquery(self, question: str) -> str:
|
| 32 |
+
"""Run the latest async multi-agent system directly."""
|
| 33 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
|
|
| 34 |
try:
|
| 35 |
+
# Generate unique session ID for this query
|
| 36 |
+
session_id = f"app_session_{uuid.uuid4().hex[:8]}"
|
| 37 |
+
|
| 38 |
+
# Call the latest async agent system directly
|
| 39 |
+
answer = await run_agent_system(
|
| 40 |
+
query=question,
|
| 41 |
+
user_id="gradio_app_user",
|
| 42 |
+
session_id=session_id,
|
| 43 |
+
max_iterations=3
|
| 44 |
+
)
|
| 45 |
print(f"Agent returning answer: {answer}")
|
| 46 |
return answer
|
| 47 |
except Exception as e:
|
|
|
|
| 243 |
api_name="submit_answers",
|
| 244 |
)
|
| 245 |
|
| 246 |
+
def cleanup_agent_system():
|
| 247 |
+
"""Cleanup function for the agent system."""
|
| 248 |
+
try:
|
| 249 |
+
flush_traces(background=False)
|
| 250 |
+
shutdown_observability()
|
| 251 |
+
print("✅ Agent system cleanup completed")
|
| 252 |
+
except Exception as e:
|
| 253 |
+
print(f"⚠️ Cleanup warning: {e}")
|
| 254 |
+
|
| 255 |
if __name__ == "__main__":
|
| 256 |
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 257 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
|
|
|
| 273 |
|
| 274 |
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 275 |
|
| 276 |
+
print("Launching Gradio Interface with Latest Multi-Agent System...")
|
| 277 |
+
print("🤖 Using: LangGraph Multi-Agent System (Lead → Research → Code → Formatter)")
|
| 278 |
+
print("📊 Features: Langfuse v3 observability, iterative workflows, GAIA compliance")
|
| 279 |
+
|
| 280 |
+
try:
|
| 281 |
+
demo.launch(debug=True, share=False)
|
| 282 |
+
except KeyboardInterrupt:
|
| 283 |
+
print("\n🛑 Shutting down gracefully...")
|
| 284 |
+
cleanup_agent_system()
|
| 285 |
+
except Exception as e:
|
| 286 |
+
print(f"❌ Error during app execution: {e}")
|
| 287 |
+
cleanup_agent_system()
|
| 288 |
+
raise
|