fix widget I/Os
Browse files
app.py
CHANGED
|
@@ -1,24 +1,26 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import random
|
| 4 |
-
|
|
|
|
| 5 |
import torch
|
| 6 |
|
| 7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
|
| 9 |
if torch.cuda.is_available():
|
| 10 |
torch.cuda.max_memory_allocated(device=device)
|
| 11 |
-
pipe =
|
| 12 |
-
pipe.enable_xformers_memory_efficient_attention()
|
| 13 |
pipe = pipe.to(device)
|
| 14 |
else:
|
| 15 |
-
pipe =
|
| 16 |
pipe = pipe.to(device)
|
| 17 |
|
| 18 |
MAX_SEED = np.iinfo(np.int32).max
|
| 19 |
MAX_IMAGE_SIZE = 1024
|
| 20 |
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
if randomize_seed:
|
| 24 |
seed = random.randint(0, MAX_SEED)
|
|
@@ -35,7 +37,7 @@ def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance
|
|
| 35 |
generator = generator
|
| 36 |
).images[0]
|
| 37 |
|
| 38 |
-
return image
|
| 39 |
|
| 40 |
examples = [
|
| 41 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
|
@@ -65,13 +67,23 @@ with gr.Blocks(css=css) as demo:
|
|
| 65 |
|
| 66 |
with gr.Row():
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
run_button = gr.Button("Run", scale=0)
|
| 77 |
|
|
@@ -136,14 +148,15 @@ with gr.Blocks(css=css) as demo:
|
|
| 136 |
examples = examples,
|
| 137 |
fn = infer,
|
| 138 |
inputs = [prompt],
|
| 139 |
-
outputs = [result],
|
| 140 |
cache_examples="lazy"
|
| 141 |
)
|
| 142 |
|
| 143 |
-
|
|
|
|
| 144 |
fn = infer,
|
| 145 |
-
inputs = [prompt, negative_prompt, seed, randomize_seed, width, height,
|
| 146 |
-
outputs = [result]
|
| 147 |
)
|
| 148 |
|
| 149 |
demo.queue().launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 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 |
|
| 10 |
if torch.cuda.is_available():
|
| 11 |
torch.cuda.max_memory_allocated(device=device)
|
| 12 |
+
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16)
|
| 13 |
+
#pipe.enable_xformers_memory_efficient_attention()
|
| 14 |
pipe = pipe.to(device)
|
| 15 |
else:
|
| 16 |
+
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
|
| 17 |
pipe = pipe.to(device)
|
| 18 |
|
| 19 |
MAX_SEED = np.iinfo(np.int32).max
|
| 20 |
MAX_IMAGE_SIZE = 1024
|
| 21 |
|
| 22 |
+
|
| 23 |
+
def infer(prompt, negative_prompt="", seed=42, randomize_seed=False, width=512, height=512, guidance_scale=7.0, num_inference_steps=25):
|
| 24 |
|
| 25 |
if randomize_seed:
|
| 26 |
seed = random.randint(0, MAX_SEED)
|
|
|
|
| 37 |
generator = generator
|
| 38 |
).images[0]
|
| 39 |
|
| 40 |
+
return image, seed
|
| 41 |
|
| 42 |
examples = [
|
| 43 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
|
|
|
| 67 |
|
| 68 |
with gr.Row():
|
| 69 |
|
| 70 |
+
with gr.Column():
|
| 71 |
+
|
| 72 |
+
prompt = gr.Text(
|
| 73 |
+
label="Prompt",
|
| 74 |
+
show_label=False,
|
| 75 |
+
max_lines=1,
|
| 76 |
+
placeholder="Enter your prompt",
|
| 77 |
+
container=False,
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
negative_prompt = gr.Text(
|
| 81 |
+
label="Prompt",
|
| 82 |
+
show_label=False,
|
| 83 |
+
max_lines=1,
|
| 84 |
+
placeholder="(option) Enter your negative prompt",
|
| 85 |
+
container=False,
|
| 86 |
+
)
|
| 87 |
|
| 88 |
run_button = gr.Button("Run", scale=0)
|
| 89 |
|
|
|
|
| 148 |
examples = examples,
|
| 149 |
fn = infer,
|
| 150 |
inputs = [prompt],
|
| 151 |
+
outputs = [result, seed],
|
| 152 |
cache_examples="lazy"
|
| 153 |
)
|
| 154 |
|
| 155 |
+
gr.on(
|
| 156 |
+
triggers=[run_button.click, prompt.submit],
|
| 157 |
fn = infer,
|
| 158 |
+
inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, num_inference_steps],
|
| 159 |
+
outputs = [result, seed]
|
| 160 |
)
|
| 161 |
|
| 162 |
demo.queue().launch()
|