primerz commited on
Commit
069fe14
·
verified ·
1 Parent(s): 16dc50a

Update generator.py

Browse files
Files changed (1) hide show
  1. generator.py +31 -21
generator.py CHANGED
@@ -20,23 +20,30 @@ class Generator:
20
 
21
  # Generate lineart map
22
  lineart_map_raw = self.mh.lineart_anime_detector(image)
 
 
 
23
  # --- END MODIFIED ---
24
 
25
  # Manually resize maps to match the exact output resolution
26
  depth_map = depth_map_raw.resize((width, height), Image.LANCZOS)
27
  lineart_map = lineart_map_raw.resize((width, height), Image.LANCZOS)
 
28
 
29
- return depth_map, lineart_map
30
 
31
  def predict(
32
  self,
33
  input_image,
34
  user_prompt="",
 
35
  guidance_scale=1.5,
36
  num_inference_steps=6,
37
  img2img_strength=0.3,
38
  depth_strength=0.3,
39
- lineart_strength=0.3
 
 
40
  ):
41
  # 1. Pre-process Inputs
42
  print("Processing Input...")
@@ -58,39 +65,46 @@ class Generator:
58
  final_prompt = f"{Config.STYLE_TRIGGER}, {user_prompt}"
59
 
60
  print(f"Prompt: {final_prompt}")
 
61
 
62
  # 4. Generate Control Maps (Structure)
63
- print("Generating Control Maps (Depth, LineArt)...")
64
- depth_map, lineart_map = self.prepare_control_images(processed_image, target_width, target_height)
65
 
66
  # 5. Logic for Face vs No-Face
67
- # ControlNet order: [InstantID, Zoe, LineArt]
 
68
  if face_emb is not None:
69
  print("Face detected: Applying InstantID.")
70
- # Use strengths from UI
71
- controlnet_conditioning_scale = [0.6, depth_strength, lineart_strength]
72
- control_guidance_end = [0.3, 0.6, 0.6] # Stop InstantID early
73
- self.mh.pipeline.set_ip_adapter_scale(0.6) # Set IP-Adapter (likeness) strength
74
  else:
75
  print("No face detected: Disabling InstantID.")
76
- # Use strengths from UI, but keep InstantID at 0.0
77
- controlnet_conditioning_scale = [0.0, depth_strength, lineart_strength]
78
- control_guidance_end = [0.3, 0.6, 0.6]
79
  self.mh.pipeline.set_ip_adapter_scale(0.0)
80
 
81
  # --- START FIX for NoneType Error ---
82
- # Create a dummy tensor instead of passing None
83
- # Shape is (batch_size, embedding_dim)
84
  face_emb = torch.zeros((1, 512), dtype=Config.DTYPE, device=Config.DEVICE)
85
  # --- END FIX ---
86
 
 
 
 
 
 
 
 
87
  # 6. Run Inference
88
  print("Running pipeline...")
89
  result = self.mh.pipeline(
90
  prompt=final_prompt,
91
- image=processed_image, # Base image for Img2Img
92
- control_image=[processed_image, depth_map, lineart_map], # ControlNet inputs
93
- image_embeds=face_emb, # Face embedding (or dummy)
 
 
94
 
95
  # --- Parameters from UI ---
96
  strength=img2img_strength,
@@ -103,10 +117,6 @@ class Generator:
103
 
104
  clip_skip=2,
105
 
106
- # --- LoRA Strength REMOVED ---
107
- # No longer needed, as LoRA is fused into the model weights
108
- # cross_attention_kwargs={"scale": 1.25}
109
-
110
  ).images[0]
111
 
112
  return result
 
20
 
21
  # Generate lineart map
22
  lineart_map_raw = self.mh.lineart_anime_detector(image)
23
+
24
+ # Generate color map
25
+ color_map_raw = self.mh.color_detector(image)
26
  # --- END MODIFIED ---
27
 
28
  # Manually resize maps to match the exact output resolution
29
  depth_map = depth_map_raw.resize((width, height), Image.LANCZOS)
30
  lineart_map = lineart_map_raw.resize((width, height), Image.LANCZOS)
31
+ color_map = color_map_raw.resize((width, height), Image.LANCZOS)
32
 
33
+ return depth_map, lineart_map, color_map
34
 
35
  def predict(
36
  self,
37
  input_image,
38
  user_prompt="",
39
+ negative_prompt="", # <-- ADDED
40
  guidance_scale=1.5,
41
  num_inference_steps=6,
42
  img2img_strength=0.3,
43
  depth_strength=0.3,
44
+ lineart_strength=0.3,
45
+ color_strength=0.7, # <-- ADDED
46
+ seed=-1 # <-- ADDED
47
  ):
48
  # 1. Pre-process Inputs
49
  print("Processing Input...")
 
65
  final_prompt = f"{Config.STYLE_TRIGGER}, {user_prompt}"
66
 
67
  print(f"Prompt: {final_prompt}")
68
+ print(f"Negative Prompt: {negative_prompt}")
69
 
70
  # 4. Generate Control Maps (Structure)
71
+ print("Generating Control Maps (Depth, LineArt, Color)...")
72
+ depth_map, lineart_map, color_map = self.prepare_control_images(processed_image, target_width, target_height)
73
 
74
  # 5. Logic for Face vs No-Face
75
+ # --- MODIFIED: Added Color Control ---
76
+ # ControlNet order: [InstantID, Zoe, LineArt, Color]
77
  if face_emb is not None:
78
  print("Face detected: Applying InstantID.")
79
+ controlnet_conditioning_scale = [0.6, depth_strength, lineart_strength, color_strength]
80
+ control_guidance_end = [0.3, 0.6, 0.6, 0.9] # Stop InstantID early, let color run longer
81
+ self.mh.pipeline.set_ip_adapter_scale(0.6)
 
82
  else:
83
  print("No face detected: Disabling InstantID.")
84
+ controlnet_conditioning_scale = [0.0, depth_strength, lineart_strength, color_strength]
85
+ control_guidance_end = [0.3, 0.6, 0.6, 0.9]
 
86
  self.mh.pipeline.set_ip_adapter_scale(0.0)
87
 
88
  # --- START FIX for NoneType Error ---
 
 
89
  face_emb = torch.zeros((1, 512), dtype=Config.DTYPE, device=Config.DEVICE)
90
  # --- END FIX ---
91
 
92
+ # --- ADDED: Seed/Generator Logic ---
93
+ if seed == -1 or seed is None:
94
+ seed = torch.Generator().seed()
95
+ generator = torch.Generator(device=Config.DEVICE).manual_seed(int(seed))
96
+ print(f"Using seed: {seed}")
97
+ # --- END ADDED ---
98
+
99
  # 6. Run Inference
100
  print("Running pipeline...")
101
  result = self.mh.pipeline(
102
  prompt=final_prompt,
103
+ negative_prompt=negative_prompt, # <-- ADDED
104
+ image=processed_image,
105
+ control_image=[processed_image, depth_map, lineart_map, color_map], # <-- MODIFIED
106
+ image_embeds=face_emb,
107
+ generator=generator, # <-- ADDED
108
 
109
  # --- Parameters from UI ---
110
  strength=img2img_strength,
 
117
 
118
  clip_skip=2,
119
 
 
 
 
 
120
  ).images[0]
121
 
122
  return result