Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,17 +2,23 @@ import torch
|
|
| 2 |
from diffusers import StableDiffusionInpaintPipeline
|
| 3 |
import gradio as gr
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
# Ensure we use GPU if available
|
| 7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def inpaint(image, mask, prompt):
|
|
|
|
| 16 |
image = Image.fromarray(image)
|
| 17 |
mask = Image.fromarray(mask).convert("L") # Convert mask to grayscale
|
| 18 |
|
|
@@ -20,17 +26,17 @@ def inpaint(image, mask, prompt):
|
|
| 20 |
result = pipe(prompt=prompt, image=image, mask_image=mask).images[0]
|
| 21 |
return result
|
| 22 |
|
| 23 |
-
# Create Gradio UI
|
| 24 |
demo = gr.Interface(
|
| 25 |
fn=inpaint,
|
| 26 |
inputs=[
|
| 27 |
-
gr.Image(type="numpy", label="Upload Image"),
|
| 28 |
-
gr.Image(type="numpy", label="
|
| 29 |
gr.Textbox(label="Prompt for Inpainting"),
|
| 30 |
],
|
| 31 |
outputs=gr.Image(type="pil"),
|
| 32 |
-
title="Stable Diffusion 2 Inpainting",
|
| 33 |
-
description="Upload an image
|
| 34 |
)
|
| 35 |
|
| 36 |
demo.launch()
|
|
|
|
| 2 |
from diffusers import StableDiffusionInpaintPipeline
|
| 3 |
import gradio as gr
|
| 4 |
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
|
| 7 |
# Ensure we use GPU if available
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
|
| 10 |
+
def load_model():
|
| 11 |
+
"""Load Stable Diffusion 2 Inpainting model."""
|
| 12 |
+
pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
| 13 |
+
"stabilityai/stable-diffusion-2-inpainting",
|
| 14 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 15 |
+
).to(device)
|
| 16 |
+
return pipe
|
| 17 |
+
|
| 18 |
+
pipe = load_model()
|
| 19 |
|
| 20 |
def inpaint(image, mask, prompt):
|
| 21 |
+
"""Perform inpainting based on user input."""
|
| 22 |
image = Image.fromarray(image)
|
| 23 |
mask = Image.fromarray(mask).convert("L") # Convert mask to grayscale
|
| 24 |
|
|
|
|
| 26 |
result = pipe(prompt=prompt, image=image, mask_image=mask).images[0]
|
| 27 |
return result
|
| 28 |
|
| 29 |
+
# Create Gradio UI with an editable brush tool
|
| 30 |
demo = gr.Interface(
|
| 31 |
fn=inpaint,
|
| 32 |
inputs=[
|
| 33 |
+
gr.Image(type="numpy", tool="editor", label="Upload Image & Draw Mask"),
|
| 34 |
+
gr.Image(type="numpy", tool="sketch", label="Paint Mask"),
|
| 35 |
gr.Textbox(label="Prompt for Inpainting"),
|
| 36 |
],
|
| 37 |
outputs=gr.Image(type="pil"),
|
| 38 |
+
title="Stable Diffusion 2 Inpainting with Brush Tool",
|
| 39 |
+
description="Upload an image, paint over areas to be inpainted, and provide a text prompt.",
|
| 40 |
)
|
| 41 |
|
| 42 |
demo.launch()
|