Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,72 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import cv2
|
| 3 |
-
import tempfile
|
| 4 |
-
from modelscope.outputs import OutputKeys
|
| 5 |
-
from modelscope.pipelines import pipeline
|
| 6 |
-
from modelscope.utils.constant import Tasks
|
| 7 |
-
import PIL
|
| 8 |
-
from pathlib import Path
|
| 9 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 10 |
import numpy as np
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
gr.Interface(
|
| 30 |
-
inference,
|
| 31 |
-
[gr.inputs.Image(type="filepath", label="Input")],
|
| 32 |
-
gr.outputs.Image(type="pil", label="Output"),
|
| 33 |
-
title=title
|
| 34 |
-
).launch(enable_queue=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import DDColorPipeline
|
| 4 |
+
from PIL import Image
|
| 5 |
import numpy as np
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
# Model loading with optimized settings
|
| 9 |
+
cache_dir = "./model_cache"
|
| 10 |
+
os.makedirs(cache_dir, exist_ok=True)
|
| 11 |
|
| 12 |
+
# Load model once at startup
|
| 13 |
+
pipe = DDColorPipeline.from_pretrained(
|
| 14 |
+
"camenduru/cv_ddcolor_image-colorization",
|
| 15 |
+
torch_dtype=torch.float16,
|
| 16 |
+
cache_dir=cache_dir
|
| 17 |
+
).to("cuda")
|
| 18 |
|
| 19 |
+
def colorize_image(input_image):
|
| 20 |
+
"""Process B&W image and return colorized version"""
|
| 21 |
+
# Ensure image is in grayscale mode
|
| 22 |
+
if input_image.mode != 'L':
|
| 23 |
+
input_image = input_image.convert('L')
|
| 24 |
+
|
| 25 |
+
# Resize to model's expected input size (based on DDColor paper)
|
| 26 |
+
target_size = (256, 256)
|
| 27 |
+
resized_image = input_image.resize(target_size)
|
| 28 |
+
|
| 29 |
+
# Convert to numpy array for pipeline input
|
| 30 |
+
grayscale_array = np.array(resized_image)
|
| 31 |
+
|
| 32 |
+
# Generate colorized image
|
| 33 |
+
with torch.inference_mode():
|
| 34 |
+
result = pipe(grayscale_array).images[0]
|
| 35 |
+
|
| 36 |
+
return result
|
| 37 |
|
| 38 |
+
# Custom CSS for vintage styling
|
| 39 |
+
custom_css = """
|
| 40 |
+
#output-image {max-width: 100%; border: 2px solid #ccc; border-radius: 8px;}
|
| 41 |
+
"""
|
| 42 |
|
| 43 |
+
# UI Layout
|
| 44 |
+
with gr.Blocks(theme="soft", css=custom_css) as demo:
|
| 45 |
+
gr.Markdown("""
|
| 46 |
+
# 📸 Vintage Photo Colorizer
|
| 47 |
+
Transform old black & white photos into vibrant color images using DDColor AI.
|
| 48 |
+
|
| 49 |
+
## How to Use
|
| 50 |
+
1. Upload a grayscale image (or color image will be converted to B&W)
|
| 51 |
+
2. Click "Colorize" to process
|
| 52 |
+
3. Download your new colorized photo!
|
| 53 |
+
""")
|
| 54 |
+
|
| 55 |
+
with gr.Row():
|
| 56 |
+
input_img = gr.Image(label="Upload Black & White Image", type="pil")
|
| 57 |
+
|
| 58 |
+
colorize_btn = gr.Button("🎨 Colorize Photo", variant="primary")
|
| 59 |
+
output_img = gr.Image(label="Colorized Image", elem_id="output-image")
|
| 60 |
+
|
| 61 |
+
colorize_btn.click(
|
| 62 |
+
fn=colorize_image,
|
| 63 |
+
inputs=[input_img],
|
| 64 |
+
outputs=[output_img]
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
gr.Markdown("""
|
| 68 |
+
### Powered by [DDColor](https://huggingface.co/papers/2212.11613 )
|
| 69 |
+
*Dual Decoders for Photo-Realistic Image Colorization*
|
| 70 |
+
""")
|
| 71 |
|
| 72 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|