Update app.py
#393
by GionaZardini - opened
app.py
CHANGED
|
@@ -13,11 +13,93 @@ 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 |
"""
|
|
|
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
print("BasicAgent initialized.")
|
| 16 |
+
|
| 17 |
+
if os.getenv("OPENAI_API_KEY") and OpenAIServerModel is not None:
|
| 18 |
+
print("Using OpenAI model.")
|
| 19 |
+
self.model = OpenAIServerModel(
|
| 20 |
+
model_id=os.getenv("OPENAI_MODEL", "gpt-4o-mini"),
|
| 21 |
+
api_base="https://api.openai.com/v1",
|
| 22 |
+
api_key=os.environ["OPENAI_API_KEY"],
|
| 23 |
+
temperature=0,
|
| 24 |
+
max_tokens=4096,
|
| 25 |
+
)
|
| 26 |
+
else:
|
| 27 |
+
print("Using Hugging Face model.")
|
| 28 |
+
self.model = InferenceClientModel(
|
| 29 |
+
model_id=os.getenv("HF_MODEL_ID", "Qwen/Qwen2.5-72B-Instruct"),
|
| 30 |
+
token=os.getenv("HF_TOKEN"),
|
| 31 |
+
temperature=0,
|
| 32 |
+
max_tokens=4096,
|
| 33 |
+
timeout=120,
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
self.agent = CodeAgent(
|
| 37 |
+
tools=[
|
| 38 |
+
DuckDuckGoSearchTool(),
|
| 39 |
+
VisitWebpageTool(),
|
| 40 |
+
],
|
| 41 |
+
model=self.model,
|
| 42 |
+
add_base_tools=True,
|
| 43 |
+
additional_authorized_imports=[
|
| 44 |
+
"collections",
|
| 45 |
+
"csv",
|
| 46 |
+
"datetime",
|
| 47 |
+
"itertools",
|
| 48 |
+
"json",
|
| 49 |
+
"math",
|
| 50 |
+
"os",
|
| 51 |
+
"pandas",
|
| 52 |
+
"pathlib",
|
| 53 |
+
"re",
|
| 54 |
+
"statistics",
|
| 55 |
+
"string",
|
| 56 |
+
"time",
|
| 57 |
+
],
|
| 58 |
+
max_steps=10,
|
| 59 |
+
verbosity_level=1,
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
def __call__(self, question: str) -> str:
|
| 63 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 64 |
+
|
| 65 |
+
prompt = f"""
|
| 66 |
+
You are solving a GAIA-style benchmark question.
|
| 67 |
+
|
| 68 |
+
Use web search when needed.
|
| 69 |
+
Use Python when calculations, files, dates, tables, counting, or exact transformations are needed.
|
| 70 |
+
|
| 71 |
+
Return only the final answer.
|
| 72 |
+
No explanation.
|
| 73 |
+
No markdown.
|
| 74 |
+
No full sentence unless the question explicitly asks for one.
|
| 75 |
+
Preserve exact requested formatting.
|
| 76 |
+
|
| 77 |
+
Question:
|
| 78 |
+
{question}
|
| 79 |
+
"""
|
| 80 |
+
|
| 81 |
+
try:
|
| 82 |
+
answer = self.agent.run(prompt)
|
| 83 |
+
answer = str(answer).strip()
|
| 84 |
+
|
| 85 |
+
for prefix in [
|
| 86 |
+
"Final answer:",
|
| 87 |
+
"Final Answer:",
|
| 88 |
+
"FINAL ANSWER:",
|
| 89 |
+
"Answer:",
|
| 90 |
+
"The answer is",
|
| 91 |
+
"The final answer is",
|
| 92 |
+
]:
|
| 93 |
+
if answer.startswith(prefix):
|
| 94 |
+
answer = answer[len(prefix):].strip()
|
| 95 |
+
|
| 96 |
+
answer = answer.strip().strip('"').strip("'").strip()
|
| 97 |
+
print(f"Agent returning answer: {answer}")
|
| 98 |
+
return answer
|
| 99 |
+
|
| 100 |
+
except Exception as e:
|
| 101 |
+
print(f"Agent error: {e}")
|
| 102 |
+
return f"AGENT ERROR: {e}"
|
| 103 |
|
| 104 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 105 |
"""
|