Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,19 @@ import json
|
|
| 3 |
import logging
|
| 4 |
import torch
|
| 5 |
from PIL import Image
|
| 6 |
-
from diffusers import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import spaces
|
| 8 |
|
| 9 |
# Load LoRAs from JSON file
|
|
@@ -27,7 +39,7 @@ def update_selection(evt: gr.SelectData):
|
|
| 27 |
)
|
| 28 |
|
| 29 |
@spaces.GPU
|
| 30 |
-
def run_lora(prompt, negative_prompt, cfg_scale, steps, selected_index, scheduler):
|
| 31 |
if selected_index is None:
|
| 32 |
raise gr.Error("You must select a LoRA before proceeding.")
|
| 33 |
|
|
@@ -39,10 +51,44 @@ def run_lora(prompt, negative_prompt, cfg_scale, steps, selected_index, schedule
|
|
| 39 |
pipe.load_lora_weights(lora_path)
|
| 40 |
|
| 41 |
# Set scheduler
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# Generate image
|
| 48 |
image = pipe(
|
|
@@ -50,6 +96,10 @@ def run_lora(prompt, negative_prompt, cfg_scale, steps, selected_index, schedule
|
|
| 50 |
negative_prompt=negative_prompt,
|
| 51 |
num_inference_steps=steps,
|
| 52 |
guidance_scale=cfg_scale,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
).images[0]
|
| 54 |
|
| 55 |
# Unload LoRA weights
|
|
@@ -57,8 +107,8 @@ def run_lora(prompt, negative_prompt, cfg_scale, steps, selected_index, schedule
|
|
| 57 |
|
| 58 |
return image
|
| 59 |
|
| 60 |
-
with gr.Blocks(
|
| 61 |
-
gr.Markdown("# artificialguybr LoRA
|
| 62 |
gr.Markdown(
|
| 63 |
"### This is my portfolio. Follow me on Twitter [@artificialguybr](https://twitter.com/artificialguybr).\n"
|
| 64 |
"**Note**: Generation quality may vary. For best results, adjust the parameters.\n"
|
|
@@ -68,33 +118,53 @@ with gr.Blocks(css="custom.css") as app:
|
|
| 68 |
selected_index = gr.State(None)
|
| 69 |
|
| 70 |
with gr.Row():
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
|
|
|
| 78 |
with gr.Column():
|
| 79 |
prompt_title = gr.Markdown("### Click on a LoRA in the gallery to select it")
|
| 80 |
selected_info = gr.Markdown("")
|
| 81 |
prompt = gr.Textbox(label="Prompt", lines=3, placeholder="Type a prompt after selecting a LoRA")
|
| 82 |
negative_prompt = gr.Textbox(label="Negative Prompt", lines=2, value="low quality, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry")
|
| 83 |
-
|
|
|
|
| 84 |
with gr.Row():
|
| 85 |
cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=7.5)
|
| 86 |
steps = gr.Slider(label="Steps", minimum=1, maximum=100, step=1, value=30)
|
| 87 |
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
gallery.select(update_selection, outputs=[prompt, selected_info, selected_index])
|
| 94 |
|
| 95 |
generate_button.click(
|
| 96 |
fn=run_lora,
|
| 97 |
-
inputs=[prompt, negative_prompt, cfg_scale, steps, selected_index, scheduler],
|
| 98 |
outputs=[result]
|
| 99 |
)
|
| 100 |
|
|
|
|
| 3 |
import logging
|
| 4 |
import torch
|
| 5 |
from PIL import Image
|
| 6 |
+
from diffusers import (
|
| 7 |
+
DiffusionPipeline,
|
| 8 |
+
EulerDiscreteScheduler,
|
| 9 |
+
DPMSolverMultistepScheduler,
|
| 10 |
+
DPMSolverSinglestepScheduler,
|
| 11 |
+
KDPM2DiscreteScheduler,
|
| 12 |
+
KDPM2AncestralDiscreteScheduler,
|
| 13 |
+
EulerAncestralDiscreteScheduler,
|
| 14 |
+
HeunDiscreteScheduler,
|
| 15 |
+
LMSDiscreteScheduler,
|
| 16 |
+
DEISMultistepScheduler,
|
| 17 |
+
UniPCMultistepScheduler
|
| 18 |
+
)
|
| 19 |
import spaces
|
| 20 |
|
| 21 |
# Load LoRAs from JSON file
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
@spaces.GPU
|
| 42 |
+
def run_lora(prompt, negative_prompt, cfg_scale, steps, selected_index, scheduler, seed, width, height, lora_scale):
|
| 43 |
if selected_index is None:
|
| 44 |
raise gr.Error("You must select a LoRA before proceeding.")
|
| 45 |
|
|
|
|
| 51 |
pipe.load_lora_weights(lora_path)
|
| 52 |
|
| 53 |
# Set scheduler
|
| 54 |
+
scheduler_config = pipe.scheduler.config
|
| 55 |
+
if scheduler == "DPM++ 2M":
|
| 56 |
+
pipe.scheduler = DPMSolverMultistepScheduler.from_config(scheduler_config)
|
| 57 |
+
elif scheduler == "DPM++ 2M Karras":
|
| 58 |
+
pipe.scheduler = DPMSolverMultistepScheduler.from_config(scheduler_config, use_karras_sigmas=True)
|
| 59 |
+
elif scheduler == "DPM++ 2M SDE":
|
| 60 |
+
pipe.scheduler = DPMSolverMultistepScheduler.from_config(scheduler_config, algorithm_type="sde-dpmsolver++")
|
| 61 |
+
elif scheduler == "DPM++ 2M SDE Karras":
|
| 62 |
+
pipe.scheduler = DPMSolverMultistepScheduler.from_config(scheduler_config, use_karras_sigmas=True, algorithm_type="sde-dpmsolver++")
|
| 63 |
+
elif scheduler == "DPM++ SDE":
|
| 64 |
+
pipe.scheduler = DPMSolverSinglestepScheduler.from_config(scheduler_config)
|
| 65 |
+
elif scheduler == "DPM++ SDE Karras":
|
| 66 |
+
pipe.scheduler = DPMSolverSinglestepScheduler.from_config(scheduler_config, use_karras_sigmas=True)
|
| 67 |
+
elif scheduler == "DPM2":
|
| 68 |
+
pipe.scheduler = KDPM2DiscreteScheduler.from_config(scheduler_config)
|
| 69 |
+
elif scheduler == "DPM2 Karras":
|
| 70 |
+
pipe.scheduler = KDPM2DiscreteScheduler.from_config(scheduler_config, use_karras_sigmas=True)
|
| 71 |
+
elif scheduler == "DPM2 a":
|
| 72 |
+
pipe.scheduler = KDPM2AncestralDiscreteScheduler.from_config(scheduler_config)
|
| 73 |
+
elif scheduler == "DPM2 a Karras":
|
| 74 |
+
pipe.scheduler = KDPM2AncestralDiscreteScheduler.from_config(scheduler_config, use_karras_sigmas=True)
|
| 75 |
+
elif scheduler == "Euler":
|
| 76 |
+
pipe.scheduler = EulerDiscreteScheduler.from_config(scheduler_config)
|
| 77 |
+
elif scheduler == "Euler a":
|
| 78 |
+
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(scheduler_config)
|
| 79 |
+
elif scheduler == "Heun":
|
| 80 |
+
pipe.scheduler = HeunDiscreteScheduler.from_config(scheduler_config)
|
| 81 |
+
elif scheduler == "LMS":
|
| 82 |
+
pipe.scheduler = LMSDiscreteScheduler.from_config(scheduler_config)
|
| 83 |
+
elif scheduler == "LMS Karras":
|
| 84 |
+
pipe.scheduler = LMSDiscreteScheduler.from_config(scheduler_config, use_karras_sigmas=True)
|
| 85 |
+
elif scheduler == "DEIS":
|
| 86 |
+
pipe.scheduler = DEISMultistepScheduler.from_config(scheduler_config)
|
| 87 |
+
elif scheduler == "UniPC":
|
| 88 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(scheduler_config)
|
| 89 |
+
|
| 90 |
+
# Set random seed for reproducibility
|
| 91 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
| 92 |
|
| 93 |
# Generate image
|
| 94 |
image = pipe(
|
|
|
|
| 96 |
negative_prompt=negative_prompt,
|
| 97 |
num_inference_steps=steps,
|
| 98 |
guidance_scale=cfg_scale,
|
| 99 |
+
width=width,
|
| 100 |
+
height=height,
|
| 101 |
+
generator=generator,
|
| 102 |
+
cross_attention_kwargs={"scale": lora_scale},
|
| 103 |
).images[0]
|
| 104 |
|
| 105 |
# Unload LoRA weights
|
|
|
|
| 107 |
|
| 108 |
return image
|
| 109 |
|
| 110 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 111 |
+
gr.Markdown("# artificialguybr LoRA Portfolio")
|
| 112 |
gr.Markdown(
|
| 113 |
"### This is my portfolio. Follow me on Twitter [@artificialguybr](https://twitter.com/artificialguybr).\n"
|
| 114 |
"**Note**: Generation quality may vary. For best results, adjust the parameters.\n"
|
|
|
|
| 118 |
selected_index = gr.State(None)
|
| 119 |
|
| 120 |
with gr.Row():
|
| 121 |
+
with gr.Column(scale=2):
|
| 122 |
+
result = gr.Image(label="Generated Image", height=768)
|
| 123 |
+
generate_button = gr.Button("Generate", variant="primary")
|
| 124 |
+
|
| 125 |
+
with gr.Column(scale=1):
|
| 126 |
+
gallery = gr.Gallery(
|
| 127 |
+
[(item["image"], item["title"]) for item in loras],
|
| 128 |
+
label="LoRA Gallery",
|
| 129 |
+
allow_preview=False,
|
| 130 |
+
columns=2
|
| 131 |
+
)
|
| 132 |
|
| 133 |
+
with gr.Row():
|
| 134 |
with gr.Column():
|
| 135 |
prompt_title = gr.Markdown("### Click on a LoRA in the gallery to select it")
|
| 136 |
selected_info = gr.Markdown("")
|
| 137 |
prompt = gr.Textbox(label="Prompt", lines=3, placeholder="Type a prompt after selecting a LoRA")
|
| 138 |
negative_prompt = gr.Textbox(label="Negative Prompt", lines=2, value="low quality, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry")
|
| 139 |
+
|
| 140 |
+
with gr.Column():
|
| 141 |
with gr.Row():
|
| 142 |
cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=7.5)
|
| 143 |
steps = gr.Slider(label="Steps", minimum=1, maximum=100, step=1, value=30)
|
| 144 |
|
| 145 |
+
with gr.Row():
|
| 146 |
+
width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=1024)
|
| 147 |
+
height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=1024)
|
| 148 |
+
|
| 149 |
+
with gr.Row():
|
| 150 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=2**32-1, step=1, value=0, randomize=True)
|
| 151 |
+
lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=1, step=0.01, value=0.75)
|
| 152 |
|
| 153 |
+
scheduler = gr.Dropdown(
|
| 154 |
+
label="Scheduler",
|
| 155 |
+
choices=[
|
| 156 |
+
"DPM++ 2M", "DPM++ 2M Karras", "DPM++ 2M SDE", "DPM++ 2M SDE Karras",
|
| 157 |
+
"DPM++ SDE", "DPM++ SDE Karras", "DPM2", "DPM2 Karras", "DPM2 a", "DPM2 a Karras",
|
| 158 |
+
"Euler", "Euler a", "Heun", "LMS", "LMS Karras", "DEIS", "UniPC"
|
| 159 |
+
],
|
| 160 |
+
value="Euler"
|
| 161 |
+
)
|
| 162 |
|
| 163 |
gallery.select(update_selection, outputs=[prompt, selected_info, selected_index])
|
| 164 |
|
| 165 |
generate_button.click(
|
| 166 |
fn=run_lora,
|
| 167 |
+
inputs=[prompt, negative_prompt, cfg_scale, steps, selected_index, scheduler, seed, width, height, lora_scale],
|
| 168 |
outputs=[result]
|
| 169 |
)
|
| 170 |
|