Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,25 +4,18 @@ 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 |
-
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 |
prompt,
|
| 27 |
negative_prompt,
|
| 28 |
seed,
|
|
@@ -38,6 +31,22 @@ def infer(
|
|
| 38 |
|
| 39 |
generator = torch.Generator().manual_seed(seed)
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
image = pipe(
|
| 42 |
prompt=prompt,
|
| 43 |
negative_prompt=negative_prompt,
|
|
@@ -50,13 +59,6 @@ def infer(
|
|
| 50 |
|
| 51 |
return image, seed
|
| 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 {
|
| 62 |
margin: 0 auto;
|
|
@@ -68,6 +70,16 @@ 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",
|
|
@@ -81,12 +93,13 @@ with gr.Blocks(css=css) as demo:
|
|
| 81 |
|
| 82 |
result = gr.Image(label="Result", show_label=False)
|
| 83 |
|
| 84 |
-
with gr.Accordion("Advanced Settings", open=
|
| 85 |
negative_prompt = gr.Text(
|
| 86 |
label="Negative prompt",
|
|
|
|
| 87 |
max_lines=1,
|
| 88 |
placeholder="Enter a negative prompt",
|
| 89 |
-
visible=
|
| 90 |
)
|
| 91 |
|
| 92 |
seed = gr.Slider(
|
|
@@ -94,7 +107,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 94 |
minimum=0,
|
| 95 |
maximum=MAX_SEED,
|
| 96 |
step=1,
|
| 97 |
-
value=
|
| 98 |
)
|
| 99 |
|
| 100 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
|
@@ -122,7 +135,7 @@ 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(
|
|
@@ -130,14 +143,14 @@ with gr.Blocks(css=css) as demo:
|
|
| 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 |
prompt,
|
| 142 |
negative_prompt,
|
| 143 |
seed,
|
|
@@ -151,4 +164,4 @@ with gr.Blocks(css=css) as demo:
|
|
| 151 |
)
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
| 154 |
-
demo.launch()
|
|
|
|
| 4 |
|
| 5 |
# import spaces #[uncomment to use ZeroGPU]
|
| 6 |
from diffusers import DiffusionPipeline
|
| 7 |
+
from peft import PeftModel, LoraConfig
|
| 8 |
import torch
|
| 9 |
|
|
|
|
| 10 |
model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
MAX_SEED = np.iinfo(np.int32).max
|
| 13 |
MAX_IMAGE_SIZE = 1024
|
| 14 |
|
| 15 |
|
| 16 |
# @spaces.GPU #[uncomment to use ZeroGPU]
|
| 17 |
def infer(
|
| 18 |
+
model,
|
| 19 |
prompt,
|
| 20 |
negative_prompt,
|
| 21 |
seed,
|
|
|
|
| 31 |
|
| 32 |
generator = torch.Generator().manual_seed(seed)
|
| 33 |
|
| 34 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 35 |
+
|
| 36 |
+
if torch.cuda.is_available():
|
| 37 |
+
torch_dtype = torch.float16
|
| 38 |
+
else:
|
| 39 |
+
torch_dtype = torch.float32
|
| 40 |
+
|
| 41 |
+
if model == "Ramzes":
|
| 42 |
+
pipe = StableDiffusionPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch_dtype)
|
| 43 |
+
pipe.unet = PeftModel.from_pretrained(pipe_v5_2.unet, "Bordoglor/Ramzes_adapter_sd_v1.5/unet")
|
| 44 |
+
pipe.text_encoder = PeftModel.from_pretrained(pipe_v5_2.text_encoder, "Bordoglor/Ramzes_adapter_sd_v1.5/text_encoder")
|
| 45 |
+
pipe = pipe.to(device)
|
| 46 |
+
else:
|
| 47 |
+
pipe = DiffusionPipeline.from_pretrained(model, torch_dtype=torch_dtype)
|
| 48 |
+
pipe = pipe.to(device)
|
| 49 |
+
|
| 50 |
image = pipe(
|
| 51 |
prompt=prompt,
|
| 52 |
negative_prompt=negative_prompt,
|
|
|
|
| 59 |
|
| 60 |
return image, seed
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
css = """
|
| 63 |
#col-container {
|
| 64 |
margin: 0 auto;
|
|
|
|
| 70 |
with gr.Column(elem_id="col-container"):
|
| 71 |
gr.Markdown(" # Text-to-Image Gradio Template")
|
| 72 |
|
| 73 |
+
|
| 74 |
+
with gr.Row():
|
| 75 |
+
model = gr.Dropdown(
|
| 76 |
+
choices=["stabilityai/sdxl-turbo", "CompVis/stable-diffusion-v1-4", "Ramzes"],
|
| 77 |
+
value=model_repo_id,
|
| 78 |
+
label="Model",
|
| 79 |
+
info="Choose which diffusion model to use"
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
|
| 83 |
with gr.Row():
|
| 84 |
prompt = gr.Text(
|
| 85 |
label="Prompt",
|
|
|
|
| 93 |
|
| 94 |
result = gr.Image(label="Result", show_label=False)
|
| 95 |
|
| 96 |
+
with gr.Accordion("Advanced Settings", open=True):
|
| 97 |
negative_prompt = gr.Text(
|
| 98 |
label="Negative prompt",
|
| 99 |
+
value="dog, cat",
|
| 100 |
max_lines=1,
|
| 101 |
placeholder="Enter a negative prompt",
|
| 102 |
+
visible=True,
|
| 103 |
)
|
| 104 |
|
| 105 |
seed = gr.Slider(
|
|
|
|
| 107 |
minimum=0,
|
| 108 |
maximum=MAX_SEED,
|
| 109 |
step=1,
|
| 110 |
+
value=42,
|
| 111 |
)
|
| 112 |
|
| 113 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
|
|
|
| 135 |
minimum=0.0,
|
| 136 |
maximum=10.0,
|
| 137 |
step=0.1,
|
| 138 |
+
value=7.0, # Replace with defaults that work for your model
|
| 139 |
)
|
| 140 |
|
| 141 |
num_inference_steps = gr.Slider(
|
|
|
|
| 143 |
minimum=1,
|
| 144 |
maximum=50,
|
| 145 |
step=1,
|
| 146 |
+
value=20, # Replace with defaults that work for your model
|
| 147 |
)
|
| 148 |
|
|
|
|
| 149 |
gr.on(
|
| 150 |
triggers=[run_button.click, prompt.submit],
|
| 151 |
fn=infer,
|
| 152 |
inputs=[
|
| 153 |
+
model,
|
| 154 |
prompt,
|
| 155 |
negative_prompt,
|
| 156 |
seed,
|
|
|
|
| 164 |
)
|
| 165 |
|
| 166 |
if __name__ == "__main__":
|
| 167 |
+
demo.launch()
|