Spaces:
Sleeping
Sleeping
update
Browse files- app.py +83 -6
- requirements.txt +5 -2
app.py
CHANGED
|
@@ -3,23 +3,100 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
from smolagents import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
class BasicAgent:
|
| 13 |
def __init__(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
self.agent = CodeAgent(
|
| 15 |
-
tools=[
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def __call__(self, question: str) -> str:
|
| 19 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 25 |
"""
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
from smolagents import (
|
| 7 |
+
CodeAgent,
|
| 8 |
+
ToolCallingAgent,
|
| 9 |
+
DuckDuckGoSearchTool,
|
| 10 |
+
VisitWebpageTool,
|
| 11 |
+
WikipediaSearchTool,
|
| 12 |
+
GradioUI,
|
| 13 |
+
InferenceClientModel,
|
| 14 |
+
LiteLLMModel,
|
| 15 |
+
)
|
| 16 |
|
| 17 |
# (Keep Constants as is)
|
| 18 |
# --- Constants ---
|
| 19 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 20 |
|
| 21 |
+
# GAIA benchmark prompt — enforces exact-match answer formatting
|
| 22 |
+
GAIA_SYSTEM_PROMPT = (
|
| 23 |
+
"You are a general AI assistant. I will ask you a question. "
|
| 24 |
+
"Report your thoughts, and finish your answer with the following template: "
|
| 25 |
+
"FINAL ANSWER: [YOUR FINAL ANSWER]. "
|
| 26 |
+
"YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. "
|
| 27 |
+
"If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. "
|
| 28 |
+
"If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. "
|
| 29 |
+
"If you are asked for a comma separated list, apply the above rules depending on whether the element is a number or a string."
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
class BasicAgent:
|
| 34 |
def __init__(self):
|
| 35 |
+
print("Initializing BasicAgent...")
|
| 36 |
+
model = self._get_model()
|
| 37 |
+
|
| 38 |
+
web_agent = ToolCallingAgent(
|
| 39 |
+
tools=[DuckDuckGoSearchTool(), VisitWebpageTool()],
|
| 40 |
+
model=model,
|
| 41 |
+
name="web_search_agent",
|
| 42 |
+
description=(
|
| 43 |
+
"A web search agent that can search the internet and visit web pages. "
|
| 44 |
+
"Give it a clear, specific research question and it will return the relevant information found."
|
| 45 |
+
),
|
| 46 |
+
max_steps=10,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
self.agent = CodeAgent(
|
| 50 |
+
tools=[
|
| 51 |
+
DuckDuckGoSearchTool(),
|
| 52 |
+
WikipediaSearchTool(),
|
| 53 |
+
VisitWebpageTool(),
|
| 54 |
+
],
|
| 55 |
+
model=model,
|
| 56 |
+
managed_agents=[web_agent],
|
| 57 |
+
additional_authorized_imports=[
|
| 58 |
+
"json", "re", "math", "statistics", "datetime",
|
| 59 |
+
"csv", "collections", "itertools", "functools",
|
| 60 |
+
"unicodedata", "string", "fractions", "decimal",
|
| 61 |
+
],
|
| 62 |
+
planning_interval=2,
|
| 63 |
+
max_steps=20,
|
| 64 |
)
|
| 65 |
+
print("BasicAgent initialized successfully.")
|
| 66 |
+
|
| 67 |
+
@staticmethod
|
| 68 |
+
def _get_model():
|
| 69 |
+
|
| 70 |
+
if os.getenv("OPENAI_API_KEY"):
|
| 71 |
+
print("Using OpenAI GPT-4o")
|
| 72 |
+
return LiteLLMModel(model_id="gpt-4o", temperature=0.1)
|
| 73 |
+
if os.getenv("ANTHROPIC_API_KEY"):
|
| 74 |
+
print("Using Anthropic Claude Sonnet")
|
| 75 |
+
return LiteLLMModel(
|
| 76 |
+
model_id="anthropic/claude-sonnet-4-20250514",
|
| 77 |
+
temperature=0.1,
|
| 78 |
+
)
|
| 79 |
+
print("Using Qwen/Qwen2.5-72B-Instruct via HF Inference")
|
| 80 |
+
return InferenceClientModel(model_id="Qwen/Qwen2.5-72B-Instruct")
|
| 81 |
+
|
| 82 |
def __call__(self, question: str) -> str:
|
| 83 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 84 |
+
prompt = (
|
| 85 |
+
f"{GAIA_SYSTEM_PROMPT}\n\n"
|
| 86 |
+
f"Now here is the question you need to answer.\n\n"
|
| 87 |
+
f"Question: {question}"
|
| 88 |
+
)
|
| 89 |
+
response = self.agent.run(prompt)
|
| 90 |
+
answer = self._extract_final_answer(str(response))
|
| 91 |
+
print(f"Agent returning answer: {answer}")
|
| 92 |
+
return answer
|
| 93 |
+
|
| 94 |
+
@staticmethod
|
| 95 |
+
def _extract_final_answer(response: str) -> str:
|
| 96 |
+
"""Extract the FINAL ANSWER from the agent response for exact-match scoring."""
|
| 97 |
+
if "FINAL ANSWER:" in response:
|
| 98 |
+
return response.split("FINAL ANSWER:")[-1].strip()
|
| 99 |
+
return response.strip()
|
| 100 |
|
| 101 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 102 |
"""
|
requirements.txt
CHANGED
|
@@ -1,4 +1,7 @@
|
|
| 1 |
gradio
|
| 2 |
requests
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
requests
|
| 3 |
+
pandas
|
| 4 |
+
smolagents[toolkit]
|
| 5 |
+
litellm
|
| 6 |
+
wikipedia-api
|
| 7 |
+
markdownify
|