Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,76 @@
|
|
| 1 |
-
|
| 2 |
-
import gradio as gr
|
| 3 |
from diffusers import StableDiffusionXLPipeline, DPMSolverMultistepScheduler
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 12 |
-
|
| 13 |
-
torch_dtype=
|
| 14 |
-
use_safetensors=True
|
|
|
|
|
|
|
| 15 |
)
|
| 16 |
-
|
| 17 |
-
# Replace scheduler for better quality
|
| 18 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
pipe
|
| 22 |
-
|
| 23 |
-
# Step 3: Load LoRA weights
|
| 24 |
-
pipe.load_lora_weights(lora_repo, weight_name=lora_file)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
-
# Inference function (returns list of images)
|
| 42 |
def generate_images():
|
| 43 |
-
|
| 44 |
-
for
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
return results
|
| 57 |
|
| 58 |
-
# Step 6: Gradio UI
|
| 59 |
demo = gr.Interface(
|
| 60 |
fn=generate_images,
|
| 61 |
inputs=None,
|
| 62 |
-
outputs=[gr.Image(label=f"Image {i+1}") for i in range(len(
|
| 63 |
-
title="SDXL + LoRA:
|
| 64 |
-
description="
|
| 65 |
)
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import torch, gradio as gr
|
| 3 |
from diffusers import StableDiffusionXLPipeline, DPMSolverMultistepScheduler
|
| 4 |
|
| 5 |
+
# Models
|
| 6 |
+
BASE = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 7 |
+
LORA_REPO = "freshcodestech/LingoSpace"
|
| 8 |
+
LORA_FILE = "LingoSpace-10.safetensors"
|
| 9 |
+
LORA_STRENGTH = 0.8 # 0.6–1.0
|
| 10 |
|
| 11 |
+
PROMPTS = [
|
| 12 |
+
"A cartoon raccoon in a space suit with some rice food, highly detailed, vibrant, futuristic art style",
|
| 13 |
+
"A cartoon bear in a space suit with some meat food, cinematic lighting, sci-fi background",
|
| 14 |
+
"A cartoon giraffe in a space suit with some pizza food, whimsical, colorful, space kitchen scene",
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
# Device / dtype
|
| 18 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 19 |
+
dtype = torch.float16 if device == "cuda" else torch.float32
|
| 20 |
+
|
| 21 |
+
# Load SDXL (disable watermarker to avoid extra deps on Spaces)
|
| 22 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 23 |
+
BASE,
|
| 24 |
+
torch_dtype=dtype,
|
| 25 |
+
use_safetensors=True,
|
| 26 |
+
add_watermarker=False,
|
| 27 |
+
**({"variant": "fp16"} if device == "cuda" else {})
|
| 28 |
)
|
|
|
|
|
|
|
| 29 |
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 30 |
+
pipe = pipe.to(device)
|
| 31 |
|
| 32 |
+
# Load LoRA from Hub
|
| 33 |
+
pipe.load_lora_weights(LORA_REPO, weight_name=LORA_FILE)
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# If your diffusers supports adapter weights:
|
| 36 |
+
if hasattr(pipe, "set_adapters"):
|
| 37 |
+
# (adapter name is "default" when not specified in load_lora_weights)
|
| 38 |
+
pipe.set_adapters("default", weight=LORA_STRENGTH)
|
| 39 |
|
| 40 |
+
# Memory helpers
|
| 41 |
+
if device == "cuda":
|
| 42 |
+
if hasattr(pipe, "enable_xformers_memory_efficient_attention"):
|
| 43 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 44 |
+
pipe.enable_attention_slicing()
|
| 45 |
+
pipe.enable_vae_tiling()
|
| 46 |
|
| 47 |
+
# Gen settings (smaller & fewer steps on CPU so it doesn’t crawl)
|
| 48 |
+
STEPS = 30 if device == "cuda" else 15
|
| 49 |
+
CFG = 7.5
|
| 50 |
+
H = 1024 if device == "cuda" else 768
|
| 51 |
+
W = 1024 if device == "cuda" else 768
|
| 52 |
|
|
|
|
| 53 |
def generate_images():
|
| 54 |
+
images = []
|
| 55 |
+
for i, p in enumerate(PROMPTS):
|
| 56 |
+
out = pipe(
|
| 57 |
+
p,
|
| 58 |
+
num_inference_steps=STEPS,
|
| 59 |
+
guidance_scale=CFG,
|
| 60 |
+
height=H,
|
| 61 |
+
width=W,
|
| 62 |
+
# Works even if set_adapters isn't available (older diffusers):
|
| 63 |
+
cross_attention_kwargs={"scale": LORA_STRENGTH},
|
| 64 |
+
)
|
| 65 |
+
images.append(out.images[0])
|
| 66 |
+
return images
|
|
|
|
| 67 |
|
|
|
|
| 68 |
demo = gr.Interface(
|
| 69 |
fn=generate_images,
|
| 70 |
inputs=None,
|
| 71 |
+
outputs=[gr.Image(label=f"Image {i+1}") for i in range(len(PROMPTS))],
|
| 72 |
+
title="SDXL + LoRA: LingoSpace",
|
| 73 |
+
description=f"Device: {device.upper()} | Base: SDXL | LoRA: {LORA_REPO}/{LORA_FILE}",
|
| 74 |
)
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|