seawolf2357 commited on
Commit
6e333b0
ยท
verified ยท
1 Parent(s): 6b5f417

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -36
app.py CHANGED
@@ -1,14 +1,12 @@
1
  import gradio as gr
2
  import spaces
3
  import torch
4
- from huggingface_hub import hf_hub_download
5
- from diffusers import FluxKontextPipeline
6
  from diffusers.utils import load_image
7
  from PIL import Image
8
  import os
9
- import numpy as np
10
 
11
- # Style dictionary - ์™„์ „ํ•œ ์Šคํƒ€์ผ ๋ชฉ๋ก
12
  style_type_lora_dict = {
13
  "3D_Chibi": "3D_Chibi_lora_weights.safetensors",
14
  "American_Cartoon": "American_Cartoon_lora_weights.safetensors",
@@ -34,29 +32,6 @@ style_type_lora_dict = {
34
  "Rick_Morty": "Rick_Morty_lora_weights.safetensors"
35
  }
36
 
37
- # Create LoRAs directory if it doesn't exist
38
- os.makedirs("./LoRAs", exist_ok=True)
39
-
40
- # Download LoRA weights on demand
41
- def download_lora(style_name):
42
- lora_file = style_type_lora_dict[style_name]
43
- lora_path = f"./LoRAs/{lora_file}"
44
-
45
- if not os.path.exists(lora_path):
46
- gr.Info(f"Downloading {style_name} LoRA...")
47
- try:
48
- hf_hub_download(
49
- repo_id="Owen777/Kontext-Style-Loras",
50
- filename=lora_file,
51
- local_dir="./LoRAs"
52
- )
53
- print(f"Downloaded {lora_file}")
54
- except Exception as e:
55
- print(f"Error downloading {lora_file}: {e}")
56
- raise e
57
-
58
- return lora_path
59
-
60
  # Initialize pipeline globally
61
  pipeline = None
62
 
@@ -64,13 +39,14 @@ def load_pipeline():
64
  global pipeline
65
  if pipeline is None:
66
  gr.Info("Loading FLUX.1-Kontext model...")
67
- pipeline = FluxKontextPipeline.from_pretrained(
 
68
  "black-forest-labs/FLUX.1-Kontext-dev",
69
  torch_dtype=torch.bfloat16
70
  )
71
  return pipeline
72
 
73
- @spaces.GPU(duration=120) # Request GPU for 120 seconds
74
  def style_transfer(input_image, style_name, prompt_suffix, num_inference_steps, seed):
75
  """
76
  Apply style transfer to the input image using selected style
@@ -84,6 +60,9 @@ def style_transfer(input_image, style_name, prompt_suffix, num_inference_steps,
84
  pipe = load_pipeline()
85
  pipe = pipe.to('cuda')
86
 
 
 
 
87
  # Set seed for reproducibility
88
  if seed > 0:
89
  generator = torch.Generator(device="cuda").manual_seed(seed)
@@ -99,12 +78,17 @@ def style_transfer(input_image, style_name, prompt_suffix, num_inference_steps,
99
  # Resize to 1024x1024 (required for Kontext)
100
  image = image.resize((1024, 1024), Image.Resampling.LANCZOS)
101
 
102
- # Download and load the selected LoRA
103
  gr.Info(f"Loading {style_name} style...")
104
- lora_path = download_lora(style_name)
105
 
106
- pipe.load_lora_weights(lora_path, adapter_name="style")
107
- pipe.set_adapters(["style"], adapter_weights=[1])
 
 
 
 
 
108
 
109
  # Create prompt
110
  style_name_readable = style_name.replace('_', ' ')
@@ -121,10 +105,12 @@ def style_transfer(input_image, style_name, prompt_suffix, num_inference_steps,
121
  height=1024,
122
  width=1024,
123
  num_inference_steps=num_inference_steps,
124
- generator=generator
 
125
  )
126
 
127
  # Clear GPU memory
 
128
  torch.cuda.empty_cache()
129
 
130
  return result.images[0]
@@ -226,8 +212,8 @@ with gr.Blocks(title="FLUX.1 Kontext Style Transfer", theme=gr.themes.Soft()) as
226
  gr.Markdown("""
227
  ### ๐Ÿ’ก Tips:
228
  - All images are resized to 1024x1024
229
- - First run may take longer to download the model
230
- - Each style LoRA is ~359MB and downloaded on first use
231
  - Try different styles to find the best match!
232
  """)
233
 
@@ -248,6 +234,8 @@ with gr.Blocks(title="FLUX.1 Kontext Style Transfer", theme=gr.themes.Soft()) as
248
  ["https://huggingface.co/datasets/black-forest-labs/kontext-bench/resolve/main/test/images/0003.jpg", "3D_Chibi", "make it extra cute"],
249
  ["https://huggingface.co/datasets/black-forest-labs/kontext-bench/resolve/main/test/images/0003.jpg", "Van_Gogh", "with swirling sky"],
250
  ["https://huggingface.co/datasets/black-forest-labs/kontext-bench/resolve/main/test/images/0003.jpg", "Pixel", "8-bit retro game style"],
 
 
251
  ],
252
  inputs=[input_image, style_dropdown, prompt_suffix],
253
  outputs=output_image,
@@ -278,6 +266,15 @@ with gr.Blocks(title="FLUX.1 Kontext Style Transfer", theme=gr.themes.Soft()) as
278
 
279
  ---
280
 
 
 
 
 
 
 
 
 
 
281
  Created with โค๏ธ using [Owen777/Kontext-Style-Loras](https://huggingface.co/Owen777/Kontext-Style-Loras)
282
  """)
283
 
 
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
 
8
 
9
+ # Style dictionary
10
  style_type_lora_dict = {
11
  "3D_Chibi": "3D_Chibi_lora_weights.safetensors",
12
  "American_Cartoon": "American_Cartoon_lora_weights.safetensors",
 
32
  "Rick_Morty": "Rick_Morty_lora_weights.safetensors"
33
  }
34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  # Initialize pipeline globally
36
  pipeline = None
37
 
 
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
 
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)
 
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('_', ' ')
 
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
 
116
  return result.images[0]
 
212
  gr.Markdown("""
213
  ### ๐Ÿ’ก Tips:
214
  - All images are resized to 1024x1024
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
 
 
234
  ["https://huggingface.co/datasets/black-forest-labs/kontext-bench/resolve/main/test/images/0003.jpg", "3D_Chibi", "make it extra cute"],
235
  ["https://huggingface.co/datasets/black-forest-labs/kontext-bench/resolve/main/test/images/0003.jpg", "Van_Gogh", "with swirling sky"],
236
  ["https://huggingface.co/datasets/black-forest-labs/kontext-bench/resolve/main/test/images/0003.jpg", "Pixel", "8-bit retro game style"],
237
+ ["https://huggingface.co/datasets/black-forest-labs/kontext-bench/resolve/main/test/images/0003.jpg", "Chinese_Ink", "mountain landscape"],
238
+ ["https://huggingface.co/datasets/black-forest-labs/kontext-bench/resolve/main/test/images/0003.jpg", "LEGO", "colorful blocks"],
239
  ],
240
  inputs=[input_image, style_dropdown, prompt_suffix],
241
  outputs=output_image,
 
266
 
267
  ---
268
 
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
+
276
+ ---
277
+
278
  Created with โค๏ธ using [Owen777/Kontext-Style-Loras](https://huggingface.co/Owen777/Kontext-Style-Loras)
279
  """)
280