Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -51,8 +51,6 @@ class LocalAgent:
|
|
| 51 |
def __init__(self):
|
| 52 |
print("Initializing agent with smolagents...")
|
| 53 |
self.tools = [CalculatorTool(), TimeTool()]
|
| 54 |
-
|
| 55 |
-
# Using a free Hugging Face model
|
| 56 |
self.agent = ToolCallingAgent(
|
| 57 |
tools=self.tools,
|
| 58 |
model=InferenceClientModel(
|
|
@@ -60,20 +58,20 @@ class LocalAgent:
|
|
| 60 |
api_base="https://api-inference.huggingface.co/models"
|
| 61 |
)
|
| 62 |
)
|
| 63 |
-
|
| 64 |
def __call__(self, question: str) -> str:
|
| 65 |
print(f"Processing: {question[:100]}...")
|
| 66 |
question_lower = question.lower()
|
| 67 |
-
|
| 68 |
# Direct tool usage for simple queries
|
| 69 |
if any(word in question_lower for word in ["calculate", "what is", "how much is", "+", "-", "*", "/"]):
|
| 70 |
result = CalculatorTool().use(question.replace("?", ""))
|
| 71 |
return result["result"]
|
| 72 |
-
|
| 73 |
if any(word in question_lower for word in ["time", "current time"]):
|
| 74 |
result = TimeTool().use()
|
| 75 |
return result["time"]
|
| 76 |
-
|
| 77 |
# Use full agent for complex questions
|
| 78 |
try:
|
| 79 |
response = self.agent.run(question)
|
|
@@ -86,53 +84,53 @@ class LocalAgent:
|
|
| 86 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 87 |
if not profile:
|
| 88 |
return "Please login first", None
|
| 89 |
-
|
| 90 |
space_id = os.getenv("SPACE_ID", "local-test")
|
| 91 |
api_url = os.getenv("API_URL", DEFAULT_API_URL)
|
| 92 |
-
|
| 93 |
try:
|
| 94 |
agent = LocalAgent()
|
| 95 |
response = requests.get(f"{api_url}/questions", timeout=15)
|
| 96 |
response.raise_for_status()
|
| 97 |
questions = response.json()
|
| 98 |
-
|
| 99 |
results = []
|
| 100 |
answers = []
|
| 101 |
for q in questions:
|
| 102 |
try:
|
| 103 |
answer = agent(q["question"])
|
| 104 |
answers.append({
|
| 105 |
-
"task_id": q["task_id"],
|
| 106 |
"submitted_answer": answer
|
| 107 |
})
|
| 108 |
results.append({
|
| 109 |
-
"Task ID": q["task_id"],
|
| 110 |
-
"Question": q["question"],
|
| 111 |
"Answer": answer
|
| 112 |
})
|
| 113 |
except Exception as e:
|
| 114 |
results.append({
|
| 115 |
-
"Task ID": q["task_id"],
|
| 116 |
-
"Question": q["question"],
|
| 117 |
"Answer": f"Error: {e}"
|
| 118 |
})
|
| 119 |
-
|
| 120 |
submission = {
|
| 121 |
"username": profile.username,
|
| 122 |
"agent_code": f"https://huggingface.co/spaces/{space_id}",
|
| 123 |
"answers": answers
|
| 124 |
}
|
| 125 |
-
|
| 126 |
response = requests.post(f"{api_url}/submit", json=submission, timeout=60)
|
| 127 |
response.raise_for_status()
|
| 128 |
result = response.json()
|
| 129 |
-
|
| 130 |
return (
|
| 131 |
f"Success! Score: {result.get('score', 'N/A')}%\n"
|
| 132 |
f"Correct: {result.get('correct_count', '?')}/{result.get('total_attempted', '?')}",
|
| 133 |
pd.DataFrame(results)
|
| 134 |
)
|
| 135 |
-
|
| 136 |
except Exception as e:
|
| 137 |
return f"Evaluation failed: {str(e)}", pd.DataFrame(results if 'results' in locals() else [])
|
| 138 |
|
|
@@ -142,16 +140,17 @@ with gr.Blocks(title="Agent Evaluation Runner") as app:
|
|
| 142 |
## Advanced Agent Evaluation
|
| 143 |
Uses smolagents with proper tool schemas
|
| 144 |
""")
|
| 145 |
-
|
| 146 |
gr.LoginButton()
|
| 147 |
run_btn = gr.Button("Run Evaluation")
|
| 148 |
output = gr.Textbox(label="Results")
|
| 149 |
results_table = gr.DataFrame(label="Question Log")
|
| 150 |
-
|
| 151 |
run_btn.click(
|
| 152 |
fn=run_and_submit_all,
|
|
|
|
| 153 |
outputs=[output, results_table]
|
| 154 |
)
|
| 155 |
|
| 156 |
if __name__ == "__main__":
|
| 157 |
-
app.launch()
|
|
|
|
| 51 |
def __init__(self):
|
| 52 |
print("Initializing agent with smolagents...")
|
| 53 |
self.tools = [CalculatorTool(), TimeTool()]
|
|
|
|
|
|
|
| 54 |
self.agent = ToolCallingAgent(
|
| 55 |
tools=self.tools,
|
| 56 |
model=InferenceClientModel(
|
|
|
|
| 58 |
api_base="https://api-inference.huggingface.co/models"
|
| 59 |
)
|
| 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 |
response = self.agent.run(question)
|
|
|
|
| 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 |
response = requests.get(f"{api_url}/questions", timeout=15)
|
| 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"])
|
| 102 |
answers.append({
|
| 103 |
+
"task_id": q["task_id"],
|
| 104 |
"submitted_answer": answer
|
| 105 |
})
|
| 106 |
results.append({
|
| 107 |
+
"Task ID": q["task_id"],
|
| 108 |
+
"Question": q["question"],
|
| 109 |
"Answer": answer
|
| 110 |
})
|
| 111 |
except Exception as e:
|
| 112 |
results.append({
|
| 113 |
+
"Task ID": q["task_id"],
|
| 114 |
+
"Question": q["question"],
|
| 115 |
"Answer": f"Error: {e}"
|
| 116 |
})
|
| 117 |
+
|
| 118 |
submission = {
|
| 119 |
"username": profile.username,
|
| 120 |
"agent_code": f"https://huggingface.co/spaces/{space_id}",
|
| 121 |
"answers": answers
|
| 122 |
}
|
| 123 |
+
|
| 124 |
response = requests.post(f"{api_url}/submit", json=submission, timeout=60)
|
| 125 |
response.raise_for_status()
|
| 126 |
result = response.json()
|
| 127 |
+
|
| 128 |
return (
|
| 129 |
f"Success! Score: {result.get('score', 'N/A')}%\n"
|
| 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(results if 'results' in locals() else [])
|
| 136 |
|
|
|
|
| 140 |
## Advanced Agent Evaluation
|
| 141 |
Uses smolagents with proper tool schemas
|
| 142 |
""")
|
| 143 |
+
|
| 144 |
gr.LoginButton()
|
| 145 |
run_btn = gr.Button("Run Evaluation")
|
| 146 |
output = gr.Textbox(label="Results")
|
| 147 |
results_table = gr.DataFrame(label="Question Log")
|
| 148 |
+
|
| 149 |
run_btn.click(
|
| 150 |
fn=run_and_submit_all,
|
| 151 |
+
inputs=[], # <-- This line is the required fix
|
| 152 |
outputs=[output, results_table]
|
| 153 |
)
|
| 154 |
|
| 155 |
if __name__ == "__main__":
|
| 156 |
+
app.launch()
|