Update generate_slideshow.py
Browse files- generate_slideshow.py +67 -9
generate_slideshow.py
CHANGED
|
@@ -22,8 +22,24 @@ from google import genai
|
|
| 22 |
from google.genai import types
|
| 23 |
from PIL import Image
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
GEMINI_API_KEY = os.environ.get("GEMINI_KEY")
|
|
|
|
| 27 |
|
| 28 |
# Dictionary to store temporary directories for cleanup
|
| 29 |
_temp_dirs: Dict[str, str] = {}
|
|
@@ -214,9 +230,9 @@ Include:
|
|
| 214 |
|
| 215 |
For each slide provide:
|
| 216 |
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)
|
| 217 |
-
2. 3-4 concise bullet points
|
| 218 |
3. Clear prose speaker notes suitable for narration that is accessible to general audiences
|
| 219 |
-
4. A detailed and specific image prompt for an AI image generator that is relevent to the slide's content
|
| 220 |
|
| 221 |
|
| 222 |
Respond with a JSON array where each element represents a slide in the following format:
|
|
@@ -271,6 +287,7 @@ async def _generate_tts(narration: str, out_path: Path):
|
|
| 271 |
pass
|
| 272 |
|
| 273 |
# Try models in sequence until one works
|
|
|
|
| 274 |
for model in models_to_try:
|
| 275 |
try:
|
| 276 |
print(f"Attempting TTS with model: {model}")
|
|
@@ -306,12 +323,12 @@ async def _generate_tts(narration: str, out_path: Path):
|
|
| 306 |
)
|
| 307 |
with open(out_path, "ab") as f:
|
| 308 |
f.write(data)
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
|
| 316 |
except Exception as e:
|
| 317 |
if hasattr(e, 'code') and getattr(e, 'code', None) == 429:
|
|
@@ -322,12 +339,53 @@ async def _generate_tts(narration: str, out_path: Path):
|
|
| 322 |
print(f"Error with model {model}: {e}")
|
| 323 |
raise
|
| 324 |
|
| 325 |
-
# If we've tried all models and none worked
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
print("All TTS models quota exhausted. Creating empty audio file.")
|
| 327 |
with open(out_path, "wb") as f:
|
| 328 |
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')
|
| 329 |
|
| 330 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
# ββββββββββββββββββββββββ Public Entry Point ββββββββββββββββββοΏ½οΏ½
|
| 332 |
async def generate_slideshow_with_audio_async(topic: str, **kwargs):
|
| 333 |
"""
|
|
|
|
| 22 |
from google.genai import types
|
| 23 |
from PIL import Image
|
| 24 |
|
| 25 |
+
# Deepgram imports for TTS fallback
|
| 26 |
+
try:
|
| 27 |
+
from deepgram import DeepgramClient
|
| 28 |
+
# Try different import paths based on SDK version
|
| 29 |
+
try:
|
| 30 |
+
from deepgram.clients.speak.v1.speak_client import SpeakOptions
|
| 31 |
+
except ImportError:
|
| 32 |
+
try:
|
| 33 |
+
from deepgram.clients.speak.v1 import SpeakOptions
|
| 34 |
+
except ImportError:
|
| 35 |
+
from deepgram.clients.speak import SpeakOptions
|
| 36 |
+
DEEPGRAM_AVAILABLE = True
|
| 37 |
+
except ImportError:
|
| 38 |
+
print("Deepgram SDK not available. Install with 'pip install deepgram-sdk'")
|
| 39 |
+
DEEPGRAM_AVAILABLE = False
|
| 40 |
|
| 41 |
GEMINI_API_KEY = os.environ.get("GEMINI_KEY")
|
| 42 |
+
DEEPGRAM_KEY = os.environ.get("DEEPGRAM_KEY")
|
| 43 |
|
| 44 |
# Dictionary to store temporary directories for cleanup
|
| 45 |
_temp_dirs: Dict[str, str] = {}
|
|
|
|
| 230 |
|
| 231 |
For each slide provide:
|
| 232 |
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)
|
| 233 |
+
2. 3-4 concise bullet points, you will go into more detail in the speaker notes.
|
| 234 |
3. Clear prose speaker notes suitable for narration that is accessible to general audiences
|
| 235 |
+
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.
|
| 236 |
|
| 237 |
|
| 238 |
Respond with a JSON array where each element represents a slide in the following format:
|
|
|
|
| 287 |
pass
|
| 288 |
|
| 289 |
# Try models in sequence until one works
|
| 290 |
+
gemini_exhausted = True
|
| 291 |
for model in models_to_try:
|
| 292 |
try:
|
| 293 |
print(f"Attempting TTS with model: {model}")
|
|
|
|
| 323 |
)
|
| 324 |
with open(out_path, "ab") as f:
|
| 325 |
f.write(data)
|
| 326 |
+
|
| 327 |
+
await process_stream()
|
| 328 |
+
# If we get here, the model worked successfully
|
| 329 |
+
print(f"Successfully generated TTS using model: {model}")
|
| 330 |
+
gemini_exhausted = False
|
| 331 |
+
return
|
| 332 |
|
| 333 |
except Exception as e:
|
| 334 |
if hasattr(e, 'code') and getattr(e, 'code', None) == 429:
|
|
|
|
| 339 |
print(f"Error with model {model}: {e}")
|
| 340 |
raise
|
| 341 |
|
| 342 |
+
# If we've tried all Gemini models and none worked, try Deepgram
|
| 343 |
+
if gemini_exhausted and DEEPGRAM_AVAILABLE and DEEPGRAM_KEY:
|
| 344 |
+
try:
|
| 345 |
+
print("Attempting TTS with Deepgram...")
|
| 346 |
+
# Run Deepgram in executor to avoid blocking
|
| 347 |
+
loop = asyncio.get_event_loop()
|
| 348 |
+
await loop.run_in_executor(None, lambda: _generate_tts_with_deepgram(narration, out_path))
|
| 349 |
+
print("Successfully generated TTS using Deepgram")
|
| 350 |
+
return
|
| 351 |
+
except Exception as e:
|
| 352 |
+
print(f"Error with Deepgram TTS: {e}")
|
| 353 |
+
# Continue to fallback empty WAV if Deepgram fails
|
| 354 |
+
|
| 355 |
+
# Last resort fallback - create empty audio file
|
| 356 |
print("All TTS models quota exhausted. Creating empty audio file.")
|
| 357 |
with open(out_path, "wb") as f:
|
| 358 |
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')
|
| 359 |
|
| 360 |
|
| 361 |
+
def _generate_tts_with_deepgram(narration: str, out_path: Path):
|
| 362 |
+
"""Generate TTS using Deepgram API"""
|
| 363 |
+
# Initialize the Deepgram client
|
| 364 |
+
deepgram = DeepgramClient(DEEPGRAM_KEY)
|
| 365 |
+
print(f"Using Deepgram for TTS generation")
|
| 366 |
+
|
| 367 |
+
# Configure speech options for v2.x API (which we confirmed works)
|
| 368 |
+
options = SpeakOptions(
|
| 369 |
+
model="aura-2-thalia-en", # Use Thalia voice
|
| 370 |
+
encoding="linear16", # This produces WAV format
|
| 371 |
+
container="wav", # Specify WAV container
|
| 372 |
+
sample_rate=24000 # Sample rate in Hz
|
| 373 |
+
)
|
| 374 |
+
|
| 375 |
+
# Convert text to speech and save directly to file using the v2.x API
|
| 376 |
+
try:
|
| 377 |
+
response = deepgram.speak.rest.v("1").save(
|
| 378 |
+
str(out_path), # Output filename
|
| 379 |
+
{"text": narration}, # Text to convert
|
| 380 |
+
options
|
| 381 |
+
)
|
| 382 |
+
print(f"Successfully generated TTS with Deepgram: {out_path}")
|
| 383 |
+
return response
|
| 384 |
+
except Exception as e:
|
| 385 |
+
print(f"Error generating TTS with Deepgram: {e}")
|
| 386 |
+
raise
|
| 387 |
+
|
| 388 |
+
|
| 389 |
# ββββββββββββββββββββββββ Public Entry Point ββββββββββββββββββοΏ½οΏ½
|
| 390 |
async def generate_slideshow_with_audio_async(topic: str, **kwargs):
|
| 391 |
"""
|