Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,26 +2,28 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
|
| 5 |
-
# import spaces #[uncomment to use ZeroGPU]
|
| 6 |
from diffusers import DiffusionPipeline
|
| 7 |
import torch
|
| 8 |
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
-
model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
| 18 |
pipe = pipe.to(device)
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
MAX_SEED = np.iinfo(np.int32).max
|
| 21 |
MAX_IMAGE_SIZE = 1024
|
| 22 |
|
| 23 |
|
| 24 |
-
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 25 |
def infer(
|
| 26 |
prompt,
|
| 27 |
negative_prompt,
|
|
@@ -38,35 +40,36 @@ def infer(
|
|
| 38 |
|
| 39 |
generator = torch.Generator().manual_seed(seed)
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
| 44 |
guidance_scale=guidance_scale,
|
| 45 |
num_inference_steps=num_inference_steps,
|
| 46 |
width=width,
|
| 47 |
height=height,
|
| 48 |
generator=generator,
|
| 49 |
-
).images
|
| 50 |
|
| 51 |
-
return
|
| 52 |
|
| 53 |
|
| 54 |
examples = [
|
| 55 |
-
"
|
| 56 |
-
"
|
| 57 |
-
"
|
| 58 |
]
|
| 59 |
|
| 60 |
css = """
|
| 61 |
#col-container {
|
| 62 |
margin: 0 auto;
|
| 63 |
-
max-width:
|
| 64 |
}
|
| 65 |
"""
|
| 66 |
|
| 67 |
with gr.Blocks(css=css) as demo:
|
| 68 |
with gr.Column(elem_id="col-container"):
|
| 69 |
-
gr.Markdown("
|
| 70 |
|
| 71 |
with gr.Row():
|
| 72 |
prompt = gr.Text(
|
|
@@ -76,17 +79,16 @@ with gr.Blocks(css=css) as demo:
|
|
| 76 |
placeholder="Enter your prompt",
|
| 77 |
container=False,
|
| 78 |
)
|
| 79 |
-
|
| 80 |
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 81 |
|
| 82 |
-
|
|
|
|
| 83 |
|
| 84 |
with gr.Accordion("Advanced Settings", open=False):
|
| 85 |
negative_prompt = gr.Text(
|
| 86 |
label="Negative prompt",
|
| 87 |
max_lines=1,
|
| 88 |
-
placeholder="
|
| 89 |
-
visible=False,
|
| 90 |
)
|
| 91 |
|
| 92 |
seed = gr.Slider(
|
|
@@ -105,7 +107,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 105 |
minimum=256,
|
| 106 |
maximum=MAX_IMAGE_SIZE,
|
| 107 |
step=32,
|
| 108 |
-
value=1024,
|
| 109 |
)
|
| 110 |
|
| 111 |
height = gr.Slider(
|
|
@@ -113,7 +115,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 113 |
minimum=256,
|
| 114 |
maximum=MAX_IMAGE_SIZE,
|
| 115 |
step=32,
|
| 116 |
-
value=1024,
|
| 117 |
)
|
| 118 |
|
| 119 |
with gr.Row():
|
|
@@ -122,18 +124,19 @@ with gr.Blocks(css=css) as demo:
|
|
| 122 |
minimum=0.0,
|
| 123 |
maximum=10.0,
|
| 124 |
step=0.1,
|
| 125 |
-
value=
|
| 126 |
)
|
| 127 |
|
| 128 |
num_inference_steps = gr.Slider(
|
| 129 |
-
label="
|
| 130 |
minimum=1,
|
| 131 |
maximum=50,
|
| 132 |
step=1,
|
| 133 |
-
value=
|
| 134 |
)
|
| 135 |
|
| 136 |
gr.Examples(examples=examples, inputs=[prompt])
|
|
|
|
| 137 |
gr.on(
|
| 138 |
triggers=[run_button.click, prompt.submit],
|
| 139 |
fn=infer,
|
|
@@ -151,4 +154,4 @@ with gr.Blocks(css=css) as demo:
|
|
| 151 |
)
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
| 154 |
-
demo.launch()
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
|
|
|
|
| 5 |
from diffusers import DiffusionPipeline
|
| 6 |
import torch
|
| 7 |
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 9 |
|
| 10 |
+
# ✅ WAI Illustrious 1.6 model
|
| 11 |
+
model_repo_id = "WAI-Illustrious/WAI-Illustrious-1.6"
|
| 12 |
+
|
| 13 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 14 |
|
| 15 |
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
| 16 |
pipe = pipe.to(device)
|
| 17 |
|
| 18 |
+
# ✅ Performance optimizations
|
| 19 |
+
if torch.cuda.is_available():
|
| 20 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 21 |
+
pipe.enable_model_cpu_offload()
|
| 22 |
+
|
| 23 |
MAX_SEED = np.iinfo(np.int32).max
|
| 24 |
MAX_IMAGE_SIZE = 1024
|
| 25 |
|
| 26 |
|
|
|
|
| 27 |
def infer(
|
| 28 |
prompt,
|
| 29 |
negative_prompt,
|
|
|
|
| 40 |
|
| 41 |
generator = torch.Generator().manual_seed(seed)
|
| 42 |
|
| 43 |
+
# ✅ Generate 4 images
|
| 44 |
+
images = pipe(
|
| 45 |
+
prompt=[prompt] * 4,
|
| 46 |
+
negative_prompt=[negative_prompt] * 4 if negative_prompt else None,
|
| 47 |
guidance_scale=guidance_scale,
|
| 48 |
num_inference_steps=num_inference_steps,
|
| 49 |
width=width,
|
| 50 |
height=height,
|
| 51 |
generator=generator,
|
| 52 |
+
).images
|
| 53 |
|
| 54 |
+
return images, seed
|
| 55 |
|
| 56 |
|
| 57 |
examples = [
|
| 58 |
+
"masterpiece, best quality, anime girl, detailed eyes",
|
| 59 |
+
"1girl, silver hair, fantasy armor, glowing sword",
|
| 60 |
+
"anime landscape, sunset, cinematic lighting",
|
| 61 |
]
|
| 62 |
|
| 63 |
css = """
|
| 64 |
#col-container {
|
| 65 |
margin: 0 auto;
|
| 66 |
+
max-width: 720px;
|
| 67 |
}
|
| 68 |
"""
|
| 69 |
|
| 70 |
with gr.Blocks(css=css) as demo:
|
| 71 |
with gr.Column(elem_id="col-container"):
|
| 72 |
+
gr.Markdown("# WAI Illustrious 1.6 - Text to Image")
|
| 73 |
|
| 74 |
with gr.Row():
|
| 75 |
prompt = gr.Text(
|
|
|
|
| 79 |
placeholder="Enter your prompt",
|
| 80 |
container=False,
|
| 81 |
)
|
|
|
|
| 82 |
run_button = gr.Button("Run", scale=0, variant="primary")
|
| 83 |
|
| 84 |
+
# ✅ Gallery instead of single image
|
| 85 |
+
result = gr.Gallery(label="Results", show_label=False, columns=2)
|
| 86 |
|
| 87 |
with gr.Accordion("Advanced Settings", open=False):
|
| 88 |
negative_prompt = gr.Text(
|
| 89 |
label="Negative prompt",
|
| 90 |
max_lines=1,
|
| 91 |
+
placeholder="low quality, bad anatomy",
|
|
|
|
| 92 |
)
|
| 93 |
|
| 94 |
seed = gr.Slider(
|
|
|
|
| 107 |
minimum=256,
|
| 108 |
maximum=MAX_IMAGE_SIZE,
|
| 109 |
step=32,
|
| 110 |
+
value=1024,
|
| 111 |
)
|
| 112 |
|
| 113 |
height = gr.Slider(
|
|
|
|
| 115 |
minimum=256,
|
| 116 |
maximum=MAX_IMAGE_SIZE,
|
| 117 |
step=32,
|
| 118 |
+
value=1024,
|
| 119 |
)
|
| 120 |
|
| 121 |
with gr.Row():
|
|
|
|
| 124 |
minimum=0.0,
|
| 125 |
maximum=10.0,
|
| 126 |
step=0.1,
|
| 127 |
+
value=5.0, # ✅ better default
|
| 128 |
)
|
| 129 |
|
| 130 |
num_inference_steps = gr.Slider(
|
| 131 |
+
label="Steps",
|
| 132 |
minimum=1,
|
| 133 |
maximum=50,
|
| 134 |
step=1,
|
| 135 |
+
value=25, # ✅ better default
|
| 136 |
)
|
| 137 |
|
| 138 |
gr.Examples(examples=examples, inputs=[prompt])
|
| 139 |
+
|
| 140 |
gr.on(
|
| 141 |
triggers=[run_button.click, prompt.submit],
|
| 142 |
fn=infer,
|
|
|
|
| 154 |
)
|
| 155 |
|
| 156 |
if __name__ == "__main__":
|
| 157 |
+
demo.launch()
|