Shrutipanchal086's picture
Update app.py
a15f756 verified
Raw
History Blame
3.92 kB
# ============================================================
# INSTALL
# ============================================================
# Put this at the TOP of app.py if smolagents is not already
# available in your Space requirements.
import subprocess
import sys
subprocess.check_call([
sys.executable,
"-m",
"pip",
"install",
"-q",
"smolagents",
"ddgs"
])
# ============================================================
# IMPORTS
# ============================================================
import os
from smolagents import (
CodeAgent,
InferenceClientModel,
DuckDuckGoSearchTool,
PythonInterpreterTool
)
# ============================================================
# YOUR GAIA AGENT
# ============================================================
class BasicAgent:
def __init__(self):
print("Starting GAIA Agent...")
# Get Hugging Face token from Space Secret
hf_token = os.getenv("HF_TOKEN")
if not hf_token:
raise ValueError(
"HF_TOKEN is missing. "
"Go to Space Settings → Secrets and add HF_TOKEN."
)
# ----------------------------------------------------
# MODEL
# ----------------------------------------------------
model = InferenceClientModel(
model_id="Qwen/Qwen2.5-72B-Instruct",
token=hf_token
)
# ----------------------------------------------------
# WEB SEARCH
# ----------------------------------------------------
search = DuckDuckGoSearchTool(
max_results=5
)
# ----------------------------------------------------
# PYTHON / CALCULATOR
# ----------------------------------------------------
calculator = PythonInterpreterTool(
authorized_imports=[
"math",
"statistics",
"datetime",
"json",
"re"
]
)
# ----------------------------------------------------
# AGENT
# ----------------------------------------------------
self.agent = CodeAgent(
model=model,
tools=[
search,
calculator
],
max_steps=8
)
print("GAIA Agent ready!")
# ========================================================
# THIS IS CALLED BY THE COURSE EVALUATOR
# ========================================================
def __call__(self, question: str) -> str:
print("\n" + "=" * 60)
print("QUESTION:")
print(question)
print("=" * 60)
prompt = f"""
You are a highly capable general-purpose AI agent.
Solve the user's question accurately.
IMPORTANT:
1. Understand exactly what the question asks.
2. Use web search when the question requires:
- current information
- factual information
- information not available from your knowledge
3. Use the Python tool whenever calculations are required.
4. Carefully check arithmetic.
5. For questions involving dates, numbers, percentages,
units, names, or lists, verify your answer carefully.
6. If several steps are required, solve them systematically.
7. Do not invent information.
8. Use the available tools whenever they improve accuracy.
9. The final response must directly answer the question.
10. Follow the requested answer format exactly.
11. Do not include unnecessary discussion in the final answer.
USER QUESTION:
{question}
"""
try:
result = self.agent.run(prompt)
answer = str(result).strip()
print("\nANSWER:")
print(answer)
return answer
except Exception as e:
print("AGENT ERROR:")
print(e)
return f"Agent error: {e}"