Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,52 @@
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
3 |
-
from diffusers import StableDiffusionInpaintPipeline
|
4 |
import torch
|
|
|
|
|
5 |
|
6 |
-
# Load
|
7 |
-
|
8 |
-
# Open image
|
9 |
-
image = Image.open(image_path).convert('RGB')
|
10 |
-
return image
|
11 |
|
12 |
-
#
|
13 |
def process_image(image, prompt):
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
return edited_image
|
24 |
|
25 |
-
# Streamlit
|
26 |
def main():
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
# Process the image based on the prompt
|
41 |
-
|
|
|
42 |
|
43 |
# Display the edited image
|
44 |
-
st.image(edited_image, caption="Edited Image",
|
45 |
-
else:
|
46 |
-
st.write("Please upload an image to begin.")
|
47 |
|
48 |
if __name__ == "__main__":
|
49 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
|
|
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")
|
|
|
|
|
|
|
9 |
|
10 |
+
# Function to process the image with the provided prompt
|
11 |
def process_image(image, prompt):
|
12 |
+
# Ensure the image is in the correct format (PIL.Image)
|
13 |
+
if isinstance(image, torch.Tensor):
|
14 |
+
image = Image.fromarray(image.numpy())
|
15 |
+
elif isinstance(image, np.ndarray):
|
16 |
+
image = Image.fromarray(image)
|
17 |
+
elif not isinstance(image, Image.Image):
|
18 |
+
raise ValueError("The image should be either a PIL Image or a numpy array.")
|
19 |
+
|
20 |
+
# Resize image if needed (e.g., to 512x512 for compatibility with the model)
|
21 |
+
image = image.resize((512, 512))
|
22 |
+
|
23 |
+
# Preprocess the image
|
24 |
+
image_tensor = pipe.image_processor.preprocess(image, return_tensors="pt").pixel_values
|
25 |
+
|
26 |
+
# Use the model pipeline for inpainting
|
27 |
+
edited_image = pipe(prompt=prompt, init_image=image_tensor, strength=0.75).images[0]
|
28 |
return edited_image
|
29 |
|
30 |
+
# Streamlit app
|
31 |
def main():
|
32 |
+
st.title("Image Inpainting with Stable Diffusion")
|
33 |
+
|
34 |
+
# Upload an image
|
35 |
+
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
36 |
+
|
37 |
+
# Input prompt for the image editing
|
38 |
+
prompt = st.text_input("Enter your prompt", "Change the color of the dragon and add fire in the background")
|
39 |
+
|
40 |
+
if uploaded_file is not None and prompt:
|
41 |
+
# Open the uploaded image
|
42 |
+
image = Image.open(uploaded_file)
|
43 |
+
|
|
|
44 |
# Process the image based on the prompt
|
45 |
+
with st.spinner("Processing the image..."):
|
46 |
+
edited_image = process_image(image, prompt)
|
47 |
|
48 |
# Display the edited image
|
49 |
+
st.image(edited_image, caption="Edited Image", use_column_width=True)
|
|
|
|
|
50 |
|
51 |
if __name__ == "__main__":
|
52 |
main()
|