Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,72 @@
|
|
| 1 |
import os
|
| 2 |
-
os.system("wget https://huggingface.co/akhaliq/lama/resolve/main/best.ckpt")
|
| 3 |
os.system("pip install imageio")
|
| 4 |
-
|
| 5 |
-
|
| 6 |
import gradio as gr
|
| 7 |
-
import
|
| 8 |
-
from PIL import Image
|
| 9 |
import numpy as np
|
| 10 |
-
import
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
def
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
title = "LaMa Image Inpainting"
|
| 38 |
description = "Gradio demo for LaMa: Resolution-robust Large Mask Inpainting with Fourier Convolutions. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below. Masks are generated by U^2net"
|
| 39 |
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2109.07161' target='_blank'>Resolution-robust Large Mask Inpainting with Fourier Convolutions</a> | <a href='https://github.com/saic-mdal/lama' target='_blank'>Github Repo</a></p>"
|
| 40 |
-
gr.Interface(infer, inputs, outputs, title=title, description=description, article=article).launch()
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
os.system("pip install imageio")
|
| 3 |
+
# app.py for Hugging Face Spaces
|
| 4 |
+
|
| 5 |
import gradio as gr
|
| 6 |
+
from simple_lama_inpainting import SimpleLama
|
| 7 |
+
from PIL import Image
|
| 8 |
import numpy as np
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
# Initialize the SimpleLama model
|
| 12 |
+
simple_lama = SimpleLama()
|
| 13 |
+
|
| 14 |
+
def inpaint_image(input_data):
|
| 15 |
+
if input_data is None:
|
| 16 |
+
raise gr.Error("Please upload an image and draw a mask.")
|
| 17 |
+
|
| 18 |
+
# input_data is a dict with 'image' and 'mask' as paths or arrays
|
| 19 |
+
# In Gradio 4+, it's dict with 'image' and 'mask' as PIL Images or paths
|
| 20 |
+
image_path = input_data['image']
|
| 21 |
+
mask_path = input_data['mask']
|
| 22 |
+
|
| 23 |
+
# Open image and mask
|
| 24 |
+
image = Image.open(image_path).convert("RGB")
|
| 25 |
+
mask = Image.open(mask_path).convert("L")
|
| 26 |
+
|
| 27 |
+
# In Gradio sketch, mask is white where drawn (to inpaint), black elsewhere
|
| 28 |
+
# But ensure it's binary if needed, though SimpleLama likely handles grayscale
|
| 29 |
+
|
| 30 |
+
# Perform inpainting
|
| 31 |
+
result = simple_lama(image, mask)
|
| 32 |
+
|
| 33 |
+
return result
|
| 34 |
+
|
| 35 |
+
with gr.Blocks(title="Image Inpainting with SimpleLama") as demo:
|
| 36 |
+
gr.Markdown("# Image Inpainting App")
|
| 37 |
+
gr.Markdown("Upload an image, draw a mask on the areas to inpaint, and click Submit.")
|
| 38 |
+
|
| 39 |
+
with gr.Row():
|
| 40 |
+
input_image = gr.Image(
|
| 41 |
+
label="Upload and Mask Image",
|
| 42 |
+
tool="sketch",
|
| 43 |
+
type="filepath", # Returns paths
|
| 44 |
+
brush_color="#FFFFFF", # White brush for mask
|
| 45 |
+
interactive=True
|
| 46 |
+
)
|
| 47 |
+
output_image = gr.Image(label="Inpainted Image")
|
| 48 |
+
|
| 49 |
+
submit_btn = gr.Button("Submit")
|
| 50 |
+
submit_btn.click(inpaint_image, inputs=[input_image], outputs=[output_image])
|
| 51 |
+
|
| 52 |
+
gr.Examples(
|
| 53 |
+
examples=[
|
| 54 |
+
# ["examples/example1.jpg"],
|
| 55 |
+
["person512.png"],
|
| 56 |
+
],
|
| 57 |
+
inputs=[input_image],
|
| 58 |
+
label="Try these examples (upload and draw mask)"
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# For HF Spaces, requirements.txt would include:
|
| 62 |
+
# simple-lama-inpainting
|
| 63 |
+
# gradio
|
| 64 |
+
# pillow
|
| 65 |
+
# torch (if not included in simple-lama)
|
| 66 |
+
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
demo.launch()
|
| 69 |
title = "LaMa Image Inpainting"
|
| 70 |
description = "Gradio demo for LaMa: Resolution-robust Large Mask Inpainting with Fourier Convolutions. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below. Masks are generated by U^2net"
|
| 71 |
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2109.07161' target='_blank'>Resolution-robust Large Mask Inpainting with Fourier Convolutions</a> | <a href='https://github.com/saic-mdal/lama' target='_blank'>Github Repo</a></p>"
|
| 72 |
+
# gr.Interface(infer, inputs, outputs, title=title, description=description, article=article).launch()
|