Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ from smolagents.models import InferenceClientModel
|
|
| 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"
|
|
@@ -35,21 +35,21 @@ class CalculatorTool(Tool):
|
|
| 35 |
class TimeTool(Tool):
|
| 36 |
name = "current_time"
|
| 37 |
description = "Gets current UTC time"
|
| 38 |
-
input_schema = {} # No
|
| 39 |
output_schema = {
|
| 40 |
"time": {
|
| 41 |
"type": "string",
|
| 42 |
-
"description": "Current time
|
| 43 |
}
|
| 44 |
}
|
| 45 |
|
| 46 |
def use(self) -> dict:
|
| 47 |
return {"time": datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")}
|
| 48 |
|
| 49 |
-
# ---
|
| 50 |
class LocalAgent:
|
| 51 |
def __init__(self):
|
| 52 |
-
print("Initializing agent
|
| 53 |
self.tools = [CalculatorTool(), TimeTool()]
|
| 54 |
self.agent = ToolCallingAgent(
|
| 55 |
tools=self.tools,
|
|
@@ -60,42 +60,32 @@ class LocalAgent:
|
|
| 60 |
)
|
| 61 |
|
| 62 |
def __call__(self, question: str) -> str:
|
| 63 |
-
print(f"Processing: {question[:100]}...")
|
| 64 |
question_lower = question.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
# Direct tool usage for simple queries
|
| 67 |
-
if any(word in question_lower for word in ["calculate", "what is", "how much is", "+", "-", "*", "/"]):
|
| 68 |
-
result = CalculatorTool().use(question.replace("?", ""))
|
| 69 |
-
return result["result"]
|
| 70 |
-
|
| 71 |
-
if any(word in question_lower for word in ["time", "current time"]):
|
| 72 |
-
result = TimeTool().use()
|
| 73 |
-
return result["time"]
|
| 74 |
-
|
| 75 |
-
# Use full agent for complex questions
|
| 76 |
try:
|
| 77 |
-
|
| 78 |
-
return str(response)
|
| 79 |
except Exception as e:
|
| 80 |
-
|
| 81 |
-
return "I couldn't process this question."
|
| 82 |
|
| 83 |
-
# --- Evaluation
|
| 84 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 85 |
if not profile:
|
| 86 |
-
return "Please login first", None
|
| 87 |
|
| 88 |
space_id = os.getenv("SPACE_ID", "local-test")
|
| 89 |
api_url = os.getenv("API_URL", DEFAULT_API_URL)
|
| 90 |
|
| 91 |
try:
|
| 92 |
agent = LocalAgent()
|
| 93 |
-
|
| 94 |
-
response.raise_for_status()
|
| 95 |
-
questions = response.json()
|
| 96 |
|
| 97 |
-
results = []
|
| 98 |
answers = []
|
|
|
|
|
|
|
| 99 |
for q in questions:
|
| 100 |
try:
|
| 101 |
answer = agent(q["question"])
|
|
@@ -121,36 +111,34 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 121 |
"answers": answers
|
| 122 |
}
|
| 123 |
|
| 124 |
-
|
| 125 |
-
response.raise_for_status()
|
| 126 |
-
result = response.json()
|
| 127 |
|
| 128 |
return (
|
| 129 |
-
f"
|
| 130 |
f"Correct: {result.get('correct_count', '?')}/{result.get('total_attempted', '?')}",
|
| 131 |
pd.DataFrame(results)
|
| 132 |
)
|
| 133 |
|
| 134 |
except Exception as e:
|
| 135 |
-
return f"Evaluation failed: {str(e)}", pd.DataFrame(
|
| 136 |
|
| 137 |
-
# --- Gradio
|
| 138 |
with gr.Blocks(title="Agent Evaluation Runner") as app:
|
| 139 |
-
gr.Markdown(""
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
results_table = gr.DataFrame(label="Question Log")
|
| 148 |
|
| 149 |
run_btn.click(
|
| 150 |
fn=run_and_submit_all,
|
| 151 |
-
inputs=[
|
| 152 |
outputs=[output, results_table]
|
| 153 |
)
|
| 154 |
|
| 155 |
if __name__ == "__main__":
|
| 156 |
app.launch()
|
|
|
|
|
|
| 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"
|
|
|
|
| 35 |
class TimeTool(Tool):
|
| 36 |
name = "current_time"
|
| 37 |
description = "Gets current UTC time"
|
| 38 |
+
input_schema = {} # No input
|
| 39 |
output_schema = {
|
| 40 |
"time": {
|
| 41 |
"type": "string",
|
| 42 |
+
"description": "Current UTC time (YYYY-MM-DD HH:MM:SS)"
|
| 43 |
}
|
| 44 |
}
|
| 45 |
|
| 46 |
def use(self) -> dict:
|
| 47 |
return {"time": datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")}
|
| 48 |
|
| 49 |
+
# --- Agent Setup ---
|
| 50 |
class LocalAgent:
|
| 51 |
def __init__(self):
|
| 52 |
+
print("Initializing agent...")
|
| 53 |
self.tools = [CalculatorTool(), TimeTool()]
|
| 54 |
self.agent = ToolCallingAgent(
|
| 55 |
tools=self.tools,
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
def __call__(self, question: str) -> str:
|
|
|
|
| 63 |
question_lower = question.lower()
|
| 64 |
+
if any(op in question_lower for op in ["calculate", "what is", "+", "-", "*", "/"]):
|
| 65 |
+
return CalculatorTool().use(question.replace("?", ""))["result"]
|
| 66 |
+
if "time" in question_lower:
|
| 67 |
+
return TimeTool().use()["time"]
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
try:
|
| 70 |
+
return str(self.agent.run(question))
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
+
return f"Error: {e}"
|
|
|
|
| 73 |
|
| 74 |
+
# --- Evaluation Logic ---
|
| 75 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 76 |
if not profile:
|
| 77 |
+
return "Please login first.", None
|
| 78 |
|
| 79 |
space_id = os.getenv("SPACE_ID", "local-test")
|
| 80 |
api_url = os.getenv("API_URL", DEFAULT_API_URL)
|
| 81 |
|
| 82 |
try:
|
| 83 |
agent = LocalAgent()
|
| 84 |
+
questions = requests.get(f"{api_url}/questions", timeout=15).json()
|
|
|
|
|
|
|
| 85 |
|
|
|
|
| 86 |
answers = []
|
| 87 |
+
results = []
|
| 88 |
+
|
| 89 |
for q in questions:
|
| 90 |
try:
|
| 91 |
answer = agent(q["question"])
|
|
|
|
| 111 |
"answers": answers
|
| 112 |
}
|
| 113 |
|
| 114 |
+
result = requests.post(f"{api_url}/submit", json=submission, timeout=60).json()
|
|
|
|
|
|
|
| 115 |
|
| 116 |
return (
|
| 117 |
+
f"✅ Score: {result.get('score', 'N/A')}%\n"
|
| 118 |
f"Correct: {result.get('correct_count', '?')}/{result.get('total_attempted', '?')}",
|
| 119 |
pd.DataFrame(results)
|
| 120 |
)
|
| 121 |
|
| 122 |
except Exception as e:
|
| 123 |
+
return f"Evaluation failed: {str(e)}", pd.DataFrame([])
|
| 124 |
|
| 125 |
+
# --- Gradio UI ---
|
| 126 |
with gr.Blocks(title="Agent Evaluation Runner") as app:
|
| 127 |
+
gr.Markdown("## 🤖 Agent Evaluation with smolagents")
|
| 128 |
+
gr.Markdown("Login, then run the evaluation to test your agent.")
|
| 129 |
+
|
| 130 |
+
gr.LoginButton() # OAuth Login UI
|
| 131 |
|
| 132 |
+
run_btn = gr.Button("🚀 Run Evaluation")
|
| 133 |
+
output = gr.Textbox(label="Result Summary")
|
| 134 |
+
results_table = gr.DataFrame(label="Answers and Logs")
|
|
|
|
| 135 |
|
| 136 |
run_btn.click(
|
| 137 |
fn=run_and_submit_all,
|
| 138 |
+
inputs=[], # No need to pass profile manually
|
| 139 |
outputs=[output, results_table]
|
| 140 |
)
|
| 141 |
|
| 142 |
if __name__ == "__main__":
|
| 143 |
app.launch()
|
| 144 |
+
|