Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
""")
|