Spaces:
Sleeping
Sleeping
first version
Browse files
app.py
CHANGED
|
@@ -5,34 +5,39 @@ import random
|
|
| 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 |
-
if torch.cuda.is_available():
|
| 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 = 1024
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 25 |
def infer(
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
if randomize_seed:
|
| 37 |
seed = random.randint(0, MAX_SEED)
|
| 38 |
|
|
@@ -52,10 +57,12 @@ def infer(
|
|
| 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 {
|
|
@@ -68,6 +75,10 @@ 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 |
with gr.Row():
|
| 72 |
prompt = gr.Text(
|
| 73 |
label="Prompt",
|
|
@@ -75,6 +86,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 75 |
max_lines=1,
|
| 76 |
placeholder="Enter your prompt",
|
| 77 |
container=False,
|
|
|
|
| 78 |
)
|
| 79 |
|
| 80 |
run_button = gr.Button("Run", scale=0, variant="primary")
|
|
@@ -86,15 +98,17 @@ with gr.Blocks(css=css) as demo:
|
|
| 86 |
label="Negative prompt",
|
| 87 |
max_lines=1,
|
| 88 |
placeholder="Enter a negative prompt",
|
| 89 |
-
visible=
|
|
|
|
| 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)
|
|
@@ -122,7 +136,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 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(
|
|
@@ -134,10 +148,12 @@ with gr.Blocks(css=css) as demo:
|
|
| 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,
|
|
@@ -151,4 +167,4 @@ with gr.Blocks(css=css) as demo:
|
|
| 151 |
)
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
| 154 |
-
demo.launch()
|
|
|
|
| 5 |
# import spaces #[uncomment to use ZeroGPU]
|
| 6 |
from diffusers import DiffusionPipeline
|
| 7 |
import torch
|
| 8 |
+
from sympy.core.random import choice
|
| 9 |
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
MAX_SEED = np.iinfo(np.int32).max
|
| 13 |
MAX_IMAGE_SIZE = 1024
|
| 14 |
+
# model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
|
| 15 |
|
| 16 |
|
| 17 |
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 18 |
def infer(
|
| 19 |
+
model_id,
|
| 20 |
+
prompt,
|
| 21 |
+
negative_prompt,
|
| 22 |
+
seed,
|
| 23 |
+
randomize_seed,
|
| 24 |
+
width,
|
| 25 |
+
height,
|
| 26 |
+
guidance_scale,
|
| 27 |
+
num_inference_steps,
|
| 28 |
+
progress=gr.Progress(track_tqdm=True),
|
| 29 |
):
|
| 30 |
+
if torch.cuda.is_available():
|
| 31 |
+
torch_dtype = torch.float16
|
| 32 |
+
else:
|
| 33 |
+
torch_dtype = torch.float32
|
| 34 |
+
#print(model_id)
|
| 35 |
+
|
| 36 |
+
pipe = DiffusionPipeline.from_pretrained(model_id, torch_dtype=torch_dtype)
|
| 37 |
+
pipe = pipe.to(device)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
if randomize_seed:
|
| 42 |
seed = random.randint(0, MAX_SEED)
|
| 43 |
|
|
|
|
| 57 |
|
| 58 |
|
| 59 |
examples = [
|
| 60 |
+
"cute_animal",
|
| 61 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
| 62 |
"An astronaut riding a green horse",
|
| 63 |
"A delicious ceviche cheesecake slice",
|
| 64 |
]
|
| 65 |
+
neg_examples = ["cat, dog",]
|
| 66 |
|
| 67 |
css = """
|
| 68 |
#col-container {
|
|
|
|
| 75 |
with gr.Column(elem_id="col-container"):
|
| 76 |
gr.Markdown(" # Text-to-Image Gradio Template")
|
| 77 |
|
| 78 |
+
with gr.Row():
|
| 79 |
+
model_id = gr.Dropdown(
|
| 80 |
+
choices=["CompVis/stable-diffusion-v1-4", "stabilityai/sdxl-turbo"], info="Choose model")
|
| 81 |
+
|
| 82 |
with gr.Row():
|
| 83 |
prompt = gr.Text(
|
| 84 |
label="Prompt",
|
|
|
|
| 86 |
max_lines=1,
|
| 87 |
placeholder="Enter your prompt",
|
| 88 |
container=False,
|
| 89 |
+
value="cute_animal",
|
| 90 |
)
|
| 91 |
|
| 92 |
run_button = gr.Button("Run", scale=0, variant="primary")
|
|
|
|
| 98 |
label="Negative prompt",
|
| 99 |
max_lines=1,
|
| 100 |
placeholder="Enter a negative prompt",
|
| 101 |
+
visible=True,
|
| 102 |
+
value="cat, dog",
|
| 103 |
)
|
| 104 |
+
gr.Examples(examples=neg_examples, inputs=[negative_prompt])
|
| 105 |
|
| 106 |
seed = gr.Slider(
|
| 107 |
label="Seed",
|
| 108 |
minimum=0,
|
| 109 |
maximum=MAX_SEED,
|
| 110 |
step=1,
|
| 111 |
+
value=42,#0,
|
| 112 |
)
|
| 113 |
|
| 114 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
|
|
|
| 136 |
minimum=0.0,
|
| 137 |
maximum=10.0,
|
| 138 |
step=0.1,
|
| 139 |
+
value=7.0,#0.0, # Replace with defaults that work for your model
|
| 140 |
)
|
| 141 |
|
| 142 |
num_inference_steps = gr.Slider(
|
|
|
|
| 148 |
)
|
| 149 |
|
| 150 |
gr.Examples(examples=examples, inputs=[prompt])
|
| 151 |
+
|
| 152 |
gr.on(
|
| 153 |
triggers=[run_button.click, prompt.submit],
|
| 154 |
fn=infer,
|
| 155 |
inputs=[
|
| 156 |
+
model_id,
|
| 157 |
prompt,
|
| 158 |
negative_prompt,
|
| 159 |
seed,
|
|
|
|
| 167 |
)
|
| 168 |
|
| 169 |
if __name__ == "__main__":
|
| 170 |
+
demo.launch()
|