Mccscs2 commited on
Commit
a69f969
·
verified ·
1 Parent(s): fff1b72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -29
app.py CHANGED
@@ -19,14 +19,14 @@ pipe_t2i = DiffusionPipeline.from_pretrained(
19
  )
20
  pipe_t2i.to("cuda")
21
 
22
- # Image to image - FLUX
23
  pipe_flux = FluxImg2ImgPipeline.from_pretrained(
24
  "black-forest-labs/FLUX.1-schnell",
25
  torch_dtype=torch.bfloat16,
26
  )
27
  pipe_flux.to("cuda")
28
 
29
- # Image to image - InstructPix2Pix
30
  pipe_ip2p = StableDiffusionInstructPix2PixPipeline.from_pretrained(
31
  "timbrooks/instruct-pix2pix",
32
  torch_dtype=torch.float16,
@@ -34,16 +34,52 @@ pipe_ip2p = StableDiffusionInstructPix2PixPipeline.from_pretrained(
34
  )
35
  pipe_ip2p.to("cuda")
36
 
37
- # Image to image - SDXL
38
- pipe_sdxl = StableDiffusionXLImg2ImgPipeline.from_pretrained(
39
- "stabilityai/stable-diffusion-xl-base-1.0",
40
- torch_dtype=torch.float16,
41
- variant="fp16",
42
- use_safetensors=True,
43
- )
44
- pipe_sdxl.to("cuda")
45
 
46
- print("Pipelines loaded!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  def resize_image(image, max_size=1024):
49
  orig_w, orig_h = image.size
@@ -101,8 +137,10 @@ def generate_i2i(model_choice, input_image, prompt, strength, num_inference_step
101
  generator=generator,
102
  ).images[0]
103
 
104
- elif model_choice == "SDXL (Balanced quality & control)":
105
- image = pipe_sdxl(
 
 
106
  prompt=prompt,
107
  image=input_image,
108
  strength=float(strength),
@@ -134,6 +172,11 @@ custom_theme = gr.themes.Soft(
134
  block_title_text_weight="600",
135
  )
136
 
 
 
 
 
 
137
  with gr.Blocks(fill_height=True) as demo:
138
  gr.Markdown(
139
  """
@@ -180,30 +223,23 @@ with gr.Blocks(fill_height=True) as demo:
180
  with gr.Tab("🖼️ Image to Image"):
181
  with gr.Row(equal_height=False):
182
  with gr.Column(scale=1, min_width=320):
183
-
184
  model_choice = gr.Radio(
185
- choices=[
186
- "FLUX.1-schnell (Creative, high change)",
187
- "InstructPix2Pix (Precise, preserves identity)",
188
- "SDXL (Balanced quality & control)",
189
- ],
190
  value="InstructPix2Pix (Precise, preserves identity)",
191
  label="🤖 Model",
192
- info="InstructPix2Pix: best for targeted edits like colours/styles. SDXL: balanced quality and control. FLUX: creative transformations."
193
  )
194
-
195
  i2i_input = gr.Image(label="Upload Image", type="pil")
196
  i2i_prompt = gr.Textbox(
197
  label="✨ Edit Instruction",
198
- placeholder="e.g. 'change the dress to blue' or 'make it a sunset'",
199
  lines=4,
200
  )
201
-
202
  with gr.Accordion("⚙️ Advanced Settings", open=False):
203
- i2i_strength = gr.Slider(0.1, 1.0, value=0.75, step=0.05,
204
- label="Strength (FLUX & SDXL only)",
205
- info="Higher = more change. Not used by InstructPix2Pix.")
206
- i2i_steps = gr.Slider(1, 50, value=20, step=1, label="Inference Steps")
207
  with gr.Row():
208
  i2i_randomize = gr.Checkbox(label="🎲 Random Seed", value=True)
209
  i2i_seed = gr.Number(label="Seed", value=42, precision=0, visible=False)
@@ -211,7 +247,6 @@ with gr.Blocks(fill_height=True) as demo:
211
  lambda r: gr.Number(visible=not r),
212
  inputs=[i2i_randomize], outputs=[i2i_seed]
213
  )
214
-
215
  i2i_btn = gr.Button("🚀 Edit Image", variant="primary", size="lg")
216
 
217
  with gr.Column(scale=1, min_width=320):
@@ -229,7 +264,7 @@ with gr.Blocks(fill_height=True) as demo:
229
  ---
230
  <div style="text-align: center; opacity: 0.7; font-size: 0.9em;">
231
  <strong>T2I:</strong> Tongyi-MAI/Z-Image-Turbo •
232
- <strong>I2I:</strong> FLUX.1-schnell + InstructPix2Pix + SDXL
233
  </div>
234
  """
235
  )
 
19
  )
20
  pipe_t2i.to("cuda")
21
 
22
+ # FLUX
23
  pipe_flux = FluxImg2ImgPipeline.from_pretrained(
24
  "black-forest-labs/FLUX.1-schnell",
25
  torch_dtype=torch.bfloat16,
26
  )
27
  pipe_flux.to("cuda")
28
 
29
+ # InstructPix2Pix
30
  pipe_ip2p = StableDiffusionInstructPix2PixPipeline.from_pretrained(
31
  "timbrooks/instruct-pix2pix",
32
  torch_dtype=torch.float16,
 
34
  )
35
  pipe_ip2p.to("cuda")
36
 
37
+ print("Pipelines loaded! Portrait models load on first use.")
38
+
39
+ # Portrait models — lazy loaded on first selection to save memory
40
+ portrait_pipes = {}
 
 
 
 
41
 
42
+ PORTRAIT_MODELS = {
43
+ "SDXL Base (Balanced quality & control)": {
44
+ "repo": "stabilityai/stable-diffusion-xl-base-1.0",
45
+ "variant": "fp16",
46
+ },
47
+ "Juggernaut XL (Photorealistic portraits)": {
48
+ "repo": "RunDiffusion/Juggernaut-XL-v9",
49
+ "variant": None,
50
+ },
51
+ "DreamShaper XL (Creative portraits)": {
52
+ "repo": "Lykon/dreamshaper-xl-v2-turbo",
53
+ "variant": None,
54
+ },
55
+ "Realistic Vision XL (Portrait photography)": {
56
+ "repo": "SG161222/RealVisXL_V4.0",
57
+ "variant": "fp16",
58
+ },
59
+ "CyberRealistic XL (Detailed skin & faces)": {
60
+ "repo": "stablediffusionapi/cyberrealistic-xl",
61
+ "variant": None,
62
+ },
63
+ "DreamShaper (SD1.5, fast portraits)": {
64
+ "repo": "Lykon/DreamShaper",
65
+ "variant": None,
66
+ "sd15": True,
67
+ },
68
+ }
69
+
70
+ def get_portrait_pipe(model_name):
71
+ """Lazy load portrait models on first use."""
72
+ if model_name not in portrait_pipes:
73
+ print(f"Loading {model_name} for the first time...")
74
+ cfg = PORTRAIT_MODELS[model_name]
75
+ kwargs = dict(torch_dtype=torch.float16, use_safetensors=True)
76
+ if cfg.get("variant"):
77
+ kwargs["variant"] = cfg["variant"]
78
+ pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(cfg["repo"], **kwargs)
79
+ pipe.to("cuda")
80
+ portrait_pipes[model_name] = pipe
81
+ print(f"{model_name} loaded!")
82
+ return portrait_pipes[model_name]
83
 
84
  def resize_image(image, max_size=1024):
85
  orig_w, orig_h = image.size
 
137
  generator=generator,
138
  ).images[0]
139
 
140
+ else:
141
+ # Portrait models (lazy loaded)
142
+ pipe = get_portrait_pipe(model_choice)
143
+ image = pipe(
144
  prompt=prompt,
145
  image=input_image,
146
  strength=float(strength),
 
172
  block_title_text_weight="600",
173
  )
174
 
175
+ I2I_MODELS = [
176
+ "FLUX.1-schnell (Creative, high change)",
177
+ "InstructPix2Pix (Precise, preserves identity)",
178
+ ] + list(PORTRAIT_MODELS.keys())
179
+
180
  with gr.Blocks(fill_height=True) as demo:
181
  gr.Markdown(
182
  """
 
223
  with gr.Tab("🖼️ Image to Image"):
224
  with gr.Row(equal_height=False):
225
  with gr.Column(scale=1, min_width=320):
 
226
  model_choice = gr.Radio(
227
+ choices=I2I_MODELS,
 
 
 
 
228
  value="InstructPix2Pix (Precise, preserves identity)",
229
  label="🤖 Model",
230
+ info="InstructPix2Pix: best for targeted edits. Portrait models: high quality people & faces. FLUX: creative transformations. Note: portrait models load on first use."
231
  )
 
232
  i2i_input = gr.Image(label="Upload Image", type="pil")
233
  i2i_prompt = gr.Textbox(
234
  label="✨ Edit Instruction",
235
+ placeholder="e.g. 'woman in blue dress, photorealistic portrait' or 'make it a sunset'",
236
  lines=4,
237
  )
 
238
  with gr.Accordion("⚙️ Advanced Settings", open=False):
239
+ i2i_strength = gr.Slider(0.1, 1.0, value=0.65, step=0.05,
240
+ label="Strength (all except InstructPix2Pix)",
241
+ info="Lower = more faithful to original. Higher = more creative change.")
242
+ i2i_steps = gr.Slider(1, 50, value=25, step=1, label="Inference Steps")
243
  with gr.Row():
244
  i2i_randomize = gr.Checkbox(label="🎲 Random Seed", value=True)
245
  i2i_seed = gr.Number(label="Seed", value=42, precision=0, visible=False)
 
247
  lambda r: gr.Number(visible=not r),
248
  inputs=[i2i_randomize], outputs=[i2i_seed]
249
  )
 
250
  i2i_btn = gr.Button("🚀 Edit Image", variant="primary", size="lg")
251
 
252
  with gr.Column(scale=1, min_width=320):
 
264
  ---
265
  <div style="text-align: center; opacity: 0.7; font-size: 0.9em;">
266
  <strong>T2I:</strong> Tongyi-MAI/Z-Image-Turbo •
267
+ <strong>I2I:</strong> FLUX.1-schnell + InstructPix2Pix + Juggernaut XL + DreamShaper XL + RealVisXL + CyberRealistic XL
268
  </div>
269
  """
270
  )