|
import streamlit as st |
|
from diffusers import StableDiffusionInpaintPipeline |
|
from PIL import Image |
|
import torch |
|
|
|
|
|
st.title("Insert Yourself Into Historical Photos with Stable Diffusion!") |
|
st.write("Upload a historical photo and a mask, then describe how you'd like to place yourself into the scene.") |
|
|
|
|
|
@st.cache_resource |
|
def load_pipeline(): |
|
pipe = StableDiffusionInpaintPipeline.from_pretrained( |
|
"stabilityai/stable-diffusion-2-inpainting", |
|
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32 |
|
) |
|
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu") |
|
return pipe |
|
|
|
pipe = load_pipeline() |
|
|
|
|
|
base_image = st.file_uploader("Upload the Historical Photo", type=["jpg", "jpeg", "png"]) |
|
mask_image = st.file_uploader("Upload the Mask (Black & White Image)", type=["jpg", "jpeg", "png"]) |
|
|
|
|
|
prompt = st.text_input("Describe the scene. How do you want to place yourself?") |
|
|
|
if base_image and mask_image and prompt: |
|
|
|
st.image(base_image, caption="Historical Photo", use_column_width=True) |
|
st.image(mask_image, caption="Mask", use_column_width=True) |
|
|
|
|
|
base_image = Image.open(base_image).convert("RGB") |
|
mask_image = Image.open(mask_image).convert("RGB") |
|
|
|
|
|
st.write("Generating the image... This may take a moment.") |
|
result = pipe(prompt=prompt, image=base_image, mask_image=mask_image).images[0] |
|
|
|
|
|
st.image(result, caption="Inpainted Image", use_column_width=True) |
|
st.write("Right-click on the image to download!") |
|
|
|
|