Spaces:
Sleeping
Sleeping
Update app.py
Browse files123456451 unmodified lines45845946046146246322 unmodified lines4864874884894904912 unmodified lines494495496497498499207 unmodified lines707708709710711712713714715716import osimport jsonimport timeimport inspectimport yamlimport requests451 unmodified lineslitellm.completion = _throttled_completion# =========================================================# Load Prompts# =========================================================22 unmodified linesmodel="gemini-2.5-flash")tools = [web_search,visit_webpage,2 unmodified linesyoutube_transcript,final_answer,youtube_video_analysis,]207 unmodified linesresults_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": cached_answer})continuestart_time = time.monotonic()try:submitted_answer = _run_agent_with_backoff(agent_runner, question_text)answer_cache[task_id] = submitted_answer_save_answer_cache(answer_cache)answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})12345678451 unmodified lines46046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654722 unmodified lines5705715725735745755765772 unmodified lines580581582583584585586207 unmodified lines794795796797798799800801802803804805806807808809810811812813814import ioimport osimport jsonimport timeimport mimetypesimport inspectimport yamlimport requests451 unmodified lineslitellm.completion = _throttled_completion# =========================================================# Attached-file tool (images, audio, PDFs, spreadsheets, text/code)# =========================================================# Tip: use a different model here than MODEL_ID so the two draw from# separate daily quotas.FILE_ANALYSIS_MODEL = "gemini-2.5-flash"MAX_FILE_TEXT_CHARS = 20000TEXT_FILE_EXTENSIONS = (".txt", ".py", ".csv", ".json", ".md", ".xml", ".html", ".tsv", ".log")# Set by the evaluation loop before each question, so the agent never has to# guess task ids or file names.current_task_info = {"task_id": None, "file_name": ""}class AnalyzeTaskFileTool(Tool):name = "analyze_task_file"description = ("Reads the file attached to the current task and answers a question about it. ""Supports images (chess boards, charts, photos), audio recordings, PDFs, ""spreadsheets, and text/code files. Only use this when the task says it has ""an attached file.")inputs = {"question": {"type": "string","description": "What you want to know about the attached file.",}}output_type = "string"def forward(self, question: str) -> str:task_id = current_task_info.get("task_id")file_name = current_task_info.get("file_name") or ""if not task_id or not file_name:return "The current task has no attached file."response = _request_with_backoff("GET", f"{DEFAULT_API_URL}/files/{task_id}", timeout=60)data = response.contentext = os.path.splitext(file_name)[1].lower()mime, _ = mimetypes.guess_type(file_name)if not mime:mime = response.headers.get("Content-Type", "application/octet-stream").split(";")[0]# Spreadsheets: dump the cells as text so the agent can reason/calculate.if ext in (".xlsx", ".xlsm", ".xls"):sheets = pd.read_excel(io.BytesIO(data), sheet_name=None)parts = [f"## Sheet: {sheet_name}\n{df.to_csv(index=False)}"for sheet_name, df in sheets.items()]return "\n".join(parts)[:MAX_FILE_TEXT_CHARS]# Plain text / code files.if ext in TEXT_FILE_EXTENSIONS:return data.decode("utf-8", errors="replace")[:MAX_FILE_TEXT_CHARS]# Images, audio and PDFs go to Gemini together with the question.if mime.startswith(("image/", "audio/")) or mime == "application/pdf":from google import genaifrom google.genai import typesclient = genai.Client(api_key=os.environ["GEMINI_API_KEY"])result = client.models.generate_content(model=FILE_ANALYSIS_MODEL,contents=[types.Part.from_bytes(data=data, mime_type=mime),question,],)return result.text or "The model returned no text for this file."return f"Unsupported file type: {file_name} ({mime})."# =========================================================# Load Prompts# =========================================================22 unmodified linesmodel="gemini-2.5-flash")analyze_task_file = AnalyzeTaskFileTool()tools = [web_search,visit_webpage,2 unmodified linesyoutube_transcript,final_answer,youtube_video_analysis,analyze_task_file,]207 unmodified linesresults_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": cached_answer})continuefile_name = item.get("file_name") or ""current_task_info["task_id"] = task_idcurrent_task_info["file_name"] = file_nameagent_input = question_textif file_name:agent_input += (f"\n\n(This task has an attached file named '{file_name}'. "f"Use the analyze_task_file tool to read it.)")start_time = time.monotonic()try:submitted_answer = _run_agent_with_backoff(agent_runner, agent_input)answer_cache[task_id] = submitted_answer_save_answer_cache(answer_cache)answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
app.py+99-1