Alexander Bagus commited on
Commit
317f25c
·
1 Parent(s): c4f1166
Files changed (2) hide show
  1. app.py +71 -45
  2. static/header.html +1 -1
app.py CHANGED
@@ -7,6 +7,7 @@ from diffsynth.pipelines.qwen_image import (
7
  QwenImageUnit_Image2LoRAEncode, QwenImageUnit_Image2LoRADecode
8
  )
9
  from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler
 
10
  from safetensors.torch import save_file
11
  import torch, math
12
  from PIL import Image
@@ -20,28 +21,28 @@ URL_PUBLIC = "https://huggingface.co/spaces/AiSudo/Qwen-Image-to-LoRA/blob/main"
20
  DTYPE = torch.bfloat16
21
  MAX_SEED = np.iinfo(np.int32).max
22
 
23
- vram_config_disk_offload = {
24
- "offload_dtype": "disk",
25
- "offload_device": "disk",
26
- "onload_dtype": "disk",
27
- "onload_device": "disk",
28
- "preparing_dtype": torch.bfloat16,
29
- "preparing_device": "cuda",
30
- "computation_dtype": torch.bfloat16,
31
- "computation_device": "cuda",
32
- }
33
-
34
  # vram_config_disk_offload = {
35
- # "offload_dtype": torch.bfloat16,
36
- # "offload_device": "cuda",
37
- # "onload_dtype": torch.bfloat16,
38
- # "onload_device": "cuda",
39
  # "preparing_dtype": torch.bfloat16,
40
  # "preparing_device": "cuda",
41
  # "computation_dtype": torch.bfloat16,
42
  # "computation_device": "cuda",
43
  # }
44
 
 
 
 
 
 
 
 
 
 
 
 
45
  # Load models
