Create generate_slideshows.py
Browse files- generate_slideshows.py +93 -0
generate_slideshows.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
"""
|
3 |
+
Generates slide markdown plus TTS audio and images using Gemini models.
|
4 |
+
|
5 |
+
Functions exposed:
|
6 |
+
generate_slideshow_with_audio(topic) -> (list_of_slide_markdown, list_of_audio_paths, list_of_image_paths)
|
7 |
+
"""
|
8 |
+
|
9 |
+
import asyncio
|
10 |
+
import atexit
|
11 |
+
import datetime
|
12 |
+
import os
|
13 |
+
import re
|
14 |
+
import shutil
|
15 |
+
import struct
|
16 |
+
import tempfile
|
17 |
+
from pathlib import Path
|
18 |
+
from io import BytesIO
|
19 |
+
from typing import Dict, List, Optional
|
20 |
+
|
21 |
+
from dotenv import load_dotenv
|
22 |
+
from google import genai
|
23 |
+
from google.genai import types
|
24 |
+
from PIL import Image
|
25 |
+
|
26 |
+
load_dotenv()
|
27 |
+
GEMINI_API_KEY = os.environ.get("GEMINI_KEY")
|
28 |
+
|
29 |
+
# Dictionary to store temporary directories for cleanup
|
30 |
+
_temp_dirs: Dict[str, str] = {}
|
31 |
+
|
32 |
+
def get_temp_dir(session_id: str) -> str:
|
33 |
+
"""Get or create a temporary directory for a user session"""
|
34 |
+
if session_id not in _temp_dirs:
|
35 |
+
temp_dir = tempfile.mkdtemp(prefix=f"slideshow_{session_id}_")
|
36 |
+
_temp_dirs[session_id] = temp_dir
|
37 |
+
return _temp_dirs[session_id]
|
38 |
+
|
39 |
+
# Add main functionality - to be implemented based on requirements
|
40 |
+
def generate_slideshow_with_audio(topic: str, session_id: str = None) -> tuple:
|
41 |
+
"""Generate slides, audio, and images for a presentation on a topic
|
42 |
+
|
43 |
+
Args:
|
44 |
+
topic: The presentation topic
|
45 |
+
session_id: Optional session identifier for temp file management
|
46 |
+
|
47 |
+
Returns:
|
48 |
+
tuple: (list_of_slide_markdown, list_of_audio_paths, list_of_image_paths)
|
49 |
+
"""
|
50 |
+
# Synchronous wrapper for the async function
|
51 |
+
return asyncio.run(generate_slideshow_with_audio_async(topic, session_id))
|
52 |
+
|
53 |
+
async def generate_slideshow_with_audio_async(topic: str, session_id: str = None) -> tuple:
|
54 |
+
"""Async version of generate_slideshow_with_audio
|
55 |
+
|
56 |
+
Args:
|
57 |
+
topic: The presentation topic
|
58 |
+
session_id: Optional session identifier for temp file management
|
59 |
+
|
60 |
+
Returns:
|
61 |
+
tuple: (list_of_slide_markdown, list_of_audio_paths, list_of_image_paths)
|
62 |
+
"""
|
63 |
+
# Implement the actual slideshow generation with Gemini
|
64 |
+
# This would include calls to Gemini API for content, TTS for audio, etc.
|
65 |
+
# Using temp directories for generated content
|
66 |
+
|
67 |
+
# Placeholder implementation
|
68 |
+
slides = ["# Example Slide\n\n- This is a placeholder"]
|
69 |
+
audio_files = []
|
70 |
+
image_files = []
|
71 |
+
|
72 |
+
return slides, audio_files, image_files
|
73 |
+
|
74 |
+
def validate_topic(topic: str) -> bool:
|
75 |
+
"""Validate if the topic is appropriate for generating a slideshow
|
76 |
+
|
77 |
+
Args:
|
78 |
+
topic: The topic to validate
|
79 |
+
|
80 |
+
Returns:
|
81 |
+
bool: True if the topic is valid, False otherwise
|
82 |
+
"""
|
83 |
+
# Simple validation
|
84 |
+
topic = (topic or "").strip()
|
85 |
+
return len(topic) > 0
|
86 |
+
|
87 |
+
# Register cleanup handler
|
88 |
+
@atexit.register
|
89 |
+
def cleanup_temp_dirs():
|
90 |
+
"""Clean up all temporary directories on application exit"""
|
91 |
+
for session_id, temp_dir in _temp_dirs.items():
|
92 |
+
if os.path.exists(temp_dir):
|
93 |
+
shutil.rmtree(temp_dir, ignore_errors=True)
|