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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import random
|
| 4 |
+
import spaces
|
| 5 |
+
import torch
|
| 6 |
+
from diffusers import QwenImagePipeline
|
| 7 |
+
|
| 8 |
+
dtype = torch.bfloat16
|
| 9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
+
|
| 11 |
+
pipe = QwenImagePipeline.from_pretrained("Qwen/Qwen-Image", torch_dtype=dtype).to(device)
|
| 12 |
+
|
| 13 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 14 |
+
MAX_IMAGE_SIZE = 1536
|
| 15 |
+
|
| 16 |
+
@spaces.GPU()
|
| 17 |
+
def infer(prompt, negative_prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, true_cfg_scale=4.0, distilled_cfg_scale=1.0, progress=gr.Progress(track_tqdm=True)):
|
| 18 |
+
"""
|
| 19 |
+
Generates an image based on a user's prompt using the Qwen-Image pipeline.
|
| 20 |
+
|
| 21 |
+
This function takes textual prompts and various generation parameters,
|
| 22 |
+
handles seed randomization, and runs the diffusion model to produce an image.
|
| 23 |
+
|
| 24 |
+
Args:
|
| 25 |
+
prompt (str): The positive text prompt to guide image generation.
|
| 26 |
+
negative_prompt (str): The negative text prompt to guide the model
|
| 27 |
+
on what to avoid in the generated image.
|
| 28 |
+
seed (int, optional): The seed for the random number generator to ensure
|
| 29 |
+
reproducible results. Defaults to 42.
|
| 30 |
+
randomize_seed (bool, optional): If True, a random seed is generated,
|
| 31 |
+
overriding the `seed` parameter. Defaults to False.
|
| 32 |
+
width (int, optional): The width of the generated image in pixels.
|
| 33 |
+
Defaults to 1024.
|
| 34 |
+
height (int, optional): The height of the generated image in pixels.
|
| 35 |
+
Defaults to 1024.
|
| 36 |
+
num_inference_steps (int, optional): The number of denoising steps.
|
| 37 |
+
More steps can lead to higher quality but take longer. Defaults to 4.
|
| 38 |
+
true_cfg_scale (float, optional): The Classifier-Free Guidance scale.
|
| 39 |
+
Controls how strictly the model follows the prompt. Defaults to 4.0.
|
| 40 |
+
progress (gr.Progress, optional): A Gradio Progress object to track
|
| 41 |
+
the inference progress in the UI.
|
| 42 |
+
|
| 43 |
+
Returns:
|
| 44 |
+
tuple: A tuple containing:
|
| 45 |
+
- PIL.Image.Image: The generated image.
|
| 46 |
+
- int: The seed used for the generation, which is useful for
|
| 47 |
+
reproducibility, especially when `randomize_seed` is True.
|
| 48 |
+
"""
|
| 49 |
+
if randomize_seed:
|
| 50 |
+
seed = random.randint(0, MAX_SEED)
|
| 51 |
+
|
| 52 |
+
generator = torch.Generator().manual_seed(seed)
|
| 53 |
+
|
| 54 |
+
image = pipe(
|
| 55 |
+
prompt=prompt,
|
| 56 |
+
negative_prompt=negative_prompt,
|
| 57 |
+
width=width,
|
| 58 |
+
height=height,
|
| 59 |
+
num_inference_steps=num_inference_steps,
|
| 60 |
+
generator=generator,
|
| 61 |
+
true_cfg_scale=true_cfg_scale,
|
| 62 |
+
guidance_scale=distilled_cfg_scale
|
| 63 |
+
).images[0]
|
| 64 |
+
|
| 65 |
+
return image, seed
|
| 66 |
+
|
| 67 |
+
examples = [
|
| 68 |
+
"a tiny dragon hatching from a crystal egg on Mars",
|
| 69 |
+
"a red panda holding a sign that says 'I love bamboo'",
|
| 70 |
+
"a photo of a capybara riding a tricycle in Paris. It is wearing a beret and a striped shirt.",
|
| 71 |
+
"an anime illustration of a delicious ramen bowl",
|
| 72 |
+
"A logo for a bookstore called 'The Whispering Page'. The logo should feature an open book with a tree growing out of it.",
|
| 73 |
+
]
|
| 74 |
+
|
| 75 |
+
css="""
|
| 76 |
+
#col-container {
|
| 77 |
+
margin: 0 auto;
|
| 78 |
+
max-width: 580px;
|
| 79 |
+
}
|
| 80 |
+
"""
|
| 81 |
+
|
| 82 |
+
# Build the Gradio UI.
|
| 83 |
+
with gr.Blocks(css=css) as demo:
|
| 84 |
+
|
| 85 |
+
with gr.Column(elem_id="col-container"):
|
| 86 |
+
# Title and description for the demo.
|
| 87 |
+
gr.Markdown(f"""# Qwen-Image Text-to-Image
|
| 88 |
+
Gradio demo for [Qwen-Image](https://huggingface.co/Qwen/Qwen-Image), a powerful text-to-image model from the Qwen (通义千问) team at Alibaba.
|
| 89 |
+
""")
|
| 90 |
+
|
| 91 |
+
with gr.Row():
|
| 92 |
+
# Main prompt input.
|
| 93 |
+
prompt = gr.Text(
|
| 94 |
+
label="Prompt",
|
| 95 |
+
show_label=False,
|
| 96 |
+
max_lines=1,
|
| 97 |
+
placeholder="Enter your prompt",
|
| 98 |
+
container=False,
|
| 99 |
+
)
|
| 100 |
+
# The "Run" button.
|
| 101 |
+
run_button = gr.Button("Run", scale=0)
|
| 102 |
+
|
| 103 |
+
# Negative prompt input.
|
| 104 |
+
negative_prompt = gr.Text(
|
| 105 |
+
label="Negative Prompt",
|
| 106 |
+
max_lines=1,
|
| 107 |
+
placeholder="Enter a negative prompt",
|
| 108 |
+
value="text, watermark, copyright, blurry, low resolution",
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
# Display area for the generated image.
|
| 112 |
+
result = gr.Image(label="Result", show_label=False)
|
| 113 |
+
|
| 114 |
+
# Accordion for advanced settings.
|
| 115 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 116 |
+
|
| 117 |
+
seed = gr.Slider(
|
| 118 |
+
label="Seed",
|
| 119 |
+
minimum=0,
|
| 120 |
+
maximum=MAX_SEED,
|
| 121 |
+
step=1,
|
| 122 |
+
value=42,
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 126 |
+
|
| 127 |
+
with gr.Row():
|
| 128 |
+
width = gr.Slider(
|
| 129 |
+
label="Width",
|
| 130 |
+
minimum=256,
|
| 131 |
+
maximum=MAX_IMAGE_SIZE,
|
| 132 |
+
step=32,
|
| 133 |
+
value=1024,
|
| 134 |
+
)
|
| 135 |
+
height = gr.Slider(
|
| 136 |
+
label="Height",
|
| 137 |
+
minimum=256,
|
| 138 |
+
maximum=MAX_IMAGE_SIZE,
|
| 139 |
+
step=32,
|
| 140 |
+
value=1024,
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
with gr.Row():
|
| 144 |
+
num_inference_steps = gr.Slider(
|
| 145 |
+
label="Inference Steps",
|
| 146 |
+
minimum=1,
|
| 147 |
+
maximum=50,
|
| 148 |
+
step=1,
|
| 149 |
+
value=4,
|
| 150 |
+
)
|
| 151 |
+
true_cfg_scale = gr.Slider(
|
| 152 |
+
label="CFG Scale",
|
| 153 |
+
info="Controls how much the model follows the prompt. Higher values mean stricter adherence.",
|
| 154 |
+
minimum=1.0,
|
| 155 |
+
maximum=10.0,
|
| 156 |
+
step=0.1,
|
| 157 |
+
value=4.0
|
| 158 |
+
)
|
| 159 |
+
distilled_cfg_scale = gr.Slider(
|
| 160 |
+
label="Distilled Guidance",
|
| 161 |
+
minimum=0.0,
|
| 162 |
+
maximum=20.0,
|
| 163 |
+
step=0.1,
|
| 164 |
+
value=1.0
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
gr.Examples(
|
| 168 |
+
examples=examples,
|
| 169 |
+
fn=infer,
|
| 170 |
+
inputs=[prompt, negative_prompt],
|
| 171 |
+
outputs=[result, seed],
|
| 172 |
+
cache_examples="lazy"
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
gr.on(
|
| 176 |
+
triggers=[run_button.click, prompt.submit, negative_prompt.submit],
|
| 177 |
+
fn=infer,
|
| 178 |
+
inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, num_inference_steps, true_cfg_scale, distilled_cfg_scale],
|
| 179 |
+
outputs=[result, seed]
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
demo.launch(mcp_server=True)
|