File size: 3,595 Bytes
f837c69 109f376 2c834ce 6005177 760e17f 9e631e2 bd2a75a 760e17f 9d7438a f837c69 bd2a75a 57cfb76 bd2a75a 015f1eb bd2a75a 9e631e2 bd2a75a 760e17f 2c834ce 760e17f 833e758 760e17f 2c834ce 760e17f 833e758 f837c69 760e17f 2c834ce e16c6ed 3f3baec 9d7438a 3f3baec f15c259 2c834ce e16c6ed f15c259 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | """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()
@observe(name="π§± compile_presentation")
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!"
|