Spaces:
Runtime error
Runtime error
updated app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,54 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
import cv2
|
| 4 |
import numpy as np
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
| 14 |
demo = gr.Interface(
|
| 15 |
-
fn=
|
| 16 |
-
inputs=[gr.Image(type=
|
| 17 |
-
outputs=gr.Image(type=
|
| 18 |
-
title="
|
| 19 |
-
description=
|
| 20 |
)
|
| 21 |
|
| 22 |
-
demo.launch(
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
import gradio as gr
|
| 4 |
import torch
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
+
import cv2
|
| 7 |
+
|
| 8 |
+
# Clone LaMa repo if not present
|
| 9 |
+
LAMA_PATH = "lama"
|
| 10 |
+
if not os.path.exists(LAMA_PATH):
|
| 11 |
+
subprocess.run(["git", "clone", "https://github.com/saic-mdal/lama.git", LAMA_PATH])
|
| 12 |
+
subprocess.run(["pip", "install", "-r", f"{LAMA_PATH}/requirements.txt"])
|
| 13 |
+
|
| 14 |
+
# Add LaMa to Python path
|
| 15 |
+
import sys
|
| 16 |
+
sys.path.append(LAMA_PATH)
|
| 17 |
+
|
| 18 |
+
# Import LaMa inpainting module
|
| 19 |
+
from lama.saicinpainting.evaluation.data import move_to_device
|
| 20 |
+
from lama.saicinpainting.training.trainers import load_checkpoint
|
| 21 |
+
|
| 22 |
+
# Load LaMa model
|
| 23 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 24 |
+
checkpoint_path = os.path.join(LAMA_PATH, "big-lama")
|
| 25 |
+
|
| 26 |
+
if not os.path.exists(checkpoint_path):
|
| 27 |
+
subprocess.run(["wget", "https://huggingface.co/spaces/pg56714/Inpaint-Anything/resolve/main/lama/big-lama.zip", "-O", "lama/big-lama.zip"])
|
| 28 |
+
subprocess.run(["unzip", "-o", "lama/big-lama.zip", "-d", "lama"])
|
| 29 |
+
|
| 30 |
+
model = load_checkpoint(checkpoint_path, map_location=device)
|
| 31 |
+
model = model.to(device).eval()
|
| 32 |
|
| 33 |
+
def inpaint(image, mask):
|
| 34 |
+
# Convert image and mask to proper tensor format
|
| 35 |
+
image = torch.tensor(image).permute(2, 0, 1).unsqueeze(0).float().to(device)
|
| 36 |
+
mask = torch.tensor(mask).unsqueeze(0).unsqueeze(0).float().to(device)
|
| 37 |
|
| 38 |
+
# Perform inpainting
|
| 39 |
+
with torch.no_grad():
|
| 40 |
+
inpainted = model(image, mask)
|
| 41 |
+
|
| 42 |
+
inpainted = inpainted.squeeze(0).permute(1, 2, 0).cpu().numpy()
|
| 43 |
+
return (inpainted * 255).astype(np.uint8)
|
| 44 |
|
| 45 |
+
# Create Gradio interface
|
| 46 |
demo = gr.Interface(
|
| 47 |
+
fn=inpaint,
|
| 48 |
+
inputs=[gr.Image(type="numpy"), gr.Image(type="numpy", label="Mask")],
|
| 49 |
+
outputs=gr.Image(type="numpy"),
|
| 50 |
+
title="LaMa Inpainting",
|
| 51 |
+
description="Upload an image and a mask (white for areas to be inpainted)."
|
| 52 |
)
|
| 53 |
|
| 54 |
+
demo.launch()
|