Update app.py
Browse files
app.py
CHANGED
|
@@ -6,30 +6,52 @@ from PIL import Image
|
|
| 6 |
# β
Base model (commercial use allowed)
|
| 7 |
BASE_MODEL = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
LORA_1 = "gh1bli-style.safetensors"
|
| 11 |
-
LORA_2 = "ghibli_landscape_lora.safetensors"
|
| 12 |
|
|
|
|
| 13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 15 |
|
| 16 |
-
# π§ Load base
|
| 17 |
print("πΉ Loading SDXL base model...")
|
| 18 |
pipe_txt2img = DiffusionPipeline.from_pretrained(
|
| 19 |
BASE_MODEL,
|
| 20 |
-
|
| 21 |
use_safetensors=True,
|
| 22 |
).to(device)
|
| 23 |
|
| 24 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
print("πΉ Applying Ghibli-style LoRAs...")
|
| 26 |
-
pipe_txt2img.load_lora_weights(LORA_1)
|
| 27 |
-
pipe_txt2img.load_lora_weights(LORA_2)
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
# πΌοΈ Image-to-
|
| 30 |
print("πΉ Setting up image-to-image pipeline...")
|
| 31 |
pipe_img2img = StableDiffusionXLImg2ImgPipeline(**pipe_txt2img.components)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
|
|
|
| 33 |
def generate(prompt, steps=30, guidance=7.5, seed=42, strength=0.6, image=None):
|
| 34 |
generator = torch.Generator(device=device).manual_seed(int(seed))
|
| 35 |
|
|
@@ -55,7 +77,7 @@ def generate(prompt, steps=30, guidance=7.5, seed=42, strength=0.6, image=None):
|
|
| 55 |
|
| 56 |
return result
|
| 57 |
|
| 58 |
-
#
|
| 59 |
demo = gr.Interface(
|
| 60 |
fn=generate,
|
| 61 |
inputs=[
|
|
@@ -67,8 +89,8 @@ demo = gr.Interface(
|
|
| 67 |
gr.Image(label="Upload Image (optional)", type="filepath"),
|
| 68 |
],
|
| 69 |
outputs=gr.Image(label="Generated Image"),
|
| 70 |
-
title="Ghibli Style Maker β Text & Image to Image",
|
| 71 |
-
description="
|
| 72 |
)
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
|
|
|
| 6 |
# β
Base model (commercial use allowed)
|
| 7 |
BASE_MODEL = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 8 |
|
| 9 |
+
# β
Local LoRA weights
|
| 10 |
+
LORA_1 = "./gh1bli-style.safetensors"
|
| 11 |
+
LORA_2 = "./ghibli_landscape_lora.safetensors"
|
| 12 |
|
| 13 |
+
# β
Device setup
|
| 14 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 16 |
|
| 17 |
+
# π§ Load base pipeline
|
| 18 |
print("πΉ Loading SDXL base model...")
|
| 19 |
pipe_txt2img = DiffusionPipeline.from_pretrained(
|
| 20 |
BASE_MODEL,
|
| 21 |
+
dtype=dtype,
|
| 22 |
use_safetensors=True,
|
| 23 |
).to(device)
|
| 24 |
|
| 25 |
+
# β
Enable CPU/GPU memory optimization
|
| 26 |
+
if device == "cuda":
|
| 27 |
+
print("π Using GPU optimization")
|
| 28 |
+
pipe_txt2img.enable_model_cpu_offload() # For big SDXL weights
|
| 29 |
+
else:
|
| 30 |
+
print("π§© Using CPU memory optimization")
|
| 31 |
+
pipe_txt2img.enable_attention_slicing() # Reduce RAM spikes
|
| 32 |
+
pipe_txt2img.enable_sequential_cpu_offload()
|
| 33 |
+
|
| 34 |
+
# π§© Load both LoRAs (PEFT-compatible)
|
| 35 |
print("πΉ Applying Ghibli-style LoRAs...")
|
| 36 |
+
pipe_txt2img.load_lora_weights(LORA_1, adapter_name="ghibli_style")
|
| 37 |
+
pipe_txt2img.load_lora_weights(LORA_2, adapter_name="ghibli_landscape")
|
| 38 |
+
|
| 39 |
+
# π§ Merge both styles
|
| 40 |
+
pipe_txt2img.set_adapters(["ghibli_style", "ghibli_landscape"], adapter_weights=[0.7, 0.6])
|
| 41 |
|
| 42 |
+
# πΌοΈ Image-to-Image pipeline (inherits adapters)
|
| 43 |
print("πΉ Setting up image-to-image pipeline...")
|
| 44 |
pipe_img2img = StableDiffusionXLImg2ImgPipeline(**pipe_txt2img.components)
|
| 45 |
+
pipe_img2img.set_adapters(["ghibli_style", "ghibli_landscape"], adapter_weights=[0.7, 0.6])
|
| 46 |
+
|
| 47 |
+
# β
Add same memory optimization for img2img
|
| 48 |
+
if device == "cuda":
|
| 49 |
+
pipe_img2img.enable_model_cpu_offload()
|
| 50 |
+
else:
|
| 51 |
+
pipe_img2img.enable_attention_slicing()
|
| 52 |
+
pipe_img2img.enable_sequential_cpu_offload()
|
| 53 |
|
| 54 |
+
# π¨ Generation function
|
| 55 |
def generate(prompt, steps=30, guidance=7.5, seed=42, strength=0.6, image=None):
|
| 56 |
generator = torch.Generator(device=device).manual_seed(int(seed))
|
| 57 |
|
|
|
|
| 77 |
|
| 78 |
return result
|
| 79 |
|
| 80 |
+
# ποΈ Gradio UI
|
| 81 |
demo = gr.Interface(
|
| 82 |
fn=generate,
|
| 83 |
inputs=[
|
|
|
|
| 89 |
gr.Image(label="Upload Image (optional)", type="filepath"),
|
| 90 |
],
|
| 91 |
outputs=gr.Image(label="Generated Image"),
|
| 92 |
+
title="π¨ Ghibli Style Maker β Text & Image to Image",
|
| 93 |
+
description="Generate or transform images in Studio Ghibli-inspired style using SDXL and LoRAs.",
|
| 94 |
)
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|