Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import spaces
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from diffusers import DiffusionPipeline
|
| 5 |
+
import diffusers
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
# =========================================================
|
| 9 |
+
# MODEL CONFIGURATION
|
| 10 |
+
# =========================================================
|
| 11 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 12 |
+
|
| 13 |
+
# =========================================================
|
| 14 |
+
# LOAD PIPELINE
|
| 15 |
+
# =========================================================
|
| 16 |
+
print("Loading Z-Image-Turbo pipeline...")
|
| 17 |
+
diffusers.utils.logging.set_verbosity_info()
|
| 18 |
+
|
| 19 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 20 |
+
"Tongyi-MAI/Z-Image-Turbo",
|
| 21 |
+
torch_dtype=torch.bfloat16,
|
| 22 |
+
low_cpu_mem_usage=False,
|
| 23 |
+
attn_implementation="kernels-community/vllm-flash-attn3",
|
| 24 |
+
)
|
| 25 |
+
pipe.to("cuda")
|
| 26 |
+
|
| 27 |
+
# =========================================================
|
| 28 |
+
# IMAGE GENERATOR
|
| 29 |
+
# =========================================================
|
| 30 |
+
@spaces.GPU
|
| 31 |
+
def generate_image(prompt, height, width, num_inference_steps, seed, randomize_seed, num_images):
|
| 32 |
+
if not prompt:
|
| 33 |
+
raise gr.Error("Please enter a prompt.")
|
| 34 |
+
|
| 35 |
+
if randomize_seed:
|
| 36 |
+
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
| 37 |
+
|
| 38 |
+
num_images = min(max(1, int(num_images)), 4)
|
| 39 |
+
|
| 40 |
+
generator = torch.Generator("cuda").manual_seed(int(seed))
|
| 41 |
+
|
| 42 |
+
result = pipe(
|
| 43 |
+
prompt=prompt,
|
| 44 |
+
height=int(height),
|
| 45 |
+
width=int(width),
|
| 46 |
+
num_inference_steps=int(num_inference_steps),
|
| 47 |
+
guidance_scale=0.0,
|
| 48 |
+
generator=generator,
|
| 49 |
+
max_sequence_length=1024,
|
| 50 |
+
num_images_per_prompt=num_images,
|
| 51 |
+
output_type="pil",
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
return result.images, seed
|
| 55 |
+
|
| 56 |
+
# =========================================================
|
| 57 |
+
# GRADIO UI
|
| 58 |
+
# =========================================================
|
| 59 |
+
with gr.Blocks() as demo:
|
| 60 |
+
|
| 61 |
+
gr.HTML("""
|
| 62 |
+
<style>
|
| 63 |
+
.gradio-container {
|
| 64 |
+
background: linear-gradient(135deg, #fef9f3 0%, #f0e6fa 50%, #e6f0fa 100%) !important;
|
| 65 |
+
}
|
| 66 |
+
footer {display: none !important;}
|
| 67 |
+
</style>
|
| 68 |
+
<div style="text-align: center; margin-bottom: 20px;">
|
| 69 |
+
<h1 style="color: #6b5b7a; font-size: 2.2rem; font-weight: 700; margin-bottom: 0.3rem;">
|
| 70 |
+
πΌοΈ NSFW Uncensored "Text to Image"
|
| 71 |
+
</h1>
|
| 72 |
+
<p style="color: #8b7b9b; font-size: 1rem;">Powered by Z-Image-Turbo Model</p>
|
| 73 |
+
</div>
|
| 74 |
+
""")
|
| 75 |
+
|
| 76 |
+
with gr.Row():
|
| 77 |
+
with gr.Column(scale=1):
|
| 78 |
+
prompt_input = gr.Textbox(
|
| 79 |
+
label="βοΈ Prompt",
|
| 80 |
+
placeholder="Describe the image you want to create...",
|
| 81 |
+
lines=3
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
with gr.Row():
|
| 85 |
+
height_input = gr.Slider(512, 2048, 1024, step=64, label="Height")
|
| 86 |
+
width_input = gr.Slider(512, 2048, 1024, step=64, label="Width")
|
| 87 |
+
|
| 88 |
+
num_images_input = gr.Slider(1, 4, 2, step=1, label="πΌοΈ Number of Images")
|
| 89 |
+
|
| 90 |
+
with gr.Accordion("βοΈ Options", open=False):
|
| 91 |
+
steps_slider = gr.Slider(
|
| 92 |
+
minimum=1,
|
| 93 |
+
maximum=30,
|
| 94 |
+
step=1,
|
| 95 |
+
value=18,
|
| 96 |
+
label="Inference Steps"
|
| 97 |
+
)
|
| 98 |
+
seed_input = gr.Slider(
|
| 99 |
+
label="Seed",
|
| 100 |
+
minimum=0,
|
| 101 |
+
maximum=MAX_SEED,
|
| 102 |
+
step=1,
|
| 103 |
+
value=42
|
| 104 |
+
)
|
| 105 |
+
randomize_seed_checkbox = gr.Checkbox(
|
| 106 |
+
label="Randomize Seed",
|
| 107 |
+
value=True
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
generate_button = gr.Button(
|
| 111 |
+
"β¨ Generate Image",
|
| 112 |
+
variant="primary"
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
with gr.Column(scale=1):
|
| 116 |
+
output_gallery = gr.Gallery(
|
| 117 |
+
label="π¨ Generated Images",
|
| 118 |
+
height=450,
|
| 119 |
+
columns=2
|
| 120 |
+
)
|
| 121 |
+
used_seed_output = gr.Number(label="Seed Used", interactive=False)
|
| 122 |
+
|
| 123 |
+
generate_button.click(
|
| 124 |
+
fn=generate_image,
|
| 125 |
+
inputs=[prompt_input, height_input, width_input, steps_slider, seed_input, randomize_seed_checkbox, num_images_input],
|
| 126 |
+
outputs=[output_gallery, used_seed_output],
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
if __name__ == "__main__":
|
| 130 |
+
demo.queue().launch()
|