Download src/compilation/latex_compilation.py from RAGnTeX/ragntex: direct link, hf CLI and curl.
- Browser
- Download file 3.6 kB
-
https://huggingface.co/spaces/RAGnTeX/ragntex/resolve/main/src/compilation/latex_compilation.py
- Command line
-
hf download hf://spaces/RAGnTeX/ragntex/src/compilation/latex_compilation.py
-
curl -L -o latex_compilation.py https://huggingface.co/spaces/RAGnTeX/ragntex/resolve/main/src/compilation/latex_compilation.py
3.6 kB
| """Module for compiling LaTeX presentations into PDF format.""" | |
| import os # nosec: checked usage, inputs are sanitized | |
| import subprocess # nosec: checked usage, inputs are sanitized | |
| from pathlib import Path | |
| from langfuse.decorators import langfuse_context, observe | |
| from ..telemetry import Logger | |
| LOGGER = Logger.get_logger() | |
| def compile_presentation(latex_code, work_dir) -> str: | |
| """Compile LaTeX code into a PDF presentation. | |
| Args: | |
| latex_code (str): LaTeX code to be compiled. | |
| work_dir (str): Directory where the LaTeX file will be saved and compiled. | |
| """ | |
| LOGGER.info("Compiling presentation...") | |
| # Remove possible Markdown wrapping around the output | |
| if latex_code.startswith("```latex"): | |
| latex_code = latex_code.split("\n", 1)[1] # Remove the first line | |
| if latex_code.endswith("```"): | |
| latex_code = latex_code.rsplit("\n", 1)[0] | |
| # Save LaTeX code to file | |
| tex_file = os.path.join(work_dir, "presentation.tex") | |
| with open(tex_file, "w", encoding="utf-8") as f: | |
| f.write(latex_code) | |
| LOGGER.info("π Files in the directory: %s", work_dir) | |
| # List files in the directory using Python (instead of `!ls`) | |
| for file in os.listdir(work_dir): | |
| LOGGER.info(file) | |
| # Compile with pdflatex (using subprocess instead of `!`) | |
| try: | |
| # Run pdflatex twice to ensure proper slide enumeration | |
| subprocess.run( | |
| ["pdflatex", "-interaction=nonstopmode", "presentation.tex"], | |
| cwd=work_dir, | |
| check=True, | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.PIPE, | |
| ) # nosec B603 B607 - fixed command, no untrusted input | |
| subprocess.run( | |
| ["pdflatex", "-interaction=nonstopmode", "presentation.tex"], | |
| cwd=work_dir, | |
| check=True, | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.PIPE, | |
| ) # nosec B603 B607 - fixed command, no untrusted input | |
| except subprocess.CalledProcessError as e: | |
| # Handle error in case of failed LaTeX compilation | |
| langfuse_context.update_current_observation( | |
| output={"pdf.success": False, "pdf.error_log": e.stderr.decode()} | |
| ) | |
| # Check for PDF output | |
| pdf_file = Path(work_dir) / "presentation.pdf" | |
| if not pdf_file.exists(): | |
| LOGGER.error( | |
| "β PDF generation failed and no PDF file found. Here's the log: %s", | |
| e.stderr.decode(), | |
| ) | |
| return "β Presentation compilation failed. Please try again later." | |
| LOGGER.error( | |
| "β οΈ PDF generation failed, but PDF file exists. Here's the log: %s", | |
| e.stderr.decode(), | |
| ) | |
| return ( | |
| "β οΈ Presentation compilation contains errors. Please cross-check the output." | |
| ) | |
| # Success, but still check for PDF output | |
| pdf_file = Path(work_dir) / "presentation.pdf" | |
| if not pdf_file.exists(): | |
| langfuse_context.update_current_observation( | |
| output={ | |
| "pdf.success": False, | |
| "pdf.error_log": "β PDF generation succeded, but no PDF file found.", | |
| } | |
| ) | |
| LOGGER.error("β PDF generation failed. No PDF file found.") | |
| return "β Presentation compilation failed. Please try again later." | |
| langfuse_context.update_current_observation(output={"pdf.success": True}) | |
| LOGGER.info("β PDF generated successfully in: %s", work_dir) | |
| return "βοΈ Presentation generated successfully!" | |