ariG23498 HF staff commited on
Commit
f6ed1c1
β€’
1 Parent(s): 8ef4174
Files changed (3) hide show
  1. Dockerfile +34 -0
  2. app.py +63 -0
  3. requirements.txt +1 -0
Dockerfile ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ RUN apt-get update && apt-get install -y libgl1-mesa-glx
8
+ RUN pip install --no-cache-dir torch
9
+ RUN pip install --no-cache-dir torchvision
10
+ RUN pip install --no-cache-dir git+https://github.com/luca-medeiros/lang-segment-anything.git
11
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
12
+
13
+ # Set up a new user named "user" with user ID 1000
14
+ RUN useradd -m -u 1000 user
15
+ # Switch to the "user" user
16
+ USER user
17
+ # Set home to the user's home directory
18
+ ENV HOME=/home/user \
19
+ PATH=/home/user/.local/bin:$PATH \
20
+ PYTHONPATH=$HOME/app \
21
+ PYTHONUNBUFFERED=1 \
22
+ GRADIO_ALLOW_FLAGGING=never \
23
+ GRADIO_NUM_PORTS=1 \
24
+ GRADIO_SERVER_NAME=0.0.0.0 \
25
+ GRADIO_THEME=huggingface \
26
+ SYSTEM=spaces
27
+
28
+ # Set the working directory to the user's home directory
29
+ WORKDIR $HOME/app
30
+
31
+ # Copy the current directory contents into the container at $HOME/app setting the owner to the user
32
+ COPY --chown=user . $HOME/app
33
+
34
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # IMPORTS
2
+ import torch
3
+ import numpy as np
4
+ from PIL import Image
5
+ from lang_sam import LangSAM
6
+ import gradio as gr
7
+
8
+
9
+ def run_lang_sam(input_image, text_prompt, model):
10
+ image = input_image.convert("RGB").resize((512, 512))
11
+ masks, _, _, _ = model.predict(
12
+ image,
13
+ text_prompt
14
+ )
15
+ masks_int = masks.to(torch.uint8)
16
+ masks_max, _ = masks_int.max(dim=0, keepdim=True)
17
+ unified_mask = masks_max.squeeze(0).to(torch.bool)
18
+ return Image.fromarray(
19
+ (unified_mask[..., None].numpy() * np.array(image)).astype(np.uint8)
20
+ )
21
+
22
+
23
+ def setup_gradio_interface(model):
24
+ block = gr.Blocks()
25
+
26
+ with block:
27
+ gr.Markdown("<h1><center>Lang SAM<h1><center>")
28
+
29
+ with gr.Row():
30
+ with gr.Column():
31
+ input_image = gr.Image(type="pil", label="Input Image")
32
+ text_prompt = gr.Textbox(label="Enter what you want to segment")
33
+ run_button = gr.Button(value="Run")
34
+
35
+ with gr.Column():
36
+ output_mask = gr.Image(type="numpy", label="Segmentation Mask")
37
+
38
+ run_button.click(
39
+ fn=lambda image, prompt: run_lang_sam(
40
+ image, prompt, model,
41
+ ),
42
+ inputs=[input_image, text_prompt],
43
+ outputs=[output_mask],
44
+ )
45
+
46
+ gr.Examples(
47
+ examples=[["bw-image.jpeg", "road"]],
48
+ inputs=[input_image, text_prompt],
49
+ outputs=[output_mask],
50
+ fn=lambda image, prompt: run_lang_sam(
51
+ image, prompt, model,
52
+ ),
53
+ cache_examples=True,
54
+ label="Try this example input!",
55
+ )
56
+
57
+ return block
58
+
59
+
60
+ if __name__ == "__main__":
61
+ model = LangSAM()
62
+ gradio_interface = setup_gradio_interface(model)
63
+ gradio_interface.launch(share=False, show_api=False, show_error=True)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ gradio==4.5.0