Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,154 +1,76 @@
|
|
| 1 |
-
import
|
| 2 |
-
import numpy as np
|
| 3 |
-
import random
|
| 4 |
|
| 5 |
-
|
| 6 |
-
from diffusers import DiffusionPipeline
|
| 7 |
import torch
|
| 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 |
-
progress=gr.Progress(track_tqdm=True),
|
| 35 |
-
):
|
| 36 |
-
if randomize_seed:
|
| 37 |
-
seed = random.randint(0, MAX_SEED)
|
| 38 |
-
|
| 39 |
-
generator = torch.Generator().manual_seed(seed)
|
| 40 |
-
|
| 41 |
image = pipe(
|
| 42 |
-
prompt
|
| 43 |
-
negative_prompt=negative_prompt,
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
generator=generator,
|
| 49 |
).images[0]
|
|
|
|
| 50 |
|
| 51 |
-
return image, seed
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
examples = [
|
| 55 |
-
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 56 |
-
"An astronaut riding a green horse",
|
| 57 |
-
"A delicious ceviche cheesecake slice",
|
| 58 |
-
]
|
| 59 |
-
|
| 60 |
-
css = """
|
| 61 |
-
#col-container {
|
| 62 |
-
margin: 0 auto;
|
| 63 |
-
max-width: 640px;
|
| 64 |
-
}
|
| 65 |
-
"""
|
| 66 |
-
|
| 67 |
-
with gr.Blocks(css=css) as demo:
|
| 68 |
-
with gr.Column(elem_id="col-container"):
|
| 69 |
-
gr.Markdown(" # Text-to-Image Gradio Template")
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
label="Prompt",
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
placeholder="Enter your prompt",
|
| 77 |
-
container=False,
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 81 |
-
|
| 82 |
-
result = gr.Image(label="Result", show_label=False)
|
| 83 |
-
|
| 84 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 85 |
-
negative_prompt = gr.Text(
|
| 86 |
-
label="Negative prompt",
|
| 87 |
-
max_lines=1,
|
| 88 |
-
placeholder="Enter a negative prompt",
|
| 89 |
-
visible=False,
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
seed = gr.Slider(
|
| 93 |
-
label="Seed",
|
| 94 |
-
minimum=0,
|
| 95 |
-
maximum=MAX_SEED,
|
| 96 |
-
step=1,
|
| 97 |
-
value=0,
|
| 98 |
)
|
| 99 |
-
|
| 100 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 101 |
-
|
| 102 |
with gr.Row():
|
| 103 |
-
width = gr.Slider(
|
| 104 |
-
|
| 105 |
-
minimum=256,
|
| 106 |
-
maximum=MAX_IMAGE_SIZE,
|
| 107 |
-
step=32,
|
| 108 |
-
value=1024, # Replace with defaults that work for your model
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
height = gr.Slider(
|
| 112 |
-
label="Height",
|
| 113 |
-
minimum=256,
|
| 114 |
-
maximum=MAX_IMAGE_SIZE,
|
| 115 |
-
step=32,
|
| 116 |
-
value=1024, # Replace with defaults that work for your model
|
| 117 |
-
)
|
| 118 |
-
|
| 119 |
with gr.Row():
|
| 120 |
-
guidance_scale = gr.Slider(
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
maximum=50,
|
| 132 |
-
step=1,
|
| 133 |
-
value=2, # Replace with defaults that work for your model
|
| 134 |
-
)
|
| 135 |
-
|
| 136 |
-
gr.Examples(examples=examples, inputs=[prompt])
|
| 137 |
-
gr.on(
|
| 138 |
-
triggers=[run_button.click, prompt.submit],
|
| 139 |
-
fn=infer,
|
| 140 |
-
inputs=[
|
| 141 |
-
prompt,
|
| 142 |
-
negative_prompt,
|
| 143 |
-
seed,
|
| 144 |
-
randomize_seed,
|
| 145 |
-
width,
|
| 146 |
-
height,
|
| 147 |
-
guidance_scale,
|
| 148 |
-
num_inference_steps,
|
| 149 |
-
],
|
| 150 |
-
outputs=[result, seed],
|
| 151 |
)
|
| 152 |
|
| 153 |
-
|
| 154 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
import spaces
|
|
|
|
| 4 |
import torch
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from diffusers import Ideogram4Pipeline
|
| 7 |
+
|
| 8 |
+
# Use the nf4-quantized repo -- this is the one diffusers' Ideogram4Pipeline
|
| 9 |
+
# actually supports. The -fp8 repo ships its own custom runtime and is not
|
| 10 |
+
# loadable via from_pretrained.
|
| 11 |
+
MODEL_ID = "ideogram-ai/ideogram-4-nf4"
|
| 12 |
+
|
| 13 |
+
# ZeroGPU Spaces need a Space secret (Settings -> Variables and secrets) named
|
| 14 |
+
# HF_TOKEN, since Ideogram 4's weights are gated and require an authenticated,
|
| 15 |
+
# license-accepted account to download.
|
| 16 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 17 |
+
|
| 18 |
+
# Load once at import time, on CPU. ZeroGPU intercepts `.to("cuda")` here so
|
| 19 |
+
# this is safe even though no GPU is attached to the process yet -- the real
|
| 20 |
+
# GPU is only attached for the duration of a call into an @spaces.GPU function.
|
| 21 |
+
pipe = Ideogram4Pipeline.from_pretrained(
|
| 22 |
+
MODEL_ID,
|
| 23 |
+
dtype=torch.bfloat16,
|
| 24 |
+
token=HF_TOKEN,
|
| 25 |
+
).to("cuda")
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
@spaces.GPU(duration=120) # seconds of GPU time granted per call; raise if you increase steps/resolution
|
| 29 |
+
def generate(prompt, negative_prompt, width, height, guidance_scale, steps, seed):
|
| 30 |
+
generator = torch.Generator("cuda").manual_seed(int(seed))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
image = pipe(
|
| 32 |
+
prompt,
|
| 33 |
+
negative_prompt=negative_prompt or None,
|
| 34 |
+
height=int(height),
|
| 35 |
+
width=int(width),
|
| 36 |
+
guidance_scale=float(guidance_scale),
|
| 37 |
+
num_inference_steps=int(steps),
|
| 38 |
generator=generator,
|
| 39 |
).images[0]
|
| 40 |
+
return image
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
with gr.Blocks(title="Ideogram 4") as demo:
|
| 44 |
+
gr.Markdown(
|
| 45 |
+
"# Ideogram 4\n"
|
| 46 |
+
"Text-to-image with [Ideogram 4](https://huggingface.co/ideogram-ai/ideogram-4-nf4), "
|
| 47 |
+
"running on ZeroGPU. Note: Ideogram 4 was trained on structured JSON captions -- "
|
| 48 |
+
"short natural-language prompts still work, but for best results expand your "
|
| 49 |
+
"prompt into their JSON schema first (see the model card)."
|
| 50 |
+
)
|
| 51 |
+
with gr.Row():
|
| 52 |
+
with gr.Column():
|
| 53 |
+
prompt = gr.Textbox(
|
| 54 |
label="Prompt",
|
| 55 |
+
lines=3,
|
| 56 |
+
placeholder="A photo of a cat holding a sign that says hello world",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
)
|
| 58 |
+
negative_prompt = gr.Textbox(label="Negative prompt (optional)", lines=2)
|
|
|
|
|
|
|
| 59 |
with gr.Row():
|
| 60 |
+
width = gr.Slider(512, 2048, value=1024, step=64, label="Width")
|
| 61 |
+
height = gr.Slider(512, 2048, value=1024, step=64, label="Height")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
with gr.Row():
|
| 63 |
+
guidance_scale = gr.Slider(1.0, 15.0, value=7.0, step=0.5, label="Guidance scale")
|
| 64 |
+
steps = gr.Slider(4, 60, value=48, step=1, label="Steps")
|
| 65 |
+
seed = gr.Number(value=0, label="Seed", precision=0)
|
| 66 |
+
run_btn = gr.Button("Generate", variant="primary")
|
| 67 |
+
with gr.Column():
|
| 68 |
+
output = gr.Image(label="Result")
|
| 69 |
+
|
| 70 |
+
run_btn.click(
|
| 71 |
+
fn=generate,
|
| 72 |
+
inputs=[prompt, negative_prompt, width, height, guidance_scale, steps, seed],
|
| 73 |
+
outputs=output,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
)
|
| 75 |
|
| 76 |
+
demo.queue().launch()
|
|
|