Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files732 unmodified lines7337347357367377387398 unmodified lines7487497507517507527537547537547557567577587597607617627637647657667557567577587597607617627637642 unmodified lines767768769775776770777771772773732 unmodified lines super().__init__(*args, **kwargs) self._explicit_api_key = kwargs.get("api_key")
@staticmethod @staticmethod def _sanitize_code_tags(text: str) -> str: import re8 unmodified lines lines.append(line) return "\n".join(lines).strip()
cleaned_text = re.sub(r"<\|[^|]+\|>", "", text) cleaned_text = re.sub(r"</?code[^>]*>\s*```", "<code>\n", text, flags=re.IGNORECASE) cleaned_text = re.sub(r"</?code[^>]*>\s*```", "<code>\n", cleaned_text, flags=re.IGNORECASE) cleaned_text = re.sub(r"```\s*</?code[^>]*>", "\n</code>", cleaned_text, flags=re.IGNORECASE)
if re.search(r"<code>.*?</code>", cleaned_text, flags=re.DOTALL | re.IGNORECASE): def _clean_html_block(match): inner = match.group(1) 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{_clean_python_code(inner)}\n</code>"
return re.sub( r"<code>(.*?)</code>", _clean_html_block, cleaned_text, flags=re.DOTALL | re.IGNORECASE, ) html_match = re.search(r"<code>(.*?)</code>", cleaned_text, flags=re.DOTALL | re.IGNORECASE) if html_match: inner = html_match.group(1) 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{_clean_python_code(inner)}\n</code>"
md_match = re.search( r"```(?:py|python)?\s*\n(.*?)```",2 unmodified lines ) if md_match: inner = re.sub(r"</?code[^>]*>", "", md_match.group(1), flags=re.IGNORECASE) prefix = cleaned_text[: md_match.start()].strip() body = f"<code>\n{_clean_python_code(inner)}\n</code>" return 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:app.py+11-18