Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files1818 unmodified lines18191820182118221823182418253 unmodified lines18291830183118311832183318344 unmodified lines1839184018411842184218431844184512 unmodified lines185818591860186118611862186318641865186624 unmodified lines189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944206 unmodified lines2151215221532104210521062107210821542155215621572158215921602161216221632164216521662167216821692170217121721818 unmodified lines executor.shutdown(wait=False, cancel_futures=True) print(f"[LLM] {self.model_id}: {time.monotonic() - t0:.1f}s", flush=True)
@classmethod @classmethod def _sanitize_code_tags(cls, text: str) -> str: import ast3 unmodified lines open_t = cls.open_tag close_t = cls.close_tag
# Usuń ewentualne bloki <think>...</think> przed ekstrakcją kodu text = re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL | re.IGNORECASE).strip()
def _convert_xml_tool_calls(raw: str) -> str | None:4 unmodified lines ) if qwen_calls: py_lines = [] for fn_name, params_blob in qwen_calls: for idx, (fn_name, params_blob) in enumerate(qwen_calls): params = re.findall( r"<parameter=([A-Za-z_][A-Za-z0-9_]*)>(.*?)</parameter>", params_blob,12 unmodified lines if code_arg: py_lines.append(code_arg) else: py_lines.append(f"print({fn_name}({', '.join(kwargs_parts)}))") var_name = f"tool_res_{idx}" py_lines.append(f"{var_name} = {fn_name}({', '.join(kwargs_parts)})") py_lines.append(f"print({var_name})") if py_lines: return "\n".join(py_lines)
24 unmodified lines return "\n".join(py_lines) return None
def _extract_valid_python(raw: str) -> str: converted = _convert_xml_tool_calls(raw) if converted is not None: raw = converted
lines = [] for line in raw.splitlines(): stripped = line.lstrip() if re.match(r"^(?:thoughts?|thinking)\s*:", stripped, flags=re.IGNORECASE): lines.append("# " + stripped) else: lines.append(line)
n = len(lines) for start in range(n): if not lines[start].strip(): continue for end in range(n, start, -1): candidate = "\n".join(lines[start:end]).strip() if not candidate: continue try: tree = ast.parse(candidate) if ( len(tree.body) == 1 and isinstance(tree.body[0], ast.Expr) and isinstance(tree.body[0].value, ast.Name) ): ident = tree.body[0].value.id return f"final_answer({json.dumps(ident, ensure_ascii=False)})" return candidate except SyntaxError: continue plain = "\n".join(lines).strip() if plain and len(plain) <= 250 and "\n" not in plain: return f"final_answer({json.dumps(plain, ensure_ascii=False)})" return plain
converted_top = _convert_xml_tool_calls(text) if converted_top is not None: return f"{open_t}\n{_extract_valid_python(converted_top)}\n{close_t}"
cleaned_text = re.sub(r"<\|[^|]+\|>", "", text) extracted_main = _extract_valid_python(cleaned_text) if extracted_main: return f"{open_t}\n{extracted_main}\n{close_t}" return cleaned_text
def _extract_valid_python(raw: str) -> str: converted = _convert_xml_tool_calls(raw) if converted is not None:206 unmodified lines ) if fa_matches and fa_matches[-1].strip(): ans_val = fa_matches[-1].strip() content_str = ( f"{cls.open_tag}\n" f"final_answer({json.dumps(ans_val, ensure_ascii=False)})\n" f"{cls.close_tag}" ) else: non_empty_lines = [ ln.strip().strip("*`\"'") for ln in reasoning_str.splitlines() if ln.strip() ] ans_val = non_empty_lines[-1] if non_empty_lines else "" if not ans_val: raise RuntimeError( "Error in generating model output: reasoning-only response without code block or final_answer()" ) content_str = ( f"{cls.open_tag}\n" f"final_answer({json.dumps(ans_val, ensure_ascii=False)})\n" f"{cls.close_tag}" )
if not content_str: print(app.py+65-8