Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,8 +3,6 @@ from PIL import Image
|
|
3 |
import torch
|
4 |
from diffusers import StableDiffusionInpaintPipeline
|
5 |
import numpy as np
|
6 |
-
import requests
|
7 |
-
from io import BytesIO
|
8 |
|
9 |
# Load the StableDiffusionInpaintPipeline
|
10 |
pipe = StableDiffusionInpaintPipeline.from_pretrained("stabilityai/stable-diffusion-2-inpainting")
|
@@ -12,21 +10,15 @@ pipe.to("cuda" if torch.cuda.is_available() else "cpu") # Move model to GPU if
|
|
12 |
|
13 |
# Function to process the image with the provided prompt
|
14 |
def process_image(image, prompt):
|
15 |
-
# Ensure the image is
|
16 |
-
if isinstance(image,
|
17 |
-
image = Image.fromarray(image
|
18 |
-
elif isinstance(image,
|
19 |
-
image = Image.fromarray(image)
|
20 |
-
|
21 |
-
raise ValueError("The image should be either a PIL Image or a numpy array.")
|
22 |
-
|
23 |
# Resize image to the required size (512x512)
|
24 |
image = image.resize((512, 512))
|
25 |
|
26 |
-
# Convert the PIL image to the format that the model expects
|
27 |
-
image = np.array(image) # Convert to numpy array
|
28 |
-
image = torch.from_numpy(image).unsqueeze(0).float() # Convert to tensor and add batch dimension
|
29 |
-
|
30 |
# Process the image through the pipeline
|
31 |
edited_image = pipe(prompt=prompt, init_image=image, strength=0.75).images[0]
|
32 |
|
|
|
3 |
import torch
|
4 |
from diffusers import StableDiffusionInpaintPipeline
|
5 |
import numpy as np
|
|
|
|
|
6 |
|
7 |
# Load the StableDiffusionInpaintPipeline
|
8 |
pipe = StableDiffusionInpaintPipeline.from_pretrained("stabilityai/stable-diffusion-2-inpainting")
|
|
|
10 |
|
11 |
# Function to process the image with the provided prompt
|
12 |
def process_image(image, prompt):
|
13 |
+
# Ensure the image is a PIL Image
|
14 |
+
if isinstance(image, np.ndarray):
|
15 |
+
image = Image.fromarray(image) # Convert numpy array to PIL Image
|
16 |
+
elif isinstance(image, torch.Tensor):
|
17 |
+
image = Image.fromarray(image.numpy()) # Convert torch tensor to PIL Image
|
18 |
+
|
|
|
|
|
19 |
# Resize image to the required size (512x512)
|
20 |
image = image.resize((512, 512))
|
21 |
|
|
|
|
|
|
|
|
|
22 |
# Process the image through the pipeline
|
23 |
edited_image = pipe(prompt=prompt, init_image=image, strength=0.75).images[0]
|
24 |
|