Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files736 unmodified lines7377387397407417427437447457467477487497507517523 unmodified lines7567577587497597607617628 unmodified lines771772773764774775766776777778779780770781782783784736 unmodified lines def _sanitize_code_tags(text: str) -> str: import re
def _clean_python_code(code_str: str) -> str: lines = [] for line in code_str.splitlines(): stripped = line.lstrip() if re.match(r"^(?:thoughts?|thinking)\s*:", stripped, flags=re.IGNORECASE): lines.append("# " + stripped) else: lines.append(line) return "\n".join(lines).strip()
cleaned_text = re.sub(r"</?code[^>]*>\s*```", "<code>\n", text, flags=re.IGNORECASE) cleaned_text = re.sub(r"```\s*</?code[^>]*>", "\n</code>", cleaned_text, flags=re.IGNORECASE)
3 unmodified lines inner = re.sub(r"```(?:py|python)?\s*\n?", "", inner, flags=re.IGNORECASE) inner = re.sub(r"\n?```", "", inner) inner = re.sub(r"</?code[^>]*>", "", inner, flags=re.IGNORECASE) return f"<code>\n{inner.strip()}\n</code>" return f"<code>\n{_clean_python_code(inner)}\n</code>"
return re.sub( r"<code>(.*?)</code>",8 unmodified lines flags=re.DOTALL | re.IGNORECASE, ) if md_match: inner = re.sub(r"</?code[^>]*>", "", md_match.group(1), flags=re.IGNORECASE).strip() inner = re.sub(r"</?code[^>]*>", "", md_match.group(1), flags=re.IGNORECASE) prefix = cleaned_text[: md_match.start()].strip() return f"{prefix}\n<code>\n{inner}\n</code>" if prefix else f"<code>\n{inner}\n</code>" body = f"<code>\n{_clean_python_code(inner)}\n</code>" return f"{prefix}\n{body}" if prefix else body
stripped_orphan_tags = re.sub(r"</?code[^>]*>", "", cleaned_text, flags=re.IGNORECASE).strip() if stripped_orphan_tags != cleaned_text.strip() and stripped_orphan_tags: return f"<code>\n{stripped_orphan_tags}\n</code>" return f"<code>\n{_clean_python_code(stripped_orphan_tags)}\n</code>"
return cleaned_text
app.py+15-4