Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -359,83 +359,79 @@ def generate_all_scenes(prompts_text):
|
|
359 |
|
360 |
@spaces.GPU(duration=60)
|
361 |
def add_text_to_scenes(gallery_images, prompts_text):
|
362 |
-
"""Add text
|
|
|
|
|
|
|
363 |
if not isinstance(gallery_images, list):
|
364 |
-
print("
|
365 |
return [], []
|
366 |
-
|
367 |
clear_memory()
|
368 |
|
|
|
369 |
sections = prompts_text.split('='*50)
|
370 |
overlaid_images = []
|
371 |
output_files = []
|
372 |
|
|
|
373 |
temp_dir = "temp_book_pages"
|
374 |
os.makedirs(temp_dir, exist_ok=True)
|
375 |
|
376 |
-
for i, (
|
377 |
if not section.strip():
|
378 |
continue
|
379 |
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
if paragraph and image_data is not None:
|
389 |
-
try:
|
390 |
-
print(f"Processing image {i+1}, type: {type(image_data)}")
|
391 |
-
print(f"Image data: {str(image_data)[:100]}...") # Print first 100 chars
|
392 |
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
try:
|
397 |
-
import ast
|
398 |
-
# Convert string representation of array to actual array
|
399 |
-
array_data = ast.literal_eval(image_data)
|
400 |
-
image = Image.fromarray(np.array(array_data, dtype=np.uint8))
|
401 |
-
except:
|
402 |
-
print(f"Failed to convert string to array for image {i+1}")
|
403 |
-
continue
|
404 |
-
elif isinstance(image_data, tuple):
|
405 |
-
# Handle gallery tuple format (image, label)
|
406 |
-
image_data = image_data[0]
|
407 |
-
if isinstance(image_data, np.ndarray):
|
408 |
-
image = Image.fromarray(image_data)
|
409 |
-
else:
|
410 |
-
print(f"Unexpected tuple data type: {type(image_data)}")
|
411 |
-
continue
|
412 |
-
elif isinstance(image_data, np.ndarray):
|
413 |
-
image = Image.fromarray(image_data)
|
414 |
-
elif isinstance(image_data, Image.Image):
|
415 |
-
image = image_data
|
416 |
else:
|
417 |
-
print(f"
|
418 |
continue
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
424 |
overlaid_img = overlay_text_on_image(image, paragraph)
|
425 |
if overlaid_img is not None:
|
|
|
426 |
overlaid_array = np.array(overlaid_img)
|
427 |
overlaid_images.append(overlaid_array)
|
428 |
|
|
|
429 |
output_path = os.path.join(temp_dir, f"panel_{i+1}.png")
|
430 |
overlaid_img.save(output_path)
|
431 |
output_files.append(output_path)
|
432 |
print(f"Successfully processed image {i+1}")
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
|
|
|
|
439 |
|
440 |
if not overlaid_images:
|
441 |
print("No images were successfully processed")
|
@@ -446,7 +442,7 @@ def add_text_to_scenes(gallery_images, prompts_text):
|
|
446 |
return overlaid_images, output_files
|
447 |
|
448 |
def overlay_text_on_image(image, text):
|
449 |
-
"""
|
450 |
if image is None:
|
451 |
return None
|
452 |
|
@@ -460,6 +456,7 @@ def overlay_text_on_image(image, text):
|
|
460 |
try:
|
461 |
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", font_size)
|
462 |
except:
|
|
|
463 |
font = ImageFont.load_default()
|
464 |
|
465 |
# Calculate text positioning
|
|
|
359 |
|
360 |
@spaces.GPU(duration=60)
|
361 |
def add_text_to_scenes(gallery_images, prompts_text):
|
362 |
+
"""Add text overlays to all scenes"""
|
363 |
+
print(f"Received gallery_images type: {type(gallery_images)}")
|
364 |
+
print(f"Number of images in gallery: {len(gallery_images) if isinstance(gallery_images, list) else 0}")
|
365 |
+
|
366 |
if not isinstance(gallery_images, list):
|
367 |
+
print("Gallery images must be a list")
|
368 |
return [], []
|
369 |
+
|
370 |
clear_memory()
|
371 |
|
372 |
+
# Process text sections
|
373 |
sections = prompts_text.split('='*50)
|
374 |
overlaid_images = []
|
375 |
output_files = []
|
376 |
|
377 |
+
# Create temporary directory for saving files
|
378 |
temp_dir = "temp_book_pages"
|
379 |
os.makedirs(temp_dir, exist_ok=True)
|
380 |
|
381 |
+
for i, (img_data, section) in enumerate(zip(gallery_images, sections)):
|
382 |
if not section.strip():
|
383 |
continue
|
384 |
|
385 |
+
print(f"\nProcessing image {i+1}:")
|
386 |
+
print(f"Image data type: {type(img_data)}")
|
387 |
+
|
388 |
+
try:
|
389 |
+
# Handle tuple from Gradio gallery
|
390 |
+
if isinstance(img_data, tuple):
|
391 |
+
filepath = img_data[0] if isinstance(img_data[0], str) else None
|
392 |
+
print(f"Found filepath: {filepath}")
|
|
|
|
|
|
|
|
|
393 |
|
394 |
+
if filepath and os.path.exists(filepath):
|
395 |
+
print(f"Loading image from: {filepath}")
|
396 |
+
image = Image.open(filepath).convert('RGB')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
397 |
else:
|
398 |
+
print(f"Invalid filepath: {filepath}")
|
399 |
continue
|
400 |
+
else:
|
401 |
+
print(f"Unexpected image data type: {type(img_data)}")
|
402 |
+
continue
|
403 |
+
|
404 |
+
# Extract paragraph text
|
405 |
+
lines = [line.strip() for line in section.split('\n') if line.strip()]
|
406 |
+
paragraph = None
|
407 |
+
for j, line in enumerate(lines):
|
408 |
+
if line.startswith('Paragraph'):
|
409 |
+
if j + 1 < len(lines):
|
410 |
+
paragraph = lines[j + 1]
|
411 |
+
print(f"Found paragraph text for image {i+1}")
|
412 |
+
break
|
413 |
+
|
414 |
+
if paragraph and image:
|
415 |
+
# Add text overlay
|
416 |
overlaid_img = overlay_text_on_image(image, paragraph)
|
417 |
if overlaid_img is not None:
|
418 |
+
# Convert to numpy array for gallery display
|
419 |
overlaid_array = np.array(overlaid_img)
|
420 |
overlaid_images.append(overlaid_array)
|
421 |
|
422 |
+
# Save file for download
|
423 |
output_path = os.path.join(temp_dir, f"panel_{i+1}.png")
|
424 |
overlaid_img.save(output_path)
|
425 |
output_files.append(output_path)
|
426 |
print(f"Successfully processed image {i+1}")
|
427 |
+
else:
|
428 |
+
print(f"Failed to overlay text on image {i+1}")
|
429 |
+
|
430 |
+
except Exception as e:
|
431 |
+
print(f"Error processing image {i+1}: {str(e)}")
|
432 |
+
import traceback
|
433 |
+
print(traceback.format_exc())
|
434 |
+
continue
|
435 |
|
436 |
if not overlaid_images:
|
437 |
print("No images were successfully processed")
|
|
|
442 |
return overlaid_images, output_files
|
443 |
|
444 |
def overlay_text_on_image(image, text):
|
445 |
+
"""Add black text with white outline for better visibility."""
|
446 |
if image is None:
|
447 |
return None
|
448 |
|
|
|
456 |
try:
|
457 |
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", font_size)
|
458 |
except:
|
459 |
+
print("Using default font as DejaVuSans-Bold.ttf not found")
|
460 |
font = ImageFont.load_default()
|
461 |
|
462 |
# Calculate text positioning
|