File size: 2,949 Bytes
7e5516a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Generates slide markdown plus TTS audio and images using Gemini models.

Functions exposed:
    generate_slideshow_with_audio(topic) -> (list_of_slide_markdown, list_of_audio_paths, list_of_image_paths)
"""

import asyncio
import atexit
import datetime
import os
import re
import shutil
import struct
import tempfile
from pathlib import Path
from io import BytesIO
from typing import Dict, List, Optional

from dotenv import load_dotenv
from google import genai
from google.genai import types
from PIL import Image

load_dotenv()
GEMINI_API_KEY = os.environ.get("GEMINI_KEY")

# Dictionary to store temporary directories for cleanup
_temp_dirs: Dict[str, str] = {}

def get_temp_dir(session_id: str) -> str:
    """Get or create a temporary directory for a user session"""
    if session_id not in _temp_dirs:
        temp_dir = tempfile.mkdtemp(prefix=f"slideshow_{session_id}_")
        _temp_dirs[session_id] = temp_dir
    return _temp_dirs[session_id]

# Add main functionality - to be implemented based on requirements
def generate_slideshow_with_audio(topic: str, session_id: str = None) -> tuple:
    """Generate slides, audio, and images for a presentation on a topic
    
    Args:
        topic: The presentation topic
        session_id: Optional session identifier for temp file management
        
    Returns:
        tuple: (list_of_slide_markdown, list_of_audio_paths, list_of_image_paths)
    """
    # Synchronous wrapper for the async function
    return asyncio.run(generate_slideshow_with_audio_async(topic, session_id))

async def generate_slideshow_with_audio_async(topic: str, session_id: str = None) -> tuple:
    """Async version of generate_slideshow_with_audio
    
    Args:
        topic: The presentation topic
        session_id: Optional session identifier for temp file management
        
    Returns:
        tuple: (list_of_slide_markdown, list_of_audio_paths, list_of_image_paths)
    """
    # Implement the actual slideshow generation with Gemini
    # This would include calls to Gemini API for content, TTS for audio, etc.
    # Using temp directories for generated content
    
    # Placeholder implementation
    slides = ["# Example Slide\n\n- This is a placeholder"]
    audio_files = []
    image_files = []
    
    return slides, audio_files, image_files

def validate_topic(topic: str) -> bool:
    """Validate if the topic is appropriate for generating a slideshow
    
    Args:
        topic: The topic to validate
        
    Returns:
        bool: True if the topic is valid, False otherwise
    """
    # Simple validation
    topic = (topic or "").strip()
    return len(topic) > 0

# Register cleanup handler
@atexit.register
def cleanup_temp_dirs():
    """Clean up all temporary directories on application exit"""
    for session_id, temp_dir in _temp_dirs.items():
        if os.path.exists(temp_dir):
            shutil.rmtree(temp_dir, ignore_errors=True)