File size: 3,594 Bytes
f837c69 015f1eb e16c6ed 109f376 e16c6ed 760e17f 015f1eb 7f86dca f837c69 f402198 3f3baec 109f376 f402198 015f1eb f837c69 015f1eb e16c6ed 760e17f 109f376 f837c69 109f376 f837c69 109f376 417ab3c 109f376 f837c69 109f376 f837c69 e16c6ed f837c69 760e17f f837c69 e16c6ed f837c69 894ddd1 7f86dca f837c69 760e17f e16c6ed 760e17f e16c6ed 894ddd1 e16c6ed 760e17f b52c266 760e17f f837c69 894ddd1 760e17f e16c6ed 3f3baec f837c69 9d7438a 3f3baec | 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 94 95 96 97 98 99 100 101 102 103 | """Module to put it all together and manage the generation of a presentation."""
from dataclasses import dataclass
from typing import Optional
from langfuse.decorators import langfuse_context, observe
from src.compilation import (
compile_presentation,
json_to_tex,
)
# from datetime import datetime
from src.database import retrive_files_from_db
from src.processing import create_output_folder, find_used_gfx
from src.services import build_prompt, client, generate_with_retry
from src.telemetry import Logger
LOGGER = Logger.get_logger()
@dataclass
class PresentationConfig:
"""Configuration for the presentation generation."""
model_name: str = "gemini-2.0-flash"
aspect_ratio: str = "16:9"
theme: str = "default"
color_theme: str = "default"
topic: str = ""
@observe(name="π§βπ¨ generate_presentation")
def generate_presentation(
config_dict, session_id
) -> tuple[str, Optional[str], Optional[str]]:
"""Main function to generate a presentation based on the provided theme, color, and topic.
Args:
config_dict (dict): Configuration dictionary containing:
- model_name (str): Name of the model to use for generation.
- aspect_ratio (str): Aspect ratio for the presentation, e.g., "16:9", "4:3".
- theme (str): Beamer theme to use.
- color_theme (str): Beamer color theme to use.
- topic (str): Topic of the presentation.
session_id (str): Unique identifier for the current session.
Returns:
tuple:
A 3-element tuple containing:
- str: Status message (success or error).
- Optional[str]: Trace ID for telemetry purposes, or None if unavailable.
- Optional[str]: Path to the presentation folder, or None if unavailable.
"""
# Cast dict to dataclass
try:
config = PresentationConfig(**config_dict)
except TypeError as e:
# Handle missing or extra keys gracefully
return f"Config error: {str(e)}", None, None
trace_id = langfuse_context.get_current_trace_id()
[documents], [metadatas] = retrive_files_from_db(config.topic, session_id)
if not documents or not metadatas:
LOGGER.error("β No documents found for the topic: %s", config.topic)
return "β No documents found for the given topic.", trace_id, None
prompt = build_prompt(documents, metadatas, config.aspect_ratio, config.topic)
# Generate the presentation code
model_name = config.model_name
LOGGER.info("βοΈ Generating response...")
try:
answer = generate_with_retry(client, model_name, prompt)
except Exception as e:
LOGGER.error("β Gemini is not reponding: %s", str(e))
return f"β Gemini error: {str(e)}", trace_id, None
langfuse_context.update_current_observation(
output={
"model_name": model_name,
"session_id": session_id,
"aspect_ratio": config.aspect_ratio,
"prompt_length": len(prompt),
"output.response": answer.text,
}
)
LOGGER.info("π² Generated the response using: %s", model_name)
work_dir = create_output_folder(session_id)
find_used_gfx(answer, work_dir, metadatas)
latex_code = json_to_tex(
answer.text, config.theme, config.color_theme, config.aspect_ratio
)
compilation_status = compile_presentation(latex_code, work_dir)
if compilation_status:
return compilation_status, trace_id, work_dir
return "β Unknown critial error. Please try again later.", trace_id, work_dir
|