Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,37 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
!pip install --upgrade diffusers accelerate transformers safetensors
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from diffusers import StableDiffusionPipeline
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
# Load base model
|
| 9 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 10 |
+
"runwayml/stable-diffusion-v1-5",
|
| 11 |
+
torch_dtype=torch.float16
|
| 12 |
+
).to("cuda")
|
| 13 |
+
|
| 14 |
+
# Load LoRA weights from the 'lora' folder
|
| 15 |
+
pipe.load_attn_procs("./lora")
|
| 16 |
+
|
| 17 |
+
# Move to GPU if available
|
| 18 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 19 |
+
pipe = pipe.to(device)
|
| 20 |
+
|
| 21 |
+
# Define image generation function
|
| 22 |
+
def generate(prompt):
|
| 23 |
+
image = pipe(prompt).images[0]
|
| 24 |
+
return image
|
| 25 |
+
|
| 26 |
+
# Build Gradio UI
|
| 27 |
+
demo = gr.Interface(
|
| 28 |
+
fn=generate,
|
| 29 |
+
inputs=gr.Textbox(label="Enter your prompt"),
|
| 30 |
+
outputs=gr.Image(label="Generated Image"),
|
| 31 |
+
title="Fine-tuned Stable Diffusion with LoRA",
|
| 32 |
+
description="Enter a prompt and get an AI-generated image based on your fine-tuned model."
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Launch app
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
demo.launch()
|