prithivMLmods commited on
Commit
fdba7ba
Β·
verified Β·
1 Parent(s): 014a6c2

update app

Browse files
Files changed (1) hide show
  1. app.py +47 -53
app.py CHANGED
@@ -1,16 +1,16 @@
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
- import torch
5
  import spaces
6
  import math
 
7
  from typing import Iterable
8
  from gradio.themes import Soft
9
  from gradio.themes.utils import colors, fonts, sizes
10
  from PIL import Image
11
- from diffusers import QwenImagePipeline, FlowMatchEulerDiscreteScheduler
12
- from qwenimage.qwen_fa3_processor import QwenDoubleStreamAttnProcessorFA3
13
- import os
14
 
15
  # -----------------------------------------------------------------------------
16
  # 1. THEME CONFIGURATION
@@ -85,52 +85,47 @@ class OrangeRedTheme(Soft):
85
  orange_red_theme = OrangeRedTheme()
86
 
87
  # -----------------------------------------------------------------------------
88
- # 2. MODEL LOADING
89
  # -----------------------------------------------------------------------------
90
 
91
- dtype = torch.bfloat16
92
- device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
 
 
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  scheduler_config = {
95
- "base_image_seq_len": 256,
96
- "base_shift": math.log(3),
97
- "invert_sigmas": False,
98
- "max_image_seq_len": 8192,
99
- "max_shift": math.log(3),
100
- "num_train_timesteps": 1000,
101
- "shift": 1.0,
102
- "shift_terminal": None,
103
- "stochastic_sampling": False,
104
- "time_shift_type": "exponential",
105
- "use_beta_sigmas": False,
106
  "use_dynamic_shifting": True,
107
- "use_exponential_sigmas": False,
108
- "use_karras_sigmas": False,
109
  }
110
- scheduler = FlowMatchEulerDiscreteScheduler.from_config(scheduler_config)
111
-
112
- print("Loading Qwen-Image-2512 with Turbo LoRA...")
113
- pipe = QwenImagePipeline.from_pretrained(
114
- "Qwen/Qwen-Image-2512",
115
- scheduler=scheduler,
116
- torch_dtype=dtype
117
- ).to(device)
118
-
119
- try:
120
- pipe.load_lora_weights(
121
- "Wuli-art/Qwen-Image-2512-Turbo-LoRA",
122
- weight_name="Wuli-Qwen-Image-2512-Turbo-LoRA-4steps-V1.0-bf16.safetensors"
123
- )
124
- pipe.fuse_lora()
125
- print("Turbo LoRA fused successfully.")
126
- except Exception as e:
127
- print(f"Error loading LoRA: {e}")
128
 
129
- try:
130
- pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
131
- print("FA3 Attention Processor set.")
132
- except Exception as e:
133
- print(f"Warning: FA3 not available: {e}")
134
 
135
  # -----------------------------------------------------------------------------
136
  # 3. INFERENCE LOGIC
@@ -157,7 +152,7 @@ def get_image_size(aspect_ratio):
157
  else:
158
  return 1328, 1328
159
 
