Upload app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ import sys
|
|
| 9 |
from PIL import Image, ImageFilter, ImageDraw
|
| 10 |
from diffusers import StableDiffusionInpaintPipeline
|
| 11 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
|
|
|
| 12 |
|
| 13 |
import torchvision.transforms.functional as F
|
| 14 |
sys.modules["torchvision.transforms.functional_tensor"] = F
|
|
@@ -57,6 +58,18 @@ inpaint = StableDiffusionInpaintPipeline.from_pretrained(
|
|
| 57 |
)
|
| 58 |
print("SD Inpainting ready.")
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# BLIP
|
| 62 |
print("Loading BLIP...")
|
|
@@ -207,6 +220,53 @@ def enhance_image(image, scale_factor):
|
|
| 207 |
|
| 208 |
return output_image, f"Original: {original_size} → Enhanced: {new_size}"
|
| 209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
@spaces.GPU
|
| 211 |
def outpaint_image(image, direction, extend_percent, custom_prompt, progress=gr.Progress()):
|
| 212 |
if image is None:
|
|
@@ -312,6 +372,39 @@ with gr.Blocks(title="CanvasAI — Enhance & Outpaint") as demo:
|
|
| 312 |
outputs=[enh_output, enh_info]
|
| 313 |
)
|
| 314 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
with gr.Tab(" Outpaint"):
|
| 316 |
gr.Markdown(
|
| 317 |
"Upload an image and extend it in any direction. "
|
|
|
|
| 9 |
from PIL import Image, ImageFilter, ImageDraw
|
| 10 |
from diffusers import StableDiffusionInpaintPipeline
|
| 11 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 12 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
| 13 |
|
| 14 |
import torchvision.transforms.functional as F
|
| 15 |
sys.modules["torchvision.transforms.functional_tensor"] = F
|
|
|
|
| 58 |
)
|
| 59 |
print("SD Inpainting ready.")
|
| 60 |
|
| 61 |
+
# COLORING- IMG2IMG Pipeline
|
| 62 |
+
colour= StableDiffusionImg2ImgPipeline(
|
| 63 |
+
vae= inpaint.vae,
|
| 64 |
+
text_encoder= inpaint.text_encoder,
|
| 65 |
+
tokenizer= inpaint.tokenizer,
|
| 66 |
+
unet= inpaint.unet,
|
| 67 |
+
scheduler= inpaint.scheduler,
|
| 68 |
+
safety_checker= None,
|
| 69 |
+
feature_extractor= None,
|
| 70 |
+
requires_safety_checker= False,
|
| 71 |
+
).to("cuda")
|
| 72 |
+
|
| 73 |
|
| 74 |
# BLIP
|
| 75 |
print("Loading BLIP...")
|
|
|
|
| 220 |
|
| 221 |
return output_image, f"Original: {original_size} → Enhanced: {new_size}"
|
| 222 |
|
| 223 |
+
@spaces.GPU
|
| 224 |
+
def colour_image(image, prompt, strength):
|
| 225 |
+
if Image is None:
|
| 226 |
+
raise gr.Error("Please Upload the Image")
|
| 227 |
+
# Convert to RGB
|
| 228 |
+
image= image.convert("RGB")
|
| 229 |
+
# Resize to 512 for SD
|
| 230 |
+
target= 512
|
| 231 |
+
ratio= min(target/ image.width, target/image.height)
|
| 232 |
+
image= image.resize(
|
| 233 |
+
(int(image.width * ratio), int(image.height * ratio)),
|
| 234 |
+
Image.LANCZOS
|
| 235 |
+
)
|
| 236 |
+
|
| 237 |
+
#Build Prompt
|
| 238 |
+
if not prompt.strip():
|
| 239 |
+
# auto generate with BLIP
|
| 240 |
+
caption= get_caption(image)
|
| 241 |
+
full_prompt= (
|
| 242 |
+
f"colorized photograph, {caption}, "
|
| 243 |
+
f"natural realistic colors, vivid, sharp, "
|
| 244 |
+
f"professional color grading, high quality"
|
| 245 |
+
)
|
| 246 |
+
else:
|
| 247 |
+
full_prompt= (
|
| 248 |
+
f"colorized photograph, {prompt}, "
|
| 249 |
+
f"natural realistic colors, high quality"
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
+
negative_prompt= (
|
| 253 |
+
"black and white, greyscale, monochrome, "
|
| 254 |
+
"blurry, bad quality, oversaturated, unnatural colors"
|
| 255 |
+
)
|
| 256 |
+
|
| 257 |
+
output= colour(
|
| 258 |
+
prompt= full_prompt,
|
| 259 |
+
image= image,
|
| 260 |
+
strength= strength,
|
| 261 |
+
negative_prompt= negative_prompt,
|
| 262 |
+
num_inference_steps= 30,
|
| 263 |
+
guidance_scale= 7.5
|
| 264 |
+
)
|
| 265 |
+
|
| 266 |
+
result= output.images[0]
|
| 267 |
+
return result, f"Prompt used: \n {full_prompt}"
|
| 268 |
+
|
| 269 |
+
|
| 270 |
@spaces.GPU
|
| 271 |
def outpaint_image(image, direction, extend_percent, custom_prompt, progress=gr.Progress()):
|
| 272 |
if image is None:
|
|
|
|
| 372 |
outputs=[enh_output, enh_info]
|
| 373 |
)
|
| 374 |
|
| 375 |
+
with gr.Tab("Colourize"):
|
| 376 |
+
gr.Markdown("Upload a black and white image. "
|
| 377 |
+
"AI adds natural, realistic colours while preserving the original structure.")
|
| 378 |
+
with gr.Row:
|
| 379 |
+
with gr.Column:
|
| 380 |
+
col_input= gr.Image(
|
| 381 |
+
label="Upload Image",
|
| 382 |
+
type="PIL",
|
| 383 |
+
)
|
| 384 |
+
col_prompt= gr.Textbox(label= "Custom Prompt(optional)",
|
| 385 |
+
placeholder="Leave empty for auto-detection, or type: portrait of a young woman in 1960s clothing",
|
| 386 |
+
lines=2)
|
| 387 |
+
col_strength= gr.Slider(
|
| 388 |
+
minimum= 0.3,
|
| 389 |
+
maximum= 0.7,
|
| 390 |
+
value= 0.5,
|
| 391 |
+
step= 0.05,
|
| 392 |
+
label= "Colorization Strength",
|
| 393 |
+
info= "Low = subtle tint, stays close to original. High = vivid colours."
|
| 394 |
+
)
|
| 395 |
+
|
| 396 |
+
col_btn= gr.Button(
|
| 397 |
+
"Colorize", variant="primary"
|
| 398 |
+
)
|
| 399 |
+
with gr.Column():
|
| 400 |
+
col_output= gr.Image(label="Colorized Result", type="pil", interactive=False)
|
| 401 |
+
col_caption = gr.Textbox(label="Prompt Used", interactive=False, lines=3)
|
| 402 |
+
col_btn.click(
|
| 403 |
+
fn= colour_image,
|
| 404 |
+
inputs= [col_input, col_prompt, col_strength],
|
| 405 |
+
outputs= [col_output, col_caption]
|
| 406 |
+
)
|
| 407 |
+
|
| 408 |
with gr.Tab(" Outpaint"):
|
| 409 |
gr.Markdown(
|
| 410 |
"Upload an image and extend it in any direction. "
|