Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,8 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
from datetime import datetime
|
| 6 |
-
from
|
| 7 |
-
from
|
| 8 |
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
@@ -13,21 +13,8 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 13 |
class CalculatorTool(Tool):
|
| 14 |
name = "calculator"
|
| 15 |
description = "Performs mathematical calculations"
|
| 16 |
-
inputs = {
|
| 17 |
-
"expression": {
|
| 18 |
-
"type": "text",
|
| 19 |
-
"description": "Mathematical expression to evaluate"
|
| 20 |
-
}
|
| 21 |
-
}
|
| 22 |
-
outputs = {
|
| 23 |
-
"result": {
|
| 24 |
-
"type": "text",
|
| 25 |
-
"description": "Result of the calculation"
|
| 26 |
-
}
|
| 27 |
-
}
|
| 28 |
-
output_type = "text"
|
| 29 |
|
| 30 |
-
def
|
| 31 |
try:
|
| 32 |
return str(eval(expression))
|
| 33 |
except:
|
|
@@ -36,32 +23,26 @@ class CalculatorTool(Tool):
|
|
| 36 |
class TimeTool(Tool):
|
| 37 |
name = "current_time"
|
| 38 |
description = "Gets current UTC time"
|
| 39 |
-
inputs = {}
|
| 40 |
-
outputs = {
|
| 41 |
-
"time": {
|
| 42 |
-
"type": "text",
|
| 43 |
-
"description": "Current time in UTC format"
|
| 44 |
-
}
|
| 45 |
-
}
|
| 46 |
-
output_type = "text"
|
| 47 |
|
| 48 |
-
def
|
| 49 |
return datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
|
| 50 |
|
| 51 |
# --- Enhanced Agent ---
|
| 52 |
-
class
|
| 53 |
def __init__(self):
|
| 54 |
-
print("Initializing local
|
| 55 |
self.tools = {
|
| 56 |
"calculator": CalculatorTool(),
|
| 57 |
"time": TimeTool()
|
| 58 |
}
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
self.
|
| 62 |
-
|
| 63 |
-
model=
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
)
|
| 66 |
|
| 67 |
def __call__(self, question: str) -> str:
|
|
@@ -70,23 +51,89 @@ class HFLocalAgent:
|
|
| 70 |
|
| 71 |
# Math questions
|
| 72 |
if any(word in question_lower for word in ["calculate", "what is", "how much is", "+", "-", "*", "/"]):
|
| 73 |
-
return self.tools["calculator"](question.replace("?", ""))
|
| 74 |
|
| 75 |
# Time questions
|
| 76 |
if any(word in question_lower for word in ["time", "current time"]):
|
| 77 |
-
return self.tools["time"]()
|
| 78 |
|
| 79 |
-
# Fallback to
|
| 80 |
try:
|
| 81 |
-
|
| 82 |
-
f"Answer concisely: {question}",
|
| 83 |
-
max_new_tokens=100,
|
| 84 |
-
temperature=0.7
|
| 85 |
-
)
|
| 86 |
-
return response[0]['generated_text'].split(":")[-1].strip()
|
| 87 |
except Exception as e:
|
| 88 |
-
print(f"
|
| 89 |
return "I couldn't process this question."
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
from datetime import datetime
|
| 6 |
+
from smolagents import Tool, ToolCallingAgent
|
| 7 |
+
from smolagents.models import InferenceClientModel
|
| 8 |
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 13 |
class CalculatorTool(Tool):
|
| 14 |
name = "calculator"
|
| 15 |
description = "Performs mathematical calculations"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
def use(self, expression: str) -> str:
|
| 18 |
try:
|
| 19 |
return str(eval(expression))
|
| 20 |
except:
|
|
|
|
| 23 |
class TimeTool(Tool):
|
| 24 |
name = "current_time"
|
| 25 |
description = "Gets current UTC time"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
def use(self) -> str:
|
| 28 |
return datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
|
| 29 |
|
| 30 |
# --- Enhanced Agent ---
|
| 31 |
+
class LocalAgent:
|
| 32 |
def __init__(self):
|
| 33 |
+
print("Initializing local agent with smolagents...")
|
| 34 |
self.tools = {
|
| 35 |
"calculator": CalculatorTool(),
|
| 36 |
"time": TimeTool()
|
| 37 |
}
|
| 38 |
|
| 39 |
+
# Using a local model through smolagents
|
| 40 |
+
self.agent = ToolCallingAgent(
|
| 41 |
+
tools=list(self.tools.values()),
|
| 42 |
+
model=InferenceClientModel(
|
| 43 |
+
model_id="HuggingFaceH4/zephyr-7b-beta",
|
| 44 |
+
api_base="https://api-inference.huggingface.co/models"
|
| 45 |
+
)
|
| 46 |
)
|
| 47 |
|
| 48 |
def __call__(self, question: str) -> str:
|
|
|
|
| 51 |
|
| 52 |
# Math questions
|
| 53 |
if any(word in question_lower for word in ["calculate", "what is", "how much is", "+", "-", "*", "/"]):
|
| 54 |
+
return self.tools["calculator"].use(question.replace("?", ""))
|
| 55 |
|
| 56 |
# Time questions
|
| 57 |
if any(word in question_lower for word in ["time", "current time"]):
|
| 58 |
+
return self.tools["time"].use()
|
| 59 |
|
| 60 |
+
# Fallback to agent with tools
|
| 61 |
try:
|
| 62 |
+
return self.agent.run(question)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
except Exception as e:
|
| 64 |
+
print(f"Agent error: {e}")
|
| 65 |
return "I couldn't process this question."
|
| 66 |
|
| 67 |
+
# --- Evaluation Runner ---
|
| 68 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 69 |
+
if not profile:
|
| 70 |
+
return "Please login first", None
|
| 71 |
+
|
| 72 |
+
space_id = os.getenv("SPACE_ID", "local-test")
|
| 73 |
+
api_url = os.getenv("API_URL", DEFAULT_API_URL)
|
| 74 |
+
|
| 75 |
+
try:
|
| 76 |
+
agent = LocalAgent()
|
| 77 |
+
response = requests.get(f"{api_url}/questions", timeout=15)
|
| 78 |
+
response.raise_for_status()
|
| 79 |
+
questions = response.json()
|
| 80 |
+
|
| 81 |
+
results = []
|
| 82 |
+
answers = []
|
| 83 |
+
for q in questions:
|
| 84 |
+
try:
|
| 85 |
+
answer = agent(q["question"])
|
| 86 |
+
answers.append({
|
| 87 |
+
"task_id": q["task_id"],
|
| 88 |
+
"submitted_answer": answer
|
| 89 |
+
})
|
| 90 |
+
results.append({
|
| 91 |
+
"Task ID": q["task_id"],
|
| 92 |
+
"Question": q["question"],
|
| 93 |
+
"Answer": answer
|
| 94 |
+
})
|
| 95 |
+
except Exception as e:
|
| 96 |
+
results.append({
|
| 97 |
+
"Task ID": q["task_id"],
|
| 98 |
+
"Question": q["question"],
|
| 99 |
+
"Answer": f"Error: {e}"
|
| 100 |
+
})
|
| 101 |
+
|
| 102 |
+
submission = {
|
| 103 |
+
"username": profile.username,
|
| 104 |
+
"agent_code": f"https://huggingface.co/spaces/{space_id}",
|
| 105 |
+
"answers": answers
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
response = requests.post(f"{api_url}/submit", json=submission, timeout=60)
|
| 109 |
+
response.raise_for_status()
|
| 110 |
+
result = response.json()
|
| 111 |
+
|
| 112 |
+
return (
|
| 113 |
+
f"Success! Score: {result.get('score', 'N/A')}%\n"
|
| 114 |
+
f"Correct: {result.get('correct_count', '?')}/{result.get('total_attempted', '?')}",
|
| 115 |
+
pd.DataFrame(results)
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
except Exception as e:
|
| 119 |
+
return f"Evaluation failed: {str(e)}", pd.DataFrame(results if 'results' in locals() else [])
|
| 120 |
+
|
| 121 |
+
# --- Gradio Interface ---
|
| 122 |
+
with gr.Blocks(title="Local Agent Evaluator") as app:
|
| 123 |
+
gr.Markdown("""
|
| 124 |
+
## Local Agent Evaluation
|
| 125 |
+
Uses smolagents with local Hugging Face models
|
| 126 |
+
""")
|
| 127 |
+
|
| 128 |
+
gr.LoginButton()
|
| 129 |
+
run_btn = gr.Button("Start Evaluation")
|
| 130 |
+
output = gr.Textbox(label="Results")
|
| 131 |
+
results = gr.DataFrame(label="Details")
|
| 132 |
+
|
| 133 |
+
run_btn.click(
|
| 134 |
+
fn=run_and_submit_all,
|
| 135 |
+
outputs=[output, results]
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
if __name__ == "__main__":
|
| 139 |
+
app.launch()
|