seawolf2357 commited on
Commit
2579011
Β·
verified Β·
1 Parent(s): 6e333b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -24
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import spaces
3
  import torch
4
- from diffusers import DiffusionPipeline
5
  from diffusers.utils import load_image
6
  from PIL import Image
7
  import os
@@ -39,15 +39,15 @@ def load_pipeline():
39
  global pipeline
40
  if pipeline is None:
41
  gr.Info("Loading FLUX.1-Kontext model...")
42
- # Use DiffusionPipeline to load FLUX.1-Kontext-dev
43
- pipeline = DiffusionPipeline.from_pretrained(
44
  "black-forest-labs/FLUX.1-Kontext-dev",
45
  torch_dtype=torch.bfloat16
46
  )
47
  return pipeline
48
 
49
  @spaces.GPU(duration=120)
50
- def style_transfer(input_image, style_name, prompt_suffix, num_inference_steps, seed):
51
  """
52
  Apply style transfer to the input image using selected style
53
  """
@@ -60,14 +60,14 @@ def style_transfer(input_image, style_name, prompt_suffix, num_inference_steps,
60
  pipe = load_pipeline()
61
  pipe = pipe.to('cuda')
62
 
63
- # Enable memory efficient attention
64
  pipe.enable_model_cpu_offload()
65
 
66
  # Set seed for reproducibility
67
  if seed > 0:
68
  generator = torch.Generator(device="cuda").manual_seed(seed)
69
  else:
70
- generator = None
71
 
72
  # Process input image
73
  if isinstance(input_image, str):
@@ -76,40 +76,39 @@ def style_transfer(input_image, style_name, prompt_suffix, num_inference_steps,
76
  image = input_image
77
 
78
  # Resize to 1024x1024 (required for Kontext)
79
- image = image.resize((1024, 1024), Image.Resampling.LANCZOS)
80
 
81
- # Load the selected LoRA from the repository
82
  gr.Info(f"Loading {style_name} style...")
83
  lora_filename = style_type_lora_dict[style_name]
84
 
85
  # Load LoRA weights directly from the repository
86
  pipe.load_lora_weights(
87
  "Owen777/Kontext-Style-Loras",
88
- weight_name=lora_filename,
89
- adapter_name="style"
90
  )
91
- pipe.set_adapters(["style"], adapter_weights=[1.0])
92
 
93
- # Create prompt
94
  style_name_readable = style_name.replace('_', ' ')
95
  prompt = f"Turn this image into the {style_name_readable} style."
96
- if prompt_suffix:
97
- prompt += f" {prompt_suffix}"
98
 
99
  gr.Info("Generating styled image...")
100
 
101
- # Generate the styled image
102
  result = pipe(
103
  image=image,
104
  prompt=prompt,
105
- height=1024,
106
- width=1024,
107
  num_inference_steps=num_inference_steps,
108
  generator=generator,
109
- guidance_scale=3.5
 
110
  )
111
 
112
- # Clear GPU memory
113
  pipe.unload_lora_weights()
114
  torch.cuda.empty_cache()
115
 
@@ -179,8 +178,8 @@ with gr.Blocks(title="FLUX.1 Kontext Style Transfer", theme=gr.themes.Soft()) as
179
  )
180
 
181
  prompt_suffix = gr.Textbox(
182
- label="Additional Prompt (Optional)",
183
- placeholder="Add extra details to the transformation...",
184
  lines=2
185
  )
186
 
@@ -194,6 +193,15 @@ with gr.Blocks(title="FLUX.1 Kontext Style Transfer", theme=gr.themes.Soft()) as
194
  info="More steps = better quality but slower"
195
  )
