Sutirtha commited on
Commit
c63316c
·
verified ·
1 Parent(s): 779eb3b

uppdated app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -22
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
- from diffusers import StableDiffusionInpaintPipeline
10
- from realesrgan import RealESRGAN
11
  from PIL import Image
 
12
 
 
 
 
13
 
14
- # 1. Initialize the inpainting pipeline on CPU (fp32)
15
  pipe = StableDiffusionInpaintPipeline.from_pretrained(
16
  "runwayml/stable-diffusion-inpainting",
17
  torch_dtype=torch.float32,
18
  )
19
  pipe.to("cpu")
20
 
21
- # 2. Initialize Real-ESRGAN (4×) on CPU, download weights if needed
22
- sr = RealESRGAN(device="cpu", scale=4)
23
- sr.load_weights("RealESRGAN_x4plus.pth", download=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  def fill_and_upscale(input_img: Image.Image,
26
- mask_img: Image.Image,
27
- prompt: str):
28
- # Ensure correct modes
29
  init = input_img.convert("RGB")
30
  mask = mask_img.convert("RGB")
 
 
 
31
 
32
- # 3. Generative fill (inpainting)
33
- filled = pipe(prompt=prompt, image=init, mask_image=mask).images[0]
 
34
 
35
- # 4. Upscale with Real-ESRGAN
36
- upscaled = sr.predict(filled)
 
 
37
 
38
  return filled, upscaled
39
 
40
- # 5. Build Gradio UI
41
  with gr.Blocks() as demo:
42
- gr.Markdown("## Stable-Diffusion Inpainting + Real-ESRGAN Upscale (CPU)")
43
  with gr.Row():
44
  with gr.Column():
45
- inp = gr.Image(type="pil", label="Input Image")
46
- msk = gr.Image(type="pil", label="Mask (white=fill)")
47
- prompt = gr.Textbox(label="Prompt", placeholder="e.g. A misty mountain lake")
48
- run_btn = gr.Button("Generate & Upscale")
 
 
 
49
  with gr.Column():
50
  out1 = gr.Image(type="pil", label="Inpainted")
51
  out2 = gr.Image(type="pil", label="Upscaled")
52
 
53
- run_btn.click(fill_and_upscale, inputs=[inp, msk, prompt], outputs=[out1, out2])
54
 
55
- # 6. Launch (HuggingFace Spaces picks up port automatically)
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 + 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()