Spaces:
Running
Running
File size: 19,157 Bytes
8815598 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 | """
Agent GAIA – Unité 4 du Hugging Face AI Agents Course
Sarra Laouini
Architecture LangGraph :
START → load_and_extract → assistant ⇄ tools → format_answer → END
- load_and_extract : télécharge le fichier joint et le transforme en texte
(Excel → pandas, MP3 → Whisper, image → VLM, code → texte)
- assistant : LLM Groq (gpt-oss) avec outils, budget de 6 tours d'outils
- tools : web_search (Tavily), wiki_rag (RAG LlamaIndex + bge-small,
découpage par sections), python, youtube_transcript
- format_answer : extrait la réponse après "FINAL ANSWER:" pour l'exact match
Secrets nécessaires : GROQ_API_KEY, TAVILY_API_KEY, HF_TOKEN
Option : MODEL_ID (défaut openai/gpt-oss-120b)
"""
import os
import re
import sys
import json
import uuid
import base64
import mimetypes
import subprocess
from typing import TypedDict, Annotated
import requests
import pandas as pd
from groq import Groq
from tavily import TavilyClient
from markdownify import markdownify
from langchain_core.tools import tool
from langchain_core.messages import (AnyMessage, AIMessage, HumanMessage,
SystemMessage, ToolMessage)
from langchain_groq import ChatGroq
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langgraph.prebuilt import ToolNode, tools_condition
from llama_index.core import Document, VectorStoreIndex, Settings
from llama_index.core.node_parser import SentenceSplitter
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
API_URL = "https://agents-course-unit4-scoring.hf.space"
FILES_DIR = "/tmp/gaia_files"
MODEL_ID = os.getenv("MODEL_ID", "openai/gpt-oss-120b")
VISION_MODEL = "qwen/qwen3.8-27b"
HEADERS = {"User-Agent": "gaia-course-agent/1.0"}
client = Groq() # lit GROQ_API_KEY
# =============================================================================
# 1. State
# =============================================================================
class AgentState(TypedDict):
task_id: str
question: str
file_name: str
file_path: str
file_content: str
messages: Annotated[list[AnyMessage], add_messages]
answer: str
# =============================================================================
# 2. Fichiers joints (nœud load_and_extract)
# =============================================================================
_GAIA_FILES = None
def gaia_files() -> dict:
"""task_id → chemin du fichier dans le dataset GAIA (si le serveur du cours renvoie 404)."""
global _GAIA_FILES
if _GAIA_FILES is None:
from datasets import load_dataset
from huggingface_hub import snapshot_download
data_dir = snapshot_download(repo_id="gaia-benchmark/GAIA", repo_type="dataset",
token=os.getenv("HF_TOKEN"))
val = load_dataset(data_dir, "2023_level1", split="validation")
# On ne lit QUE le chemin du fichier, jamais la réponse.
_GAIA_FILES = {ex["task_id"]: os.path.join(data_dir, ex["file_path"])
for ex in val if ex["file_path"]}
return _GAIA_FILES
def download_file(task_id, file_name):
os.makedirs(FILES_DIR, exist_ok=True)
path = os.path.join(FILES_DIR, file_name)
if os.path.exists(path):
return path
r = requests.get(f"{API_URL}/files/{task_id}", timeout=60)
if r.status_code == 200:
with open(path, "wb") as f:
f.write(r.content)
return path
files = gaia_files()
if task_id in files:
print(f" ↳ serveur {r.status_code}, fichier pris dans le dataset GAIA")
return files[task_id]
raise FileNotFoundError(f"{file_name} introuvable (serveur {r.status_code})")
def transcribe(path):
with open(path, "rb") as f:
out = client.audio.transcriptions.create(file=(os.path.basename(path), f.read()),
model="whisper-large-v3")
return out.text
def describe_image(path, question):
mime = mimetypes.guess_type(path)[0] or "image/png"
with open(path, "rb") as f:
b64 = base64.b64encode(f.read()).decode()
params = dict(
model=VISION_MODEL, temperature=0, max_tokens=800,
messages=[{"role": "user", "content": [
{"type": "text",
"text": f"Describe precisely and concisely everything in this image that helps answer: {question}"},
{"type": "image_url", "image_url": {"url": f"data:{mime};base64,{b64}"}},
]}],
)
try:
out = client.chat.completions.create(**params, reasoning_effort="none")
except Exception:
out = client.chat.completions.create(**params)
text = out.choices[0].message.content or ""
return re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL).strip()
def extract(path, question):
ext = os.path.splitext(path)[1].lower()
if ext in (".py", ".txt", ".md", ".json", ".csv"):
with open(path, encoding="utf-8", errors="ignore") as f:
return f.read()
if ext in (".xlsx", ".xls"):
sheets = pd.read_excel(path, sheet_name=None)
return "\n\n".join(f"Sheet '{n}':\n{df.to_csv(index=False)}" for n, df in sheets.items())
if ext == ".pdf":
from pypdf import PdfReader
return "\n".join(p.extract_text() or "" for p in PdfReader(path).pages)
if ext in (".mp3", ".wav", ".m4a"):
return transcribe(path)
if ext in (".png", ".jpg", ".jpeg", ".webp"):
return describe_image(path, question)
return f"(type de fichier non géré : {ext})"
def load_and_extract(state: AgentState):
if not state.get("file_name"):
return {"file_path": "", "file_content": ""}
try:
path = download_file(state["task_id"], state["file_name"])
content = extract(path, state["question"])
except Exception as e:
path, content = "", f"(fichier indisponible : {e})"
return {"file_path": path, "file_content": content[:20000]}
# =============================================================================
# 3. Outils
# =============================================================================
tavily = TavilyClient(api_key=os.getenv("TAVILY_API_KEY"))
@tool
def web_search(query: str) -> str:
"""Searches the web and returns the top results (title, url, content).
Use it for recent facts or anything not on Wikipedia.
Args:
query: a short, precise search query (e.g. 'Malko Competition winners list').
"""
try:
res = tavily.search(query, max_results=5)
except Exception as e:
return f"web_search error: {e}"
return "\n\n".join(f"{r['title']}\n{r['url']}\n{r['content']}" for r in res["results"]) or "No results."
# --- RAG LlamaIndex -----------------------------------------------------------
Settings.embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
Settings.node_parser = SentenceSplitter(chunk_size=384, chunk_overlap=48)
Settings.llm = None
_INDEX_CACHE = {}
def fetch_page(source):
if source.startswith("http"):
html = requests.get(source, headers=HEADERS, timeout=30).text
return source, markdownify(html, heading_style="ATX")
api = "https://en.wikipedia.org/w/api.php"
p = requests.get(api, headers=HEADERS, timeout=30, params={
"action": "parse", "page": source, "prop": "text", "format": "json", "redirects": 1}).json()
if "error" in p:
hits = requests.get(api, headers=HEADERS, timeout=30, params={
"action": "query", "list": "search", "srsearch": source, "format": "json",
"srlimit": 1}).json()["query"]["search"]
if not hits:
raise ValueError(f"Aucune page Wikipedia pour '{source}'")
return fetch_page(hits[0]["title"])
return p["parse"]["title"], markdownify(p["parse"]["text"]["*"], heading_style="ATX")
def clean_md(t):
t = re.sub(r"\[([^\]]*)\]\([^)]*\)", r"\1", t)
t = re.sub(r"\[\[?edit\]?\]", "", t)
t = re.sub(r"[ \t]+", " ", t)
return re.sub(r"\n\s*\n+", "\n\n", t)
def to_section_docs(page_title, md):
"""Un Document par section, avec son chemin de titres en métadonnée."""
docs, heads, buf = [], {}, []
def flush():
text = "\n".join(buf).strip()
if text:
section = " > ".join(heads[k] for k in sorted(heads)) or "Introduction"
docs.append(Document(text=text, metadata={"page": page_title, "section": section}))
for line in md.split("\n"):
m = re.match(r"^(#{1,6})\s+(.*)", line)
if m:
flush()
buf = []
level = len(m.group(1))
heads = {k: v for k, v in heads.items() if k < level}
heads[level] = m.group(2).strip()
else:
buf.append(line)
flush()
return docs
@tool
def wiki_rag(source: str, query: str) -> str:
"""Reads a long web page or English Wikipedia page and returns the most relevant passages,
each labelled with its section title (e.g. 'Discography > Studio albums').
Use a precise query (include years, names). If the passages don't answer, change the query words.
Args:
source: a Wikipedia page title (e.g. 'Mercedes Sosa') or a full URL.
query: what you are looking for (e.g. 'studio albums discography 2000 2009').
"""
try:
if source not in _INDEX_CACHE:
title, md = fetch_page(source)
docs = to_section_docs(title, clean_md(md))
_INDEX_CACHE[source] = (title, VectorStoreIndex.from_documents(docs))
title, index = _INDEX_CACHE[source]
engine = index.as_query_engine(similarity_top_k=3, response_mode="no_text")
nodes = engine.query(query).source_nodes
return f"Page: {title}\n\n" + "\n\n".join(
f"[Section: {n.node.metadata['section']}]\n{n.node.get_content()}" for n in nodes)
except Exception as e:
return f"wiki_rag error: {e}"
@tool("python")
def python_exec(code: str) -> str:
"""Runs Python code and returns what it prints. Use it for ANY calculation, counting,
date arithmetic, text manipulation (e.g. reversing a string) or reading files with pandas.
Always print() the result you need.
Args:
code: complete Python code to run, with print() for the results.
"""
try:
r = subprocess.run([sys.executable, "-c", code], capture_output=True, text=True, timeout=60)
except subprocess.TimeoutExpired:
return "Error: code took more than 60 seconds."
out = (r.stdout + ("\nERROR:\n" + r.stderr if r.returncode else "")).strip()
return out[:5000] or "(no output — did you forget print()?)"
@tool
def youtube_transcript(url: str) -> str:
"""Returns the transcript (subtitles) of a YouTube video, if available.
Args:
url: the YouTube video url.
"""
m = re.search(r"(?:v=|youtu\.be/|shorts/)([\w-]{11})", url)
if not m:
return "Invalid YouTube url."
try:
from youtube_transcript_api import YouTubeTranscriptApi
parts = YouTubeTranscriptApi().fetch(m.group(1))
return " ".join(p.text for p in parts)[:15000]
except Exception as e:
return f"No transcript available ({type(e).__name__}). Try web_search about this video instead."
tools = [web_search, wiki_rag, python_exec, youtube_transcript]
TOOL_NAMES = [t.name for t in tools]
# =============================================================================
# 4. Nœud assistant
# =============================================================================
GAIA_PROMPT = """You are a general AI assistant. I will ask you a question.
Report your thoughts, and finish your answer with the following template:
FINAL ANSWER: [YOUR FINAL ANSWER]
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
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.
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.
If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
Always follow any format given in the question (order, decimals, units, first name only...)."""
SYSTEM_PROMPT = GAIA_PROMPT + """
Tools you can use:
- wiki_rag(source, query): read a Wikipedia page or URL and get the relevant passages. Prefer it for facts on Wikipedia.
- web_search(query): search the web, then use wiki_rag on a good URL to read details.
- python(code): for ANY calculation, counting, sorting or text manipulation. Read attached files with pandas using the local path.
- youtube_transcript(url): subtitles of a YouTube video.
Check facts with tools instead of guessing. If the question looks strange (e.g. reversed text), decode it first with python."""
llm = ChatGroq(model=MODEL_ID, temperature=0, max_tokens=2000, max_retries=5)
llm_with_tools = llm.bind_tools(tools)
llm_final = llm.bind_tools(tools, tool_choice="none")
MAX_TOOL_ROUNDS = 6
def clean_text(t):
t = re.sub(r"\[([^\]]*)\]\([^)]*\)", r"\1", t)
t = re.sub(r"\[\[?edit\]?\]", "", t)
t = re.sub(r"[ \t]+", " ", t)
t = re.sub(r"\n\s*\n+", "\n", t)
return t.strip()
def compact(messages, keep_full=3, full_len=4000, old_len=800):
"""Copie allégée de l'historique pour rester sous la limite de tokens de Groq."""
tool_idx = [i for i, m in enumerate(messages) if isinstance(m, ToolMessage)]
recent = set(tool_idx[-keep_full:])
out = []
for i, m in enumerate(messages):
if isinstance(m, ToolMessage):
limit = full_len if i in recent else old_len
m = ToolMessage(content=clean_text(str(m.content))[:limit],
tool_call_id=m.tool_call_id, name=m.name)
out.append(m)
return out
def build_user_message(state: AgentState):
text = f"Question:\n{state['question']}"
if state.get("file_content"):
text += (f"\n\nAttached file '{state['file_name']}' (local path: {state['file_path']}). "
f"Content:\n{state['file_content'][:6000]}")
return HumanMessage(content=text)
def repair_tool_call(failed: str):
"""Transforme un appel d'outil mal formé (code brut) en vrai appel d'outil."""
m = re.search(r'"name"\s*:\s*"([\w.]+)"\s*,\s*"arguments"\s*:\s*(.*)', failed, re.DOTALL)
if not m:
return None
name, raw = m.group(1), m.group(2).strip()
if raw.endswith("}"):
raw = raw[:-1].rstrip()
try:
args = json.loads(raw)
except Exception:
if name != "python":
return None
args = {"code": raw}
if name == "python" and "code" not in args:
args = {"code": next(iter(args.values()), "")} if args else {"code": raw}
return AIMessage(content="", tool_calls=[{"name": name, "args": args,
"id": "fix_" + uuid.uuid4().hex[:8]}])
def call_llm(model, messages, tries=3):
for _ in range(tries):
try:
return model.invoke(messages)
except Exception as e:
msg = str(e)
body = getattr(e, "body", None) or {}
failed = (body.get("error") or {}).get("failed_generation", "") if isinstance(body, dict) else ""
if failed:
fixed = repair_tool_call(failed)
if fixed and fixed.tool_calls[0]["name"] in TOOL_NAMES:
return fixed
if "400" in msg and "tool" in msg.lower():
messages = messages + [HumanMessage(content=(
f"Error: invalid tool call. You can ONLY use these tools: {', '.join(TOOL_NAMES)}, "
f"with JSON arguments, e.g. python(code=\"print(1)\"). Try again."))]
continue
raise
return model.invoke(messages)
def get_reasoning(msg):
return str(msg.additional_kwargs.get("reasoning_content", "") or "")
def assistant(state: AgentState):
new = [] if state["messages"] else [build_user_message(state)]
history = compact(state["messages"] + new)
rounds = sum(1 for m in history if getattr(m, "tool_calls", None))
model = llm_final if rounds >= MAX_TOOL_ROUNDS else llm_with_tools
if rounds >= MAX_TOOL_ROUNDS:
history.append(HumanMessage(content="Stop using tools. Using only the information above, "
"give your best answer now, ending with 'FINAL ANSWER: ...'."))
response = call_llm(model, [SystemMessage(content=SYSTEM_PROMPT)] + history)
if not response.tool_calls and not str(response.content).strip():
reasoning = get_reasoning(response)
if "FINAL ANSWER" in reasoning.upper():
response = AIMessage(content=reasoning)
else:
retry = history + [HumanMessage(content="Your last reply was empty. "
"Give your answer now, ending with 'FINAL ANSWER: ...'.")]
response = call_llm(llm_final, [SystemMessage(content=SYSTEM_PROMPT)] + retry)
if not str(response.content).strip():
response = AIMessage(content=get_reasoning(response))
return {"messages": new + [response]}
# =============================================================================
# 5. Nœud format_answer
# =============================================================================
def clean(raw: str) -> str:
m = re.search(r"FINAL ANSWER\s*:\s*(.+)", raw, re.IGNORECASE | re.DOTALL)
ans = m.group(1) if m else raw
ans = ans.strip().split("\n")[0]
ans = ans.replace("**", "").strip().strip("\"'`[]").strip()
if ans.endswith("."):
ans = ans[:-1]
ans = re.sub(r"\s*,\s*", ", ", ans)
return ans
def format_answer(state: AgentState):
return {"answer": clean(str(state["messages"][-1].content))}
# =============================================================================
# 6. Graphe
# =============================================================================
builder = StateGraph(AgentState)
builder.add_node("load_and_extract", load_and_extract)
builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode(tools))
builder.add_node("format_answer", format_answer)
builder.add_edge(START, "load_and_extract")
builder.add_edge("load_and_extract", "assistant")
builder.add_conditional_edges("assistant", tools_condition, {"tools": "tools", END: "format_answer"})
builder.add_edge("tools", "assistant")
builder.add_edge("format_answer", END)
graph = builder.compile()
class GaiaAgent:
"""Interface simple utilisée par app.py."""
def __call__(self, question: str, task_id: str = "", file_name: str = "") -> str:
state = graph.invoke(
{"task_id": task_id, "question": question, "file_name": file_name or "", "messages": []},
{"recursion_limit": 40},
)
return state["answer"]
|