Update app.py
Browse files
app.py
CHANGED
|
@@ -3,21 +3,56 @@ import gradio as gr
|
|
| 3 |
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"
|
| 10 |
|
| 11 |
# --- Basic Agent Definition ---
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
-
print(f"Agent received question
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
import re
|
| 7 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
# --- Basic Agent Definition ---
|
| 13 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 14 |
+
# --- smolagents Agent Definition ---
|
| 15 |
+
GAIA_SYSTEM_PROMPT = """You are a helpful assistant solving a benchmark question.
|
| 16 |
+
Report your thoughts and end your response strictly with:
|
| 17 |
+
FINAL ANSWER: [YOUR FINAL ANSWER]
|
| 18 |
+
|
| 19 |
+
Rules for YOUR FINAL ANSWER:
|
| 20 |
+
1. It must be either a number, as few words as possible, or a comma-separated list.
|
| 21 |
+
2. If asking for a number, do not include units ($ or %) or commas in thousands (e.g. 1000 not 1,000).
|
| 22 |
+
3. If asking for a string, do not include articles (a, an, the) or abbreviations.
|
| 23 |
+
4. Keep it strictly to the exact value requested.
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
class BasicAgent:
|
| 27 |
def __init__(self):
|
| 28 |
+
print("Initializing smolagents CodeAgent...")
|
| 29 |
+
self.model = HfApiModel(
|
| 30 |
+
model_id="Qwen/Qwen2.5-72B-Instruct",
|
| 31 |
+
token=os.getenv("HF_TOKEN")
|
| 32 |
+
)
|
| 33 |
+
self.agent = CodeAgent(
|
| 34 |
+
tools=[DuckDuckGoSearchTool()],
|
| 35 |
+
model=self.model,
|
| 36 |
+
system_prompt=GAIA_SYSTEM_PROMPT,
|
| 37 |
+
max_steps=6,
|
| 38 |
+
additional_authorized_imports=["pandas", "math", "datetime", "re", "json"]
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
def __call__(self, question: str) -> str:
|
| 42 |
+
print(f"Agent received question: {question[:100]}...")
|
| 43 |
+
try:
|
| 44 |
+
raw_output = str(self.agent.run(question))
|
| 45 |
+
# Extract content from FINAL ANSWER: [...]
|
| 46 |
+
match = re.search(r"FINAL ANSWER:\s*(.*)", raw_output, re.IGNORECASE)
|
| 47 |
+
if match:
|
| 48 |
+
ans = match.group(1).strip()
|
| 49 |
+
if ans.startswith("[") and ans.endswith("]"):
|
| 50 |
+
ans = ans[1:-1].strip()
|
| 51 |
+
return ans
|
| 52 |
+
return raw_output.strip()
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"Agent error: {e}")
|
| 55 |
+
return "0"
|
| 56 |
|
| 57 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 58 |
"""
|