Spaces:
Runtime error
Runtime error
Create Paintify.py
Browse files- Paintify.py +26 -0
Paintify.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from diffusers import StableDiffusionImg2ImgPipeline, DPMSolverMultistepScheduler
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
MODEL_ID = "runwayml/stable-diffusion-v1-5"
|
| 7 |
+
LORA_PATH = "./models/ms_paint_style.safetensors"
|
| 8 |
+
|
| 9 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 10 |
+
MODEL_ID,
|
| 11 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 12 |
+
safety_checker=None,
|
| 13 |
+
)
|
| 14 |
+
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 15 |
+
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
+
|
| 17 |
+
# Load LoRA
|
| 18 |
+
pipe.load_lora_weights(LORA_PATH)
|
| 19 |
+
pipe.fuse_lora()
|
| 20 |
+
|
| 21 |
+
def stylize_image(image: Image.Image) -> Image.Image:
|
| 22 |
+
image = image.convert("RGB").resize((512, 512))
|
| 23 |
+
prompt = "MS Paint drawing, crude lines, fanart, child-like, cartoonish, naive"
|
| 24 |
+
result = pipe(prompt=prompt, image=image, strength=0.75, guidance_scale=7.5).images[0]
|
| 25 |
+
return result
|
| 26 |
+
|