Spaces:
Runtime error
Runtime error
added print statements for debugging
Browse files
app.py
CHANGED
|
@@ -3,28 +3,48 @@ from diffusers import StableDiffusionInpaintPipeline
|
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
import numpy as np
|
| 6 |
-
from torchvision import transforms
|
| 7 |
|
| 8 |
# Load the Stable Diffusion inpainting model (ensure GPU usage if available)
|
| 9 |
pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
| 10 |
-
"stabilityai/stable-diffusion-2-1", #
|
| 11 |
torch_dtype=torch.float16
|
| 12 |
).to("cuda")
|
| 13 |
|
| 14 |
-
# Define the transform to convert image to tensor
|
| 15 |
-
transform = transforms.Compose([
|
| 16 |
-
transforms.Resize((512, 512)), # Resize to 512x512 (or whatever input size the model expects)
|
| 17 |
-
transforms.ToTensor(), # Convert to tensor
|
| 18 |
-
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]), # Normalize to the range [-1, 1]
|
| 19 |
-
])
|
| 20 |
-
|
| 21 |
# Function to remove makeup based on prompt and image
|
| 22 |
def remove_makeup(image, prompt):
|
| 23 |
-
#
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# Use the model to process the image with the given prompt
|
| 27 |
result = pipe(prompt=prompt, image=image_tensor).images[0]
|
|
|
|
|
|
|
| 28 |
return result
|
| 29 |
|
| 30 |
# Define the Gradio interface
|
|
|
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
|
| 7 |
# Load the Stable Diffusion inpainting model (ensure GPU usage if available)
|
| 8 |
pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
| 9 |
+
"stabilityai/stable-diffusion-2-1", # You can use another inpainting model here
|
| 10 |
torch_dtype=torch.float16
|
| 11 |
).to("cuda")
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# Function to remove makeup based on prompt and image
|
| 14 |
def remove_makeup(image, prompt):
|
| 15 |
+
# Debugging: Check the type of the input image
|
| 16 |
+
print(f"Input image type: {type(image)}")
|
| 17 |
+
|
| 18 |
+
# Ensure the input image is a PIL Image
|
| 19 |
+
if not isinstance(image, Image.Image):
|
| 20 |
+
image = Image.fromarray(image)
|
| 21 |
+
|
| 22 |
+
# Debugging: Print the size before resizing
|
| 23 |
+
print(f"Original image size: {image.size}")
|
| 24 |
+
|
| 25 |
+
# Resize the image to 512x512 (standard input size for Stable Diffusion)
|
| 26 |
+
image = image.resize((512, 512))
|
| 27 |
+
|
| 28 |
+
# Debugging: Print the size after resizing
|
| 29 |
+
print(f"Resized image size: {image.size}")
|
| 30 |
+
|
| 31 |
+
# Convert the image to a numpy array and normalize to range [-1, 1]
|
| 32 |
+
image_array = np.array(image) / 255.0
|
| 33 |
+
image_array = (image_array - 0.5) / 0.5 # Normalize to [-1, 1]
|
| 34 |
+
|
| 35 |
+
# Debugging: Check the shape and type of the image array
|
| 36 |
+
print(f"Image array shape: {image_array.shape}, type: {type(image_array)}")
|
| 37 |
+
|
| 38 |
+
# Convert to tensor and move to GPU
|
| 39 |
+
image_tensor = torch.tensor(image_array).permute(2, 0, 1).unsqueeze(0).float().to("cuda") # Convert to tensor
|
| 40 |
+
|
| 41 |
+
# Debugging: Check the shape and type of the tensor before sending it to the model
|
| 42 |
+
print(f"Image tensor shape: {image_tensor.shape}, type: {type(image_tensor)}")
|
| 43 |
|
| 44 |
# Use the model to process the image with the given prompt
|
| 45 |
result = pipe(prompt=prompt, image=image_tensor).images[0]
|
| 46 |
+
|
| 47 |
+
# Return the result
|
| 48 |
return result
|
| 49 |
|
| 50 |
# Define the Gradio interface
|