muneeb487 commited on
Commit
b303b1d
·
verified ·
1 Parent(s): 786fe91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -63
app.py CHANGED
@@ -1,66 +1,49 @@
1
  import streamlit as st
2
- from PIL import Image, ImageDraw, ImageFont
3
- import io
4
- import requests
5
- from transformers import pipeline
6
-
7
- # Initialize the AI model for image generation or editing
8
- # This uses HuggingFace's Stable Diffusion pipeline
9
- stability_pipeline = pipeline("image-to-image", model="stabilityai/stable-diffusion-2-inpainting")
10
-
11
- # Function to edit the image based on the command
12
- def process_command(image, command):
13
- """
14
- Processes the command and modifies the image using AI tools.
15
- :param image: PIL Image object
16
- :param command: Natural language command (e.g., "Add a cat in the bottom-right corner.")
17
- :return: Edited PIL Image
18
- """
19
- # Convert the image to bytes for the AI model
20
- img_byte_arr = io.BytesIO()
21
- image.save(img_byte_arr, format='PNG')
22
- img_byte_arr = img_byte_arr.getvalue()
23
-
24
- # Use the Stable Diffusion pipeline to modify the image
25
- result = stability_pipeline(images=image, prompt=command)
26
- edited_image = result[0]['image']
27
- return edited_image
28
-
29
- # Streamlit App Interface
30
- st.title("AI-Powered Image Editing")
31
- st.write("Upload an image, enter a command, and let AI handle the editing!")
32
-
33
- # Step 1: Upload Image
34
- uploaded_file = st.file_uploader("Upload an image to edit", type=["jpg", "jpeg", "png"])
35
-
36
- if uploaded_file:
37
- # Open the uploaded image
38
- input_image = Image.open(uploaded_file).convert("RGB")
39
- st.image(input_image, caption="Uploaded Image", use_column_width=True)
40
-
41
- # Step 2: Enter a Command
42
- command = st.text_input("Enter a command to edit the image (e.g., 'Add a tree in the background')")
43
-
44
- if st.button("Process Command"):
45
- if command:
46
- # Step 3: Process the Command
47
  try:
48
- with st.spinner("Processing your command..."):
49
- edited_image = process_command(input_image, command)
50
- # Step 4: Display the Edited Image
51
- st.image(edited_image, caption="Edited Image", use_column_width=True)
 
 
52
  except Exception as e:
53
- st.error(f"Error processing the image: {e}")
54
- else:
55
- st.warning("Please enter a command.")
56
-
57
- # Instructions for the user
58
- st.write("""
59
- ### How to use this app:
60
- 1. Upload an image in JPG or PNG format.
61
- 2. Enter a natural language command like:
62
- - "Add a cat to the bottom-right corner."
63
- - "Remove the text from the top-left corner."
64
- - "Make the background cloudy."
65
- 3. Click "Process Command" and view the results!
66
- """)
 
1
  import streamlit as st
2
+ from PIL import Image
3
+ from diffusers import StableDiffusionInpaintPipeline
4
+ import torch
5
+ import os
6
+
7
+ # Load Stable Diffusion Inpainting Pipeline
8
+ @st.cache_resource
9
+ def load_pipeline():
10
+ pipeline = StableDiffusionInpaintPipeline.from_pretrained(
11
+ "stabilityai/stable-diffusion-2-inpainting",
12
+ torch_dtype=torch.float16,
13
+ )
14
+ return pipeline.to("cuda" if torch.cuda.is_available() else "cpu")
15
+
16
+ stability_pipeline = load_pipeline()
17
+
18
+ # Streamlit App Title
19
+ st.title("AI-Powered Image Editor")
20
+ st.markdown("Upload an image, optionally upload a mask, and provide a command to edit the image.")
21
+
22
+ # Image Upload
23
+ uploaded_image = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
24
+ uploaded_mask = st.file_uploader("Upload a Mask Image (optional)", type=["jpg", "jpeg", "png"])
25
+ prompt = st.text_input("Enter your prompt (e.g., 'Add a sunset in the background')")
26
+
27
+ if st.button("Generate"):
28
+ if uploaded_image is None:
29
+ st.error("Please upload an image to proceed.")
30
+ else:
31
+ # Load and process the uploaded image
32
+ image = Image.open(uploaded_image).convert("RGB")
33
+ mask = Image.open(uploaded_mask).convert("RGB") if uploaded_mask else None
34
+
35
+ st.image(image, caption="Uploaded Image", use_column_width=True)
36
+ if mask:
37
+ st.image(mask, caption="Uploaded Mask", use_column_width=True)
38
+
39
+ # Generate the edited image
40
+ with st.spinner("Generating edited image..."):
 
 
 
 
 
 
41
  try:
42
+ result = stability_pipeline(prompt=prompt, image=image, mask_image=mask).images[0]
43
+ st.image(result, caption="Edited Image", use_column_width=True)
44
+ # Save the result
45
+ output_path = "edited_image.jpg"
46
+ result.save(output_path)
47
+ st.success(f"Image generated and saved as {output_path}")
48
  except Exception as e:
49
+ st.error(f"Error: {e}")