Spaces:
Running
Running
Download agent.py from sarra2002/GAIA_Agent_LangGraph: direct link, hf CLI and curl.
- Browser
- Download file 19.2 kB
-
https://huggingface.co/spaces/sarra2002/GAIA_Agent_LangGraph/resolve/main/agent.py
- Command line
-
hf download hf://spaces/sarra2002/GAIA_Agent_LangGraph/agent.py
-
curl -L -o agent.py https://huggingface.co/spaces/sarra2002/GAIA_Agent_LangGraph/resolve/main/agent.py
19.2 kB
| """ | |
| 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")) | |
| 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 | |
| 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}" | |
| 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()?)" | |
| 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"] | |