druvx13 commited on
Commit
6c2a659
·
verified ·
1 Parent(s): d730b31

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -29
app.py CHANGED
@@ -1,54 +1,49 @@
1
  import gradio as gr
2
  import torch
3
- from diffusers import AutoPipelineForImage2Image
4
- from transformers import pipeline
5
  from PIL import Image
6
  import numpy as np
7
  import os
8
 
9
- # Model loading with dynamic pipeline selection
10
  cache_dir = "./model_cache"
11
  os.makedirs(cache_dir, exist_ok=True)
12
 
13
- # Load model using AutoPipeline
14
- pipe = AutoPipelineForImage2Image.from_pretrained(
 
 
 
 
15
  "camenduru/cv_ddcolor_image-colorization",
16
  torch_dtype=torch.float16,
17
- cache_dir=cache_dir,
18
- variant="fp16"
19
  ).to("cuda")
20
 
21
  def colorize_image(input_image):
22
  """Process B&W image and return colorized version"""
23
- # Ensure image is in grayscale mode
24
  if input_image.mode != 'L':
25
  input_image = input_image.convert('L')
26
 
27
- # Resize to model's expected input size
28
- target_size = (512, 512) # Increased resolution for better quality
29
- resized_image = input_image.resize(target_size)
30
-
31
- # Convert to RGB as required by model
32
- grayscale_image = resized_image.convert("RGB")
33
 
34
- # Generate colorized image
35
  with torch.inference_mode():
36
- result = pipe(
37
- prompt="colorized photo",
38
- image=grayscale_image,
39
- num_inference_steps=20,
40
- strength=0.8
41
- ).images[0]
 
 
42
 
43
- return result
44
-
45
- # Custom CSS for vintage styling
46
- custom_css = """
47
- #output-image {max-width: 100%; border: 2px solid #ccc; border-radius: 8px;}
48
- """
49
 
50
  # UI Layout
51
- with gr.Blocks(theme="soft", css=custom_css) as demo:
52
  gr.Markdown("""
53
  # 📸 Vintage Photo Colorizer
54
  Transform old black & white photos into vibrant color images using DDColor AI.
@@ -63,7 +58,7 @@ with gr.Blocks(theme="soft", css=custom_css) as demo:
63
  input_img = gr.Image(label="Upload Black & White Image", type="pil")
64
 
65
  colorize_btn = gr.Button("🎨 Colorize Photo", variant="primary")
66
- output_img = gr.Image(label="Colorized Image", elem_id="output-image")
67
 
68
  colorize_btn.click(
69
  fn=colorize_image,
 
1
  import gradio as gr
2
  import torch
3
+ from transformers import AutoModelForImageToImage, AutoImageProcessor
 
4
  from PIL import Image
5
  import numpy as np
6
  import os
7
 
8
+ # Model loading with manual configuration
9
  cache_dir = "./model_cache"
10
  os.makedirs(cache_dir, exist_ok=True)
11
 
12
+ # Load model components separately
13
+ image_processor = AutoImageProcessor.from_pretrained(
14
+ "camenduru/cv_ddcolor_image-colorization",
15
+ cache_dir=cache_dir
16
+ )
17
+ model = AutoModelForImageToImage.from_pretrained(
18
  "camenduru/cv_ddcolor_image-colorization",
19
  torch_dtype=torch.float16,
20
+ cache_dir=cache_dir
 
21
  ).to("cuda")
22
 
23
  def colorize_image(input_image):
24
  """Process B&W image and return colorized version"""
25
+ # Ensure grayscale input
26
  if input_image.mode != 'L':
27
  input_image = input_image.convert('L')
28
 
29
+ # Convert to RGB for model input
30
+ rgb_image = input_image.convert("RGB")
 
 
 
 
31
 
32
+ # Process through model
33
  with torch.inference_mode():
34
+ # Preprocess
35
+ pixel_values = image_processor(rgb_image, return_tensors="pt").pixel_values.to("cuda")
36
+
37
+ # Forward pass
38
+ outputs = model(pixel_values=pixel_values)
39
+
40
+ # Postprocess
41
+ output_image = image_processor.post_process(outputs, output_type="pil")[0]
42
 
43
+ return output_image
 
 
 
 
 
44
 
45
  # UI Layout
46
+ with gr.Blocks(theme="soft") as demo:
47
  gr.Markdown("""
48
  # 📸 Vintage Photo Colorizer
49
  Transform old black & white photos into vibrant color images using DDColor AI.
 
58
  input_img = gr.Image(label="Upload Black & White Image", type="pil")
59
 
60
  colorize_btn = gr.Button("🎨 Colorize Photo", variant="primary")
61
+ output_img = gr.Image(label="Colorized Image")
62
 
63
  colorize_btn.click(
64
  fn=colorize_image,