Update app.py
#530
by Shrutipanchal086 - opened
app.py
CHANGED
|
@@ -11,13 +11,76 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
class BasicAgent:
|
|
|
|
| 14 |
def __init__(self):
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
|
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
class BasicAgent:
|
| 14 |
+
|
| 15 |
def __init__(self):
|
| 16 |
+
|
| 17 |
+
import os
|
| 18 |
+
|
| 19 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 20 |
+
|
| 21 |
+
if not hf_token:
|
| 22 |
+
raise ValueError(
|
| 23 |
+
"HF_TOKEN is missing. Add it in Space Settings → Secrets."
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
from smolagents import (
|
| 27 |
+
CodeAgent,
|
| 28 |
+
InferenceClientModel,
|
| 29 |
+
DuckDuckGoSearchTool,
|
| 30 |
+
PythonInterpreterTool
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
model = InferenceClientModel(
|
| 34 |
+
model_id="Qwen/Qwen2.5-72B-Instruct",
|
| 35 |
+
token=hf_token
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
search = DuckDuckGoSearchTool(max_results=5)
|
| 39 |
+
|
| 40 |
+
calculator = PythonInterpreterTool(
|
| 41 |
+
authorized_imports=[
|
| 42 |
+
"math",
|
| 43 |
+
"statistics",
|
| 44 |
+
"datetime",
|
| 45 |
+
"json",
|
| 46 |
+
"re"
|
| 47 |
+
]
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
self.agent = CodeAgent(
|
| 51 |
+
model=model,
|
| 52 |
+
tools=[search, calculator],
|
| 53 |
+
max_steps=8
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
print("GAIA Agent initialized successfully!")
|
| 57 |
+
|
| 58 |
def __call__(self, question: str) -> str:
|
| 59 |
+
|
| 60 |
+
prompt = f"""
|
| 61 |
+
You are a powerful general-purpose AI agent solving GAIA benchmark questions.
|
| 62 |
+
|
| 63 |
+
Solve this question accurately:
|
| 64 |
+
|
| 65 |
+
{question}
|
| 66 |
+
|
| 67 |
+
Rules:
|
| 68 |
+
- Use web search when current or specific information is required.
|
| 69 |
+
- Use Python for calculations.
|
| 70 |
+
- Carefully verify numbers, dates and facts.
|
| 71 |
+
- If a URL is provided, use the available tools when appropriate.
|
| 72 |
+
- Give the final answer directly.
|
| 73 |
+
- Do not say that you cannot answer unless absolutely necessary.
|
| 74 |
+
- Do not return a generic response.
|
| 75 |
+
"""
|
| 76 |
+
|
| 77 |
+
try:
|
| 78 |
+
answer = self.agent.run(prompt)
|
| 79 |
+
return str(answer).strip()
|
| 80 |
+
|
| 81 |
+
except Exception as e:
|
| 82 |
+
print("Agent error:", e)
|
| 83 |
+
return f"Unable to solve: {e}"
|
| 84 |
|
| 85 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 86 |
"""
|