Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files223 unmodified lines22422522622722822923015 unmodified lines2462472482492502482492502512522532542552562572582512522532542552562572582592602612622632642652662672682692702712722732742752762602612772782792632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043052802812822833083092842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363371409 unmodified lines17471748174917251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175017511752223 unmodified lines preferred_filename: Optional original attachment filename from the task metadata. """ import re import time from urllib.parse import unquote
clean_id = (task_id or "").strip()15 unmodified lines
last_err = None for url in candidate_urls: resp = None for attempt in range(3): 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: continue resp.raise_for_status() if not resp.content: continue 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)) continue break except Exception as exc: last_err = exc if "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)) continue except Exception: resp = None break resp = None break
content_disp = resp.headers.get("Content-Disposition", "") filename = safe_pref_name if resp is None or resp.status_code == 404: continue
if 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_path try: resp.raise_for_status() except Exception as exc: last_err = exc if "task time budget exhausted" in str(exc).lower(): break continue
if not resp.content: continue
content_disp = resp.headers.get("Content-Disposition", "") filename = safe_pref_name
if 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_path
if last_err is not None: return f"Error downloading file for task '{clean_id}': {str(last_err)}"1409 unmodified lines raise 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 = []app.py+83-87