160
- @spaces.GPU
161
  def infer(
162
  prompt,
163
  seed=42,
@@ -174,21 +169,20 @@ def infer(
174
  seed = random.randint(0, MAX_SEED)
175
 
176
  width, height = get_image_size(aspect_ratio)
177
- generator = torch.Generator(device=device).manual_seed(seed)
178
 
179
  print(f"Prompt: '{prompt}'")
180
  print(f"Seed: {seed}, Size: {width}x{height}, Steps: {num_inference_steps}")
181
 
 
182
  image = pipe(
183
  prompt=prompt,
184
  negative_prompt=negative_prompt,
185
- width=width,
186
- height=height,
187
  num_inference_steps=num_inference_steps,
188
- generator=generator,
189
- true_cfg_scale=guidance_scale,
190
- guidance_scale=1.0
191
- ).images[0]
192
 
193
  return image, seed
194
 
@@ -219,7 +213,7 @@ with gr.Blocks() as demo:
219
  <h2 style="font-style: italic;color: #E63E00;margin-top: -33px !important;">Fast, 4-steps with Turbo LoRA</h2>
220
  </div>
221
  """)
222
- gr.Markdown("Generate high-quality images using **Qwen-Image-2512** accelerated by **Wuli-art Turbo LoRA**.")
223
 
224
  with gr.Row():
225
  prompt = gr.Text(
@@ -251,7 +245,7 @@ with gr.Blocks() as demo:
251
 
252
  with gr.Row():
253
  guidance_scale = gr.Slider(
254
- label="Guidance scale",
255
  minimum=0.0,
256
  maximum=10.0,
257
  step=0.1,
 
1
  import gradio as gr
2
  import numpy as np
3
  import random
 
4
  import spaces
5
  import math
6
+ import os
7
  from typing import Iterable
8
  from gradio.themes import Soft
9
  from gradio.themes.utils import colors, fonts, sizes
10
  from PIL import Image
11
+
12
+ # DiffSynth imports
13
+ from diffsynth_engine import fetch_model, QwenImagePipeline, QwenImagePipelineConfig
14
 
15
  # -----------------------------------------------------------------------------
16
  # 1. THEME CONFIGURATION
 
85
  orange_red_theme = OrangeRedTheme()
86
 
87
  # -----------------------------------------------------------------------------
88
+ # 2. MODEL LOADING (DiffSynth-Engine)
89
  # -----------------------------------------------------------------------------
90
 
91
+ print("Fetching models via DiffSynth...")
92
+
93
+ # Fetch model paths
94
+ model_path = fetch_model("Qwen/Qwen-Image-2512", path="transformer/*.safetensors")
95
+ encoder_path = fetch_model("Qwen/Qwen-Image-2512", path="text_encoder/*.safetensors")
96
+ vae_path = fetch_model("Qwen/Qwen-Image-2512", path="vae/*.safetensors")
97
+ lora_path = fetch_model("Wuli-Art/Qwen-Image-2512-Turbo-LoRA", path="Wuli-Qwen-Image-2512-Turbo-LoRA-4steps-V1.0-bf16.safetensors")
98
 
99
+ # Create pipeline config
100
+ config = QwenImagePipelineConfig.basic_config(
101
+ model_path=model_path,
102
+ encoder_path=encoder_path,
103
+ vae_path=vae_path,
104
+ offload_mode="cpu_offload",
105
+ )
106
+
107
+ # Initialize pipeline
108
+ print("Initializing QwenImagePipeline...")
109
+ pipe = QwenImagePipeline.from_pretrained(config)
110
+
111
+ # Load Turbo LoRA
112
+ print("Loading Turbo LoRA...")
113
+ pipe.load_lora(
114
+ path=lora_path,
115
+ scale=1.0,
116
+ fused=True,
117
+ )
118
+
119
+ # Apply Scheduler Config
120
+ print("Applying Scheduler Config...")
121
  scheduler_config = {
122
+ "exponential_shift_mu": math.log(2.5),
 
 
 
 
 
 
 
 
 
 
123
  "use_dynamic_shifting": True,
124
+ "shift_terminal": None
 
125
  }
126
+ pipe.apply_scheduler_config(scheduler_config)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
 
128
+ print("Model loaded successfully.")
 
 
 
 
129
 
130
  # -----------------------------------------------------------------------------
131
  # 3. INFERENCE LOGIC
 
152
  else:
153
  return 1328, 1328
154
 
155
+ @spaces.GPU(duration=120)
156
  def infer(
157
  prompt,
158
  seed=42,
 
169
  seed = random.randint(0, MAX_SEED)
170
 
171
  width, height = get_image_size(aspect_ratio)
 
172
 
173
  print(f"Prompt: '{prompt}'")
174
  print(f"Seed: {seed}, Size: {width}x{height}, Steps: {num_inference_steps}")
175
 
176
+ # DiffSynth Inference
177
  image = pipe(
178
  prompt=prompt,
179
  negative_prompt=negative_prompt,
180
+ cfg_scale=guidance_scale,
 
181
  num_inference_steps=num_inference_steps,
182
+ seed=seed,
183
+ width=width,
184
+ height=height
185
+ )
186
 
187
  return image, seed
188
 
 
213
  <h2 style="font-style: italic;color: #E63E00;margin-top: -33px !important;">Fast, 4-steps with Turbo LoRA</h2>
214
  </div>
215
  """)
216
+ gr.Markdown("Generate high-quality images using **Qwen-Image-2512** accelerated by **Wuli-art Turbo LoRA** (via DiffSynth-Engine).")
217
 
218
  with gr.Row():
219
  prompt = gr.Text(
 
245
 
246
  with gr.Row():
247
  guidance_scale = gr.Slider(
248
+ label="Guidance scale (CFG)",
249
  minimum=0.0,
250
  maximum=10.0,
251
  step=0.1,