Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,49 @@
|
|
1 |
import streamlit as st
|
2 |
-
from PIL import Image
|
3 |
-
import
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
if
|
37 |
-
|
38 |
-
|
39 |
-
|
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 |
-
|
49 |
-
|
50 |
-
#
|
51 |
-
|
|
|
|
|
52 |
except Exception as e:
|
53 |
-
st.error(f"Error
|
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}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|