Spaces:
Runtime error
Runtime error
first pr
Browse filesFiles changed (3)
- agent.pyAdded+333
- app.py+118-9
- requirements.txt+18-1
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333import osimport reimport ioimport contextlibimport requestsimport base64import zipfileimport jsonfrom typing import TypedDict, Annotated
from langgraph.graph import StateGraph, STARTfrom langgraph.graph.message import add_messagesfrom langgraph.prebuilt import ToolNode, tools_condition
from langchain_openai import ChatOpenAIfrom langchain_core.messages import AnyMessage, HumanMessage, SystemMessagefrom langchain_core.tools import toolfrom pydantic import BaseModel, Field
from dotenv import load_dotenvload_dotenv()
SYSTEM_PROMPT = """You are a research agent solving questions from the GAIA benchmark.
WORKFLOW:1. Analyze the question carefully before acting.2. If the question contains reversed text, reverse it back first using python_executor.3. If the question references a file (Excel, CSV, Python, etc.), use read_file to read it.4. If the question references an image file, use analyze_image to look at it.5. If the question references an audio/mp3 file, use transcribe_audio to get the text.6. If the question requires math or logic, use python_executor.7. If the question asks about a YouTube video, first try youtube_transcript. If that fails, use web_search.8. Use web_search or wikipedia_search for factual questions.9. If you find a URL that might have the answer, use fetch_webpage to read it.
RULES:- NEVER call the same tool with the same query twice.- If a tool fails, try a DIFFERENT approach.- For math/logic problems with tables, use python_executor to check ALL pairs systematically.
- For math — ALWAYS use python_executor, never calculate in your head.- Keep search queries short: 2-5 words.- NEVER say "I cannot access" or "I'm unable to" — always try tools first, then give your best guess.- For botany questions: bell peppers, corn, green beans, zucchini, tomatoes, pumpkins are botanical FRUITS, not vegetables.
CRITICAL — ANSWER FORMAT:Your response must end with exactly:FINAL ANSWER: [your answer]
The answer must be:- CONCISE: a number, name, date, or short phrase- EXACT: no extra words like "The answer is..."- If a number: just the number- If a name: just the name- If a list: comma-separated values"""
MAX_TOOL_CALLS = 10RECURSION_LIMIT = 40
@tooldef web_search(query: str) -> str: """Search the web for current events, facts, people, etc. Args: query: search query string (keep it short and specific) """ try: from langchain_tavily import TavilySearch search = TavilySearch(max_results=3) results = search.invoke(query) # TavilySearch возвращает list of dicts или string if isinstance(results, list): formatted = [] for r in results: url = r.get("url", "") content = r.get("content", "") formatted.append(f"Source: {url}\n{content}") return "\n\n---\n\n".join(formatted)[:5000] return str(results)[:5000] except Exception as e: return f"Search failed: {e}"
@tooldef wikipedia_search(query: str) -> str: """Search Wikipedia for factual information about people, places, history, science. Args: query: topic to search on Wikipedia """ try: from langchain_community.utilities import WikipediaAPIWrapper wiki = WikipediaAPIWrapper(top_k_results=2, doc_content_chars_max=4000) return wiki.run(query) except Exception as e: return f"Wikipedia search failed: {e}"
@tooldef arxiv_search(query: str) -> str: """Search academic papers on ArXiv for scientific/research questions. Args: query: search query for academic papers """ try: from langchain_community.document_loaders import ArxivLoader docs = ArxivLoader(query=query, load_max_docs=2).load() results = [] for doc in docs: title = doc.metadata.get("Title", "No title") results.append(f"**{title}**\n{doc.page_content[:1500]}") return "\n\n---\n\n".join(results) if results else "No results found." except Exception as e: return f"ArXiv search failed: {e}"
@tooldef fetch_webpage(url: str) -> str: """Fetch and read content from a URL/webpage. Args: url: full URL to fetch """ try: headers = {"User-Agent": "Mozilla/5.0"} resp = requests.get(url, headers=headers, timeout=15) resp.raise_for_status()
from bs4 import BeautifulSoup soup = BeautifulSoup(resp.text, "html.parser")
for tag in soup(["script", "style", "nav", "footer", "header"]): tag.decompose() text = soup.get_text(separator="\n", strip=True) return text[:8000] except Exception as e: return f"Failed to fetch URL: {e}"
python_state = { "__builtins__": __builtins__, "import_module": __import__}
@tooldef python_executor(code: str) -> str: """ Execute Python code with persistent state across calls. Use print() to see results. All variables are saved for the next call. """ # Очистка кода от Markdown-оберток, если модель их добавила code = re.sub(r'^```python\n|```$', '', code, flags=re.MULTILINE) output = io.StringIO() try: with contextlib.redirect_stdout(output): # Используем один и тот же словарь python_state exec(code, python_state) result = output.getvalue().strip() if not result: return "Code executed successfully, but produced no output. Remember to use print()." return result except Exception as e: return f"Python Error: {str(e)}" @tooldef read_file(file_path: str) -> str: """ Read content of files: TXT, CSV, JSON, PY, XLSX, PDF, or ZIP. For ZIP: lists files inside. For PDF: extracts text. For Tables: returns a summary and first 10 rows. """ if not os.path.exists(file_path): return f"Error: File '{file_path}' not found."
ext = file_path.lower().split('.')[-1] try: # 1. Таблицы (Excel, CSV) if ext in ['xlsx', 'xls', 'csv']: import pandas as pd df = pd.read_excel(file_path) if ext.startswith('xls') else pd.read_csv(file_path) summary = f"Rows: {len(df)}, Columns: {df.columns.tolist()}\n" return summary + df.head(15).to_string()
# 2. PDF (через PyMuPDF / fitz) elif ext == 'pdf': import fitz doc = fitz.open(file_path) text = [] for i, page in enumerate(doc[:10]): # Ограничимся 10 страницами text.append(f"--- Page {i+1} ---\n{page.get_text()}") return "\n".join(text)[:15000]
# 3. ZIP-архивы elif ext == 'zip': with zipfile.ZipFile(file_path, 'r') as z: files = z.namelist() return f"ZIP Archive contains: {files}. Use python_executor to extract if needed."
# 4. JSON elif ext == 'json': with open(file_path, 'r', encoding='utf-8') as f: data = json.load(f) return json.dumps(data, indent=2)[:10000]
# 5. Обычный текст else: with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: return f.read(15000) # Читаем первые 15к символов
except Exception as e: return f"Error processing file {file_path}: {str(e)}"
@tooldef analyze_image(image_path: str, question: str) -> str: """Analyze an image using GPT-4o vision. Use for photos, charts, chess positions, diagrams. Args: image_path: path to the image file (png, jpg, etc.) question: what you want to know about the image """ try: with open(image_path, "rb") as f: image_data = base64.b64encode(f.read()).decode("utf-8")
# Determine mime type ext = image_path.lower().split(".")[-1] mime_map = {"png": "image/png", "jpg": "image/jpeg", "jpeg": "image/jpeg", "gif": "image/gif", "webp": "image/webp"} mime_type = mime_map.get(ext, "image/png")
from openai import OpenAI client = OpenAI() response = client.chat.completions.create( model="gpt-4o", messages=[ { "role": "user", "content": [ {"type": "text", "text": question}, {"type": "image_url", "image_url": {"url": f"data:{mime_type};base64,{image_data}"}}, ], } ], max_tokens=1000, ) return response.choices[0].message.content except Exception as e: return f"Image analysis failed: {e}"
@tooldef transcribe_audio(file_path: str) -> str: """Transcribe an audio file (mp3, wav, m4a) to text using OpenAI Whisper. Args: file_path: path to the audio file """ try: from openai import OpenAI client = OpenAI() with open(file_path, "rb") as f: transcription = client.audio.transcriptions.create( model="whisper-1", file=f, ) return transcription.text[:8000] except Exception as e: return f"Transcription failed: {e}"
llm_fast = ChatOpenAI(model="gpt-4o-mini", temperature=0) # основной агентllm_strong = ChatOpenAI(model="gpt-4o", temperature=0)
tools = [ web_search, wikipedia_search, python_executor, arxiv_search, read_file, fetch_webpage, analyze_image, transcribe_audio,]
llm_with_tools = llm_fast.bind_tools(tools)
class AgentState(TypedDict): messages: Annotated[list[AnyMessage], add_messages]
def assistant(state: AgentState): tool_count = sum(1 for msg in state["messages"] if msg.type == "tool")
if tool_count >= MAX_TOOL_CALLS: force = SystemMessage( content="Provide your FINAL ANSWER now. Format: FINAL ANSWER: [answer]." ) return {"messages": [llm_fast.invoke(state["messages"] + [force])]}
return {"messages": [llm_with_tools.invoke(state["messages"])]}
class FinalAnswer(BaseModel): answer: str = Field(description="The exact final answer — concise, no extra words")
answer_extractor = llm_fast.with_structured_output(FinalAnswer)
def agent_func():
builder = StateGraph(AgentState)
# Define nodes: these do the work builder.add_node("assistant", assistant) builder.add_node("tools", ToolNode(tools, handle_tool_errors=True))
# Define edges: these determine how the control flow moves builder.add_edge(START, "assistant") builder.add_conditional_edges( "assistant", # If the latest message requires a tool, route to tools # Otherwise, provide a direct response tools_condition, ) builder.add_edge("tools", "assistant") alfred = builder.compile()
return alfred12345678910111213141516171819202122232455 unmodified lines808182838485868788899091import osimport gradio as grimport requestsimport inspectimport pandas as pd# (Keep Constants as is)# --- Constants ---DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"# --- Basic Agent Definition ---# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------class BasicAgent:def __init__(self):print("BasicAgent initialized.")def __call__(self, question: str) -> str:print(f"Agent received question (first 50 chars): {question[:50]}...")fixed_answer = "This is a default answer."print(f"Agent returning fixed answer: {fixed_answer}")return fixed_answerdef run_and_submit_all( profile: gr.OAuthProfile | None):"""Fetches all questions, runs the BasicAgent on them, submits all answers,55 unmodified linesprint(f"Skipping item with missing task_id or question: {item}")continuetry:submitted_answer = agent(question_text)answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})except Exception as e:print(f"Error running agent on task {task_id}: {e}")results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})if not answers_payload:print("Agent did not produce any answers to submit.")12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412555 unmodified lines181182183184185186187188189190191192193194195196197198199200import osimport gradio as grimport requestsimport inspectimport pandas as pdfrom langchain_core.messages import AnyMessage, HumanMessage, SystemMessagefrom agent import agent_func, SYSTEM_PROMPT, answer_extractorimport reimport timeimport csvfrom datetime import datetime# (Keep Constants as is)# --- Constants ---DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"RECURSION_LIMIT = 40# --- Basic Agent Definition ---# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------class BasicAgent:def __init__(self):self.agent = agent_func()self.log_file = f"logs_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv"# Создаём файл с заголовкамиwith open(self.log_file, "w", newline="", encoding="utf-8") as f:writer = csv.writer(f)writer.writerow(["task_id", "question", "raw_answer", "final_answer", "duration_sec", "error"])print(f"Logging to {self.log_file}")print("BasicAgent initialized.")def __call__(self, question: str, task_id: str = None) -> str:from agent import SYSTEM_PROMPT, answer_extractorimport timestart = time.time()error = ""raw = ""final = ""# Скачиваем файл если естьfile_info = ""if task_id:try:file_path = self._download_file(task_id)if file_path:file_info = (f"\n\n[Attached file downloaded to: {file_path}. "f"Use the appropriate tool: read_file for text/excel/csv/python, "f"analyze_image for images, transcribe_audio for mp3/wav.]")except Exception as e:error = f"File download: {e}"messages = [SystemMessage(content=SYSTEM_PROMPT),HumanMessage(content=question + file_info),]try:response = self.agent.invoke({"messages": messages},config={"recursion_limit": RECURSION_LIMIT},)raw = response["messages"][-1].content.strip()except Exception as e:error = str(e)raw = f"Error: {e}"# Extract clean answermatch = re.search(r"FINAL ANSWER:\s*(.+)", raw, re.IGNORECASE | re.DOTALL)if match:final = match.group(1).strip()else:try:structured = answer_extractor.invoke(f"Question: {question}\nResponse: {raw}\n"f"Extract ONLY the final answer.")final = structured.answer.strip()except Exception:final = rawduration = round(time.time() - start, 1)# Записываем логwith open(self.log_file, "a", newline="", encoding="utf-8") as f:writer = csv.writer(f)writer.writerow([task_id, question[:200], raw[:500], final, duration, error])print(f"[{duration}s] Q: {question[:80]}...")print(f" Raw: {raw[:150]}")print(f" Final: {final}")return finaldef _download_file(self, task_id: str) -> str:api_url = DEFAULT_API_URLurl = f"{api_url}/files/{task_id}"try:resp = requests.get(url, timeout=15)print(f" File request for {task_id}: status={resp.status_code}")if resp.status_code != 200:print(f" No file for this task")return Nonecd = resp.headers.get("Content-Disposition", "")filename = "attached_file"if "filename=" in cd:filename = cd.split("filename=")[-1].strip('"').strip("'")file_path = os.path.join("/tmp", filename)with open(file_path, "wb") as f:f.write(resp.content)size = len(resp.content)print(f"Downloaded: {file_path} ({size} bytes)")return file_pathexcept Exception as e:print(f"File download error: {e}")return Nonedef run_and_submit_all( profile: gr.OAuthProfile | None):"""Fetches all questions, runs the BasicAgent on them, submits all answers,55 unmodified linesprint(f"Skipping item with missing task_id or question: {item}")continuetry:submitted_answer = agent(question_text, task_id=task_id)answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})time.sleep(3) # пауза между вопросами чтобы не упереться в лимитexcept Exception as e:print(f"Error on task {task_id}: {e}")time.sleep(5) # больше пауза после ошибки# Retry oncetry:submitted_answer = agent(question_text, task_id=task_id)answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})except Exception as e2:results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"ERROR: {e2}"})if not answers_payload:print("Agent did not produce any answers to submit.")
12gradiorequests12345678910111213141516171819gradiorequestslangchainlangchain-communitylangchain-corelangchain-openailangchain-google-genailangchain-huggingfacelangchain-groqlangchain-tavilylanggraphhuggingface_hubsupabasearxivpymupdfwikipediapgvectorpython-dotenv"gradio[oauth]"
agent.pyAdded+333
app.py+118-9
requirements.txt+18-1