Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files223 unmodified lines22422522622722822915 unmodified lines2452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113121409 unmodified lines17221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756223 unmodified linespreferred_filename: Optional original attachment filename from the task metadata."""import refrom urllib.parse import unquoteclean_id = (task_id or "").strip()15 unmodified lineslast_err = Nonefor url in candidate_urls:try:resp = requests.get(url,timeout=_tool_timeout(20.0, min_timeout=5.0),headers={"User-Agent": "Mozilla/5.0"},)if resp.status_code == 404:continueresp.raise_for_status()if not resp.content:continuecontent_disp = resp.headers.get("Content-Disposition", "")filename = safe_pref_nameif not filename:star_match = re.search(r"filename\*\s*=\s*(?:[A-Za-z0-9_-]+''|\"[A-Za-z0-9_-]+'')?([^\";]+)",content_disp,flags=re.IGNORECASE,)if star_match:raw_star = star_match.group(1).strip().strip('"')if "''" in raw_star:raw_star = raw_star.split("''", 1)[1]filename = os.path.basename(unquote(raw_star))if not filename:std_match = re.search(r'filename\s*=\s*"?([^";]+)"?',content_disp,flags=re.IGNORECASE,)if std_match:filename = os.path.basename(unquote(std_match.group(1).strip()))if not filename:content_type = resp.headers.get("Content-Type", "").split(";")[0].strip().lower()ext_map = {"text/plain": ".txt","text/csv": ".csv","application/json": ".json","application/pdf": ".pdf","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": ".xlsx","application/vnd.ms-excel": ".xls","image/png": ".png","image/jpeg": ".jpg","audio/mpeg": ".mp3","audio/wav": ".wav","text/x-python": ".py",}ext = ext_map.get(content_type) or mimetypes.guess_extension(content_type) or ".bin"filename = f"{clean_id}{ext}"out_path = os.path.join(output_dir, filename)with open(out_path, "wb") as f:f.write(resp.content)return out_pathexcept Exception as exc:last_err = excif "task time budget exhausted" in str(exc).lower():breakif last_err is not None:return f"Error downloading file for task '{clean_id}': {str(last_err)}"1409 unmodified linesraise RuntimeError("Error in generating model output: malformed_function_call with empty content")reasoning_sources = [getattr(msg, "reasoning_content", None),getattr(msg, "reasoning", None),]if raw_msg is not None:if isinstance(raw_msg, dict):reasoning_sources.extend([raw_msg.get("reasoning_content"),raw_msg.get("reasoning"),raw_msg.get("thinking_blocks"),])else:reasoning_sources.extend([getattr(raw_msg, "reasoning_content", None),getattr(raw_msg, "reasoning", None),getattr(raw_msg, "thinking_blocks", None),])reasoning_str = next((txt for src in reasoning_sources if (txt := _extract_text_from_blocks(src))),"",)tool_calls = getattr(msg, "tool_calls", None)if not content_str and not tool_calls and not reasoning_str and "malformed_function_call" in finish_reason:raise RuntimeError("Error in generating model output: malformed_function_call with empty content")if not content_str and tool_calls:rendered_calls = []223 unmodified lines22422522622722822923015 unmodified lines2462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363371409 unmodified lines174717481749175017511752223 unmodified linespreferred_filename: Optional original attachment filename from the task metadata."""import reimport timefrom urllib.parse import unquoteclean_id = (task_id or "").strip()15 unmodified lineslast_err = Nonefor url in candidate_urls:resp = Nonefor attempt in range(3):try:resp = requests.get(url,timeout=(10.0, _tool_timeout(30.0, min_timeout=5.0)),headers={"User-Agent": "Mozilla/5.0"},)if resp.status_code in {429, 502, 503, 504} and attempt < 2:_tool_timeout(4.0, min_timeout=1.0)time.sleep(1.5 * (attempt + 1))continuebreakexcept Exception as exc:last_err = excif "task time budget exhausted" in str(exc).lower():return f"Error downloading file for task '{clean_id}': {str(exc)}"if attempt < 2:try:_tool_timeout(4.0, min_timeout=1.0)time.sleep(1.5 * (attempt + 1))continueexcept Exception:resp = Nonebreakresp = Nonebreakif resp is None or resp.status_code == 404:continuetry:resp.raise_for_status()except Exception as exc:last_err = exccontinueif not resp.content:continuecontent_disp = resp.headers.get("Content-Disposition", "")filename = safe_pref_nameif not filename:star_match = re.search(r"filename\*\s*=\s*(?:[A-Za-z0-9_-]+''|\"[A-Za-z0-9_-]+'')?([^\";]+)",content_disp,flags=re.IGNORECASE,)if star_match:raw_star = star_match.group(1).strip().strip('"')if "''" in raw_star:raw_star = raw_star.split("''", 1)[1]filename = os.path.basename(unquote(raw_star))if not filename:std_match = re.search(r'filename\s*=\s*"?([^";]+)"?',content_disp,flags=re.IGNORECASE,)if std_match:filename = os.path.basename(unquote(std_match.group(1).strip()))if not filename:content_type = resp.headers.get("Content-Type", "").split(";")[0].strip().lower()ext_map = {"text/plain": ".txt","text/csv": ".csv","application/json": ".json","application/pdf": ".pdf","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": ".xlsx","application/vnd.ms-excel": ".xls","image/png": ".png","image/jpeg": ".jpg","audio/mpeg": ".mp3","audio/wav": ".wav","text/x-python": ".py",}ext = ext_map.get(content_type) or mimetypes.guess_extension(content_type) or ".bin"filename = f"{clean_id}{ext}"out_path = os.path.join(output_dir, filename)with open(out_path, "wb") as f:f.write(resp.content)return out_pathif last_err is not None:return f"Error downloading file for task '{clean_id}': {str(last_err)}"1409 unmodified linesraise RuntimeError("Error in generating model output: malformed_function_call with empty content")if not content_str and tool_calls:rendered_calls = []
app.py+83-87