Spaces:
Running
on
Zero
Running
on
Zero
File size: 9,192 Bytes
31c3d78 b8ca898 2623569 b8ca898 705e6ea b8ca898 2623569 b8ca898 92a59f7 abdde98 b8ca898 abdde98 b8ca898 f6b062b b8ca898 abdde98 b8ca898 abdde98 f6b062b b8ca898 abdde98 b8ca898 abdde98 b8ca898 921c19d 2c05742 b8ca898 abdde98 ecb1594 abdde98 b8ca898 629b69c 4cd44f0 629b69c b8ca898 abdde98 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
import gradio as gr
import numpy as np
import random
import torch
import spaces
from PIL import Image
from 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",
download_source='huggingface',
origin_file_pattern="transformer/diffusion_pytorch_model*.safetensors"),
ModelConfig(model_id="Qwen/Qwen-Image-Edit-2509",
download_source='huggingface',origin_file_pattern="text_encoder/model*.safetensors"),
ModelConfig(model_id="Qwen/Qwen-Image-Edit-2509",
download_source='huggingface',origin_file_pattern="vae/diffusion_pytorch_model.safetensors"),
],
tokenizer_config=None,
processor_config=ModelConfig(model_id="Qwen/Qwen-Image-Edit-2509",
download_source='huggingface',origin_file_pattern="processor/"),
)
speedup = hf_hub_download(repo_id="Tele-AI/TeleStyle", filename="weights/diffsynth_Qwen-Image-Edit-2509-Lightning-4steps-V1.0-bf16.safetensors")
telestyle= hf_hub_download(repo_id="Tele-AI/TeleStyle", filename="weights/diffsynth_Qwen-Image-Edit-2509-telestyle.safetensors")
pipe.load_lora(pipe.dit, telestyle)
pipe.load_lora(pipe.dit,speedup)
dtype = torch.bfloat16
device = "cuda" if torch.cuda.is_available() else "cpu"
MAX_SEED = np.iinfo(np.int32).max
@spaces.GPU
def 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=minedge-minedge%16
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;">TeleStyle</h1>
</div>
<p style="font-size: 1rem; margin-bottom: 1.5rem;">Paper: <a href='https://arxiv.org/abs/2601.20175' target='_blank'>TeleStyle: Content-Preserving Style Transfer in Images and Videos</a> | Codes: <a href='https://github.com/Tele-AI/TeleStyle/' target='_blank'>GitHub</a></p>
<p style="font-size: 1rem; margin-bottom: 1.5rem;">If you encounter an Error with this demo, the most possible reason is ZeroGPU out-of-memory and the solution is to decrease the Min Edge of the generated image from 1024 to a lower value. This is because ZeroGPU has a memory limit of 70GB, while all the examples are tested with 80GB H100 GPUs. </p>
'''
with gr.Blocks() as demo:
with gr.Column(elem_id="col-container"):
gr.Markdown(_HEADER_)
gr.Markdown("This is a demo of TeleStyle-Image, enabling Content-Preserving Style Transfer capability to 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,123,False,1.0,4,832],
['./qwenstyleref/styleref=0_content_ref.png','./qwenstyleref/110.png',default_prompt,123,False,1.0,4,832],
['./qwenstyleref/romanholiday_1.jpg','./qwenstyleref/s0099____1113_01_query_1_img_000146_1682705733350_08158389675901344.jpg.jpg',default_prompt,123,False,1.0,4,800],
['./qwenstyleref/styleref=0_content_ref.png','./qwenstyleref/125.png',default_prompt,123,False,1.0,4,832],
['./qwenstyleref/fallenangle.jpg','./qwenstyleref/styleref=s0038.png',default_prompt,123,False,1.0,4,832],
['./qwenstyleref/styleref=0_content_ref.png','./qwenstyleref/styleref=s0572.png',default_prompt,123,False,1.0,4,832],
['./qwenstyleref/startrooper1.jpg','./qwenstyleref/david-face-760x985.jpg','Style Transfer Figure 1 into marble material.',123,False,1.0,4,1024],
['./qwenstyleref/startrooper1.jpg','./qwenstyleref/125.png',default_prompt, 123,False,1.0,4,1024],
['./qwenstyleref/possession.png','./qwenstyleref/s0026____0907_01_query_0_img_000194_1682674358294_041656249089406583.jpeg.jpg',default_prompt,123,False,1.0,4,832],
['./qwenstyleref/styleref=0_content_ref.png','./qwenstyleref/Jotarokujo.webp',default_prompt,123,False,1.0,4,832],
['./qwenstyleref/wallstreet1.jpg','./qwenstyleref/034.png',default_prompt,123,False,1.0,4,1024],
['./qwenstyleref/bird.jpeg','./qwenstyleref/styleref=s0539.png',default_prompt,123,False,1.0,4,832],
],
inputs=[content_ref,
style_ref,
prompt,
seed,
randomize_seed,
true_guidance_scale,
num_inference_steps,
minedge,],
#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') |