Update app.py
Browse files
app.py
CHANGED
@@ -2,40 +2,39 @@ import gradio as gr
|
|
2 |
import numpy as np
|
3 |
import random
|
4 |
|
5 |
-
import
|
6 |
-
from diffusers import DiffusionPipeline
|
7 |
import torch
|
8 |
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
-
model_repo_id = "
|
11 |
|
12 |
if torch.cuda.is_available():
|
13 |
-
torch_dtype = torch.
|
14 |
else:
|
15 |
torch_dtype = torch.float32
|
16 |
|
17 |
-
pipe =
|
18 |
pipe = pipe.to(device)
|
19 |
|
20 |
MAX_SEED = np.iinfo(np.int32).max
|
21 |
MAX_IMAGE_SIZE = 1024
|
22 |
|
23 |
-
|
24 |
def infer(
|
25 |
prompt,
|
26 |
negative_prompt="",
|
27 |
seed=42,
|
28 |
randomize_seed=False,
|
29 |
-
width=
|
30 |
-
height=
|
31 |
-
guidance_scale=
|
32 |
-
num_inference_steps=
|
33 |
progress=gr.Progress(track_tqdm=True),
|
34 |
):
|
35 |
if randomize_seed:
|
36 |
seed = random.randint(0, MAX_SEED)
|
37 |
|
38 |
-
generator = torch.Generator().manual_seed(seed)
|
39 |
|
40 |
image = pipe(
|
41 |
prompt=prompt,
|
@@ -51,7 +50,8 @@ def infer(
|
|
51 |
|
52 |
|
53 |
examples = [
|
54 |
-
|
|
|
55 |
]
|
56 |
|
57 |
css = """
|
@@ -63,8 +63,10 @@ css = """
|
|
63 |
|
64 |
with gr.Blocks(css=css) as demo:
|
65 |
with gr.Column(elem_id="col-container"):
|
66 |
-
gr.Markdown(" #
|
67 |
-
gr.Markdown(
|
|
|
|
|
68 |
with gr.Row():
|
69 |
prompt = gr.Text(
|
70 |
label="Prompt",
|
@@ -83,7 +85,6 @@ with gr.Blocks(css=css) as demo:
|
|
83 |
label="Negative prompt",
|
84 |
max_lines=1,
|
85 |
placeholder="Enter a negative prompt",
|
86 |
-
visible=False,
|
87 |
)
|
88 |
|
89 |
seed = gr.Slider(
|
@@ -102,7 +103,7 @@ with gr.Blocks(css=css) as demo:
|
|
102 |
minimum=512,
|
103 |
maximum=MAX_IMAGE_SIZE,
|
104 |
step=32,
|
105 |
-
value=
|
106 |
)
|
107 |
|
108 |
height = gr.Slider(
|
@@ -110,27 +111,34 @@ with gr.Blocks(css=css) as demo:
|
|
110 |
minimum=512,
|
111 |
maximum=MAX_IMAGE_SIZE,
|
112 |
step=32,
|
113 |
-
value=
|
114 |
)
|
115 |
|
116 |
with gr.Row():
|
117 |
guidance_scale = gr.Slider(
|
118 |
label="Guidance scale",
|
119 |
minimum=0.0,
|
120 |
-
maximum=
|
121 |
-
step=0.
|
122 |
-
value=
|
123 |
)
|
124 |
|
125 |
num_inference_steps = gr.Slider(
|
126 |
label="Number of inference steps",
|
127 |
minimum=1,
|
128 |
-
maximum=
|
129 |
step=1,
|
130 |
-
value=
|
131 |
)
|
132 |
|
133 |
-
gr.Examples(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
gr.on(
|
135 |
triggers=[run_button.click, prompt.submit],
|
136 |
fn=infer,
|
@@ -148,4 +156,4 @@ with gr.Blocks(css=css) as demo:
|
|
148 |
)
|
149 |
|
150 |
if __name__ == "__main__":
|
151 |
-
demo.launch()
|
|
|
2 |
import numpy as np
|
3 |
import random
|
4 |
|
5 |
+
from diffusers import StableDiffusionPipeline
|
|
|
6 |
import torch
|
7 |
|
8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
+
model_repo_id = "runwayml/stable-diffusion-v1-5"
|
10 |
|
11 |
if torch.cuda.is_available():
|
12 |
+
torch_dtype = torch.float16
|
13 |
else:
|
14 |
torch_dtype = torch.float32
|
15 |
|
16 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
17 |
pipe = pipe.to(device)
|
18 |
|
19 |
MAX_SEED = np.iinfo(np.int32).max
|
20 |
MAX_IMAGE_SIZE = 1024
|
21 |
|
22 |
+
|
23 |
def infer(
|
24 |
prompt,
|
25 |
negative_prompt="",
|
26 |
seed=42,
|
27 |
randomize_seed=False,
|
28 |
+
width=512,
|
29 |
+
height=512,
|
30 |
+
guidance_scale=7.5,
|
31 |
+
num_inference_steps=50,
|
32 |
progress=gr.Progress(track_tqdm=True),
|
33 |
):
|
34 |
if randomize_seed:
|
35 |
seed = random.randint(0, MAX_SEED)
|
36 |
|
37 |
+
generator = torch.Generator(device).manual_seed(seed)
|
38 |
|
39 |
image = pipe(
|
40 |
prompt=prompt,
|
|
|
50 |
|
51 |
|
52 |
examples = [
|
53 |
+
"A futuristic cityscape with flying cars",
|
54 |
+
"A magical forest with glowing mushrooms",
|
55 |
]
|
56 |
|
57 |
css = """
|
|
|
63 |
|
64 |
with gr.Blocks(css=css) as demo:
|
65 |
with gr.Column(elem_id="col-container"):
|
66 |
+
gr.Markdown(" # Stable Diffusion v1.5 Demo")
|
67 |
+
gr.Markdown(
|
68 |
+
"[Learn more](https://huggingface.co/runwayml/stable-diffusion-v1-5) about the Stable Diffusion v1.5 model. "
|
69 |
+
)
|
70 |
with gr.Row():
|
71 |
prompt = gr.Text(
|
72 |
label="Prompt",
|
|
|
85 |
label="Negative prompt",
|
86 |
max_lines=1,
|
87 |
placeholder="Enter a negative prompt",
|
|
|
88 |
)
|
89 |
|
90 |
seed = gr.Slider(
|
|
|
103 |
minimum=512,
|
104 |
maximum=MAX_IMAGE_SIZE,
|
105 |
step=32,
|
106 |
+
value=512,
|
107 |
)
|
108 |
|
109 |
height = gr.Slider(
|
|
|
111 |
minimum=512,
|
112 |
maximum=MAX_IMAGE_SIZE,
|
113 |
step=32,
|
114 |
+
value=512,
|
115 |
)
|
116 |
|
117 |
with gr.Row():
|
118 |
guidance_scale = gr.Slider(
|
119 |
label="Guidance scale",
|
120 |
minimum=0.0,
|
121 |
+
maximum=20.0,
|
122 |
+
step=0.5,
|
123 |
+
value=7.5,
|
124 |
)
|
125 |
|
126 |
num_inference_steps = gr.Slider(
|
127 |
label="Number of inference steps",
|
128 |
minimum=1,
|
129 |
+
maximum=100,
|
130 |
step=1,
|
131 |
+
value=50,
|
132 |
)
|
133 |
|
134 |
+
gr.Examples(
|
135 |
+
examples=examples,
|
136 |
+
inputs=[prompt],
|
137 |
+
outputs=[result, seed],
|
138 |
+
fn=infer,
|
139 |
+
cache_examples=True,
|
140 |
+
cache_mode="lazy",
|
141 |
+
)
|
142 |
gr.on(
|
143 |
triggers=[run_button.click, prompt.submit],
|
144 |
fn=infer,
|
|
|
156 |
)
|
157 |
|
158 |
if __name__ == "__main__":
|
159 |
+
demo.launch()
|