uppdated app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,84 @@
|
|
| 1 |
# app.py
|
| 2 |
|
|
|
|
| 3 |
import sys
|
| 4 |
import torchvision.transforms.functional as F
|
| 5 |
sys.modules['torchvision.transforms.functional_tensor'] = F
|
| 6 |
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
import torch
|
| 9 |
-
|
| 10 |
-
|
| 11 |
from PIL import Image
|
|
|
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
# 1. Initialize
|
| 15 |
pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
| 16 |
"runwayml/stable-diffusion-inpainting",
|
| 17 |
torch_dtype=torch.float32,
|
| 18 |
)
|
| 19 |
pipe.to("cpu")
|
| 20 |
|
| 21 |
-
# 2.
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def fill_and_upscale(input_img: Image.Image,
|
| 26 |
-
mask_img:
|
| 27 |
-
prompt:
|
| 28 |
-
#
|
| 29 |
init = input_img.convert("RGB")
|
| 30 |
mask = mask_img.convert("RGB")
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
|
| 38 |
return filled, upscaled
|
| 39 |
|
| 40 |
-
#
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
-
gr.Markdown("##
|
| 43 |
with gr.Row():
|
| 44 |
with gr.Column():
|
| 45 |
-
inp
|
| 46 |
-
msk
|
| 47 |
-
prompt
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
with gr.Column():
|
| 50 |
out1 = gr.Image(type="pil", label="Inpainted")
|
| 51 |
out2 = gr.Image(type="pil", label="Upscaled")
|
| 52 |
|
| 53 |
-
|
| 54 |
|
| 55 |
-
|
| 56 |
-
demo.launch()
|
|
|
|
| 1 |
# app.py
|
| 2 |
|
| 3 |
+
# ── Monkey‐patch missing torchvision module ──
|
| 4 |
import sys
|
| 5 |
import torchvision.transforms.functional as F
|
| 6 |
sys.modules['torchvision.transforms.functional_tensor'] = F
|
| 7 |
|
| 8 |
+
import os
|
| 9 |
import gradio as gr
|
| 10 |
import torch
|
| 11 |
+
import numpy as np
|
| 12 |
+
import cv2
|
| 13 |
from PIL import Image
|
| 14 |
+
from diffusers import StableDiffusionInpaintPipeline
|
| 15 |
|
| 16 |
+
# Import the RealESRGANer helper and architecture
|
| 17 |
+
from basicsr.archs.rrdbnet_arch import RRDBNet # RRDB backbone :contentReference[oaicite:0]{index=0}
|
| 18 |
+
from realesrgan.utils import RealESRGANer # RealESRGANer class :contentReference[oaicite:1]{index=1}
|
| 19 |
|
| 20 |
+
# 1. Initialize Stable Diffusion InpaintPipeline on CPU
|
| 21 |
pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
| 22 |
"runwayml/stable-diffusion-inpainting",
|
| 23 |
torch_dtype=torch.float32,
|
| 24 |
)
|
| 25 |
pipe.to("cpu")
|
| 26 |
|
| 27 |
+
# 2. Build the RRDBNet model and RealESRGANer (4×) on CPU
|
| 28 |
+
device = torch.device("cpu")
|
| 29 |
+
rrdb = RRDBNet(
|
| 30 |
+
num_in_ch=3, num_out_ch=3,
|
| 31 |
+
num_feat=64, num_block=23,
|
| 32 |
+
num_grow_ch=32, scale=4
|
| 33 |
+
)
|
| 34 |
+
# Pass a GitHub URL so it downloads under-the-hood
|
| 35 |
+
esrgan = RealESRGANer(
|
| 36 |
+
scale=4,
|
| 37 |
+
model_path="https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth",
|
| 38 |
+
model=rrdb,
|
| 39 |
+
tile=0, tile_pad=10, pre_pad=10,
|
| 40 |
+
half=False,
|
| 41 |
+
device=device,
|
| 42 |
+
)
|
| 43 |
|
| 44 |
def fill_and_upscale(input_img: Image.Image,
|
| 45 |
+
mask_img: Image.Image,
|
| 46 |
+
prompt: str):
|
| 47 |
+
# Inpaint
|
| 48 |
init = input_img.convert("RGB")
|
| 49 |
mask = mask_img.convert("RGB")
|
| 50 |
+
filled: Image.Image = pipe(
|
| 51 |
+
prompt=prompt, image=init, mask_image=mask
|
| 52 |
+
).images[0]
|
| 53 |
|
| 54 |
+
# Prepare for Real-ESRGANer (expects BGR numpy)
|
| 55 |
+
arr = np.array(filled)
|
| 56 |
+
bgr = cv2.cvtColor(arr, cv2.COLOR_RGB2BGR)
|
| 57 |
|
| 58 |
+
# Upscale
|
| 59 |
+
out_bgr, _ = esrgan.enhance(bgr, outscale=None)
|
| 60 |
+
out_rgb = cv2.cvtColor(out_bgr, cv2.COLOR_BGR2RGB)
|
| 61 |
+
upscaled = Image.fromarray(out_rgb)
|
| 62 |
|
| 63 |
return filled, upscaled
|
| 64 |
|
| 65 |
+
# 3. Gradio UI
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
+
gr.Markdown("## Inpaint + 4× Upscale (CPU Only)")
|
| 68 |
with gr.Row():
|
| 69 |
with gr.Column():
|
| 70 |
+
inp = gr.Image(type="pil", label="Input Image")
|
| 71 |
+
msk = gr.Image(type="pil", label="Mask (white=fill)")
|
| 72 |
+
prompt = gr.Textbox(
|
| 73 |
+
label="Prompt",
|
| 74 |
+
placeholder="e.g. A serene waterfall at dawn"
|
| 75 |
+
)
|
| 76 |
+
btn = gr.Button("Run")
|
| 77 |
with gr.Column():
|
| 78 |
out1 = gr.Image(type="pil", label="Inpainted")
|
| 79 |
out2 = gr.Image(type="pil", label="Upscaled")
|
| 80 |
|
| 81 |
+
btn.click(fill_and_upscale, [inp, msk, prompt], [out1, out2])
|
| 82 |
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
demo.launch()
|