Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,100 +1,78 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Advanced Agent Evaluation Runner with Custom LangGraph Implementation
|
| 3 |
-
"""
|
| 4 |
import os
|
| 5 |
import gradio as gr
|
| 6 |
import requests
|
| 7 |
import pandas as pd
|
| 8 |
-
from typing import Dict, List, Optional
|
| 9 |
from datetime import datetime
|
| 10 |
-
from
|
| 11 |
-
from
|
| 12 |
|
| 13 |
# --- Constants ---
|
| 14 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 15 |
|
| 16 |
-
# --- Custom
|
| 17 |
-
class
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def execute(self, expression: str) -> str:
|
| 25 |
try:
|
| 26 |
-
return
|
| 27 |
except:
|
| 28 |
-
return "Error:
|
| 29 |
-
|
| 30 |
-
class TimeTool:
|
| 31 |
-
def execute(self) -> str:
|
| 32 |
-
return f"Current time: {datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC')}"
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
def route_question(state: AgentState):
|
| 40 |
-
question = state["question"].lower()
|
| 41 |
-
if any(op in question for op in ["+", "-", "*", "/", "calculate"]):
|
| 42 |
-
return "math_tool"
|
| 43 |
-
elif "time" in question or "current time" in question:
|
| 44 |
-
return "time_tool"
|
| 45 |
-
return "llm_response"
|
| 46 |
-
|
| 47 |
-
def math_node(state: AgentState):
|
| 48 |
-
tool = MathTool()
|
| 49 |
-
return {"response": tool.execute(state["question"])}
|
| 50 |
-
|
| 51 |
-
def time_node(state: AgentState):
|
| 52 |
-
tool = TimeTool()
|
| 53 |
-
return {"response": tool.execute()}
|
| 54 |
-
|
| 55 |
-
def llm_node(state: AgentState):
|
| 56 |
-
# Simulated LLM response
|
| 57 |
-
return {"response": f"AI response to: {state['question']}"}
|
| 58 |
-
|
| 59 |
-
# Build graph
|
| 60 |
-
workflow.add_node("math_tool", math_node)
|
| 61 |
-
workflow.add_node("time_tool", time_node)
|
| 62 |
-
workflow.add_node("llm_response", llm_node)
|
| 63 |
-
|
| 64 |
-
workflow.add_conditional_edges(
|
| 65 |
-
"start",
|
| 66 |
-
route_question,
|
| 67 |
-
{
|
| 68 |
-
"math_tool": "math_tool",
|
| 69 |
-
"time_tool": "time_tool",
|
| 70 |
-
"llm_response": "llm_response"
|
| 71 |
-
}
|
| 72 |
-
)
|
| 73 |
-
|
| 74 |
-
workflow.add_edge("math_tool", END)
|
| 75 |
-
workflow.add_edge("time_tool", END)
|
| 76 |
-
workflow.add_edge("llm_response", END)
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
|
| 81 |
-
# ---
|
| 82 |
-
class
|
| 83 |
def __init__(self):
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
def __call__(self, question: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
try:
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
| 92 |
except Exception as e:
|
| 93 |
-
print(f"
|
| 94 |
-
return "
|
| 95 |
|
| 96 |
# --- Evaluation Runner ---
|
| 97 |
-
def
|
| 98 |
if not profile:
|
| 99 |
return "Please login first", None
|
| 100 |
|
|
@@ -102,7 +80,7 @@ def run_evaluation(profile: gr.OAuthProfile | None):
|
|
| 102 |
api_url = os.getenv("API_URL", DEFAULT_API_URL)
|
| 103 |
|
| 104 |
try:
|
| 105 |
-
agent =
|
| 106 |
questions = requests.get(f"{api_url}/questions", timeout=15).json()
|
| 107 |
|
| 108 |
results = []
|
|
@@ -133,10 +111,10 @@ def run_evaluation(profile: gr.OAuthProfile | None):
|
|
| 133 |
return f"Evaluation failed: {e}", None
|
| 134 |
|
| 135 |
# --- Gradio Interface ---
|
| 136 |
-
with gr.Blocks(title="
|
| 137 |
gr.Markdown("""
|
| 138 |
-
##
|
| 139 |
-
|
| 140 |
""")
|
| 141 |
|
| 142 |
gr.LoginButton()
|
|
@@ -145,7 +123,7 @@ with gr.Blocks(title="Custom LangGraph Agent Evaluator") as app:
|
|
| 145 |
results = gr.DataFrame(label="Details")
|
| 146 |
|
| 147 |
run_btn.click(
|
| 148 |
-
fn=
|
| 149 |
outputs=[output, results]
|
| 150 |
)
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
|
|
|
| 5 |
from datetime import datetime
|
| 6 |
+
from transformers import pipeline, Tool
|
| 7 |
+
from transformers.agents import Agent
|
| 8 |
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
+
# --- Custom Tools ---
|
| 13 |
+
class CalculatorTool(Tool):
|
| 14 |
+
name = "calculator"
|
| 15 |
+
description = "Performs mathematical calculations"
|
| 16 |
+
inputs = ["text"]
|
| 17 |
+
outputs = ["text"]
|
| 18 |
+
|
| 19 |
+
def __call__(self, expression: str) -> str:
|
|
|
|
| 20 |
try:
|
| 21 |
+
return str(eval(expression))
|
| 22 |
except:
|
| 23 |
+
return "Error: Could not evaluate the expression"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
class TimeTool(Tool):
|
| 26 |
+
name = "current_time"
|
| 27 |
+
description = "Gets current UTC time"
|
| 28 |
+
inputs = []
|
| 29 |
+
outputs = ["text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
def __call__(self) -> str:
|
| 32 |
+
return datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
|
| 33 |
|
| 34 |
+
# --- Enhanced Agent ---
|
| 35 |
+
class HFLocalAgent:
|
| 36 |
def __init__(self):
|
| 37 |
+
print("Initializing local Hugging Face agent...")
|
| 38 |
+
self.tools = {
|
| 39 |
+
"calculator": CalculatorTool(),
|
| 40 |
+
"time": TimeTool()
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
# Load local model (small but efficient)
|
| 44 |
+
self.llm = pipeline(
|
| 45 |
+
"text-generation",
|
| 46 |
+
model="HuggingFaceH4/zephyr-7b-beta",
|
| 47 |
+
device="cpu" # Change to "cuda" if GPU available
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
def __call__(self, question: str) -> str:
|
| 51 |
+
print(f"Processing: {question[:100]}...")
|
| 52 |
+
question_lower = question.lower()
|
| 53 |
+
|
| 54 |
+
# Math questions
|
| 55 |
+
if any(word in question_lower for word in ["calculate", "what is", "how much is", "+", "-", "*", "/"]):
|
| 56 |
+
return self.tools["calculator"](question.replace("?", ""))
|
| 57 |
+
|
| 58 |
+
# Time questions
|
| 59 |
+
if any(word in question_lower for word in ["time", "current time"]):
|
| 60 |
+
return self.tools["time"]()
|
| 61 |
+
|
| 62 |
+
# Fallback to local LLM
|
| 63 |
try:
|
| 64 |
+
response = self.llm(
|
| 65 |
+
f"Answer concisely: {question}",
|
| 66 |
+
max_new_tokens=100,
|
| 67 |
+
temperature=0.7
|
| 68 |
+
)
|
| 69 |
+
return response[0]['generated_text'].split(":")[-1].strip()
|
| 70 |
except Exception as e:
|
| 71 |
+
print(f"LLM error: {e}")
|
| 72 |
+
return "I couldn't process this question."
|
| 73 |
|
| 74 |
# --- Evaluation Runner ---
|
| 75 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 76 |
if not profile:
|
| 77 |
return "Please login first", None
|
| 78 |
|
|
|
|
| 80 |
api_url = os.getenv("API_URL", DEFAULT_API_URL)
|
| 81 |
|
| 82 |
try:
|
| 83 |
+
agent = HFLocalAgent()
|
| 84 |
questions = requests.get(f"{api_url}/questions", timeout=15).json()
|
| 85 |
|
| 86 |
results = []
|
|
|
|
| 111 |
return f"Evaluation failed: {e}", None
|
| 112 |
|
| 113 |
# --- Gradio Interface ---
|
| 114 |
+
with gr.Blocks(title="Local HF Agent Evaluator") as app:
|
| 115 |
gr.Markdown("""
|
| 116 |
+
## Local Hugging Face Agent Evaluation
|
| 117 |
+
Uses completely free/local models - no API keys required
|
| 118 |
""")
|
| 119 |
|
| 120 |
gr.LoginButton()
|
|
|
|
| 123 |
results = gr.DataFrame(label="Details")
|
| 124 |
|
| 125 |
run_btn.click(
|
| 126 |
+
fn=run_and_submit_all,
|
| 127 |
outputs=[output, results]
|
| 128 |
)
|
| 129 |
|