Enes Bol commited on
Commit ·
46cb284
1
Parent(s): c64fafc
SS
Browse files
app.py
CHANGED
|
@@ -1,206 +1,3 @@
|
|
| 1 |
-
<<<<<<< HEAD
|
| 2 |
-
import streamlit as st
|
| 3 |
-
import os
|
| 4 |
-
import subprocess
|
| 5 |
-
from PIL import Image, ImageOps
|
| 6 |
-
import torch
|
| 7 |
-
from diffusers import StableDiffusionInpaintPipeline
|
| 8 |
-
import transformers
|
| 9 |
-
import cv2
|
| 10 |
-
import diffusers
|
| 11 |
-
import accelerate
|
| 12 |
-
import warnings
|
| 13 |
-
import numpy as np
|
| 14 |
-
import os
|
| 15 |
-
import shutil
|
| 16 |
-
warnings.filterwarnings("ignore")
|
| 17 |
-
st.title('Background Generation')
|
| 18 |
-
st.write('This app generates new backgrounds for images.')
|
| 19 |
-
# set environment variable for dll
|
| 20 |
-
os.environ['KMP_DUPLICATE_LIB_OK']='True'
|
| 21 |
-
|
| 22 |
-
@st.cache_data
|
| 23 |
-
def mode(width, height):
|
| 24 |
-
output_width = np.floor_divide(width, 8) * 8
|
| 25 |
-
output_height = np.floor_divide(height, 8) * 8
|
| 26 |
-
return output_width, output_height
|
| 27 |
-
|
| 28 |
-
def get_prompt():
|
| 29 |
-
prompt = st.text_input('Enter your prompt here:', placeholder="Imagine our perfume bottle amidst a lush garden, surrounded by blooming flowers and vibrant colors.")
|
| 30 |
-
return prompt
|
| 31 |
-
|
| 32 |
-
def get_negative_prompt():
|
| 33 |
-
negative_prompt = st.text_input('Enter your negative prompt here:', placeholder="low quality, out of frame, watermark.. etc.")
|
| 34 |
-
return negative_prompt
|
| 35 |
-
|
| 36 |
-
def get_user_input():
|
| 37 |
-
st.subheader("Upload an image file, Press Clean Background Button.")
|
| 38 |
-
uploaded_file = st.file_uploader("Upload a JPG image file", type=["jpg", "jpeg"])
|
| 39 |
-
|
| 40 |
-
if uploaded_file is not None:
|
| 41 |
-
user_file_path = os.path.join("data/custom_dataset/", uploaded_file.name)
|
| 42 |
-
|
| 43 |
-
# Open the uploaded image
|
| 44 |
-
uploaded_image = Image.open(uploaded_file)
|
| 45 |
-
|
| 46 |
-
# Check if the width is larger than 640
|
| 47 |
-
if uploaded_image.width > 640:
|
| 48 |
-
# Calculate the proportional height based on the desired width of 640 pixels
|
| 49 |
-
aspect_ratio = uploaded_image.width / uploaded_image.height
|
| 50 |
-
resized_height = int(640 / aspect_ratio)
|
| 51 |
-
# Resize the image to a width of 640 pixels and proportional height
|
| 52 |
-
resized_image = uploaded_image.resize((640, resized_height))
|
| 53 |
-
else:
|
| 54 |
-
resized_image = uploaded_image
|
| 55 |
-
|
| 56 |
-
return resized_image, user_file_path
|
| 57 |
-
|
| 58 |
-
return None, None
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
def clean_files(directory):
|
| 62 |
-
files = os.listdir(directory)
|
| 63 |
-
for file in files:
|
| 64 |
-
file_path = os.path.join(directory, file)
|
| 65 |
-
if os.path.isfile(file_path):
|
| 66 |
-
os.remove(file_path)
|
| 67 |
-
|
| 68 |
-
uploaded_file, user_file_path = get_user_input()
|
| 69 |
-
button_1 = st.button("Clean Background")
|
| 70 |
-
|
| 71 |
-
button_1_clicked = False # Variable to track button state
|
| 72 |
-
|
| 73 |
-
def run_subprocess():
|
| 74 |
-
mask_created = False
|
| 75 |
-
command = "python main.py inference --dataset custom_dataset/ --arch 7 --img_size 640 --save_map True"
|
| 76 |
-
subprocess.run(command, shell=True)
|
| 77 |
-
mask_created = True
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
# Perform the necessary actions when the "Clean Background" button is clicked
|
| 81 |
-
st.write(button_1)
|
| 82 |
-
|
| 83 |
-
# Log data for analyzing the app later.
|
| 84 |
-
def log(copy = False):
|
| 85 |
-
custom_dataset_directory = "data/custom_dataset/"
|
| 86 |
-
processed_directory = "data/processed"
|
| 87 |
-
for filename in os.listdir(custom_dataset_directory):
|
| 88 |
-
file_path = os.path.join(custom_dataset_directory, filename)
|
| 89 |
-
|
| 90 |
-
if copy == True:
|
| 91 |
-
shutil.copy(file_path, processed_directory) # Copy files
|
| 92 |
-
else:
|
| 93 |
-
shutil.move(file_path, processed_directory) # Move files
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
def load_images():
|
| 97 |
-
x = user_file_path.split('/')[-1]
|
| 98 |
-
uploaded_file_name = os.path.basename(user_file_path)
|
| 99 |
-
image_path = os.path.join("data/custom_dataset/", x)
|
| 100 |
-
dif_image = Image.open(image_path)
|
| 101 |
-
|
| 102 |
-
mask_path = os.path.join("mask/custom_dataset/", x.replace('.jpg', '.png'))
|
| 103 |
-
png_image = Image.open(mask_path)
|
| 104 |
-
inverted_image = ImageOps.invert(png_image)
|
| 105 |
-
return dif_image , inverted_image
|
| 106 |
-
|
| 107 |
-
if button_1:
|
| 108 |
-
button_1_clicked = True
|
| 109 |
-
# Move items from data/custom_dataset/ to data/processed
|
| 110 |
-
log( copy= True)
|
| 111 |
-
clean_files("data/custom_dataset/")
|
| 112 |
-
if uploaded_file is not None:
|
| 113 |
-
uploaded_file.save(user_file_path)
|
| 114 |
-
run_subprocess()
|
| 115 |
-
st.success("Background cleaned.")
|
| 116 |
-
log(copy = True)
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
st.subheader("Text your prompt and choose parameters, then press Run Model button")
|
| 121 |
-
|
| 122 |
-
# Create a two-column layout
|
| 123 |
-
col1, col2 = st.columns(2)
|
| 124 |
-
|
| 125 |
-
# Get user input for prompts
|
| 126 |
-
with col1:
|
| 127 |
-
input_prompt = st.text_area('Enter Prompt', height=80)
|
| 128 |
-
with col2:
|
| 129 |
-
input_negative_prompt = st.text_area('Enter Negative Prompt', height=80)
|
| 130 |
-
|
| 131 |
-
num_inference_steps = st.slider('Number of Inference Steps:', min_value=5, max_value=50, value=10)
|
| 132 |
-
num_images_per_prompt = st.slider('Image Count to be Produced:', min_value=1, max_value=2, value=1)
|
| 133 |
-
|
| 134 |
-
# use seed with torch generator
|
| 135 |
-
torch.manual_seed(0)
|
| 136 |
-
# seed
|
| 137 |
-
seed = st.slider('Seed:', min_value=0, max_value=100, value=1)
|
| 138 |
-
generator = [torch.Generator(device="cuda").manual_seed(seed) for i in range(num_images_per_prompt)]
|
| 139 |
-
|
| 140 |
-
#generator = torch.Generator(device="cuda").manual_seed(0)
|
| 141 |
-
run_model_button = st.button("Run Model")
|
| 142 |
-
|
| 143 |
-
@st.cache_resource
|
| 144 |
-
def initialize_pipe():
|
| 145 |
-
pipe = StableDiffusionInpaintPipeline.from_pretrained("runwayml/stable-diffusion-inpainting",
|
| 146 |
-
revision="fp16",
|
| 147 |
-
torch_dtype=torch.float32, #16 for gpu
|
| 148 |
-
safety_checker = None,
|
| 149 |
-
requires_safety_checker = False) #.to("cuda")
|
| 150 |
-
|
| 151 |
-
pipe.safety_checker = None
|
| 152 |
-
pipe.requires_safety_checker = False
|
| 153 |
-
return pipe
|
| 154 |
-
|
| 155 |
-
def image_resize(dif_image):
|
| 156 |
-
output_width, output_height = mode(dif_image.width, dif_image.height)
|
| 157 |
-
while output_height > 800:
|
| 158 |
-
output_height = output_height // 1.5
|
| 159 |
-
output_width = output_width // 1.5
|
| 160 |
-
output_width, output_height = mode(output_width, output_height)
|
| 161 |
-
return output_width, output_height
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
def show_output(x5):
|
| 165 |
-
if len(x5) == 1:
|
| 166 |
-
col1, col2 = st.columns(2)
|
| 167 |
-
with col1 :
|
| 168 |
-
st.image(inverted_image, width=256, caption='Generated Mask', use_column_width=False)
|
| 169 |
-
with col2:
|
| 170 |
-
st.image(x5[0], width=256, caption='Generated Image', use_column_width=False)
|
| 171 |
-
|
| 172 |
-
elif len(x5) == 2:
|
| 173 |
-
col1, col2, col3 = st.columns(3)
|
| 174 |
-
with col1 :
|
| 175 |
-
col1.image(inverted_image, width=256, caption='Generated Mask', use_column_width=False)
|
| 176 |
-
with col2 :
|
| 177 |
-
col2.image(x5[0], width=256, caption='Gener ted Image', use_column_width=False)
|
| 178 |
-
with col3 :
|
| 179 |
-
col3.image(x5[1], width=256, caption='Generated Image-2', use_column_width=False)
|
| 180 |
-
|
| 181 |
-
# Check if the button is clicked and all inputs are provided
|
| 182 |
-
if run_model_button == True and input_prompt is not None :
|
| 183 |
-
st.write("Running the model...")
|
| 184 |
-
dif_image , inverted_image = load_images()
|
| 185 |
-
output_width, output_height = image_resize(dif_image)
|
| 186 |
-
base_prompt = "high resolution, high quality, use mask. Do not distort the shape of the object. make the object stand out, show it clearly and vividly, preserving the shape of the object, use the mask"
|
| 187 |
-
prompt = input_prompt + " " + base_prompt
|
| 188 |
-
|
| 189 |
-
st.write("Pipe working with {0} inference steps and {1} image will be created for prompt".format(num_inference_steps, num_images_per_prompt))
|
| 190 |
-
|
| 191 |
-
pipe = initialize_pipe()
|
| 192 |
-
|
| 193 |
-
output_height = 128
|
| 194 |
-
output_width = 128
|
| 195 |
-
|
| 196 |
-
x5 = pipe(image=dif_image, mask_image=inverted_image, num_inference_steps=num_inference_steps, generator= generator,
|
| 197 |
-
num_images_per_prompt=num_images_per_prompt, prompt=prompt, negative_prompt=input_negative_prompt,
|
| 198 |
-
height=output_height, width=output_width).images
|
| 199 |
-
|
| 200 |
-
show_output(x5)
|
| 201 |
-
torch.cuda.empty_cache()
|
| 202 |
-
else:
|
| 203 |
-
=======
|
| 204 |
import streamlit as st
|
| 205 |
import os
|
| 206 |
import subprocess
|
|
@@ -404,4 +201,4 @@ if run_model_button == True and input_prompt is not None :
|
|
| 404 |
torch.cuda.empty_cache()
|
| 405 |
else:
|
| 406 |
>>>>>>> 76f5454639852d715b029dd10f1e299517131c28
|
| 407 |
-
st.write("Please provide prompt and click the 'Run Model' button to proceed.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
import subprocess
|
|
|
|
| 201 |
torch.cuda.empty_cache()
|
| 202 |
else:
|
| 203 |
>>>>>>> 76f5454639852d715b029dd10f1e299517131c28
|
| 204 |
+
st.write("Please provide prompt and click the 'Run Model' button to proceed.")
|