Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from diffusers import QwenImagePipeline, QwenImageTransformer2DModel
|
| 4 |
+
from transformers import Qwen2_5_VLForConditionalGeneration
|
| 5 |
+
|
| 6 |
+
# Set device and dtype
|
| 7 |
+
torch_dtype = torch.bfloat16
|
| 8 |
+
|
| 9 |
+
# Load models
|
| 10 |
+
transformer = QwenImageTransformer2DModel.from_pretrained(
|
| 11 |
+
"OzzyGT/Qwen-Image-2512-bnb-4bit-transformer",
|
| 12 |
+
torch_dtype=torch_dtype,
|
| 13 |
+
device_map="cpu" # offloading to CPU
|
| 14 |
+
)
|
| 15 |
+
text_encoder = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 16 |
+
"OzzyGT/Qwen-Image-2512-bnb-4bit-text-encoder",
|
| 17 |
+
torch_dtype=torch_dtype,
|
| 18 |
+
device_map="cpu"
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
pipe = QwenImagePipeline.from_pretrained(
|
| 22 |
+
"Qwen/Qwen-Image-2512",
|
| 23 |
+
transformer=transformer,
|
| 24 |
+
text_encoder=text_encoder,
|
| 25 |
+
torch_dtype=torch_dtype
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
pipe.enable_model_cpu_offload()
|
| 29 |
+
|
| 30 |
+
# Define the Gradio function
|
| 31 |
+
def generate_image(prompt, negative_prompt, width=1664, height=928, seed=42):
|
| 32 |
+
generator = torch.Generator(device="cuda" if torch.cuda.is_available() else "cpu").manual_seed(seed)
|
| 33 |
+
image = pipe(
|
| 34 |
+
prompt=prompt,
|
| 35 |
+
negative_prompt=negative_prompt,
|
| 36 |
+
width=width,
|
| 37 |
+
height=height,
|
| 38 |
+
num_inference_steps=28,
|
| 39 |
+
true_cfg_scale=4.0,
|
| 40 |
+
generator=generator,
|
| 41 |
+
).images[0]
|
| 42 |
+
return image
|
| 43 |
+
|
| 44 |
+
# Gradio interface
|
| 45 |
+
with gr.Blocks() as demo:
|
| 46 |
+
gr.Markdown("## Qwen Image Generation")
|
| 47 |
+
|
| 48 |
+
with gr.Row():
|
| 49 |
+
with gr.Column():
|
| 50 |
+
prompt_input = gr.Textbox(label="Prompt", lines=8, placeholder="Enter your prompt here...")
|
| 51 |
+
negative_prompt_input = gr.Textbox(label="Negative Prompt", lines=4, placeholder="Enter negative prompt...")
|
| 52 |
+
width_input = gr.Slider(256, 2048, value=1664, step=64, label="Width")
|
| 53 |
+
height_input = gr.Slider(256, 2048, value=928, step=64, label="Height")
|
| 54 |
+
seed_input = gr.Number(value=42, label="Random Seed")
|
| 55 |
+
generate_btn = gr.Button("Generate Image")
|
| 56 |
+
with gr.Column():
|
| 57 |
+
output_image = gr.Image(label="Generated Image")
|
| 58 |
+
|
| 59 |
+
generate_btn.click(
|
| 60 |
+
fn=generate_image,
|
| 61 |
+
inputs=[prompt_input, negative_prompt_input, width_input, height_input, seed_input],
|
| 62 |
+
outputs=output_image
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
demo.launch()
|