muneeb487 commited on
Commit
95069ef
·
verified ·
1 Parent(s): dd27165

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -6
app.py CHANGED
@@ -3,9 +3,12 @@ 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):
@@ -17,14 +20,16 @@ def process_image(image, prompt):
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
@@ -46,7 +51,7 @@ def main():
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()
 
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")
11
+ pipe.to("cuda" if torch.cuda.is_available() else "cpu") # Move model to GPU if available
12
 
13
  # Function to process the image with the provided prompt
14
  def process_image(image, prompt):
 
20
  elif not isinstance(image, Image.Image):
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
+
33
  return edited_image
34
 
35
  # Streamlit app
 
51
  edited_image = process_image(image, prompt)
52
 
53
  # Display the edited image
54
+ st.image(edited_image, caption="Edited Image", use_container_width=True)
55
 
56
  if __name__ == "__main__":
57
  main()