Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,8 @@ 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"
|
@@ -31,8 +33,8 @@ G_net.load_state_dict(torch.load(model_path, map_location=device))
|
|
31 |
G_net.eval()
|
32 |
|
33 |
# Preprocessing function
|
34 |
-
def preprocess_image(
|
35 |
-
img =
|
36 |
img = transforms.Resize((256, 256), Image.BICUBIC)(img)
|
37 |
img = np.array(img)
|
38 |
img_to_lab = rgb2lab(img).astype("float32")
|
@@ -41,8 +43,8 @@ def preprocess_image(img_path):
|
|
41 |
return L.unsqueeze(0).to(device)
|
42 |
|
43 |
# Inference function
|
44 |
-
def colorize_image(
|
45 |
-
L = preprocess_image(
|
46 |
with torch.no_grad():
|
47 |
ab = model(L)
|
48 |
L = (L + 1.) * 50.
|
@@ -67,8 +69,7 @@ combined_css = """
|
|
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 {
|
@@ -101,19 +102,31 @@ 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="
|
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(
|
117 |
colorized_image = colorized_images[0]
|
118 |
|
119 |
# Display original and colorized images side by side
|
@@ -121,7 +134,7 @@ if st.button("Colorize"):
|
|
121 |
col1, col2 = st.columns(2)
|
122 |
|
123 |
with col1:
|
124 |
-
st.image(
|
125 |
with col2:
|
126 |
st.image(colorized_image, caption='Colorized Image', use_column_width=True)
|
127 |
|
@@ -141,6 +154,5 @@ if st.button("Colorize"):
|
|
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.")
|
|
|
7 |
import numpy as np
|
8 |
import matplotlib.pyplot as plt
|
9 |
from io import BytesIO
|
10 |
+
import requests
|
11 |
+
from io import BytesIO
|
12 |
|
13 |
# Download the model from Hugging Face Hub
|
14 |
repo_id = "Hammad712/GAN-Colorization-Model"
|
|
|
33 |
G_net.eval()
|
34 |
|
35 |
# Preprocessing function
|
36 |
+
def preprocess_image(img):
|
37 |
+
img = img.convert("RGB")
|
38 |
img = transforms.Resize((256, 256), Image.BICUBIC)(img)
|
39 |
img = np.array(img)
|
40 |
img_to_lab = rgb2lab(img).astype("float32")
|
|
|
43 |
return L.unsqueeze(0).to(device)
|
44 |
|
45 |
# Inference function
|
46 |
+
def colorize_image(img, model):
|
47 |
+
L = preprocess_image(img)
|
48 |
with torch.no_grad():
|
49 |
ab = model(L)
|
50 |
L = (L + 1.) * 50.
|
|
|
69 |
.title {
|
70 |
font-size: 3rem;
|
71 |
font-weight: bold;
|
72 |
+
display: flex; align-items: center;
|
|
|
73 |
justify-content: center;
|
74 |
}
|
75 |
.colorful-text {
|
|
|
102 |
|
103 |
st.markdown(f"<style>{combined_css}</style>", unsafe_allow_html=True)
|
104 |
|
105 |
+
st.markdown('<div class="title"><span class="black-white-text">Image</span> <span class="colorful-text">Colorization</span></div>', unsafe_allow_html=True)
|
106 |
st.markdown('<div class="custom-text">Convert black and white images to color using AI</div>', unsafe_allow_html=True)
|
107 |
|
108 |
# Input for image URL or file upload
|
109 |
with st.expander("Input Options", expanded=True):
|
110 |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png", "webp"], key="upload_file", help="Upload an image file to convert")
|
111 |
+
url_input = st.text_input("Or enter an image URL", key="url_input", help="Enter the URL of an image to convert")
|
112 |
|
113 |
# Run inference button
|
114 |
if st.button("Colorize"):
|
115 |
+
img = None
|
116 |
+
|
117 |
if uploaded_file is not None:
|
118 |
+
img = Image.open(uploaded_file)
|
119 |
+
elif url_input:
|
120 |
+
try:
|
121 |
+
response = requests.get(url_input)
|
122 |
+
img = Image.open(BytesIO(response.content))
|
123 |
+
except Exception as e:
|
124 |
+
st.error(f"Error fetching the image from URL: {e}")
|
125 |
+
|
126 |
+
if img is not None:
|
127 |
with st.spinner('Processing...'):
|
128 |
try:
|
129 |
+
colorized_images = colorize_image(img, G_net)
|
130 |
colorized_image = colorized_images[0]
|
131 |
|
132 |
# Display original and colorized images side by side
|
|
|
134 |
col1, col2 = st.columns(2)
|
135 |
|
136 |
with col1:
|
137 |
+
st.image(img, caption='Original Image', use_column_width=True)
|
138 |
with col2:
|
139 |
st.image(colorized_image, caption='Colorized Image', use_column_width=True)
|
140 |
|
|
|
154 |
|
155 |
except Exception as e:
|
156 |
st.error(f"An error occurred: {e}")
|
|
|
157 |
else:
|
158 |
+
st.error("Please upload an image file or provide a valid URL.")
|