muneeb487 commited on
Commit
dd27165
·
verified ·
1 Parent(s): 7c6a8c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -34
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 and display an image in Streamlit
7
- def load_image(image_path):
8
- # Open image
9
- image = Image.open(image_path).convert('RGB')
10
- return image
11
 
12
- # Main function to process the image
13
  def process_image(image, prompt):
14
- # Create the pipeline using Stable Diffusion
15
- pipe = StableDiffusionInpaintPipeline.from_pretrained("stabilityai/stable-diffusion-2-inpainting")
16
-
17
- # If using GPU, send the pipeline to CUDA
18
- pipe.to("cuda" if torch.cuda.is_available() else "cpu")
19
-
20
- # Perform inpainting (change color and add fire in the background)
21
- edited_image = pipe(prompt=prompt, init_image=image, strength=0.75).images[0]
22
-
 
 
 
 
 
 
 
23
  return edited_image
24
 
25
- # Streamlit Interface
26
  def main():
27
- # Upload image
28
- uploaded_image = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
29
-
30
- if uploaded_image is not None:
31
- # Load image
32
- image = load_image(uploaded_image)
33
-
34
- # Display original image
35
- st.image(image, caption="Original Image", use_container_width=True)
36
-
37
- # Define the prompt
38
- prompt = "change the color of dragon and add fire in the background"
39
-
40
  # Process the image based on the prompt
41
- edited_image = process_image(image, prompt)
 
42
 
43
  # Display the edited image
44
- st.image(edited_image, caption="Edited Image", use_container_width=True)
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()