primerz commited on
Commit
38897a0
·
verified ·
1 Parent(s): dbceb5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -13
app.py CHANGED
@@ -15,19 +15,35 @@ import cv2
15
  from transformers import pipeline as transformers_pipeline
16
  from huggingface_hub import hf_hub_download
17
  import os
 
18
 
19
  # Configuration
20
  MODEL_REPO = "primerz/pixagram"
21
- device = "cuda" if torch.cuda.is_available() else "cpu"
22
- dtype = torch.float16 if device == "cuda" else torch.float32
23
 
24
- print(f"Using device: {device}")
25
- print(f"Loading models from: {MODEL_REPO}")
 
26
 
27
  class RetroArtConverter:
28
  def __init__(self):
29
- self.device = device
30
- self.dtype = dtype
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  # Initialize face analysis for InstantID (optional)
33
  print("Loading face analysis model...")
@@ -78,7 +94,8 @@ class RetroArtConverter:
78
  print("Loading depth estimator...")
79
  self.depth_estimator = transformers_pipeline(
80
  'depth-estimation',
81
- model="Intel/dpt-hybrid-midas"
 
82
  )
83
 
84
  # Load SDXL checkpoint from HuggingFace Hub
@@ -108,7 +125,7 @@ class RetroArtConverter:
108
  use_safetensors=True
109
  ).to(self.device)
110
 
111
- # Load LORA from HuggingFace Hub
112
  print("Loading LORA (retroart) from HuggingFace Hub...")
113
  try:
114
  lora_path = hf_hub_download(
@@ -126,19 +143,25 @@ class RetroArtConverter:
126
  self.pipe.scheduler = DPMSolverMultistepScheduler.from_config(
127
  self.pipe.scheduler.config
128
  )
129
- self.pipe.enable_model_cpu_offload()
 
 
 
130
  self.pipe.enable_vae_slicing()
131
 
132
  # Enable attention slicing for memory efficiency
133
  self.pipe.unet.set_attn_processor(AttnProcessor2_0())
134
 
135
- if hasattr(self.pipe, 'enable_xformers_memory_efficient_attention'):
 
136
  try:
137
  self.pipe.enable_xformers_memory_efficient_attention()
 
138
  except Exception as e:
139
- print(f"xformers not available: {e}")
140
 
141
- print("Model initialization complete!")
 
142
 
143
  def get_depth_map(self, image):
144
  """Generate depth map from input image"""
@@ -199,6 +222,9 @@ class RetroArtConverter:
199
  ):
200
  """Main generation function"""
201
 
 
 
 
202
  # Resize image maintaining aspect ratio
203
  original_width, original_height = input_image.size
204
  target_width, target_height = self.calculate_target_size(original_width, original_height)
@@ -244,7 +270,8 @@ class RetroArtConverter:
244
  print("Initializing RetroArt Converter...")
245
  converter = RetroArtConverter()
246
 
247
- # Gradio interface
 
248
  def process_image(
249
  image,
250
  prompt,
 
15
  from transformers import pipeline as transformers_pipeline
16
  from huggingface_hub import hf_hub_download
17
  import os
18
+ import spaces
19
 
20
  # Configuration
21
  MODEL_REPO = "primerz/pixagram"
 
 
22
 
23
+ # Note: For ZeroGPU, device detection happens dynamically
24
+ # We'll set device inside GPU-decorated functions
25
+ print("Using ZeroGPU - GPU will be allocated on-demand")
26
 
27
  class RetroArtConverter:
28
  def __init__(self):
29
+ self.models_loaded = False
30
+ self.device = None
31
+ self.dtype = None
32
+ self.face_detection_enabled = False
33
+ print("RetroArtConverter initialized - models will load on first generation")
34
+
35
+ def _initialize_models(self):
36
+ """Lazy model initialization - called on first generation when GPU is available"""
37
+ if self.models_loaded:
38
+ return
39
+
40
+ print("Initializing models...")
41
+ print(f"Loading models from: {MODEL_REPO}")
42
+
43
+ # Set device (will be cuda when called from GPU-decorated function)
44
+ self.device = "cuda" if torch.cuda.is_available() else "cpu"
45
+ self.dtype = torch.float16 if self.device == "cuda" else torch.float32
46
+ print(f"Using device: {self.device}")
47
 
48
  # Initialize face analysis for InstantID (optional)
49
  print("Loading face analysis model...")
 
94
  print("Loading depth estimator...")
95
  self.depth_estimator = transformers_pipeline(
96
  'depth-estimation',
97
+ model="Intel/dpt-hybrid-midas",
98
+ device=self.device if self.device == "cuda" else -1
99
  )
100
 
101
  # Load SDXL checkpoint from HuggingFace Hub
 
125
  use_safetensors=True
126
  ).to(self.device)
127
 
128
+ # Load LORA from HuggingFace Hub (requires PEFT)
129
  print("Loading LORA (retroart) from HuggingFace Hub...")
130
  try:
131
  lora_path = hf_hub_download(
 
143
  self.pipe.scheduler = DPMSolverMultistepScheduler.from_config(
144
  self.pipe.scheduler.config
145
  )
146
+
147
+ # For ZeroGPU, we don't use model_cpu_offload
148
+ # self.pipe.enable_model_cpu_offload()
149
+
150
  self.pipe.enable_vae_slicing()
151
 
152
  # Enable attention slicing for memory efficiency
153
  self.pipe.unet.set_attn_processor(AttnProcessor2_0())
154
 
155
+ # Try to enable xformers if available (only works on GPU)
156
+ if self.device == "cuda":
157
  try:
158
  self.pipe.enable_xformers_memory_efficient_attention()
159
+ print("✓ xformers enabled")
160
  except Exception as e:
161
+ print(f"⚠️ xformers not available: {e}")
162
 
163
+ self.models_loaded = True
164
+ print("✓ Model initialization complete!")
165
 
166
  def get_depth_map(self, image):
167
  """Generate depth map from input image"""
 
222
  ):
223
  """Main generation function"""
224
 
225
+ # Initialize models on first run (lazy loading for ZeroGPU)
226
+ self._initialize_models()
227
+
228
  # Resize image maintaining aspect ratio
229
  original_width, original_height = input_image.size
230
  target_width, target_height = self.calculate_target_size(original_width, original_height)
 
270
  print("Initializing RetroArt Converter...")
271
  converter = RetroArtConverter()
272
 
273
+ # Gradio interface with ZeroGPU support
274
+ @spaces.GPU
275
  def process_image(
276
  image,
277
  prompt,