Update app.py
Browse files
app.py
CHANGED
|
@@ -3,15 +3,24 @@ from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
|
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
import random
|
|
|
|
| 6 |
|
| 7 |
-
model_id = "CompVis/stable-diffusion-v1-4"
|
| 8 |
-
lora_model_id = "codermert/mert_flux"
|
| 9 |
|
| 10 |
-
|
| 11 |
-
pipe
|
| 12 |
-
pipe = pipe.
|
| 13 |
-
pipe.
|
| 14 |
-
pipe.safety_checker = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def generate_image(prompt, negative_prompt, steps, cfg_scale, seed, strength):
|
| 17 |
if seed == -1:
|
|
@@ -26,7 +35,6 @@ def generate_image(prompt, negative_prompt, steps, cfg_scale, seed, strength):
|
|
| 26 |
num_inference_steps=steps,
|
| 27 |
guidance_scale=cfg_scale,
|
| 28 |
generator=generator,
|
| 29 |
-
cross_attention_kwargs={"scale": strength},
|
| 30 |
).images[0]
|
| 31 |
|
| 32 |
return image, seed
|
|
@@ -56,7 +64,6 @@ with gr.Blocks(theme='default', css=css) as app:
|
|
| 56 |
steps = gr.Slider(label="Sampling steps", value=30, minimum=10, maximum=50, step=1)
|
| 57 |
cfg_scale = gr.Slider(label="CFG Scale", value=7.5, minimum=1, maximum=15, step=0.5)
|
| 58 |
with gr.Column():
|
| 59 |
-
strength = gr.Slider(label="LoRA Strength", value=0.75, minimum=0, maximum=1, step=0.01)
|
| 60 |
seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1)
|
| 61 |
|
| 62 |
with gr.Row():
|
|
@@ -70,7 +77,7 @@ with gr.Blocks(theme='default', css=css) as app:
|
|
| 70 |
|
| 71 |
generate_button.click(
|
| 72 |
generate_image,
|
| 73 |
-
inputs=[text_prompt, negative_prompt, steps, cfg_scale, seed, strength],
|
| 74 |
outputs=[image_output, seed_output]
|
| 75 |
)
|
| 76 |
|
|
|
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
import random
|
| 6 |
+
from peft import PeftModel, LoraConfig
|
| 7 |
|
| 8 |
+
model_id = "CompVis/stable-diffusion-v1-4"
|
| 9 |
+
lora_model_id = "codermert/mert_flux"
|
| 10 |
|
| 11 |
+
def load_model():
|
| 12 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
|
| 13 |
+
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 14 |
+
pipe = pipe.to("cpu")
|
| 15 |
+
pipe.safety_checker = None
|
| 16 |
+
|
| 17 |
+
# Load LoRA weights
|
| 18 |
+
config = LoraConfig.from_pretrained(lora_model_id)
|
| 19 |
+
pipe.unet = PeftModel.from_pretrained(pipe.unet, lora_model_id)
|
| 20 |
+
|
| 21 |
+
return pipe
|
| 22 |
+
|
| 23 |
+
pipe = load_model()
|
| 24 |
|
| 25 |
def generate_image(prompt, negative_prompt, steps, cfg_scale, seed, strength):
|
| 26 |
if seed == -1:
|
|
|
|
| 35 |
num_inference_steps=steps,
|
| 36 |
guidance_scale=cfg_scale,
|
| 37 |
generator=generator,
|
|
|
|
| 38 |
).images[0]
|
| 39 |
|
| 40 |
return image, seed
|
|
|
|
| 64 |
steps = gr.Slider(label="Sampling steps", value=30, minimum=10, maximum=50, step=1)
|
| 65 |
cfg_scale = gr.Slider(label="CFG Scale", value=7.5, minimum=1, maximum=15, step=0.5)
|
| 66 |
with gr.Column():
|
|
|
|
| 67 |
seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1)
|
| 68 |
|
| 69 |
with gr.Row():
|
|
|
|
| 77 |
|
| 78 |
generate_button.click(
|
| 79 |
generate_image,
|
| 80 |
+
inputs=[text_prompt, negative_prompt, steps, cfg_scale, seed, strength],
|
| 81 |
outputs=[image_output, seed_output]
|
| 82 |
)
|
| 83 |
|