Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import os | |
| from backend.tools.latex_compiler import compiler | |
| # Plantilla básica de documento LaTeX | |
| DEFAULT_LATEX = r"""\documentclass[12pt,a4paper]{article} | |
| \usepackage[utf8]{inputenc} | |
| \usepackage[spanish]{babel} | |
| \usepackage{amsmath} | |
| \usepackage{graphicx} | |
| \usepackage{hyperref} | |
| \title{Documento LetXipu} | |
| \author{Usuario} | |
| \date{\today} | |
| \begin{document} | |
| \maketitle | |
| \begin{abstract} | |
| Este es un documento de prueba compilado localmente. | |
| \end{abstract} | |
| \section{Introducción} | |
| Escribe aquí tu contenido... | |
| \end{document} | |
| """ | |
| def compile_latex(tex_content, filename): | |
| if not filename: | |
| filename = "documento" | |
| # Sanitizar nombre de archivo | |
| filename = "".join([c for c in filename if c.isalpha() or c.isdigit() or c=='_']).strip() | |
| if not filename: | |
| filename = "documento" | |
| result = compiler.compile(tex_content, filename) | |
| if result["success"]: | |
| return result["pdf_path"], "✅ Compilación exitosa." | |
| else: | |
| # Extraer el final del log para mostrar el error | |
| error_msg = result.get("error", "Error desconocido") | |
| logs = result.get("logs", "") | |
| # Mostrar las últimas 20 líneas del log de LaTeX | |
| if logs: | |
| log_lines = logs.split("\n") | |
| log_preview = "\n".join(log_lines[-20:]) | |
| return None, f"❌ {error_msg}\n\nDetalles:\n```text\n{log_preview}\n```" | |
| return None, f"❌ {error_msg}" | |
| def create_editor_tab(): | |
| with gr.Tab("✍️ Editor LaTeX", id="editor"): | |
| gr.Markdown("## Editor LaTeX Local") | |
| gr.Markdown("Escribe y compila código LaTeX directamente en la plataforma usando tu instalación local de TeX Live / MiKTeX.") | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| filename_input = gr.Textbox(label="Nombre del archivo", value="paper_letxipu", placeholder="Sin espacios (ej. mi_documento)") | |
| compile_btn = gr.Button("▶ Compilar PDF", variant="primary", size="lg") | |
| status_output = gr.Markdown("Estado: Esperando...") | |
| with gr.Column(scale=3): | |
| with gr.Row(): | |
| # Editor de código | |
| editor = gr.Code( | |
| value=DEFAULT_LATEX, | |
| language="markdown", | |
| label="Código Fuente LaTeX", | |
| lines=30, | |
| interactive=True | |
| ) | |
| # Visor de PDF (usamos File ya que gr.PDF no existe nativamente en esta versión) | |
| pdf_viewer = gr.File(label="PDF Generado (Click para descargar/ver)", interactive=False) | |
| compile_btn.click( | |
| fn=compile_latex, | |
| inputs=[editor, filename_input], | |
| outputs=[pdf_viewer, status_output] | |
| ) | |