Spaces:
Running on Zero
Running on Zero
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247import gradio as grimport numpy as npimport randomimport torchimport spaces
from PIL import Imagefrom diffsynth.pipelines.qwen_image import QwenImagePipeline, ModelConfig
import os
from huggingface_hub import hf_hub_download
pipe = QwenImagePipeline.from_pretrained( torch_dtype=torch.bfloat16, device="cuda", model_configs=[ ModelConfig(model_id="Qwen/Qwen-Image-Edit-2509", origin_file_pattern="transformer/diffusion_pytorch_model*.safetensors"), ModelConfig(model_id="Qwen/Qwen-Image", origin_file_pattern="text_encoder/model*.safetensors"), ModelConfig(model_id="Qwen/Qwen-Image", origin_file_pattern="vae/diffusion_pytorch_model.safetensors"), ], tokenizer_config=None, processor_config=ModelConfig(model_id="Qwen/Qwen-Image-Edit", origin_file_pattern="processor/"),)
speedup = hf_hub_download(repo_id="witcherderivia/Qwen-Image-Style-Transfer", filename="diffsynth_Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors")qwenstyle= hf_hub_download(repo_id="witcherderivia/Qwen-Image-Style-Transfer", filename="diffsynth_Qwen-Image-Edit-2509-Style-Transfer-V1.safetensors")
pipe.load_lora(pipe.dit, qwenstyle)pipe.load_lora(pipe.dit,speedup)
dtype = torch.bfloat16device = "cuda" if torch.cuda.is_available() else "cpu"
MAX_SEED = np.iinfo(np.int32).max
@spaces.GPUdef infer( content_ref, style_ref, prompt, seed=123, randomize_seed=False, true_guidance_scale=1.0, num_inference_steps=4, minedge=1024, progress=gr.Progress(track_tqdm=True), ):
content_ref=Image.fromarray(content_ref) style_ref=Image.fromarray(style_ref) if randomize_seed: seed = random.randint(0, MAX_SEED)
w,h=content_ref.size
#minedge=1024 if w>h: r=w/h h=minedge w=int(h*r)-int(h*r)%16 else: r=h/w w=minedge h=int(w*r)-int(w*r)%16
print(f"Calling pipeline with prompt: '{prompt}'") print(f"Seed: {seed}, Steps: {num_inference_steps}, Guidance: {true_guidance_scale}, Size: {w}x{h}") images = [ content_ref.resize((w, h)), style_ref.resize((minedge, minedge)) , ]
# Generate the image 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
return image, seed
# --- Examples and UI Layout ---examples = []
_HEADER_ = '''<div style="text-align: center; max-width: 650px; margin: 0 auto;"> <h1 style="font-size: 2.5rem; font-weight: 700; margin-bottom: 1rem; display: contents;">QwenStyle V1</h1> </div>
<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>'''
with gr.Blocks() as demo:
with gr.Column(elem_id="col-container"): 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;">') gr.Markdown(_HEADER_) gr.Markdown("This is a demo of QwenStyle v1, the first Content-Preserving Style Transfer Lora on Qwen-Image-Edit-2509.") with gr.Row(): with gr.Column(): with gr.Row(): content_ref = gr.Image(label="content ref", type="numpy", ) style_ref = gr.Image(label="style ref", type="numpy", ) #print(f"type(content_ref)={type(content_ref)}") #input_images = gr.Gallery(label="Input Images", show_label=False, type="pil", interactive=True)
result = gr.Image(label="Result", show_label=True, type="pil") #result = gr.Gallery(label="Result", show_label=True, type="pil") with gr.Row(): prompt = gr.Text( label="Prompt", value='Style Transfer the style of Figure 2 to Figure 1, and keep the content and characteristics of Figure 1.', show_label=True, placeholder='Style Transfer the style of Figure 2 to Figure 1, and keep the content and characteristics of Figure 1.', container=True, ) run_button = gr.Button("Edit!", variant="primary")
with gr.Accordion("Advanced Settings", open=True): # Negative prompt UI element is removed here
seed = gr.Slider( label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=123, )
randomize_seed = gr.Checkbox(label="Randomize seed", value=False)
with gr.Row():
true_guidance_scale = gr.Slider( label="CFG should be 1.0", minimum=0, maximum=10.0, step=0.1, value=1.0 )
num_inference_steps = gr.Slider( label="Number of inference steps should be 4", minimum=1, maximum=50, step=1, value=4, ) minedge = gr.Slider( label="Min Edge of the generated image", minimum=256, maximum=2048, step=8, value=1024, ) with gr.Row(), gr.Column(): gr.Markdown("## Examples") gr.Markdown("changing the minedge could lead to different style similarity.") default_prompt='Style Transfer the style of Figure 2 to Figure 1, and keep the content and characteristics of Figure 1.' gr.Examples(examples=[ ['./qwenstyleref/pulpfiction_2.jpg','./qwenstyleref/styleref=6_style_ref.png',default_prompt], ['./qwenstyleref/styleref=0_content_ref.png','./qwenstyleref/110.png',default_prompt], ['./qwenstyleref/romanholiday_1.jpg','./qwenstyleref/s0099____1113_01_query_1_img_000146_1682705733350_08158389675901344.jpg.jpg',default_prompt], ['./qwenstyleref/styleref=0_content_ref.png','./qwenstyleref/125.png',default_prompt], ['./qwenstyleref/fallenangle.jpg','./qwenstyleref/styleref=s0038.png',default_prompt], ['./qwenstyleref/styleref=0_content_ref.png','./qwenstyleref/styleref=s0572.png',default_prompt], ['./qwenstyleref/startrooper1.jpg','./qwenstyleref/david-face-760x985.jpg','Style Transfer Figure 1 into marble material.'], ['./qwenstyleref/possession.png','./qwenstyleref/s0026____0907_01_query_0_img_000194_1682674358294_041656249089406583.jpeg.jpg',default_prompt], ['./qwenstyleref/styleref=0_content_ref.png','./qwenstyleref/Jotarokujo.webp',default_prompt], ['./qwenstyleref/wallstreet1.jpg','./qwenstyleref/034.png',default_prompt], ['./qwenstyleref/bird.jpeg','./qwenstyleref/styleref=s0539.png',default_prompt],
], inputs=[content_ref,style_ref, prompt], outputs=[result, seed], fn=infer, cache_examples=False )
# gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=False)
gr.on( triggers=[run_button.click], fn=infer, inputs=[ content_ref, style_ref, prompt, seed, randomize_seed, true_guidance_scale, num_inference_steps, minedge, ], outputs=[result, seed], )
if __name__ == "__main__": demo.launch(server_name='0.0.0.0')app.pyAdded+247