Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from torchvision import transforms
|
| 6 |
+
from skimage.color import rgb2lab, lab2rgb
|
| 7 |
+
import numpy as np
|
| 8 |
+
import matplotlib.pyplot as plt
|
| 9 |
+
from io import BytesIO
|
| 10 |
+
|
| 11 |
+
# Download the model from Hugging Face Hub
|
| 12 |
+
repo_id = "Hammad712/GAN-Colorization-Model"
|
| 13 |
+
model_filename = "generator.pt"
|
| 14 |
+
model_path = hf_hub_download(repo_id=repo_id, filename=model_filename)
|
| 15 |
+
|
| 16 |
+
# Define the generator model (same architecture as used during training)
|
| 17 |
+
from fastai.vision.learner import create_body
|
| 18 |
+
from torchvision.models import resnet34
|
| 19 |
+
from fastai.vision.models.unet import DynamicUnet
|
| 20 |
+
|
| 21 |
+
def build_generator(n_input=1, n_output=2, size=256):
|
| 22 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 23 |
+
backbone = create_body(resnet34(), pretrained=True, n_in=n_input, cut=-2)
|
| 24 |
+
G_net = DynamicUnet(backbone, n_output, (size, size)).to(device)
|
| 25 |
+
return G_net
|
| 26 |
+
|
| 27 |
+
# Initialize and load the model
|
| 28 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 29 |
+
G_net = build_generator(n_input=1, n_output=2, size=256)
|
| 30 |
+
G_net.load_state_dict(torch.load(model_path, map_location=device))
|
| 31 |
+
G_net.eval()
|
| 32 |
+
|
| 33 |
+
# Preprocessing function
|
| 34 |
+
def preprocess_image(img_path):
|
| 35 |
+
img = Image.open(img_path).convert("RGB")
|
| 36 |
+
img = transforms.Resize((256, 256), Image.BICUBIC)(img)
|
| 37 |
+
img = np.array(img)
|
| 38 |
+
img_to_lab = rgb2lab(img).astype("float32")
|
| 39 |
+
img_to_lab = transforms.ToTensor()(img_to_lab)
|
| 40 |
+
L = img_to_lab[[0], ...] / 50. - 1.
|
| 41 |
+
return L.unsqueeze(0).to(device)
|
| 42 |
+
|
| 43 |
+
# Inference function
|
| 44 |
+
def colorize_image(img_path, model):
|
| 45 |
+
L = preprocess_image(img_path)
|
| 46 |
+
with torch.no_grad():
|
| 47 |
+
ab = model(L)
|
| 48 |
+
L = (L + 1.) * 50.
|
| 49 |
+
ab = ab * 110.
|
| 50 |
+
Lab = torch.cat([L, ab], dim=1).permute(0, 2, 3, 1).cpu().numpy()
|
| 51 |
+
rgb_imgs = []
|
| 52 |
+
for img in Lab:
|
| 53 |
+
img_rgb = lab2rgb(img)
|
| 54 |
+
rgb_imgs.append(img_rgb)
|
| 55 |
+
return np.stack(rgb_imgs, axis=0)
|
| 56 |
+
|
| 57 |
+
# Custom CSS
|
| 58 |
+
def set_css(style):
|
| 59 |
+
st.markdown(f"<style>{style}</style>", unsafe_allow_html=True)
|
| 60 |
+
|
| 61 |
+
# Combined dark mode styles
|
| 62 |
+
combined_css = """
|
| 63 |
+
.main, .sidebar .sidebar-content { background-color: #1c1c1c; color: #f0f2f6; }
|
| 64 |
+
.block-container { padding: 1rem 2rem; background-color: #333; border-radius: 10px; box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.5); }
|
| 65 |
+
.stButton>button, .stDownloadButton>button { background: linear-gradient(135deg, #ff7e5f, #feb47b); color: white; border: none; padding: 10px 24px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border-radius: 5px; }
|
| 66 |
+
.stSpinner { color: #4CAF50; }
|
| 67 |
+
.title {
|
| 68 |
+
font-size: 3rem;
|
| 69 |
+
font-weight: bold;
|
| 70 |
+
display: flex;
|
| 71 |
+
align-items: center;
|
| 72 |
+
justify-content: center;
|
| 73 |
+
}
|
| 74 |
+
.colorful-text {
|
| 75 |
+
background: -webkit-linear-gradient(135deg, #ff7e5f, #feb47b);
|
| 76 |
+
-webkit-background-clip: text;
|
| 77 |
+
-webkit-text-fill-color: transparent;
|
| 78 |
+
}
|
| 79 |
+
.black-white-text {
|
| 80 |
+
color: black;
|
| 81 |
+
}
|
| 82 |
+
.small-input .stTextInput>div>input {
|
| 83 |
+
height: 2rem;
|
| 84 |
+
font-size: 0.9rem;
|
| 85 |
+
}
|
| 86 |
+
.small-file-uploader .stFileUploader>div>div {
|
| 87 |
+
height: 2rem;
|
| 88 |
+
font-size: 0.9rem;
|
| 89 |
+
}
|
| 90 |
+
.custom-text {
|
| 91 |
+
font-size: 1.2rem;
|
| 92 |
+
color: #feb47b;
|
| 93 |
+
text-align: center;
|
| 94 |
+
margin-top: -20px;
|
| 95 |
+
margin-bottom: 20px;
|
| 96 |
+
}
|
| 97 |
+
"""
|
| 98 |
+
|
| 99 |
+
# Streamlit application
|
| 100 |
+
st.set_page_config(layout="wide")
|
| 101 |
+
|
| 102 |
+
st.markdown(f"<style>{combined_css}</style>", unsafe_allow_html=True)
|
| 103 |
+
|
| 104 |
+
st.markdown('<div class="title"><span class="colorful-text">Image</span> <span class="black-white-text">Colorization</span></div>', unsafe_allow_html=True)
|
| 105 |
+
st.markdown('<div class="custom-text">Convert black and white images to color using AI</div>', unsafe_allow_html=True)
|
| 106 |
+
|
| 107 |
+
# Input for image URL or file upload
|
| 108 |
+
with st.expander("Input Options", expanded=True):
|
| 109 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png", "webp"], key="upload_file", help="Upload an image file to convert")
|
| 110 |
+
|
| 111 |
+
# Run inference button
|
| 112 |
+
if st.button("Colorize"):
|
| 113 |
+
if uploaded_file is not None:
|
| 114 |
+
with st.spinner('Processing...'):
|
| 115 |
+
try:
|
| 116 |
+
colorized_images = colorize_image(uploaded_file, G_net)
|
| 117 |
+
colorized_image = colorized_images[0]
|
| 118 |
+
|
| 119 |
+
# Display original and colorized images side by side
|
| 120 |
+
st.markdown("### Result")
|
| 121 |
+
col1, col2 = st.columns(2)
|
| 122 |
+
|
| 123 |
+
with col1:
|
| 124 |
+
st.image(uploaded_file, caption='Original Image', use_column_width=True)
|
| 125 |
+
with col2:
|
| 126 |
+
st.image(colorized_image, caption='Colorized Image', use_column_width=True)
|
| 127 |
+
|
| 128 |
+
# Provide a download button for the colorized image
|
| 129 |
+
img_byte_arr = BytesIO()
|
| 130 |
+
Image.fromarray((colorized_image * 255).astype(np.uint8)).save(img_byte_arr, format='JPEG')
|
| 131 |
+
img_byte_arr = img_byte_arr.getvalue()
|
| 132 |
+
|
| 133 |
+
st.download_button(
|
| 134 |
+
label="Download Colorized Image",
|
| 135 |
+
data=img_byte_arr,
|
| 136 |
+
file_name="colorized_image.jpg",
|
| 137 |
+
mime="image/jpeg"
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
st.success("Image processed successfully!")
|
| 141 |
+
|
| 142 |
+
except Exception as e:
|
| 143 |
+
st.error(f"An error occurred: {e}")
|
| 144 |
+
logging.error("Error during inference", exc_info=True)
|
| 145 |
+
else:
|
| 146 |
+
st.error("Please upload an image file.")
|