Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,8 +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 |
-
outputs =
|
| 18 |
|
| 19 |
def __call__(self, expression: str) -> str:
|
| 20 |
try:
|
|
@@ -25,8 +25,8 @@ class CalculatorTool(Tool):
|
|
| 25 |
class TimeTool(Tool):
|
| 26 |
name = "current_time"
|
| 27 |
description = "Gets current UTC time"
|
| 28 |
-
inputs =
|
| 29 |
-
outputs =
|
| 30 |
|
| 31 |
def __call__(self) -> str:
|
| 32 |
return datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
|
|
@@ -81,17 +81,30 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 81 |
|
| 82 |
try:
|
| 83 |
agent = HFLocalAgent()
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
|
| 86 |
results = []
|
| 87 |
answers = []
|
| 88 |
for q in questions:
|
| 89 |
try:
|
| 90 |
answer = agent(q["question"])
|
| 91 |
-
answers.append({
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
except Exception as e:
|
| 94 |
-
results.append({
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
submission = {
|
| 97 |
"username": profile.username,
|
|
@@ -101,14 +114,16 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 101 |
|
| 102 |
response = requests.post(f"{api_url}/submit", json=submission, timeout=60)
|
| 103 |
response.raise_for_status()
|
|
|
|
| 104 |
|
| 105 |
return (
|
| 106 |
-
f"Success! Score: {
|
|
|
|
| 107 |
pd.DataFrame(results)
|
| 108 |
)
|
| 109 |
|
| 110 |
except Exception as e:
|
| 111 |
-
return f"Evaluation failed: {e}",
|
| 112 |
|
| 113 |
# --- Gradio Interface ---
|
| 114 |
with gr.Blocks(title="Local HF Agent Evaluator") as app:
|
|
|
|
| 13 |
class CalculatorTool(Tool):
|
| 14 |
name = "calculator"
|
| 15 |
description = "Performs mathematical calculations"
|
| 16 |
+
inputs = {"expression": {"type": "text"}} # Changed to dict
|
| 17 |
+
outputs = {"result": {"type": "text"}} # Changed to dict
|
| 18 |
|
| 19 |
def __call__(self, expression: str) -> str:
|
| 20 |
try:
|
|
|
|
| 25 |
class TimeTool(Tool):
|
| 26 |
name = "current_time"
|
| 27 |
description = "Gets current UTC time"
|
| 28 |
+
inputs = {} # Empty dict for no inputs
|
| 29 |
+
outputs = {"time": {"type": "text"}} # Changed to dict
|
| 30 |
|
| 31 |
def __call__(self) -> str:
|
| 32 |
return datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
|
|
|
|
| 81 |
|
| 82 |
try:
|
| 83 |
agent = HFLocalAgent()
|
| 84 |
+
response = requests.get(f"{api_url}/questions", timeout=15)
|
| 85 |
+
response.raise_for_status()
|
| 86 |
+
questions = response.json()
|
| 87 |
|
| 88 |
results = []
|
| 89 |
answers = []
|
| 90 |
for q in questions:
|
| 91 |
try:
|
| 92 |
answer = agent(q["question"])
|
| 93 |
+
answers.append({
|
| 94 |
+
"task_id": q["task_id"],
|
| 95 |
+
"submitted_answer": answer
|
| 96 |
+
})
|
| 97 |
+
results.append({
|
| 98 |
+
"Task ID": q["task_id"],
|
| 99 |
+
"Question": q["question"],
|
| 100 |
+
"Answer": answer
|
| 101 |
+
})
|
| 102 |
except Exception as e:
|
| 103 |
+
results.append({
|
| 104 |
+
"Task ID": q["task_id"],
|
| 105 |
+
"Question": q["question"],
|
| 106 |
+
"Answer": f"Error: {e}"
|
| 107 |
+
})
|
| 108 |
|
| 109 |
submission = {
|
| 110 |
"username": profile.username,
|
|
|
|
| 114 |
|
| 115 |
response = requests.post(f"{api_url}/submit", json=submission, timeout=60)
|
| 116 |
response.raise_for_status()
|
| 117 |
+
result = response.json()
|
| 118 |
|
| 119 |
return (
|
| 120 |
+
f"Success! Score: {result.get('score', 'N/A')}%\n"
|
| 121 |
+
f"Correct: {result.get('correct_count', '?')}/{result.get('total_attempted', '?')}",
|
| 122 |
pd.DataFrame(results)
|
| 123 |
)
|
| 124 |
|
| 125 |
except Exception as e:
|
| 126 |
+
return f"Evaluation failed: {str(e)}", pd.DataFrame(results if 'results' in locals() else [])
|
| 127 |
|
| 128 |
# --- Gradio Interface ---
|
| 129 |
with gr.Blocks(title="Local HF Agent Evaluator") as app:
|