196
 
 
 
 
 
 
 
 
 
 
197
  seed = gr.Number(
198
  label="Seed",
199
  value=42,
@@ -215,6 +223,7 @@ with gr.Blocks(title="FLUX.1 Kontext Style Transfer", theme=gr.themes.Soft()) as
215
  - First run downloads the model (~7GB)
216
  - Each style transformation takes ~30-60 seconds
217
  - Try different styles to find the best match!
 
218
  """)
219
 
220
  # Update style description when style changes
@@ -239,14 +248,14 @@ with gr.Blocks(title="FLUX.1 Kontext Style Transfer", theme=gr.themes.Soft()) as
239
  ],
240
  inputs=[input_image, style_dropdown, prompt_suffix],
241
  outputs=output_image,
242
- fn=lambda img, style, prompt: style_transfer(img, style, prompt, 24, 42),
243
  cache_examples=False
244
  )
245
 
246
  # Connect the generate button
247
  generate_btn.click(
248
  fn=style_transfer,
249
- inputs=[input_image, style_dropdown, prompt_suffix, num_steps, seed],
250
  outputs=output_image
251
  )
252
 
@@ -269,7 +278,7 @@ with gr.Blocks(title="FLUX.1 Kontext Style Transfer", theme=gr.themes.Soft()) as
269
  ### πŸš€ How it works:
270
  1. Upload any image
271
  2. Select a style from the dropdown
272
- 3. (Optional) Add custom prompt details
273
  4. Click "Transform Image" and wait ~30-60 seconds
274
  5. Download your styled image!
275
 
 
1
  import gradio as gr
2
  import spaces
3
  import torch
4
+ from diffusers import FluxKontextPipeline
5
  from diffusers.utils import load_image
6
  from PIL import Image
7
  import os
 
39
  global pipeline
40
  if pipeline is None:
41
  gr.Info("Loading FLUX.1-Kontext model...")
42
+ # Load FLUX.1-Kontext-dev model
43
+ pipeline = FluxKontextPipeline.from_pretrained(
44
  "black-forest-labs/FLUX.1-Kontext-dev",
45
  torch_dtype=torch.bfloat16
46
  )
47
  return pipeline
48
 
49
  @spaces.GPU(duration=120)
50
+ def style_transfer(input_image, style_name, prompt_suffix, num_inference_steps, guidance_scale, seed):
51
  """
52
  Apply style transfer to the input image using selected style
53
  """
 
60
  pipe = load_pipeline()
61
  pipe = pipe.to('cuda')
62
 
63
+ # Enable memory efficient settings
64
  pipe.enable_model_cpu_offload()
65
 
66
  # Set seed for reproducibility
67
  if seed > 0:
68
  generator = torch.Generator(device="cuda").manual_seed(seed)
69
  else:
70
+ generator = torch.manual_seed(42)
71
 
72
  # Process input image
73
  if isinstance(input_image, str):
 
76
  image = input_image
77
 
78
  # Resize to 1024x1024 (required for Kontext)
79
+ image = image.convert("RGB").resize((1024, 1024), Image.Resampling.LANCZOS)
80
 
81
+ # Load the selected LoRA
82
  gr.Info(f"Loading {style_name} style...")
83
  lora_filename = style_type_lora_dict[style_name]
84
 
85
  # Load LoRA weights directly from the repository
86
  pipe.load_lora_weights(
87
  "Owen777/Kontext-Style-Loras",
88
+ weight_name=lora_filename
 
89
  )
90
+ pipe.set_adapters(["default"], adapter_weights=[1.0])
91
 
92
+ # Create prompt for style transformation
93
  style_name_readable = style_name.replace('_', ' ')
94
  prompt = f"Turn this image into the {style_name_readable} style."
95
+ if prompt_suffix and prompt_suffix.strip():
96
+ prompt += f" {prompt_suffix.strip()}"
97
 
98
  gr.Info("Generating styled image...")
99
 
100
+ # Generate the styled image with Kontext pipeline
101
  result = pipe(
102
  image=image,
103
  prompt=prompt,
104
+ guidance_scale=guidance_scale,
 
105
  num_inference_steps=num_inference_steps,
106
  generator=generator,
107
+ height=1024,
108
+ width=1024
109
  )
110
 
111
+ # Clear LoRA and GPU memory
112
  pipe.unload_lora_weights()
113
  torch.cuda.empty_cache()
114
 
 
178
  )
179
 
180
  prompt_suffix = gr.Textbox(
181
+ label="Additional Instructions (Optional)",
182
+ placeholder="Add extra details like 'make it more colorful' or 'add dramatic lighting'...",
183
  lines=2
184
  )
185
 
 
193
  info="More steps = better quality but slower"
194
  )
195
 
196
+ guidance = gr.Slider(
197
+ minimum=1.0,
198
+ maximum=5.0,
199
+ value=2.5,
200
+ step=0.1,
201
+ label="Guidance Scale",
202
+ info="How closely to follow the prompt (2.5 recommended)"
203
+ )
204
+
205
  seed = gr.Number(
206
  label="Seed",
207
  value=42,
 
223
  - First run downloads the model (~7GB)
224
  - Each style transformation takes ~30-60 seconds
225
  - Try different styles to find the best match!
226
+ - Use additional instructions for fine control
227
  """)
228
 
229
  # Update style description when style changes
 
248
  ],
249
  inputs=[input_image, style_dropdown, prompt_suffix],
250
  outputs=output_image,
251
+ fn=lambda img, style, prompt: style_transfer(img, style, prompt, 24, 2.5, 42),
252
  cache_examples=False
253
  )
254
 
255
  # Connect the generate button
256
  generate_btn.click(
257
  fn=style_transfer,
258
+ inputs=[input_image, style_dropdown, prompt_suffix, num_steps, guidance, seed],
259
  outputs=output_image
260
  )
261
 
 
278
  ### πŸš€ How it works:
279
  1. Upload any image
280
  2. Select a style from the dropdown
281
+ 3. (Optional) Add custom instructions
282
  4. Click "Transform Image" and wait ~30-60 seconds
283
  5. Download your styled image!
284