Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,74 +1,55 @@
|
|
1 |
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 |
-
torch_dtype = torch.float16
|
14 |
-
else:
|
15 |
-
torch_dtype = torch.float32
|
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 =
|
22 |
-
|
23 |
|
24 |
-
|
25 |
-
def infer(
|
26 |
-
prompt,
|
27 |
-
negative_prompt,
|
28 |
-
seed,
|
29 |
-
randomize_seed,
|
30 |
-
width,
|
31 |
-
height,
|
32 |
-
guidance_scale,
|
33 |
-
num_inference_steps,
|
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 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
).images[0]
|
50 |
-
|
51 |
return image, seed
|
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(
|
73 |
label="Prompt",
|
74 |
show_label=False,
|
@@ -76,19 +57,13 @@ 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
|
81 |
-
|
82 |
result = gr.Image(label="Result", show_label=False)
|
83 |
-
|
84 |
with gr.Accordion("Advanced Settings", open=False):
|
85 |
-
|
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,
|
@@ -96,59 +71,51 @@ with gr.Blocks(css=css) as demo:
|
|
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 |
label="Width",
|
105 |
minimum=256,
|
106 |
maximum=MAX_IMAGE_SIZE,
|
107 |
step=32,
|
108 |
-
value=1024,
|
109 |
)
|
110 |
-
|
111 |
height = gr.Slider(
|
112 |
label="Height",
|
113 |
minimum=256,
|
114 |
maximum=MAX_IMAGE_SIZE,
|
115 |
step=32,
|
116 |
-
value=1024,
|
117 |
)
|
118 |
-
|
119 |
with gr.Row():
|
120 |
-
|
121 |
-
|
122 |
-
minimum=0.0,
|
123 |
-
maximum=10.0,
|
124 |
-
step=0.1,
|
125 |
-
value=0.0, # Replace with defaults that work for your model
|
126 |
-
)
|
127 |
-
|
128 |
num_inference_steps = gr.Slider(
|
129 |
label="Number of inference steps",
|
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,
|
140 |
-
inputs=[
|
141 |
-
|
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 gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
4 |
+
import spaces
|
|
|
|
|
5 |
import torch
|
6 |
+
from diffusers import DiffusionPipeline
|
7 |
|
8 |
+
dtype = torch.bfloat16
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
10 |
|
11 |
+
pipe = DiffusionPipeline.from_pretrained("shuttleai/shuttle-3-diffusion", torch_dtype=dtype).to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
MAX_SEED = np.iinfo(np.int32).max
|
14 |
+
MAX_IMAGE_SIZE = 2048
|
|
|
15 |
|
16 |
+
@spaces.GPU()
|
17 |
+
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
if randomize_seed:
|
19 |
seed = random.randint(0, MAX_SEED)
|
|
|
20 |
generator = torch.Generator().manual_seed(seed)
|
|
|
21 |
image = pipe(
|
22 |
+
prompt = prompt,
|
23 |
+
width = width,
|
24 |
+
height = height,
|
25 |
+
num_inference_steps = num_inference_steps,
|
26 |
+
generator = generator,
|
27 |
+
guidance_scale=0.0
|
28 |
+
).images[0]
|
|
|
|
|
29 |
return image, seed
|
30 |
+
|
|
|
31 |
examples = [
|
32 |
+
"a tiny astronaut hatching from an egg on the moon",
|
33 |
+
"a cat holding a sign that says hello world",
|
34 |
+
"an anime illustration of a wiener schnitzel",
|
35 |
]
|
36 |
|
37 |
+
css="""
|
38 |
#col-container {
|
39 |
margin: 0 auto;
|
40 |
+
max-width: 520px;
|
41 |
}
|
42 |
"""
|
43 |
|
44 |
with gr.Blocks(css=css) as demo:
|
45 |
+
|
46 |
with gr.Column(elem_id="col-container"):
|
47 |
+
gr.Markdown(f"""# Shuttle 3 Diffusion
|
48 |
+
Shuttle 3 Diffusion is a text-to-image AI model designed to create detailed and diverse images from textual prompts in just 4 steps. It offers enhanced performance in image quality, typography, understanding complex prompts, and resource efficiency.
|
49 |
+
""")
|
50 |
+
|
51 |
with gr.Row():
|
52 |
+
|
53 |
prompt = gr.Text(
|
54 |
label="Prompt",
|
55 |
show_label=False,
|
|
|
57 |
placeholder="Enter your prompt",
|
58 |
container=False,
|
59 |
)
|
60 |
+
|
61 |
+
run_button = gr.Button("Run", scale=0)
|
62 |
+
|
63 |
result = gr.Image(label="Result", show_label=False)
|
64 |
+
|
65 |
with gr.Accordion("Advanced Settings", open=False):
|
66 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
seed = gr.Slider(
|
68 |
label="Seed",
|
69 |
minimum=0,
|
|
|
71 |
step=1,
|
72 |
value=0,
|
73 |
)
|
74 |
+
|
75 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
76 |
+
|
77 |
with gr.Row():
|
78 |
+
|
79 |
width = gr.Slider(
|
80 |
label="Width",
|
81 |
minimum=256,
|
82 |
maximum=MAX_IMAGE_SIZE,
|
83 |
step=32,
|
84 |
+
value=1024,
|
85 |
)
|
86 |
+
|
87 |
height = gr.Slider(
|
88 |
label="Height",
|
89 |
minimum=256,
|
90 |
maximum=MAX_IMAGE_SIZE,
|
91 |
step=32,
|
92 |
+
value=1024,
|
93 |
)
|
94 |
+
|
95 |
with gr.Row():
|
96 |
+
|
97 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
num_inference_steps = gr.Slider(
|
99 |
label="Number of inference steps",
|
100 |
minimum=1,
|
101 |
maximum=50,
|
102 |
step=1,
|
103 |
+
value=4,
|
104 |
)
|
105 |
+
|
106 |
+
gr.Examples(
|
107 |
+
examples = examples,
|
108 |
+
fn = infer,
|
109 |
+
inputs = [prompt],
|
110 |
+
outputs = [result, seed],
|
111 |
+
cache_examples="lazy"
|
112 |
+
)
|
113 |
|
|
|
114 |
gr.on(
|
115 |
triggers=[run_button.click, prompt.submit],
|
116 |
+
fn = infer,
|
117 |
+
inputs = [prompt, seed, randomize_seed, width, height, num_inference_steps],
|
118 |
+
outputs = [result, seed]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
)
|
120 |
|
121 |
+
demo.launch()
|
|