Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
|
@@ -10,48 +15,22 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
-
|
| 14 |
-
from langchain.schema import HumanMessage
|
| 15 |
-
from langchain_community.chat_models import ChatOllama
|
| 16 |
|
| 17 |
class BasicAgent:
|
|
|
|
| 18 |
def __init__(self):
|
| 19 |
-
print("
|
| 20 |
-
self.
|
| 21 |
-
self.llm = ChatOllama(model="llama3", temperature=0) # match your agent.py setup
|
| 22 |
|
| 23 |
def __call__(self, question: str) -> str:
|
| 24 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
# Attempt to route to the best matching tool
|
| 27 |
-
for tool in self.tools.values():
|
| 28 |
-
inputs = tool.inputs
|
| 29 |
-
try:
|
| 30 |
-
# Try calling the tool with the question as input
|
| 31 |
-
if list(inputs.keys()) == ["expression"]:
|
| 32 |
-
return tool.forward(question)
|
| 33 |
-
elif list(inputs.keys()) == ["query"]:
|
| 34 |
-
return tool.forward(question)
|
| 35 |
-
elif list(inputs.keys()) == ["topic"]:
|
| 36 |
-
return tool.forward(question)
|
| 37 |
-
elif list(inputs.keys()) == ["code"]:
|
| 38 |
-
return tool.forward(question)
|
| 39 |
-
elif list(inputs.keys()) == ["question"]:
|
| 40 |
-
return tool.forward(question)
|
| 41 |
-
elif set(inputs.keys()) == {"context", "query"}:
|
| 42 |
-
return tool.forward(context=question, query=question)
|
| 43 |
-
elif set(inputs.keys()) == {"story", "question"}:
|
| 44 |
-
return tool.forward(story=question, question=question)
|
| 45 |
-
except Exception as e:
|
| 46 |
-
print(f"Tool '{tool.name}' failed: {e}")
|
| 47 |
-
continue
|
| 48 |
-
|
| 49 |
-
# Fallback: use LLM directly
|
| 50 |
-
try:
|
| 51 |
-
response = self.llm.generate([HumanMessage(content=question)])
|
| 52 |
-
return response.content.strip()
|
| 53 |
-
except Exception as e:
|
| 54 |
-
return f"Fallback LLM error: {e}"
|
| 55 |
|
| 56 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 57 |
"""
|
|
|
|
| 1 |
+
""" Basic 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 build_graph
|
| 9 |
+
|
| 10 |
+
|
| 11 |
|
| 12 |
# (Keep Constants as is)
|
| 13 |
# --- Constants ---
|
|
|
|
| 15 |
|
| 16 |
# --- Basic Agent Definition ---
|
| 17 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 18 |
+
|
|
|
|
|
|
|
| 19 |
|
| 20 |
class BasicAgent:
|
| 21 |
+
"""A langgraph agent."""
|
| 22 |
def __init__(self):
|
| 23 |
+
print("BasicAgent initialized.")
|
| 24 |
+
self.graph = build_graph()
|
|
|
|
| 25 |
|
| 26 |
def __call__(self, question: str) -> str:
|
| 27 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 28 |
+
# Wrap the question in a HumanMessage from langchain_core
|
| 29 |
+
messages = [HumanMessage(content=question)]
|
| 30 |
+
messages = self.graph.invoke({"messages": messages})
|
| 31 |
+
answer = messages['messages'][-1].content
|
| 32 |
+
return answer[14:]
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 36 |
"""
|