46
  pipe_lora = QwenImagePipeline.from_pretrained(
47
  torch_dtype=torch.bfloat16,
@@ -81,17 +82,17 @@ vram_config = {
81
  "computation_device": "cuda",
82
  }
83
 
84
- pipe_imagen = QwenImagePipeline.from_pretrained(
85
- torch_dtype=torch.bfloat16,
86
- device="cuda",
87
- model_configs=[
88
- ModelConfig(download_source="huggingface", model_id="Qwen/Qwen-Image", origin_file_pattern="transformer/diffusion_pytorch_model*.safetensors", **vram_config),
89
- ModelConfig(download_source="huggingface", model_id="Qwen/Qwen-Image", origin_file_pattern="text_encoder/model*.safetensors", **vram_config),
90
- ModelConfig(download_source="huggingface", model_id="Qwen/Qwen-Image", origin_file_pattern="vae/diffusion_pytorch_model.safetensors", **vram_config),
91
- ],
92
- tokenizer_config=ModelConfig(download_source="huggingface", model_id="Qwen/Qwen-Image", origin_file_pattern="tokenizer/"),
93
- vram_limit=torch.cuda.mem_get_info("cuda")[1] / (1024 ** 3) - 0.5,
94
- )
95
 
96
  # scheduler_config = {
97
  # "base_image_seq_len": 256,
@@ -109,14 +110,39 @@ pipe_imagen = QwenImagePipeline.from_pretrained(
109
  # "use_exponential_sigmas": False,
110
  # "use_karras_sigmas": False,
111
  # }
112
- # scheduler = FlowMatchEulerDiscreteScheduler.from_config(scheduler_config)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
 
114
- # pipe_imagen = DiffusionPipeline.from_pretrained(
115
- # "Qwen/Qwen-Image",
116
- # scheduler=scheduler,
117
- # torch_dtype=torch.bfloat16,
118
- # low_cpu_mem_usage=False,
119
- # ).to("cuda")
120
  # pipe_imagen.load_lora_weights(
121
  # "lightx2v/Qwen-Image-Lightning",
122
  # weight_name="Qwen-Image-Lightning-8steps-V2.0-bf16.safetensors",
@@ -126,7 +152,7 @@ pipe_imagen = QwenImagePipeline.from_pretrained(
126
  # pipe_imagen.fuse_lora(adapter_names=["lightning_steps"], lora_scale=1.25)
127
  # pipe_imagen.unload_lora_weights()
128
 
129
- # pipe_imagen.to("cuda")
130
 
131
  @spaces.GPU
132
  def generate_lora(
@@ -169,17 +195,17 @@ def generate_image(
169
  num_inference_steps=8,
170
  progress=gr.Progress(track_tqdm=True),
171
  ):
172
- lora_path = f"loras/{lora_name}"
173
- pipe_imagen.clear_lora()
174
- pipe_imagen.load_lora(pipe_imagen.dit, lora_path)
175
-
176
- # pipe_imagen.unload_lora_weights()
177
- # pipe_imagen.load_lora_weights(
178
- # "loras",
179
- # weight_name=lora_name,
180
- # adapter_name="generated_lora"
181
- # )
182
- # pipe_imagen.set_adapters("generated_lora", adapter_weights=lora_strength)
183
 
184
  if randomize_seed:
185
  seed = random.randint(0, MAX_SEED)
 
7
  QwenImageUnit_Image2LoRAEncode, QwenImageUnit_Image2LoRADecode
8
  )
9
  from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler
10
+ from transformers import Qwen2_5_VLForConditionalGeneration, QwenImageTransformer2DModel
11
  from safetensors.torch import save_file
12
  import torch, math
13
  from PIL import Image
 
21
  DTYPE = torch.bfloat16
22
  MAX_SEED = np.iinfo(np.int32).max
23
 
 
 
 
 
 
 
 
 
 
 
 
24
  # vram_config_disk_offload = {
25
+ # "offload_dtype": "disk",
26
+ # "offload_device": "disk",
27
+ # "onload_dtype": "disk",
28
+ # "onload_device": "disk",
29
  # "preparing_dtype": torch.bfloat16,
30
  # "preparing_device": "cuda",
31
  # "computation_dtype": torch.bfloat16,
32
  # "computation_device": "cuda",
33
  # }
34
 
35
+ vram_config_disk_offload = {
36
+ "offload_dtype": torch.bfloat16,
37
+ "offload_device": "cuda",
38
+ "onload_dtype": torch.bfloat16,
39
+ "onload_device": "cuda",
40
+ "preparing_dtype": torch.bfloat16,
41
+ "preparing_device": "cuda",
42
+ "computation_dtype": torch.bfloat16,
43
+ "computation_device": "cuda",
44
+ }
45
+
46
  # Load models
47
  pipe_lora = QwenImagePipeline.from_pretrained(
48
  torch_dtype=torch.bfloat16,
 
82
  "computation_device": "cuda",
83
  }
84
 
85
+ # pipe_imagen = QwenImagePipeline.from_pretrained(
86
+ # torch_dtype=torch.bfloat16,
87
+ # device="cuda",
88
+ # model_configs=[
89
+ # ModelConfig(download_source="huggingface", model_id="Qwen/Qwen-Image", origin_file_pattern="transformer/diffusion_pytorch_model*.safetensors", **vram_config),
90
+ # ModelConfig(download_source="huggingface", model_id="Qwen/Qwen-Image", origin_file_pattern="text_encoder/model*.safetensors", **vram_config),
91
+ # ModelConfig(download_source="huggingface", model_id="Qwen/Qwen-Image", origin_file_pattern="vae/diffusion_pytorch_model.safetensors", **vram_config),
92
+ # ],
93
+ # tokenizer_config=ModelConfig(download_source="huggingface", model_id="Qwen/Qwen-Image", origin_file_pattern="tokenizer/"),
94
+ # vram_limit=torch.cuda.mem_get_info("cuda")[1] / (1024 ** 3) - 0.5,
95
+ # )
96
 
97
  # scheduler_config = {
98
  # "base_image_seq_len": 256,
 
110
  # "use_exponential_sigmas": False,
111
  # "use_karras_sigmas": False,
112
  # }
113
+ #
114
+ quant_model = "AiSudo/Qwen-Image-fp8-4steps"
115
+
116
+ transformer=QwenImageTransformer2DModel.from_pretrained(
117
+ quant_model,
118
+ subfolder='transformer',
119
+ torch_dtype=torch.bfloat16,
120
+ use_safetensors=False,
121
+ device_map="cuda"
122
+ )
123
+
124
+ text_encoder=Qwen2_5_VLForConditionalGeneration.from_pretrained(
125
+ quant_model,
126
+ subfolder='text_encoder',
127
+ torch_dtype=torch.bfloat16,
128
+ use_safetensors=False,
129
+ device_map="cuda"
130
+ )
131
+
132
+ scheduler = FlowMatchEulerDiscreteScheduler.from_config(
133
+ quant_model,
134
+ subfolder="scheduler"
135
+ )
136
+
137
+ pipe_imagen = DiffusionPipeline.from_pretrained(
138
+ "Qwen/Qwen-Image",
139
+ transformer=transformer,
140
+ text_encoder=text_encoder,
141
+ scheduler=scheduler,
142
+ torch_dtype=torch.bfloat16,
143
+ low_cpu_mem_usage=False,
144
+ ).to("cuda")
145
 
 
 
 
 
 
 
146
  # pipe_imagen.load_lora_weights(
147
  # "lightx2v/Qwen-Image-Lightning",
148
  # weight_name="Qwen-Image-Lightning-8steps-V2.0-bf16.safetensors",
 
152
  # pipe_imagen.fuse_lora(adapter_names=["lightning_steps"], lora_scale=1.25)
153
  # pipe_imagen.unload_lora_weights()
154
 
155
+ pipe_imagen.to("cuda")
156
 
157
  @spaces.GPU
158
  def generate_lora(
 
195
  num_inference_steps=8,
196
  progress=gr.Progress(track_tqdm=True),
197
  ):
198
+ # lora_path = f"loras/{lora_name}"
199
+ # pipe_imagen.clear_lora()
200
+ # pipe_imagen.load_lora(pipe_imagen.dit, lora_path)
201
+
202
+ pipe_imagen.unload_lora_weights()
203
+ pipe_imagen.load_lora_weights(
204
+ "loras",
205
+ weight_name=lora_name,
206
+ adapter_name="generated_lora"
207
+ )
208
+ pipe_imagen.set_adapters("generated_lora", adapter_weights=lora_strength)
209
 
210
  if randomize_seed:
211
  seed = random.randint(0, MAX_SEED)
static/header.html CHANGED
@@ -6,6 +6,6 @@
6
  <p>
7
  Generate LoRA from a few images.
8
  <br>
9
- Demo by <a href="https://aisudo.com/" target="_blank">AiSudo</a> 😊
10
  </div>
11
  </div>
 
6
  <p>
7
  Generate LoRA from a few images.
8
  <br>
9
+ Demo by <a href="https://aisudo.com/ai-models/qwen-2512-8-step-mdl032qw8s" target="_blank">AiSudo</a> 😊
10
  </div>
11
  </div>