Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import tempfile
|
| 4 |
+
import numpy as np
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from modelscope.pipelines import pipeline
|
| 7 |
+
from modelscope.utils.constant import Tasks
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
# Initialize model with optimized settings
|
| 11 |
+
@gr.on(app_started=True)
|
| 12 |
+
def load_model():
|
| 13 |
+
global img_colorization
|
| 14 |
+
img_colorization = pipeline(
|
| 15 |
+
Tasks.image_colorization,
|
| 16 |
+
model='iic/cv_ddcolor_image-colorization',
|
| 17 |
+
model_revision='v1.0.0'
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
def inference(img):
|
| 21 |
+
if img is None:
|
| 22 |
+
raise gr.Error("Please upload an image first")
|
| 23 |
+
|
| 24 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
| 25 |
+
# Convert PIL image to numpy array if needed
|
| 26 |
+
if isinstance(img, np.ndarray):
|
| 27 |
+
image = img
|
| 28 |
+
else:
|
| 29 |
+
image = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
| 30 |
+
|
| 31 |
+
# Process image
|
| 32 |
+
output = img_colorization(image[..., ::-1])
|
| 33 |
+
result = output['output_img'].astype(np.uint8)
|
| 34 |
+
|
| 35 |
+
# Save result
|
| 36 |
+
out_path = os.path.join(temp_dir, 'colorized.png')
|
| 37 |
+
cv2.imwrite(out_path, result)
|
| 38 |
+
return Path(out_path), "✅ Colorization completed successfully!"
|
| 39 |
+
|
| 40 |
+
# Create modern UI with Blocks
|
| 41 |
+
with gr.Blocks(theme="soft", title="🎨 AI Color Restoration Studio") as demo:
|
| 42 |
+
gr.Markdown("""
|
| 43 |
+
# 🎨 AI Color Restoration Studio
|
| 44 |
+
Transform your black & white photos into vibrant colorized versions using state-of-the-art AI!
|
| 45 |
+
|
| 46 |
+
Upload an image and watch as our deep learning model automatically adds natural colors.
|
| 47 |
+
""")
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
with gr.Column(scale=1):
|
| 51 |
+
input_img = gr.Image(
|
| 52 |
+
label="Upload Monochrome Image",
|
| 53 |
+
type="pil",
|
| 54 |
+
height=400,
|
| 55 |
+
sources=["upload"],
|
| 56 |
+
interactive=True
|
| 57 |
+
)
|
| 58 |
+
submit_btn = gr.Button("✨ Colorize Image", variant="primary")
|
| 59 |
+
clear_btn = gr.ClearButton()
|
| 60 |
+
|
| 61 |
+
with gr.Column(scale=1):
|
| 62 |
+
output_img = gr.Image(
|
| 63 |
+
label="Colorized Result",
|
| 64 |
+
type="pil",
|
| 65 |
+
height=400,
|
| 66 |
+
interactive=False
|
| 67 |
+
)
|
| 68 |
+
download_btn = gr.File(label="Download Result")
|
| 69 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 70 |
+
|
| 71 |
+
# Examples section
|
| 72 |
+
gr.Examples(
|
| 73 |
+
examples=[
|
| 74 |
+
["examples/1.jpg"],
|
| 75 |
+
["examples/2.jpg"],
|
| 76 |
+
["examples/3.jpg"]
|
| 77 |
+
],
|
| 78 |
+
inputs=[input_img],
|
| 79 |
+
outputs=[output_img, status],
|
| 80 |
+
fn=inference,
|
| 81 |
+
cache_examples=True
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Event handlers
|
| 85 |
+
submit_btn.click(
|
| 86 |
+
fn=inference,
|
| 87 |
+
inputs=[input_img],
|
| 88 |
+
outputs=[output_img, status]
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
clear_btn.add([input_img, output_img, status])
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
demo.launch(debug=True)
|