Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,13 +17,27 @@ from huggingface_hub import hf_hub_download
|
|
| 17 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 18 |
from diffusers import (
|
| 19 |
FluxPipeline,
|
| 20 |
-
FluxControlNetPipeline,
|
| 21 |
DDIMScheduler,
|
| 22 |
DPMSolverMultistepScheduler
|
| 23 |
)
|
| 24 |
import soundfile as sf
|
| 25 |
import requests
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
class ProfessionalCartoonFilmGenerator:
|
| 28 |
def __init__(self):
|
| 29 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
@@ -76,11 +90,43 @@ class ProfessionalCartoonFilmGenerator:
|
|
| 76 |
self.flux_pipe.enable_vae_slicing()
|
| 77 |
self.flux_pipe.enable_vae_tiling()
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
print("β
FLUX pipeline loaded successfully")
|
| 80 |
|
| 81 |
except Exception as e:
|
| 82 |
print(f"β FLUX pipeline failed: {e}")
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
try:
|
| 86 |
# 2. Advanced script generation model
|
|
@@ -356,17 +402,20 @@ class ProfessionalCartoonFilmGenerator:
|
|
| 356 |
character_images = {}
|
| 357 |
|
| 358 |
if not self.flux_pipe:
|
| 359 |
-
print("β
|
| 360 |
return character_images
|
| 361 |
|
| 362 |
for character in characters:
|
| 363 |
try:
|
| 364 |
print(f"π Generating professional character: {character['name']}")
|
| 365 |
|
| 366 |
-
# Load appropriate LoRA based on character type
|
| 367 |
-
if "anime" in character.get("animation_style", "").lower():
|
| 368 |
if hasattr(self, 'cartoon_lora'):
|
| 369 |
-
|
|
|
|
|
|
|
|
|
|
| 370 |
|
| 371 |
# Professional character prompt
|
| 372 |
prompt = f"""
|
|
@@ -382,15 +431,28 @@ class ProfessionalCartoonFilmGenerator:
|
|
| 382 |
inconsistent, amateur, simple, crude, manga, sketch
|
| 383 |
"""
|
| 384 |
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
|
| 395 |
char_path = f"{self.temp_dir}/character_{character['name'].replace(' ', '_')}.png"
|
| 396 |
image.save(char_path)
|
|
@@ -411,7 +473,7 @@ class ProfessionalCartoonFilmGenerator:
|
|
| 411 |
background_images = {}
|
| 412 |
|
| 413 |
if not self.flux_pipe:
|
| 414 |
-
print("β
|
| 415 |
return background_images
|
| 416 |
|
| 417 |
for scene in scenes:
|
|
@@ -432,15 +494,28 @@ class ProfessionalCartoonFilmGenerator:
|
|
| 432 |
blurry, simple, amateur, 3D render
|
| 433 |
"""
|
| 434 |
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 444 |
|
| 445 |
bg_path = f"{self.temp_dir}/background_scene_{scene['scene_number']}.png"
|
| 446 |
image.save(bg_path)
|
|
@@ -459,21 +534,36 @@ class ProfessionalCartoonFilmGenerator:
|
|
| 459 |
try:
|
| 460 |
print("π¬ Setting up Open-Sora 2.0 for video generation...")
|
| 461 |
|
| 462 |
-
#
|
| 463 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 464 |
subprocess.run([
|
| 465 |
"git", "clone", "https://github.com/hpcaitech/Open-Sora.git"
|
| 466 |
], check=True, capture_output=True)
|
| 467 |
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 477 |
return True
|
| 478 |
|
| 479 |
except Exception as e:
|
|
@@ -526,6 +616,14 @@ class ProfessionalCartoonFilmGenerator:
|
|
| 526 |
|
| 527 |
video_path = f"{self.temp_dir}/scene_{scene['scene_number']}.mp4"
|
| 528 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 529 |
# Run Open-Sora inference
|
| 530 |
cmd = [
|
| 531 |
"torchrun", "--nproc_per_node", "1", "--standalone",
|
|
@@ -538,7 +636,7 @@ class ProfessionalCartoonFilmGenerator:
|
|
| 538 |
"--motion-score", "6" # High motion for dynamic scenes
|
| 539 |
]
|
| 540 |
|
| 541 |
-
result = subprocess.run(cmd, capture_output=True, text=True, cwd=
|
| 542 |
|
| 543 |
if result.returncode == 0:
|
| 544 |
# Find generated video file
|
|
|
|
| 17 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 18 |
from diffusers import (
|
| 19 |
FluxPipeline,
|
|
|
|
| 20 |
DDIMScheduler,
|
| 21 |
DPMSolverMultistepScheduler
|
| 22 |
)
|
| 23 |
import soundfile as sf
|
| 24 |
import requests
|
| 25 |
|
| 26 |
+
# Optional imports for enhanced performance
|
| 27 |
+
try:
|
| 28 |
+
import flash_attn
|
| 29 |
+
FLASH_ATTN_AVAILABLE = True
|
| 30 |
+
except ImportError:
|
| 31 |
+
FLASH_ATTN_AVAILABLE = False
|
| 32 |
+
print("β οΈ Flash Attention not available - using standard attention")
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
import triton
|
| 36 |
+
TRITON_AVAILABLE = True
|
| 37 |
+
except ImportError:
|
| 38 |
+
TRITON_AVAILABLE = False
|
| 39 |
+
print("β οΈ Triton not available - using standard operations")
|
| 40 |
+
|
| 41 |
class ProfessionalCartoonFilmGenerator:
|
| 42 |
def __init__(self):
|
| 43 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 90 |
self.flux_pipe.enable_vae_slicing()
|
| 91 |
self.flux_pipe.enable_vae_tiling()
|
| 92 |
|
| 93 |
+
# Enable flash attention if available
|
| 94 |
+
if FLASH_ATTN_AVAILABLE:
|
| 95 |
+
try:
|
| 96 |
+
self.flux_pipe.enable_xformers_memory_efficient_attention()
|
| 97 |
+
print("β
Flash attention enabled for better performance")
|
| 98 |
+
except Exception as e:
|
| 99 |
+
print(f"β οΈ Flash attention failed: {e}")
|
| 100 |
+
else:
|
| 101 |
+
print("βΉοΈ Using standard attention (flash attention not available)")
|
| 102 |
+
|
| 103 |
print("β
FLUX pipeline loaded successfully")
|
| 104 |
|
| 105 |
except Exception as e:
|
| 106 |
print(f"β FLUX pipeline failed: {e}")
|
| 107 |
+
print("π Falling back to Stable Diffusion...")
|
| 108 |
+
|
| 109 |
+
# Fallback to Stable Diffusion
|
| 110 |
+
try:
|
| 111 |
+
from diffusers import StableDiffusionPipeline
|
| 112 |
+
self.flux_pipe = StableDiffusionPipeline.from_pretrained(
|
| 113 |
+
"runwayml/stable-diffusion-v1-5",
|
| 114 |
+
torch_dtype=torch.float16,
|
| 115 |
+
use_safetensors=True,
|
| 116 |
+
safety_checker=None,
|
| 117 |
+
requires_safety_checker=False
|
| 118 |
+
).to(self.device)
|
| 119 |
+
|
| 120 |
+
# Enable memory optimizations
|
| 121 |
+
self.flux_pipe.enable_vae_slicing()
|
| 122 |
+
if hasattr(self.flux_pipe, 'enable_vae_tiling'):
|
| 123 |
+
self.flux_pipe.enable_vae_tiling()
|
| 124 |
+
|
| 125 |
+
print("β
Stable Diffusion fallback loaded successfully")
|
| 126 |
+
|
| 127 |
+
except Exception as e2:
|
| 128 |
+
print(f"β Stable Diffusion fallback also failed: {e2}")
|
| 129 |
+
self.flux_pipe = None
|
| 130 |
|
| 131 |
try:
|
| 132 |
# 2. Advanced script generation model
|
|
|
|
| 402 |
character_images = {}
|
| 403 |
|
| 404 |
if not self.flux_pipe:
|
| 405 |
+
print("β No image generation pipeline available")
|
| 406 |
return character_images
|
| 407 |
|
| 408 |
for character in characters:
|
| 409 |
try:
|
| 410 |
print(f"π Generating professional character: {character['name']}")
|
| 411 |
|
| 412 |
+
# Load appropriate LoRA based on character type (only for FLUX)
|
| 413 |
+
if hasattr(self.flux_pipe, 'load_lora_weights') and "anime" in character.get("animation_style", "").lower():
|
| 414 |
if hasattr(self, 'cartoon_lora'):
|
| 415 |
+
try:
|
| 416 |
+
self.flux_pipe.load_lora_weights(self.cartoon_lora)
|
| 417 |
+
except Exception as e:
|
| 418 |
+
print(f"β οΈ LoRA loading failed: {e}")
|
| 419 |
|
| 420 |
# Professional character prompt
|
| 421 |
prompt = f"""
|
|
|
|
| 431 |
inconsistent, amateur, simple, crude, manga, sketch
|
| 432 |
"""
|
| 433 |
|
| 434 |
+
# Handle different pipeline types
|
| 435 |
+
if hasattr(self.flux_pipe, 'max_sequence_length'):
|
| 436 |
+
# FLUX pipeline
|
| 437 |
+
image = self.flux_pipe(
|
| 438 |
+
prompt=prompt,
|
| 439 |
+
negative_prompt=negative_prompt,
|
| 440 |
+
num_inference_steps=25, # High quality steps
|
| 441 |
+
guidance_scale=3.5,
|
| 442 |
+
height=1024, # High resolution
|
| 443 |
+
width=1024,
|
| 444 |
+
max_sequence_length=256
|
| 445 |
+
).images[0]
|
| 446 |
+
else:
|
| 447 |
+
# Stable Diffusion pipeline
|
| 448 |
+
image = self.flux_pipe(
|
| 449 |
+
prompt=prompt,
|
| 450 |
+
negative_prompt=negative_prompt,
|
| 451 |
+
num_inference_steps=25, # High quality steps
|
| 452 |
+
guidance_scale=7.5,
|
| 453 |
+
height=1024, # High resolution
|
| 454 |
+
width=1024
|
| 455 |
+
).images[0]
|
| 456 |
|
| 457 |
char_path = f"{self.temp_dir}/character_{character['name'].replace(' ', '_')}.png"
|
| 458 |
image.save(char_path)
|
|
|
|
| 473 |
background_images = {}
|
| 474 |
|
| 475 |
if not self.flux_pipe:
|
| 476 |
+
print("β No image generation pipeline available")
|
| 477 |
return background_images
|
| 478 |
|
| 479 |
for scene in scenes:
|
|
|
|
| 494 |
blurry, simple, amateur, 3D render
|
| 495 |
"""
|
| 496 |
|
| 497 |
+
# Handle different pipeline types for backgrounds
|
| 498 |
+
if hasattr(self.flux_pipe, 'max_sequence_length'):
|
| 499 |
+
# FLUX pipeline
|
| 500 |
+
image = self.flux_pipe(
|
| 501 |
+
prompt=prompt,
|
| 502 |
+
negative_prompt=negative_prompt,
|
| 503 |
+
num_inference_steps=20,
|
| 504 |
+
guidance_scale=3.0,
|
| 505 |
+
height=768, # 4:3 aspect ratio for traditional animation
|
| 506 |
+
width=1024,
|
| 507 |
+
max_sequence_length=256
|
| 508 |
+
).images[0]
|
| 509 |
+
else:
|
| 510 |
+
# Stable Diffusion pipeline
|
| 511 |
+
image = self.flux_pipe(
|
| 512 |
+
prompt=prompt,
|
| 513 |
+
negative_prompt=negative_prompt,
|
| 514 |
+
num_inference_steps=20,
|
| 515 |
+
guidance_scale=7.0,
|
| 516 |
+
height=768, # 4:3 aspect ratio for traditional animation
|
| 517 |
+
width=1024
|
| 518 |
+
).images[0]
|
| 519 |
|
| 520 |
bg_path = f"{self.temp_dir}/background_scene_{scene['scene_number']}.png"
|
| 521 |
image.save(bg_path)
|
|
|
|
| 534 |
try:
|
| 535 |
print("π¬ Setting up Open-Sora 2.0 for video generation...")
|
| 536 |
|
| 537 |
+
# Check if we're already in the right directory
|
| 538 |
+
current_dir = os.getcwd()
|
| 539 |
+
opensora_dir = os.path.join(current_dir, "Open-Sora")
|
| 540 |
+
|
| 541 |
+
# Clone Open-Sora repository if it doesn't exist
|
| 542 |
+
if not os.path.exists(opensora_dir):
|
| 543 |
+
print("π₯ Cloning Open-Sora repository...")
|
| 544 |
subprocess.run([
|
| 545 |
"git", "clone", "https://github.com/hpcaitech/Open-Sora.git"
|
| 546 |
], check=True, capture_output=True)
|
| 547 |
|
| 548 |
+
# Check if the repository was cloned successfully
|
| 549 |
+
if not os.path.exists(opensora_dir):
|
| 550 |
+
print("β Failed to clone Open-Sora repository")
|
| 551 |
+
return False
|
| 552 |
+
|
| 553 |
+
# Check if model weights exist
|
| 554 |
+
ckpts_dir = os.path.join(opensora_dir, "ckpts")
|
| 555 |
+
if not os.path.exists(ckpts_dir):
|
| 556 |
+
print("π₯ Downloading Open-Sora 2.0 model...")
|
| 557 |
+
try:
|
| 558 |
+
subprocess.run([
|
| 559 |
+
"huggingface-cli", "download", "hpcai-tech/Open-Sora-v2",
|
| 560 |
+
"--local-dir", ckpts_dir
|
| 561 |
+
], check=True, capture_output=True)
|
| 562 |
+
except Exception as e:
|
| 563 |
+
print(f"β Model download failed: {e}")
|
| 564 |
+
return False
|
| 565 |
+
|
| 566 |
+
print("β
Open-Sora setup completed")
|
| 567 |
return True
|
| 568 |
|
| 569 |
except Exception as e:
|
|
|
|
| 616 |
|
| 617 |
video_path = f"{self.temp_dir}/scene_{scene['scene_number']}.mp4"
|
| 618 |
|
| 619 |
+
# Get the correct Open-Sora directory
|
| 620 |
+
current_dir = os.getcwd()
|
| 621 |
+
opensora_dir = os.path.join(current_dir, "Open-Sora")
|
| 622 |
+
|
| 623 |
+
if not os.path.exists(opensora_dir):
|
| 624 |
+
print("β Open-Sora directory not found")
|
| 625 |
+
return None
|
| 626 |
+
|
| 627 |
# Run Open-Sora inference
|
| 628 |
cmd = [
|
| 629 |
"torchrun", "--nproc_per_node", "1", "--standalone",
|
|
|
|
| 636 |
"--motion-score", "6" # High motion for dynamic scenes
|
| 637 |
]
|
| 638 |
|
| 639 |
+
result = subprocess.run(cmd, capture_output=True, text=True, cwd=opensora_dir)
|
| 640 |
|
| 641 |
if result.returncode == 0:
|
| 642 |
# Find generated video file
|