Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ from modelscope.outputs import OutputKeys
|
|
| 10 |
from modelscope.pipelines import pipeline
|
| 11 |
from modelscope.utils.constant import Tasks
|
| 12 |
|
| 13 |
-
# Load the colorization model
|
| 14 |
img_colorization = pipeline(
|
| 15 |
Tasks.image_colorization,
|
| 16 |
model="iic/cv_ddcolor_image-colorization"
|
|
@@ -33,8 +33,12 @@ def enhance_image(
|
|
| 33 |
edge_enhance: bool = False
|
| 34 |
) -> str:
|
| 35 |
image = Image.open(img_path)
|
|
|
|
|
|
|
| 36 |
image = ImageEnhance.Brightness(image).enhance(brightness)
|
|
|
|
| 37 |
image = ImageEnhance.Contrast(image).enhance(contrast)
|
|
|
|
| 38 |
if edge_enhance:
|
| 39 |
image = image.filter(ImageFilter.EDGE_ENHANCE)
|
| 40 |
|
|
@@ -50,220 +54,147 @@ def process_image(
|
|
| 50 |
edge_enhance: bool,
|
| 51 |
output_format: str
|
| 52 |
):
|
| 53 |
-
# 1
|
| 54 |
colorized_path = colorize_image(img_path)
|
| 55 |
-
# 2
|
| 56 |
enhanced_path = enhance_image(colorized_path, brightness, contrast, edge_enhance)
|
| 57 |
-
# 3
|
| 58 |
img = Image.open(enhanced_path)
|
| 59 |
temp_dir = tempfile.mkdtemp()
|
| 60 |
filename = f"colorized_image.{output_format.lower()}"
|
| 61 |
output_path = os.path.join(temp_dir, filename)
|
| 62 |
img.save(output_path, format=output_format.upper())
|
| 63 |
|
| 64 |
-
# Return (
|
| 65 |
return ([img_path, enhanced_path], output_path)
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
/* HEADER */
|
| 86 |
-
#header {
|
| 87 |
-
background: linear-gradient(135deg, #6b73ff 0%, #000dff 100%);
|
| 88 |
-
padding: 24px 16px;
|
| 89 |
border-radius: 8px;
|
| 90 |
-
color: #fff;
|
| 91 |
text-align: center;
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
margin: 0;
|
| 97 |
font-size: 2rem;
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
margin:
|
| 102 |
font-size: 1rem;
|
| 103 |
-
|
| 104 |
-
opacity: 0.9;
|
| 105 |
-
}
|
| 106 |
-
|
| 107 |
-
/* CONTROL PANEL */
|
| 108 |
-
#control-panel {
|
| 109 |
-
background-color: #ffffff;
|
| 110 |
-
padding: 24px;
|
| 111 |
-
border-radius: 8px;
|
| 112 |
-
box-shadow: 0 2px 8px rgba(0,0,0,0.05);
|
| 113 |
-
margin-bottom: 32px;
|
| 114 |
-
}
|
| 115 |
-
|
| 116 |
-
/* LABELS & COMPONENTS */
|
| 117 |
-
.gr-label {
|
| 118 |
-
color: #333;
|
| 119 |
-
font-weight: 600;
|
| 120 |
-
margin-bottom: 8px;
|
| 121 |
-
}
|
| 122 |
-
.gr-slider, .gr-dropdown, .gr-checkbox {
|
| 123 |
-
margin-top: 12px;
|
| 124 |
-
}
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
background:
|
| 129 |
-
|
| 130 |
-
border-radius: 8px !important;
|
| 131 |
-
font-weight: 600;
|
| 132 |
-
padding: 12px 24px !important;
|
| 133 |
-
margin-top: 16px !important;
|
| 134 |
-
transition: background 0.2s ease-in-out;
|
| 135 |
-
}
|
| 136 |
-
#submit-btn:hover {
|
| 137 |
-
background: #2029ff !important;
|
| 138 |
-
}
|
| 139 |
-
|
| 140 |
-
/* GALLERY */
|
| 141 |
-
#comparison_gallery {
|
| 142 |
-
background-color: #ffffff;
|
| 143 |
-
padding: 16px;
|
| 144 |
border-radius: 8px;
|
| 145 |
-
box-shadow:
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
background: #00c853 !important;
|
| 154 |
-
color: #fff !important;
|
| 155 |
border-radius: 8px !important;
|
| 156 |
-
font-weight:
|
| 157 |
padding: 10px 20px !important;
|
| 158 |
-
margin-top:
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
background: rgba(51, 51, 51, 0.9);
|
| 177 |
-
color: #fff;
|
| 178 |
-
padding: 6px 10px;
|
| 179 |
-
border-radius: 4px;
|
| 180 |
-
white-space: nowrap;
|
| 181 |
-
font-size: 0.875rem;
|
| 182 |
-
z-index: 10;
|
| 183 |
-
opacity: 1;
|
| 184 |
-
}
|
| 185 |
-
.has-tooltip::after {
|
| 186 |
-
opacity: 0;
|
| 187 |
-
transition: opacity 0.2s ease-in-out;
|
| 188 |
-
}
|
| 189 |
-
</style>
|
| 190 |
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
if (gallery) {
|
| 196 |
-
gallery.scrollIntoView({ behavior: "smooth", block: "start" });
|
| 197 |
-
}
|
| 198 |
-
}
|
| 199 |
-
document.addEventListener("gradio:submit", function() {
|
| 200 |
-
setTimeout(scrollToGallery, 800);
|
| 201 |
-
});
|
| 202 |
-
</script>
|
| 203 |
"""
|
| 204 |
|
| 205 |
TITLE = "🌈 Color Restorization Model"
|
| 206 |
DESCRIPTION = "Bring your old black & white photos back to life—upload, adjust, and download in vivid color."
|
| 207 |
|
| 208 |
-
with gr.Blocks(title=TITLE) as app:
|
| 209 |
-
#
|
| 210 |
-
gr.HTML(CUSTOM_STYLE)
|
| 211 |
-
|
| 212 |
-
# Header
|
| 213 |
gr.HTML(
|
| 214 |
"""
|
| 215 |
<div id="header">
|
| 216 |
-
|
| 217 |
-
|
| 218 |
</div>
|
| 219 |
"""
|
| 220 |
)
|
| 221 |
|
| 222 |
-
#
|
| 223 |
with gr.Column(elem_id="control-panel"):
|
| 224 |
with gr.Row():
|
| 225 |
# Left column: inputs and controls
|
| 226 |
-
with gr.Column(
|
| 227 |
input_image = gr.Image(
|
| 228 |
type="filepath",
|
| 229 |
label="Upload B&W Image",
|
| 230 |
-
tool="editor",
|
| 231 |
-
interactive=True
|
| 232 |
-
elem_id="input-image"
|
| 233 |
)
|
| 234 |
brightness_slider = gr.Slider(
|
| 235 |
minimum=0.5, maximum=2.0, value=1.0,
|
| 236 |
-
label="Brightness"
|
| 237 |
-
elem_id="brightness-slider"
|
| 238 |
)
|
| 239 |
contrast_slider = gr.Slider(
|
| 240 |
minimum=0.5, maximum=2.0, value=1.0,
|
| 241 |
-
label="Contrast"
|
| 242 |
-
elem_id="contrast-slider"
|
| 243 |
)
|
| 244 |
edge_enhance_checkbox = gr.Checkbox(
|
| 245 |
-
label="Apply Edge Enhancement"
|
| 246 |
-
elem_id="edge-checkbox"
|
| 247 |
)
|
| 248 |
output_format_dropdown = gr.Dropdown(
|
| 249 |
choices=["PNG", "JPEG", "TIFF"],
|
| 250 |
value="PNG",
|
| 251 |
-
label="Output Format"
|
| 252 |
-
elem_id="format-dropdown"
|
| 253 |
)
|
| 254 |
submit_btn = gr.Button(
|
| 255 |
-
"Colorize",
|
| 256 |
elem_id="submit-btn"
|
| 257 |
)
|
| 258 |
|
| 259 |
# Right column: results gallery & download
|
| 260 |
-
with gr.Column(
|
| 261 |
comparison_gallery = gr.Gallery(
|
| 262 |
label="Original vs. Colorized",
|
| 263 |
columns=2,
|
| 264 |
elem_id="comparison_gallery",
|
| 265 |
height="auto"
|
| 266 |
-
)
|
| 267 |
download_btn = gr.File(
|
| 268 |
label="Download Colorized Image",
|
| 269 |
elem_id="download-btn"
|
|
@@ -281,7 +212,7 @@ with gr.Blocks(title=TITLE) as app:
|
|
| 281 |
outputs=[comparison_gallery, download_btn]
|
| 282 |
)
|
| 283 |
|
| 284 |
-
#
|
| 285 |
if __name__ == "__main__":
|
| 286 |
port = int(os.environ.get("PORT", 7860))
|
| 287 |
-
app.queue().launch(server_name="0.0.0.0", server_port=port)
|
|
|
|
| 10 |
from modelscope.pipelines import pipeline
|
| 11 |
from modelscope.utils.constant import Tasks
|
| 12 |
|
| 13 |
+
# Load the colorization model at startup
|
| 14 |
img_colorization = pipeline(
|
| 15 |
Tasks.image_colorization,
|
| 16 |
model="iic/cv_ddcolor_image-colorization"
|
|
|
|
| 33 |
edge_enhance: bool = False
|
| 34 |
) -> str:
|
| 35 |
image = Image.open(img_path)
|
| 36 |
+
|
| 37 |
+
# Brightness adjustment
|
| 38 |
image = ImageEnhance.Brightness(image).enhance(brightness)
|
| 39 |
+
# Contrast adjustment
|
| 40 |
image = ImageEnhance.Contrast(image).enhance(contrast)
|
| 41 |
+
# Optional edge enhancement
|
| 42 |
if edge_enhance:
|
| 43 |
image = image.filter(ImageFilter.EDGE_ENHANCE)
|
| 44 |
|
|
|
|
| 54 |
edge_enhance: bool,
|
| 55 |
output_format: str
|
| 56 |
):
|
| 57 |
+
# Step 1: Colorize
|
| 58 |
colorized_path = colorize_image(img_path)
|
| 59 |
+
# Step 2: Enhance (brightness/contrast/edge)
|
| 60 |
enhanced_path = enhance_image(colorized_path, brightness, contrast, edge_enhance)
|
| 61 |
+
# Step 3: Convert to chosen format
|
| 62 |
img = Image.open(enhanced_path)
|
| 63 |
temp_dir = tempfile.mkdtemp()
|
| 64 |
filename = f"colorized_image.{output_format.lower()}"
|
| 65 |
output_path = os.path.join(temp_dir, filename)
|
| 66 |
img.save(output_path, format=output_format.upper())
|
| 67 |
|
| 68 |
+
# Return side-by-side (original, enhanced) and the downloadable file
|
| 69 |
return ([img_path, enhanced_path], output_path)
|
| 70 |
|
| 71 |
+
# CSS to give a modern, centered layout with a colored header and clean panels
|
| 72 |
+
custom_css = """
|
| 73 |
+
/* Overall background */
|
| 74 |
+
body {
|
| 75 |
+
background-color: #f0f2f5;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
/* Center the Gradio container and give it a max width */
|
| 79 |
+
.gradio-container {
|
| 80 |
+
max-width: 900px !important;
|
| 81 |
+
margin: auto !important;
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
/* Header styling */
|
| 85 |
+
#header {
|
| 86 |
+
background-color: #4CAF50;
|
| 87 |
+
padding: 20px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
border-radius: 8px;
|
|
|
|
| 89 |
text-align: center;
|
| 90 |
+
margin-bottom: 20px;
|
| 91 |
+
}
|
| 92 |
+
#header h2 {
|
| 93 |
+
color: white;
|
| 94 |
margin: 0;
|
| 95 |
font-size: 2rem;
|
| 96 |
+
}
|
| 97 |
+
#header p {
|
| 98 |
+
color: white;
|
| 99 |
+
margin: 5px 0 0 0;
|
| 100 |
font-size: 1rem;
|
| 101 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
/* White panel for controls */
|
| 104 |
+
#control-panel {
|
| 105 |
+
background-color: white;
|
| 106 |
+
padding: 20px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
border-radius: 8px;
|
| 108 |
+
box-shadow: 0px 2px 8px rgba(0,0,0,0.1);
|
| 109 |
+
margin-bottom: 20px;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
/* Style the “Colorize” button */
|
| 113 |
+
#submit-btn {
|
| 114 |
+
background-color: #4CAF50 !important;
|
| 115 |
+
color: white !important;
|
|
|
|
|
|
|
| 116 |
border-radius: 8px !important;
|
| 117 |
+
font-weight: bold;
|
| 118 |
padding: 10px 20px !important;
|
| 119 |
+
margin-top: 10px !important;
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
/* Add some spacing around sliders and checkbox */
|
| 123 |
+
#control-panel .gr-row {
|
| 124 |
+
gap: 15px;
|
| 125 |
+
}
|
| 126 |
+
.gr-slider, .gr-checkbox, .gr-dropdown {
|
| 127 |
+
margin-top: 10px;
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
/* Gallery panel styling */
|
| 131 |
+
#comparison_gallery {
|
| 132 |
+
background-color: white;
|
| 133 |
+
padding: 10px;
|
| 134 |
+
border-radius: 8px;
|
| 135 |
+
box-shadow: 0px 2px 8px rgba(0,0,0,0.1);
|
| 136 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
+
/* Download button spacing */
|
| 139 |
+
#download-btn {
|
| 140 |
+
margin-top: 15px !important;
|
| 141 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
"""
|
| 143 |
|
| 144 |
TITLE = "🌈 Color Restorization Model"
|
| 145 |
DESCRIPTION = "Bring your old black & white photos back to life—upload, adjust, and download in vivid color."
|
| 146 |
|
| 147 |
+
with gr.Blocks(title=TITLE, css=custom_css) as app:
|
| 148 |
+
# Header section
|
|
|
|
|
|
|
|
|
|
| 149 |
gr.HTML(
|
| 150 |
"""
|
| 151 |
<div id="header">
|
| 152 |
+
<h2>🌈 Color Restorization Model</h2>
|
| 153 |
+
<p>Bring your old black & white photos back to life—upload, adjust, and download in vivid color.</p>
|
| 154 |
</div>
|
| 155 |
"""
|
| 156 |
)
|
| 157 |
|
| 158 |
+
# Main control panel: white box with rounded corners
|
| 159 |
with gr.Column(elem_id="control-panel"):
|
| 160 |
with gr.Row():
|
| 161 |
# Left column: inputs and controls
|
| 162 |
+
with gr.Column():
|
| 163 |
input_image = gr.Image(
|
| 164 |
type="filepath",
|
| 165 |
label="Upload B&W Image",
|
| 166 |
+
tool="editor", # Enables zoom/pan
|
| 167 |
+
interactive=True
|
|
|
|
| 168 |
)
|
| 169 |
brightness_slider = gr.Slider(
|
| 170 |
minimum=0.5, maximum=2.0, value=1.0,
|
| 171 |
+
label="Brightness"
|
|
|
|
| 172 |
)
|
| 173 |
contrast_slider = gr.Slider(
|
| 174 |
minimum=0.5, maximum=2.0, value=1.0,
|
| 175 |
+
label="Contrast"
|
|
|
|
| 176 |
)
|
| 177 |
edge_enhance_checkbox = gr.Checkbox(
|
| 178 |
+
label="Apply Edge Enhancement"
|
|
|
|
| 179 |
)
|
| 180 |
output_format_dropdown = gr.Dropdown(
|
| 181 |
choices=["PNG", "JPEG", "TIFF"],
|
| 182 |
value="PNG",
|
| 183 |
+
label="Output Format"
|
|
|
|
| 184 |
)
|
| 185 |
submit_btn = gr.Button(
|
| 186 |
+
"Colorize",
|
| 187 |
elem_id="submit-btn"
|
| 188 |
)
|
| 189 |
|
| 190 |
# Right column: results gallery & download
|
| 191 |
+
with gr.Column():
|
| 192 |
comparison_gallery = gr.Gallery(
|
| 193 |
label="Original vs. Colorized",
|
| 194 |
columns=2,
|
| 195 |
elem_id="comparison_gallery",
|
| 196 |
height="auto"
|
| 197 |
+
).style(grid=[2], height="auto")
|
| 198 |
download_btn = gr.File(
|
| 199 |
label="Download Colorized Image",
|
| 200 |
elem_id="download-btn"
|
|
|
|
| 212 |
outputs=[comparison_gallery, download_btn]
|
| 213 |
)
|
| 214 |
|
| 215 |
+
# “Production” launch: bind to 0.0.0.0 and use PORT env var if provided
|
| 216 |
if __name__ == "__main__":
|
| 217 |
port = int(os.environ.get("PORT", 7860))
|
| 218 |
+
app.queue().launch(server_name="0.0.0.0", server_port=port)
|