Munaf1987 commited on
Commit
fe8b3a0
Β·
verified Β·
1 Parent(s): 9ea03ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -33
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 for superior image generation
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
- raise flux_error
78
  else:
79
- raise flux_error
80
-
81
- # Load cartoon/anime LoRA for character generation
 
 
 
 
 
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.enable_vae_slicing()
103
- self.flux_pipe.enable_vae_tiling()
104
-
105
- # Enable flash attention if available
106
- if FLASH_ATTN_AVAILABLE:
107
- try:
108
- self.flux_pipe.enable_xformers_memory_efficient_attention()
109
- print("βœ… Flash attention enabled for better performance")
110
- except Exception as e:
111
- print(f"⚠️ Flash attention failed: {e}")
112
- else:
113
- print("ℹ️ Using standard attention (flash attention not available)")
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
- # Fallback to Stable Diffusion
 
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
- # Create a simple download URL format
212
- # In production, replace this with your actual file hosting URL
213
- download_url = f"πŸ“₯ Download {file_type}: {file_name}"
214
- download_url += f"\n πŸ“ Local path: {file_path}"
215
- download_url += f"\n πŸ“Š File size: {os.path.getsize(file_path) / (1024*1024):.1f} MB"
 
 
216
 
217
- return download_url
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}")