Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,34 +2,53 @@ import gradio as gr
|
|
| 2 |
from diffusers import StableDiffusionPipeline
|
| 3 |
import torch
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# Load base model
|
| 6 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 7 |
"runwayml/stable-diffusion-v1-5",
|
| 8 |
-
torch_dtype=
|
| 9 |
-
)
|
| 10 |
|
| 11 |
-
# Load LoRA weights
|
| 12 |
-
#pipe.load_attn_procs("./lora")
|
| 13 |
pipe.load_lora_weights("./lora")
|
| 14 |
|
| 15 |
-
# Move to
|
| 16 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 17 |
pipe = pipe.to(device)
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
return image
|
| 23 |
|
| 24 |
# Build Gradio UI
|
| 25 |
demo = gr.Interface(
|
| 26 |
fn=generate,
|
| 27 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 28 |
outputs=gr.Image(label="Generated Image"),
|
| 29 |
-
title="Fine-tuned Stable Diffusion with LoRA",
|
| 30 |
-
description="
|
| 31 |
)
|
| 32 |
|
| 33 |
# Launch app
|
| 34 |
if __name__ == "__main__":
|
| 35 |
-
demo.launch()
|
|
|
|
| 2 |
from diffusers import StableDiffusionPipeline
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Detect device
|
| 6 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
dtype = torch.float16 if device == "cuda" else torch.float32
|
| 8 |
+
|
| 9 |
# Load base model
|
| 10 |
pipe = StableDiffusionPipeline.from_pretrained(
|
| 11 |
"runwayml/stable-diffusion-v1-5",
|
| 12 |
+
torch_dtype=dtype
|
| 13 |
+
)
|
| 14 |
|
| 15 |
+
# Load LoRA weights
|
|
|
|
| 16 |
pipe.load_lora_weights("./lora")
|
| 17 |
|
| 18 |
+
# Move to device
|
|
|
|
| 19 |
pipe = pipe.to(device)
|
| 20 |
|
| 21 |
+
# Extra CPU optimizations
|
| 22 |
+
if device == "cpu":
|
| 23 |
+
pipe.enable_attention_slicing()
|
| 24 |
+
pipe.enable_sequential_cpu_offload()
|
| 25 |
+
pipe.enable_vae_tiling()
|
| 26 |
+
|
| 27 |
+
# Define image generation function with quality toggle
|
| 28 |
+
def generate(prompt, quality):
|
| 29 |
+
if quality == "Fast":
|
| 30 |
+
steps = 20
|
| 31 |
+
guidance_scale = 7.0
|
| 32 |
+
else: # High Quality
|
| 33 |
+
steps = 40
|
| 34 |
+
guidance_scale = 8.5
|
| 35 |
+
|
| 36 |
+
with torch.no_grad():
|
| 37 |
+
image = pipe(prompt, num_inference_steps=steps, guidance_scale=guidance_scale).images[0]
|
| 38 |
return image
|
| 39 |
|
| 40 |
# Build Gradio UI
|
| 41 |
demo = gr.Interface(
|
| 42 |
fn=generate,
|
| 43 |
+
inputs=[
|
| 44 |
+
gr.Textbox(label="Enter your prompt"),
|
| 45 |
+
gr.Dropdown(["Fast", "High Quality"], value="Fast", label="Generation Mode")
|
| 46 |
+
],
|
| 47 |
outputs=gr.Image(label="Generated Image"),
|
| 48 |
+
title="Fine-tuned Stable Diffusion with LoRA (CPU-Optimized)",
|
| 49 |
+
description="Choose 'Fast' for quicker generation or 'High Quality' for better details."
|
| 50 |
)
|
| 51 |
|
| 52 |
# Launch app
|
| 53 |
if __name__ == "__main__":
|
| 54 |
+
demo.launch()
|