JosephCatrambone commited on
Commit
5d4ea36
·
1 Parent(s): b6eec52

Use HF Model Storage to hold the model.

Browse files
Files changed (1) hide show
  1. app.py +25 -15
app.py CHANGED
@@ -1,26 +1,36 @@
1
  import gradio as gr
2
  import torch
 
3
  from torchvision.transforms.functional import pil_to_tensor, to_pil_image
4
 
5
- model = torch.jit.load("models/lama.pt")
 
 
 
 
 
 
 
 
 
6
 
7
  def predict(input_img, input_mask):
8
- # numpy gives the image as (w,h,c)
9
- # Image shape should be (1, 3, 512, 512) and be in the range 0-1.
10
- # Mask shape should be (1, 1, 512, 512) AND have values 0.0 or 1.0, not in-between.
11
- #out = model(torch.tensor(input_img[None, (2,0,1), :, :])/255.0, torch.tensor(1 * (input_mask[:,:,0] > 0)).unsqueeze(0))
12
- out = model((pil_to_tensor(input_img.convert('RGB')) / 255.0).unsqueeze(0), 1 * (pil_to_tensor(input_mask.convert('L')) > 0).unsqueeze(0))[0]
13
- return to_pil_image(out)
14
 
15
  gradio_app = gr.Interface(
16
- predict,
17
- inputs=[
18
- gr.Image(label="Select Base Image", sources=['upload',], type="pil"),
19
- gr.Image(label="Select Image Mask (White will be inpainted)", sources=['upload',], type="pil"),
20
- ],
21
- outputs=[gr.Image(label="Inpainted Image"),],
22
- title="LAMA Inpainting",
23
  )
24
 
25
  if __name__ == "__main__":
26
- gradio_app.launch()
 
1
  import gradio as gr
2
  import torch
3
+ import os
4
  from torchvision.transforms.functional import pil_to_tensor, to_pil_image
5
 
6
+ MODEL_DIR = "models"
7
+ MODEL_FILENAME = "lama.pt"
8
+ LOCAL_MODEL = os.path.join(MODEL_DIR, MODEL_FILENAME)
9
+
10
+ if not os.path.exists(LOCAL_MODEL):
11
+ from huggingface_hub import hf_hub_download
12
+ REPO_ID = "JosephCatrambone/big-lama-torchscript"
13
+ LOCAL_MODEL = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILENAME, local_dir=MODEL_DIR, local_dir_use_symlinks=False)
14
+
15
+ model = torch.jit.load(LOCAL_MODEL)
16
 
17
  def predict(input_img, input_mask):
18
+ # numpy gives the image as (w,h,c)
19
+ # Image shape should be (1, 3, 512, 512) and be in the range 0-1.
20
+ # Mask shape should be (1, 1, 512, 512) AND have values 0.0 or 1.0, not in-between.
21
+ #out = model(torch.tensor(input_img[None, (2,0,1), :, :])/255.0, torch.tensor(1 * (input_mask[:,:,0] > 0)).unsqueeze(0))
22
+ out = model((pil_to_tensor(input_img.convert('RGB')) / 255.0).unsqueeze(0), 1 * (pil_to_tensor(input_mask.convert('L')) > 0).unsqueeze(0))[0]
23
+ return to_pil_image(out)
24
 
25
  gradio_app = gr.Interface(
26
+ predict,
27
+ inputs=[
28
+ gr.Image(label="Select Base Image", sources=['upload',], type="pil"),
29
+ gr.Image(label="Select Image Mask (White will be inpainted)", sources=['upload',], type="pil"),
30
+ ],
31
+ outputs=[gr.Image(label="Inpainted Image"),],
32
+ title="LAMA Inpainting",
33
  )
34
 
35
  if __name__ == "__main__":
36
+ gradio_app.launch()