Update app.py
#501
by rafa2908 - opened
app.py
CHANGED
|
@@ -4,6 +4,14 @@ import requests
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
@@ -13,11 +21,39 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
print("BasicAgent initialized.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
@@ -146,11 +182,9 @@ with gr.Blocks() as demo:
|
|
| 146 |
gr.Markdown(
|
| 147 |
"""
|
| 148 |
**Instructions:**
|
| 149 |
-
|
| 150 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 151 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 152 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
| 153 |
-
|
| 154 |
---
|
| 155 |
**Disclaimers:**
|
| 156 |
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|
|
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
+
from smolagents import (
|
| 8 |
+
CodeAgent,
|
| 9 |
+
ToolCallingAgent,
|
| 10 |
+
DuckDuckGoSearchTool,
|
| 11 |
+
VisitWebpageTool,
|
| 12 |
+
InferenceClientModel,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
# (Keep Constants as is)
|
| 16 |
# --- Constants ---
|
| 17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 21 |
class BasicAgent:
|
| 22 |
def __init__(self):
|
| 23 |
print("BasicAgent initialized.")
|
| 24 |
+
|
| 25 |
+
model = InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 26 |
+
|
| 27 |
+
web_agent = ToolCallingAgent(
|
| 28 |
+
tools=[DuckDuckGoSearchTool(), VisitWebpageTool()],
|
| 29 |
+
model=model,
|
| 30 |
+
max_steps=6,
|
| 31 |
+
name="web_search_agent",
|
| 32 |
+
description=(
|
| 33 |
+
"Searches the web and reads webpages to answer factual questions "
|
| 34 |
+
"or gather up-to-date information. Give it a specific question or "
|
| 35 |
+
"research task in plain language, and it returns a concise, "
|
| 36 |
+
"sourced answer."
|
| 37 |
+
),
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
self.agent = CodeAgent(
|
| 41 |
+
tools=[],
|
| 42 |
+
model=model,
|
| 43 |
+
managed_agents=[web_agent],
|
| 44 |
+
additional_authorized_imports=["pandas", "numpy", "json", "re"],
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
def __call__(self, question: str) -> str:
|
| 48 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 49 |
+
try:
|
| 50 |
+
answer = self.agent.run(question)
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(f"Agent error: {e}")
|
| 53 |
+
answer = f"Error: {e}"
|
| 54 |
+
answer = str(answer)
|
| 55 |
+
print(f"Agent returning answer (first 50 chars): {answer[:50]}...")
|
| 56 |
+
return answer
|
| 57 |
|
| 58 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 59 |
"""
|
|
|
|
| 182 |
gr.Markdown(
|
| 183 |
"""
|
| 184 |
**Instructions:**
|
|
|
|
| 185 |
1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
|
| 186 |
2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
|
| 187 |
3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
|
|
|
|
| 188 |
---
|
| 189 |
**Disclaimers:**
|
| 190 |
Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
|