File size: 21,742 Bytes
7e5516a 7f8dde5 7e5516a 0335934 67111fa 7f8dde5 0335934 7e5516a 6b93517 7e5516a 6b93517 7e5516a 6b93517 7e5516a 6b93517 7e5516a 6b93517 7f8dde5 6b93517 7f8dde5 6b93517 7f8dde5 6b93517 7f8dde5 6b93517 0335934 6b93517 0335934 6b93517 7f8dde5 6b93517 7f8dde5 6b93517 921b2c3 6b93517 0335934 6b93517 0335934 6b93517 0335934 6b93517 0335934 6b93517 7f8dde5 6b93517 7f8dde5 7e5516a 6b93517 7e5516a 6b93517 7f8dde5 7e5516a 6b93517 7e5516a 6b93517 7e5516a 6b93517 7e5516a 6b93517 7f8dde5 6b93517 7f8dde5 6b93517 7f8dde5 6b93517 7e5516a 6b93517 7f8dde5 6b93517 7e5516a 6b93517 7e5516a 7f8dde5 7e5516a 6b93517 7f8dde5 6b93517 7f8dde5 6b93517 67111fa 6b93517 7f8dde5 |
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
#!/usr/bin/env python3
"""
Generates slide markdown plus TTS audio and images using Gemini models.
Functions exposed:
generate_slideshow_with_audio(topic, api_key) -> (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 google import genai
from google.genai import types
from PIL import Image
# Deepgram imports for TTS fallback
try:
from deepgram import DeepgramClient
# Try different import paths based on SDK version
try:
from deepgram.clients.speak.v1.speak_client import SpeakOptions
except ImportError:
try:
from deepgram.clients.speak.v1 import SpeakOptions
except ImportError:
from deepgram.clients.speak import SpeakOptions
DEEPGRAM_AVAILABLE = True
except ImportError:
print("Deepgram SDK not available. Install with 'pip install deepgram-sdk'")
DEEPGRAM_AVAILABLE = False
# Remove the global API key - it will be passed as parameter
DEEPGRAM_KEY = os.environ.get("DEEPGRAM_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]
def cleanup_temp_dirs():
"""Clean up all temporary directories on exit"""
for session_id, temp_dir in _temp_dirs.items():
if os.path.exists(temp_dir):
print(f"Cleaning up temporary directory for session {session_id}")
shutil.rmtree(temp_dir, ignore_errors=True)
_temp_dirs.clear()
# Register cleanup function to run on exit
atexit.register(cleanup_temp_dirs)
# βββββββββββββββββββββββββββββ Helpers ββββββββββββββββββββββββββββββ
def _convert_to_wav(audio_data: bytes, mime_type: str) -> bytes:
"""Ensure Gemini's raw audio is saved as a proper WAV container."""
params = _parse_audio_mime_type(mime_type)
bits_per_sample = params["bits_per_sample"]
sample_rate = params["rate"]
num_channels, data_size = 1, len(audio_data)
bytes_per_sample = bits_per_sample // 8
block_align = num_channels * bytes_per_sample
byte_rate = sample_rate * block_align
chunk_size = 36 + data_size
header = struct.pack(
"<4sI4s4sIHHIIHH4sI",
b"RIFF",
chunk_size,
b"WAVE",
b"fmt ",
16,
1,
num_channels,
sample_rate,
byte_rate,
block_align,
bits_per_sample,
b"data",
data_size,
)
return header + audio_data
def _parse_audio_mime_type(mime_type: str) -> dict[str, int]:
"""Extract sampleβrate & bitβdepth from something like audio/L16;rate=24000;β¦"""
bits_per_sample, rate = 16, 24_000
for part in mime_type.split(";"):
part = part.strip().lower()
if part.startswith("rate="):
rate = int(part.split("=", 1)[1])
elif part.startswith("audio/l"):
bits_per_sample = int(part.split("l", 1)[1])
return {"bits_per_sample": bits_per_sample, "rate": rate}
# ββββββββββββββββββββββ JSON Parsing Utilities βββββββββββββββββββ
import json
import re as _re
def _parse_slides_json(response_text: str) -> list[dict]:
"""Parse the JSON response from Gemini and extract slides data."""
try:
# Find JSON content within response if it's not pure JSON
json_match = re.search(r'```json\s*(.+?)\s*```', response_text, re.DOTALL)
if json_match:
json_text = json_match.group(1).strip()
else:
json_text = response_text.strip()
# Handle potential JSON formatting issues
json_text = json_text.replace('\t', ' ')
# Parse the JSON
slides_data = json.loads(json_text)
return slides_data
except json.JSONDecodeError as e:
print(f"Error parsing JSON: {e}\nAttempting fallback parsing...")
return _fallback_parse(response_text)
def _fallback_parse(text: str) -> list[dict]:
"""Fallback method to extract slides if JSON parsing fails."""
# This is a simple fallback that tries to extract slide content using regex
slides = []
# Try to find slide content and speaker notes
content_matches = re.findall(r'"slide_content"\s*:\s*"([^"]+)"', text, re.DOTALL)
notes_matches = re.findall(r'"speaker_notes"\s*:\s*"([^"]+)"', text, re.DOTALL)
# Create slide entries
for i in range(min(len(content_matches), len(notes_matches))):
slides.append({
"slide_content": content_matches[i].replace('\\n', '\n'),
"speaker_notes": notes_matches[i]
})
return slides if slides else _extract_markdown_slides(text)
def _extract_markdown_slides(markdown: str) -> list[dict]:
"""Extract slides from traditional markdown format (for backwards compatibility)."""
raw = re.split(r"^##\s+", markdown, flags=re.MULTILINE)
md_slides = [s for s in raw if s.strip()]
result = []
for slide in md_slides:
# Preserve title slide format
if slide.lstrip().startswith("# "):
content = slide.lstrip()
else:
content = f"## {slide}" # restore header removed by split
# Extract narration if present
m = re.search(r"Speaker Notes:\s*(.+)", content, flags=re.I | re.S)
notes = ""
if m:
notes = m.group(1).strip()
# Remove speaker notes from content
content = content.split("Speaker Notes:")[0].strip()
result.append({"slide_content": content, "speaker_notes": notes})
return result
# ββββββββββββββββββββββββββββ Gemini Calls βββββββββββββββββββββββββββ
async def _generate_image(prompt: str, output_path: Path, api_key: str) -> str:
"""Generate an image using Gemini Imagen model and save it to the specified path."""
client = genai.Client(api_key=api_key)
try:
# Make this call in a separate thread to not block the event loop
# since the Gemini client isn't natively async
loop = asyncio.get_event_loop()
result = await loop.run_in_executor(
None,
lambda: client.models.generate_images(
model="models/imagen-3.0-generate-002",
prompt=prompt,
config=dict(
number_of_images=1,
output_mime_type="image/jpeg",
person_generation="ALLOW_ADULT",
aspect_ratio="16:9", # Better for slides
),
)
)
if not result.generated_images or len(result.generated_images) == 0:
print("No images generated.")
return ""
# Save the generated image
image = Image.open(BytesIO(result.generated_images[0].image.image_bytes))
output_path.parent.mkdir(parents=True, exist_ok=True) # Ensure directory exists
image.save(output_path)
return str(output_path)
except Exception as e:
print(f"Error generating image: {e}")
return ""
def _generate_slideshow_markdown(topic: str, api_key: str) -> str:
"""Ask Gemini 2.5 Flash for a markdown deck following strict rules."""
client = genai.Client(api_key=api_key)
#model = "gemini-2.5-flash-preview-05-20"
model = "gemini-2.5-pro-preview-06-05"
sys_prompt = f"""
<role>
You are SlideGen, an AI that creates fun and engaging narrated slide decks with visual elements about various topics.
</role>
<instructions>
Create a presentation about '{topic}'.
Include:
- An introduction slide with bullet points about the overview of the presentation topic and the key areas that will be covered
- 3 content slides with bullet points
- A conclusion slide with bullet points summarizing the key points and insights.
For each slide provide:
1. Each title should be a single concise and coherent phrase accompanied by exactly one relevant emoji. (Do NOT use the colon ":" format for titles)
2. 3-4 concise bullet points, you will go into more detail in the speaker notes.
3. Clear prose speaker notes suitable for narration that is accessible to general audiences
4. A detailed and specific image prompt for an AI image generator that is relevent to the slide's content. Do not include any text in the image.
Respond with a JSON array where each element represents a slide in the following format:
```json
[
{{
"slide_content": "## Introduction Slide Title\n\n",
"speaker_notes": "Speaker notes",
"image_prompt": "Image prompt"
}},
{{
"slide_content": "## Content Slide Title\n\n",
"speaker_notes": "Speaker notes",
"image_prompt": "Image prompt"
}},
{{
"slide_content": "## Content Slide Title\n\n",
"speaker_notes": "Speaker notes",
"image_prompt": "Image prompt"
}},
{{
"slide_content": "## Content Slide Title\n\n",
"speaker_notes": "Speaker notes",
"image_prompt": "Image prompt"
}},
{{
"slide_content": "## Conclusion Slide Title\n\n",
"speaker_notes": "Speaker notes",
"image_prompt": "Image prompt"
}},
]
</instructions>
""".strip()
response = client.models.generate_content(
model=model,
contents=[{"role": "user", "parts": [{"text": sys_prompt}]}],
config=types.GenerateContentConfig(response_mime_type="text/plain", temperature=0.7),
)
return response.text.strip()
async def _generate_tts(narration: str, out_path: Path, api_key: str):
"""GenAI TTS β WAV - Async version with fallback model support"""
client = genai.Client(api_key=api_key)
# Try with flash model first, then fall back to pro model if needed
models_to_try = ["gemini-2.5-flash-preview-tts", "gemini-2.5-pro-preview-06-05"]
# Create file with write mode first to ensure it's empty
with open(out_path, "wb") as _:
pass
# Try models in sequence until one works
gemini_exhausted = True
for model in models_to_try:
try:
print(f"Attempting TTS with model: {model}")
stream_instance = client.models.generate_content_stream(
model=model,
contents=[{"role": "user", "parts": [{"text": narration}]}],
config=types.GenerateContentConfig(
temperature=1,
response_modalities=["audio"] if "tts" in model else [],
speech_config=types.SpeechConfig(
voice_config=types.VoiceConfig(
prebuilt_voice_config=types.PrebuiltVoiceConfig(voice_name="Algenib")
)
) if "tts" in model else None,
),
)
# Process the stream
async def process_stream():
for chunk in stream_instance:
if (
chunk.candidates
and chunk.candidates[0].content
and chunk.candidates[0].content.parts
):
part = chunk.candidates[0].content.parts[0].inline_data
if part and part.data:
data = (
_convert_to_wav(part.data, part.mime_type)
if not part.mime_type.endswith("wav")
else part.data
)
with open(out_path, "ab") as f:
f.write(data)
await process_stream()
# If we get here, the model worked successfully
print(f"Successfully generated TTS using model: {model}")
gemini_exhausted = False
return
except Exception as e:
if hasattr(e, 'code') and getattr(e, 'code', None) == 429:
print(f"Model {model} quota exhausted. Trying next model...")
continue
else:
# Re-raise if it's not a quota error
print(f"Error with model {model}: {e}")
raise
# If we've tried all Gemini models and none worked, try Deepgram
if gemini_exhausted and DEEPGRAM_AVAILABLE and DEEPGRAM_KEY:
try:
print("Attempting TTS with Deepgram...")
# Run Deepgram in executor to avoid blocking
loop = asyncio.get_event_loop()
await loop.run_in_executor(None, lambda: _generate_tts_with_deepgram(narration, out_path))
print("Successfully generated TTS using Deepgram")
return
except Exception as e:
print(f"Error with Deepgram TTS: {e}")
# Continue to fallback empty WAV if Deepgram fails
# Last resort fallback - create empty audio file
print("All TTS models quota exhausted. Creating empty audio file.")
with open(out_path, "wb") as f:
f.write(b'RIFF$\x00\x00\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00\x00\x04\x00\x00\x00\x04\x00\x00\x01\x00\x08\x00data\x00\x00\x00\x00')
def _generate_tts_with_deepgram(narration: str, out_path: Path):
"""Generate TTS using Deepgram API"""
# Initialize the Deepgram client
deepgram = DeepgramClient(DEEPGRAM_KEY)
print(f"Using Deepgram for TTS generation")
# Configure speech options for v2.x API (which we confirmed works)
options = SpeakOptions(
model="aura-2-thalia-en", # Use Thalia voice
encoding="linear16", # This produces WAV format
container="wav", # Specify WAV container
sample_rate=24000 # Sample rate in Hz
)
# Convert text to speech and save directly to file using the v2.x API
try:
response = deepgram.speak.rest.v("1").save(
str(out_path), # Output filename
{"text": narration}, # Text to convert
options
)
print(f"Successfully generated TTS with Deepgram: {out_path}")
return response
except Exception as e:
print(f"Error generating TTS with Deepgram: {e}")
raise
# ββββββββββββββββββββββββ Public Entry Point βββββββββββββββββββ
async def generate_slideshow_with_audio_async(topic: str, api_key: str, **kwargs):
"""
Async version of generate_slideshow_with_audio that processes slides concurrently.
Args:
topic: The topic to generate a slideshow about
api_key: Gemini API key
**kwargs: Optional parameters including session_id
Returns:
slides_md : list[str] β markdown for each slide
audio : list[str] β file paths (one per slide, same order)
images : list[str|None] β file paths for slide images (one per slide, same order)
"""
# Get JSON response from Gemini
json_response = _generate_slideshow_markdown(topic, api_key)
# Parse JSON into slides data
slides_data = _parse_slides_json(json_response)
# Create temporary directory for this slideshow
temp_dir = get_temp_dir(str(kwargs.get("session_id", datetime.datetime.now().strftime("%Y%m%d_%H%M%S"))))
safe_topic = re.sub(r"[^\w\s-]", "", topic)[:30]
safe_topic = re.sub(r"[-\s]+", "-", safe_topic)
pres_dir = Path(temp_dir) / safe_topic
pres_dir.mkdir(parents=True, exist_ok=True)
slides_md = []
audio_files = []
slide_images = [None] * len(slides_data) # Pre-initialize with None values
print("\n====== GENERATING SLIDESHOW CONTENT ======")
# Set up async tasks
tts_tasks = []
image_tasks = []
for i, slide_info in enumerate(slides_data, start=1):
slides_md.append(slide_info["slide_content"])
# Get the title for logging
title_match = re.search(r'##\s+(.+?)\n', slide_info["slide_content"].strip())
title = title_match.group(1) if title_match else f"Slide {i}"
print(f"\n--- Processing Slide {i}: {title} ---")
# Generate and print speaker notes
narration = slide_info.get("speaker_notes", "")
print("SPEAKER NOTES:")
print(narration or "No speaker notes provided.")
# Create paths for output files
wav_path = pres_dir / f"{safe_topic}_slide_{i:02d}.wav"
audio_files.append(str(wav_path))
# Schedule TTS task
if narration:
print(f"Scheduling TTS for slide {i} -> {wav_path}")
tts_tasks.append(_generate_tts(narration, wav_path, api_key))
else:
# Create empty placeholder WAV if no narration
with open(wav_path, "wb") as f:
f.write(b'RIFF$\x00\x00\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00\x00\x04\x00\x00\x00\x04\x00\x00\x01\x00\x08\x00data\x00\x00\x00\x00')
print(f"No narration for slide {i}, created empty WAV: {wav_path}")
# Schedule image generation task
image_prompt = slide_info.get("image_prompt", "")
# Append instruction to avoid text in images
if image_prompt:
image_prompt = image_prompt.strip() + " Do not include any text in the image."
print("IMAGE PROMPT:")
print(image_prompt or "No image prompt provided.")
if image_prompt:
image_path = pres_dir / f"{safe_topic}_slide_{i:02d}_image.jpg"
print(f"Scheduling image for slide {i} -> {image_path}")
# Store task with index to track which slide it belongs to
image_tasks.append((i-1, _generate_image(image_prompt, image_path, api_key)))
else:
print(f"No image prompt for slide {i}, skipping image generation.")
print("-"*50)
# Execute all TTS tasks concurrently
print("\n====== GENERATING TTS IN PARALLEL ======")
if tts_tasks:
await asyncio.gather(*tts_tasks)
print("====== TTS GENERATION COMPLETE ======")
# Execute all image tasks concurrently
print("\n====== GENERATING IMAGES IN PARALLEL ======")
if image_tasks:
# Gather all image generation tasks while preserving their indices
image_results = await asyncio.gather(*[task for _, task in image_tasks])
# Map results back to their positions in slide_images
for (idx, _), path in zip(image_tasks, image_results):
slide_images[idx] = path
print("====== IMAGE GENERATION COMPLETE ======")
print("\n====== SLIDESHOW GENERATION COMPLETE ======\n")
# Ensure all lists have the same length as slides_md
num_slides = len(slides_md)
while len(audio_files) < num_slides:
audio_files.append(None)
while len(slide_images) < num_slides:
slide_images.append(None)
return slides_md, audio_files, slide_images
def generate_slideshow_with_audio(topic: str, api_key: str, **kwargs):
"""
Synchronous wrapper for the async slideshow generation function.
Maintains backward compatibility with existing code.
Args:
topic: The topic to generate a slideshow about
api_key: Gemini API key
**kwargs: Optional parameters including:
- session_id: Unique identifier for the user session
Returns:
slides_md : list[str] β markdown for each slide
audio : list[str] β file paths (one per slide, same order)
images : list[str|None] β file paths for slide images (one per slide, same order)
"""
return asyncio.run(generate_slideshow_with_audio_async(topic, api_key, **kwargs))
def validate_topic(topic: str, api_key: str) -> bool:
"""Use Gemini Flash Preview to determine if a topic is suitable for a slideshow."""
client = genai.Client(api_key=api_key)
system_prompt = f'''
<role>
You are SlideGenInputGuard, an AI assistant that determines if a user input is a suitable topic for a narrated slideshow presentation.
</role>
<instructions>
Evaluate if "{topic}" is a real-world topic, question, or concept suitable for an educational slideshow. It is fine to include topics that are silly and not real-world topics.
If it is a valid topic, respond with exactly: 1
If it is nonsense, gibberish, meaningless, empty, or not a valid topic, respond with exactly: 0
Only respond with a single digit: 1 or 0. No spaces, newlines or explanations. JUST THE NUMBER 1 OR 0.
</instructions>
<examples>
Input:How does lightning form?
Output:1
Input:The history of horses
Output:1
Input:basketball
Output:1
Input:boobs
Output:1
Input:King Kong
Output:1
Input:Batman
Output:1
Input:Hitler
Output:1
Input:bing bong
Output:0
Input:asdf
Output:0
Input:qwerty
Output:0
Input::)
Output:0
Input:
Output:0
</examples>
'''.strip()
response = client.models.generate_content(
model="gemini-2.5-flash-preview-05-20",
contents=[{"role": "user", "parts": [{"text": system_prompt}]}],
config=types.GenerateContentConfig(response_mime_type="text/plain", temperature=0),
)
result = response.text.strip()
return result == "1" |