witcherderivia commited on
Commit
b8ca898
·
verified ·
1 Parent(s): 995291e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +247 -0
app.py ADDED
@@ -0,0 +1,247 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import random
4
+ import torch
5
+ import spaces
6
+
7
+ from PIL import Image
8
+ from diffsynth.pipelines.qwen_image import QwenImagePipeline, ModelConfig
9
+
10
+ import os
11
+
12
+ from huggingface_hub import hf_hub_download
13
+
14
+
15
+
16
+
17
+ pipe = QwenImagePipeline.from_pretrained(
18
+ torch_dtype=torch.bfloat16,
19
+ device="cuda",
20
+ model_configs=[
21
+ ModelConfig(model_id="Qwen/Qwen-Image-Edit-2509", origin_file_pattern="transformer/diffusion_pytorch_model*.safetensors"),
22
+ ModelConfig(model_id="Qwen/Qwen-Image", origin_file_pattern="text_encoder/model*.safetensors"),
23
+ ModelConfig(model_id="Qwen/Qwen-Image", origin_file_pattern="vae/diffusion_pytorch_model.safetensors"),
24
+ ],
25
+ tokenizer_config=None,
26
+ processor_config=ModelConfig(model_id="Qwen/Qwen-Image-Edit", origin_file_pattern="processor/"),
27
+ )
28
+
29
+
30
+
31
+
32
+
33
+
34
+ speedup = hf_hub_download(repo_id="witcherderivia/Qwen-Image-Style-Transfer", filename="diffsynth_Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors")
35
+ qwenstyle= hf_hub_download(repo_id="witcherderivia/Qwen-Image-Style-Transfer", filename="diffsynth_Qwen-Image-Edit-2509-Style-Transfer-V1.safetensors")
36
+
37
+
38
+ pipe.load_lora(pipe.dit, qwenstyle)
39
+ pipe.load_lora(pipe.dit,speedup)
40
+
41
+
42
+
43
+ dtype = torch.bfloat16
44
+ device = "cuda" if torch.cuda.is_available() else "cpu"
45
+
46
+
47
+
48
+
49
+ MAX_SEED = np.iinfo(np.int32).max
50
+
51
+
52
+ @spaces.GPU
53
+ def infer(
54
+ content_ref,
55
+ style_ref,
56
+ prompt,
57
+ seed=123,
58
+ randomize_seed=False,
59
+ true_guidance_scale=1.0,
60
+ num_inference_steps=4,
61
+ minedge=1024,
62
+ progress=gr.Progress(track_tqdm=True),
63
+
64
+ ):
65
+
66
+
67
+
68
+
69
+
70
+ content_ref=Image.fromarray(content_ref)
71
+ style_ref=Image.fromarray(style_ref)
72
+
73
+ if randomize_seed:
74
+ seed = random.randint(0, MAX_SEED)
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+ w,h=content_ref.size
83
+
84
+
85
+
86
+ #minedge=1024
87
+ if w>h:
88
+ r=w/h
89
+ h=minedge
90
+ w=int(h*r)-int(h*r)%16
91
+
92
+ else:
93
+ r=h/w
94
+ w=minedge
95
+ h=int(w*r)-int(w*r)%16
96
+
97
+
98
+
99
+ print(f"Calling pipeline with prompt: '{prompt}'")
100
+
101
+ print(f"Seed: {seed}, Steps: {num_inference_steps}, Guidance: {true_guidance_scale}, Size: {w}x{h}")
102
+
103
+ images = [
104
+ content_ref.resize((w, h)),
105
+ style_ref.resize((minedge, minedge)) ,
106
+ ]
107
+
108
+
109
+ # Generate the image
110
+
111
+ image = pipe(prompt, edit_image=images, seed=seed, num_inference_steps=num_inference_steps, height=h, width=w,edit_image_auto_resize=False,cfg_scale=true_guidance_scale)#ligtning
112
+
113
+
114
+ return image, seed
115
+
116
+ # --- Examples and UI Layout ---
117
+ examples = []
118
+
119
+
120
+
121
+ _HEADER_ = '''
122
+ <div style="text-align: center; max-width: 650px; margin: 0 auto;">
123
+ <h1 style="font-size: 2.5rem; font-weight: 700; margin-bottom: 1rem; display: contents;">QwenStyle V1</h1>
124
+
125
+ </div>
126
+
127
+
128
+ <p style="font-size: 1rem; margin-bottom: 1.5rem;">Paper: <a href='https://openreview.net/forum?id=Cgb7JpOA5Q&referrer=%5Bthe%20profile%20of%20Shiwen%20Zhang%5D(%2Fprofile%3Fid%3D~Shiwen_Zhang1)' target='_blank'>QwenStyle: Content-Preserving Style Transfer with Qwen-Image-Edit</a> | Codes: <a href='https://github.com/witcherofresearch/Qwen-Image-Style-Transfer' target='_blank'>GitHub</a></p>
129
+ '''
130
+
131
+ with gr.Blocks() as demo:
132
+
133
+ with gr.Column(elem_id="col-container"):
134
+ gr.HTML('<img src="https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-Image/qwen_image_edit_logo.png" alt="Qwen-Image Logo" width="400" style="display: block; margin: 0 auto;">')
135
+ gr.Markdown(_HEADER_)
136
+ gr.Markdown("This is a demo of QwenStyle v1, the first Content-Preserving Style Transfer Lora on Qwen-Image-Edit-2509.")
137
+ with gr.Row():
138
+ with gr.Column():
139
+ with gr.Row():
140
+ content_ref = gr.Image(label="content ref", type="numpy", )
141
+ style_ref = gr.Image(label="style ref", type="numpy", )
142
+ #print(f"type(content_ref)={type(content_ref)}")
143
+
144
+ #input_images = gr.Gallery(label="Input Images", show_label=False, type="pil", interactive=True)
145
+
146
+
147
+ result = gr.Image(label="Result", show_label=True, type="pil")
148
+ #result = gr.Gallery(label="Result", show_label=True, type="pil")
149
+ with gr.Row():
150
+ prompt = gr.Text(
151
+ label="Prompt",
152
+ value='Style Transfer the style of Figure 2 to Figure 1, and keep the content and characteristics of Figure 1.',
153
+ show_label=True,
154
+ placeholder='Style Transfer the style of Figure 2 to Figure 1, and keep the content and characteristics of Figure 1.',
155
+ container=True,
156
+ )
157
+ run_button = gr.Button("Edit!", variant="primary")
158
+
159
+ with gr.Accordion("Advanced Settings", open=True):
160
+ # Negative prompt UI element is removed here
161
+
162
+ seed = gr.Slider(
163
+ label="Seed",
164
+ minimum=0,
165
+ maximum=MAX_SEED,
166
+ step=1,
167
+ value=123,
168
+ )
169
+
170
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=False)
171
+
172
+ with gr.Row():
173
+
174
+ true_guidance_scale = gr.Slider(
175
+ label="CFG should be 1.0",
176
+ minimum=0,
177
+ maximum=10.0,
178
+ step=0.1,
179
+ value=1.0
180
+ )
181
+
182
+ num_inference_steps = gr.Slider(
183
+ label="Number of inference steps should be 4",
184
+ minimum=1,
185
+ maximum=50,
186
+ step=1,
187
+ value=4,
188
+ )
189
+
190
+ minedge = gr.Slider(
191
+ label="Min Edge of the generated image",
192
+ minimum=256,
193
+ maximum=2048,
194
+ step=8,
195
+ value=1024,
196
+ )
197
+ with gr.Row(), gr.Column():
198
+ gr.Markdown("## Examples")
199
+ gr.Markdown("changing the minedge could lead to different style similarity.")
200
+ default_prompt='Style Transfer the style of Figure 2 to Figure 1, and keep the content and characteristics of Figure 1.'
201
+ gr.Examples(examples=[
202
+ ['./qwenstyleref/pulpfiction_2.jpg','./qwenstyleref/styleref=6_style_ref.png',default_prompt],
203
+ ['./qwenstyleref/styleref=0_content_ref.png','./qwenstyleref/110.png',default_prompt],
204
+ ['./qwenstyleref/romanholiday_1.jpg','./qwenstyleref/s0099____1113_01_query_1_img_000146_1682705733350_08158389675901344.jpg.jpg',default_prompt],
205
+ ['./qwenstyleref/styleref=0_content_ref.png','./qwenstyleref/125.png',default_prompt],
206
+ ['./qwenstyleref/fallenangle.jpg','./qwenstyleref/styleref=s0038.png',default_prompt],
207
+ ['./qwenstyleref/styleref=0_content_ref.png','./qwenstyleref/styleref=s0572.png',default_prompt],
208
+ ['./qwenstyleref/startrooper1.jpg','./qwenstyleref/david-face-760x985.jpg','Style Transfer Figure 1 into marble material.'],
209
+ ['./qwenstyleref/possession.png','./qwenstyleref/s0026____0907_01_query_0_img_000194_1682674358294_041656249089406583.jpeg.jpg',default_prompt],
210
+ ['./qwenstyleref/styleref=0_content_ref.png','./qwenstyleref/Jotarokujo.webp',default_prompt],
211
+ ['./qwenstyleref/wallstreet1.jpg','./qwenstyleref/034.png',default_prompt],
212
+ ['./qwenstyleref/bird.jpeg','./qwenstyleref/styleref=s0539.png',default_prompt],
213
+
214
+
215
+
216
+ ],
217
+ inputs=[content_ref,style_ref, prompt],
218
+ outputs=[result, seed],
219
+ fn=infer,
220
+ cache_examples=False
221
+ )
222
+
223
+
224
+
225
+
226
+
227
+ # gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=False)
228
+
229
+ gr.on(
230
+ triggers=[run_button.click],
231
+ fn=infer,
232
+ inputs=[
233
+ content_ref,
234
+ style_ref,
235
+ prompt,
236
+ seed,
237
+ randomize_seed,
238
+ true_guidance_scale,
239
+ num_inference_steps,
240
+ minedge,
241
+
242
+ ],
243
+ outputs=[result, seed],
244
+ )
245
+
246
+ if __name__ == "__main__":
247
+ demo.launch(server_name='0.0.0.0')