Update app.py
Browse files
app.py
CHANGED
@@ -45,8 +45,12 @@ class ProfessionalCartoonFilmGenerator:
|
|
45 |
|
46 |
# Model configurations for ZeroGPU optimization
|
47 |
self.models_loaded = False
|
|
|
48 |
self.flux_pipe = None
|
49 |
self.script_enhancer = None
|
|
|
|
|
|
|
50 |
|
51 |
@spaces.GPU
|
52 |
def load_models(self):
|
@@ -57,7 +61,7 @@ class ProfessionalCartoonFilmGenerator:
|
|
57 |
print("π Loading professional-grade models...")
|
58 |
|
59 |
try:
|
60 |
-
# 1. FLUX pipeline
|
61 |
print("π¨ Loading FLUX pipeline...")
|
62 |
try:
|
63 |
self.flux_pipe = FluxPipeline.from_pretrained(
|
@@ -66,6 +70,8 @@ class ProfessionalCartoonFilmGenerator:
|
|
66 |
variant="fp16",
|
67 |
use_safetensors=True
|
68 |
).to(self.device)
|
|
|
|
|
69 |
except Exception as flux_error:
|
70 |
if "401" in str(flux_error) or "authentication" in str(flux_error).lower():
|
71 |
print("π FLUX authentication failed - model requires Hugging Face token")
|
@@ -74,11 +80,16 @@ class ProfessionalCartoonFilmGenerator:
|
|
74 |
print(" 2. Accept the FLUX model license at https://huggingface.co/black-forest-labs/FLUX.1-dev")
|
75 |
print(" 3. Set your token: huggingface-cli login")
|
76 |
print("π Falling back to Stable Diffusion...")
|
77 |
-
|
78 |
else:
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
82 |
print("π Loading cartoon LoRA models...")
|
83 |
try:
|
84 |
# Load multiple LoRA models for different purposes
|
@@ -98,27 +109,23 @@ class ProfessionalCartoonFilmGenerator:
|
|
98 |
except Exception as e:
|
99 |
print(f"β οΈ Some LoRA models failed to load: {e}")
|
100 |
|
101 |
-
# Enable memory optimizations
|
102 |
-
self.flux_pipe
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
print("β
FLUX pipeline loaded successfully")
|
116 |
-
|
117 |
-
except Exception as e:
|
118 |
-
print(f"β FLUX pipeline failed: {e}")
|
119 |
-
print("π Falling back to Stable Diffusion...")
|
120 |
|
121 |
-
|
|
|
122 |
try:
|
123 |
from diffusers import StableDiffusionPipeline
|
124 |
print("π Loading Stable Diffusion fallback model...")
|
@@ -204,17 +211,18 @@ class ProfessionalCartoonFilmGenerator:
|
|
204 |
def create_download_url(self, file_path: str, file_type: str = "file") -> str:
|
205 |
"""Create a download URL for generated content"""
|
206 |
try:
|
207 |
-
# For Hugging Face Spaces, we can create a simple download link
|
208 |
-
# In a real deployment, this would point to your actual file hosting service
|
209 |
file_name = os.path.basename(file_path)
|
|
|
210 |
|
211 |
-
#
|
212 |
-
#
|
213 |
-
|
214 |
-
|
215 |
-
|
|
|
|
|
216 |
|
217 |
-
return
|
218 |
|
219 |
except Exception as e:
|
220 |
print(f"β οΈ Failed to create download URL: {e}")
|
|
|
45 |
|
46 |
# Model configurations for ZeroGPU optimization
|
47 |
self.models_loaded = False
|
48 |
+
self.using_flux = False
|
49 |
self.flux_pipe = None
|
50 |
self.script_enhancer = None
|
51 |
+
self.cartoon_lora = None
|
52 |
+
self.character_lora = None
|
53 |
+
self.sketch_lora = None
|
54 |
|
55 |
@spaces.GPU
|
56 |
def load_models(self):
|
|
|
61 |
print("π Loading professional-grade models...")
|
62 |
|
63 |
try:
|
64 |
+
# 1. Try FLUX pipeline first (if user has authentication)
|
65 |
print("π¨ Loading FLUX pipeline...")
|
66 |
try:
|
67 |
self.flux_pipe = FluxPipeline.from_pretrained(
|
|
|
70 |
variant="fp16",
|
71 |
use_safetensors=True
|
72 |
).to(self.device)
|
73 |
+
print("β
FLUX pipeline loaded successfully!")
|
74 |
+
self.using_flux = True
|
75 |
except Exception as flux_error:
|
76 |
if "401" in str(flux_error) or "authentication" in str(flux_error).lower():
|
77 |
print("π FLUX authentication failed - model requires Hugging Face token")
|
|
|
80 |
print(" 2. Accept the FLUX model license at https://huggingface.co/black-forest-labs/FLUX.1-dev")
|
81 |
print(" 3. Set your token: huggingface-cli login")
|
82 |
print("π Falling back to Stable Diffusion...")
|
83 |
+
self.using_flux = False
|
84 |
else:
|
85 |
+
print(f"β FLUX loading failed: {flux_error}")
|
86 |
+
self.using_flux = False
|
87 |
+
except Exception as e:
|
88 |
+
print(f"β FLUX pipeline failed: {e}")
|
89 |
+
self.using_flux = False
|
90 |
+
|
91 |
+
# Load cartoon/anime LoRA for character generation (only if FLUX is available)
|
92 |
+
if self.using_flux:
|
93 |
print("π Loading cartoon LoRA models...")
|
94 |
try:
|
95 |
# Load multiple LoRA models for different purposes
|
|
|
109 |
except Exception as e:
|
110 |
print(f"β οΈ Some LoRA models failed to load: {e}")
|
111 |
|
112 |
+
# Enable memory optimizations for FLUX
|
113 |
+
if self.flux_pipe:
|
114 |
+
self.flux_pipe.enable_vae_slicing()
|
115 |
+
self.flux_pipe.enable_vae_tiling()
|
116 |
+
|
117 |
+
# Enable flash attention if available
|
118 |
+
if FLASH_ATTN_AVAILABLE:
|
119 |
+
try:
|
120 |
+
self.flux_pipe.enable_xformers_memory_efficient_attention()
|
121 |
+
print("β
Flash attention enabled for better performance")
|
122 |
+
except Exception as e:
|
123 |
+
print(f"β οΈ Flash attention failed: {e}")
|
124 |
+
else:
|
125 |
+
print("βΉοΈ Using standard attention (flash attention not available)")
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
+
# Load Stable Diffusion fallback if FLUX is not available
|
128 |
+
if not self.using_flux:
|
129 |
try:
|
130 |
from diffusers import StableDiffusionPipeline
|
131 |
print("π Loading Stable Diffusion fallback model...")
|
|
|
211 |
def create_download_url(self, file_path: str, file_type: str = "file") -> str:
|
212 |
"""Create a download URL for generated content"""
|
213 |
try:
|
|
|
|
|
214 |
file_name = os.path.basename(file_path)
|
215 |
+
file_size = os.path.getsize(file_path) / (1024*1024)
|
216 |
|
217 |
+
# For Hugging Face Spaces, create a download info block
|
218 |
+
# Note: Replace 'your-username' and 'your-space-name' with your actual Space info
|
219 |
+
download_info = f"π₯ Download {file_type}: {file_name}"
|
220 |
+
download_info += f"\n π Local path: {file_path}"
|
221 |
+
download_info += f"\n π File size: {file_size:.1f} MB"
|
222 |
+
download_info += f"\n π Space URL: https://huggingface.co/spaces/your-username/your-space-name"
|
223 |
+
download_info += f"\n π‘ To download: Return this file as a Gradio output component"
|
224 |
|
225 |
+
return download_info
|
226 |
|
227 |
except Exception as e:
|
228 |
print(f"β οΈ Failed to create download URL: {e}")
|