Spaces:
Sleeping
Sleeping
Commit
·
287d863
1
Parent(s):
4064362
Add development environment setup with mock model functions and Gradio interface
Browse files- .gitignore +2 -0
- .python-version +1 -0
- Makefile +2 -0
- README.md +6 -0
- inference_utils/init_predict.py +76 -0
- inference_utils/init_predict_dev.py +6 -0
- main.py +11 -92
- pyproject.toml +9 -0
- uv.lock +0 -0
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.venv
|
| 2 |
+
__pycache__
|
.python-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
3.9
|
Makefile
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
run:
|
| 2 |
+
DEV_MODE=1 uv run gradio main.py
|
README.md
CHANGED
|
@@ -37,3 +37,9 @@ All rights to the model and its underlying research are held by the original aut
|
|
| 37 |
|
| 38 |
- **Code in this Space**: Licensed under the [Apache License 2.0](https://spdx.org/licenses/Apache-2.0.html), as per the original BiomedParse GitHub repository.
|
| 39 |
- **Model**: The BiomedParse model is licensed under the [Creative Commons Attribution Non Commercial Share Alike 4.0](https://spdx.org/licenses/CC-BY-NC-SA-4.0.html). Ensure that your use complies with the terms of this license.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
- **Code in this Space**: Licensed under the [Apache License 2.0](https://spdx.org/licenses/Apache-2.0.html), as per the original BiomedParse GitHub repository.
|
| 39 |
- **Model**: The BiomedParse model is licensed under the [Creative Commons Attribution Non Commercial Share Alike 4.0](https://spdx.org/licenses/CC-BY-NC-SA-4.0.html). Ensure that your use complies with the terms of this license.
|
| 40 |
+
|
| 41 |
+
## Development of the Gradio interface
|
| 42 |
+
|
| 43 |
+
To develop the Gradio interface locally against a mock ML model, execute
|
| 44 |
+
|
| 45 |
+
make run
|
inference_utils/init_predict.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import numpy as np
|
| 5 |
+
from inference_utils.inference import interactive_infer_image
|
| 6 |
+
from main import model
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
import gradio as gr
|
| 10 |
+
|
| 11 |
+
from modeling import build_model
|
| 12 |
+
from modeling.BaseModel import BaseModel
|
| 13 |
+
from utilities.arguments import load_opt_from_config_files
|
| 14 |
+
from utilities.constants import BIOMED_CLASSES
|
| 15 |
+
from utilities.distributed import init_distributed
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def generate_colors(n):
|
| 19 |
+
cmap = plt.get_cmap("tab10")
|
| 20 |
+
colors = [tuple(int(255 * val) for val in cmap(i)[:3]) for i in range(n)]
|
| 21 |
+
return colors
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def overlay_masks(image, masks, colors):
|
| 25 |
+
overlay = image.copy()
|
| 26 |
+
overlay = np.array(overlay, dtype=np.uint8)
|
| 27 |
+
for mask, color in zip(masks, colors):
|
| 28 |
+
overlay[mask > 0] = (overlay[mask > 0] * 0.4 + np.array(color) * 0.6).astype(
|
| 29 |
+
np.uint8
|
| 30 |
+
)
|
| 31 |
+
return Image.fromarray(overlay)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def predict(image: gr.Image, prompts: str):
|
| 35 |
+
if not prompts:
|
| 36 |
+
return None
|
| 37 |
+
|
| 38 |
+
# Convert string input to list
|
| 39 |
+
prompts = [p.strip() for p in prompts.split(",")]
|
| 40 |
+
|
| 41 |
+
# Convert to RGB if needed
|
| 42 |
+
if image.mode != "RGB":
|
| 43 |
+
image = image.convert("RGB")
|
| 44 |
+
|
| 45 |
+
# Get predictions
|
| 46 |
+
pred_mask = interactive_infer_image(model, image, prompts)
|
| 47 |
+
|
| 48 |
+
# Generate visualization
|
| 49 |
+
colors = generate_colors(len(prompts))
|
| 50 |
+
pred_overlay = overlay_masks(
|
| 51 |
+
image, [1 * (pred_mask[i] > 0.5) for i in range(len(prompts))], colors
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
return pred_overlay
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def init_model():
|
| 58 |
+
# Download model
|
| 59 |
+
model_file = hf_hub_download(
|
| 60 |
+
repo_id="microsoft/BiomedParse",
|
| 61 |
+
filename="biomedparse_v1.pt",
|
| 62 |
+
token=os.getenv("HF_TOKEN"),
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
# Initialize model
|
| 66 |
+
conf_files = "configs/biomedparse_inference.yaml"
|
| 67 |
+
opt = load_opt_from_config_files([conf_files])
|
| 68 |
+
opt = init_distributed(opt)
|
| 69 |
+
|
| 70 |
+
model = BaseModel(opt, build_model(opt)).from_pretrained(model_file).eval().cuda()
|
| 71 |
+
with torch.no_grad():
|
| 72 |
+
model.model.sem_seg_head.predictor.lang_encoder.get_text_embeddings(
|
| 73 |
+
BIOMED_CLASSES + ["background"], is_eval=True
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
return model
|
inference_utils/init_predict_dev.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def init_model():
|
| 2 |
+
return None
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def predict(image, prompts):
|
| 6 |
+
return image
|
main.py
CHANGED
|
@@ -11,27 +11,23 @@
|
|
| 11 |
# limitations under the License.
|
| 12 |
|
| 13 |
import os
|
| 14 |
-
import gradio as gr
|
| 15 |
-
import torch
|
| 16 |
-
from PIL import Image
|
| 17 |
-
import numpy as np
|
| 18 |
-
import matplotlib.pyplot as plt
|
| 19 |
-
from huggingface_hub import hf_hub_download
|
| 20 |
-
from modeling.BaseModel import BaseModel
|
| 21 |
-
from modeling import build_model
|
| 22 |
-
from utilities.distributed import init_distributed
|
| 23 |
-
from utilities.arguments import load_opt_from_config_files
|
| 24 |
-
from utilities.constants import BIOMED_CLASSES
|
| 25 |
-
from inference_utils.inference import interactive_infer_image
|
| 26 |
|
| 27 |
# If True, then mock init_model() and predict() functions will be used.
|
| 28 |
-
DEV_MODE = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
gr.set_static_paths(["assets"])
|
| 31 |
|
| 32 |
|
| 33 |
def run():
|
| 34 |
-
global model
|
| 35 |
model = init_model()
|
| 36 |
|
| 37 |
demo = gr.Interface(
|
|
@@ -75,68 +71,6 @@ def run():
|
|
| 75 |
],
|
| 76 |
)
|
| 77 |
|
| 78 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
def init_model_prod():
|
| 82 |
-
# Download model
|
| 83 |
-
model_file = hf_hub_download(
|
| 84 |
-
repo_id="microsoft/BiomedParse",
|
| 85 |
-
filename="biomedparse_v1.pt",
|
| 86 |
-
token=os.getenv("HF_TOKEN"),
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
-
# Initialize model
|
| 90 |
-
conf_files = "configs/biomedparse_inference.yaml"
|
| 91 |
-
opt = load_opt_from_config_files([conf_files])
|
| 92 |
-
opt = init_distributed(opt)
|
| 93 |
-
|
| 94 |
-
model = BaseModel(opt, build_model(opt)).from_pretrained(model_file).eval().cuda()
|
| 95 |
-
with torch.no_grad():
|
| 96 |
-
model.model.sem_seg_head.predictor.lang_encoder.get_text_embeddings(
|
| 97 |
-
BIOMED_CLASSES + ["background"], is_eval=True
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
-
return model
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
def init_model_dev():
|
| 104 |
-
return None
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
def predict_prod(image: gr.Image, prompts: str):
|
| 108 |
-
if not prompts:
|
| 109 |
-
return None
|
| 110 |
-
|
| 111 |
-
# Convert string input to list
|
| 112 |
-
prompts = [p.strip() for p in prompts.split(",")]
|
| 113 |
-
|
| 114 |
-
# Convert to RGB if needed
|
| 115 |
-
if image.mode != "RGB":
|
| 116 |
-
image = image.convert("RGB")
|
| 117 |
-
|
| 118 |
-
# Get predictions
|
| 119 |
-
pred_mask = interactive_infer_image(model, image, prompts)
|
| 120 |
-
|
| 121 |
-
# Generate visualization
|
| 122 |
-
colors = generate_colors(len(prompts))
|
| 123 |
-
pred_overlay = overlay_masks(
|
| 124 |
-
image, [1 * (pred_mask[i] > 0.5) for i in range(len(prompts))], colors
|
| 125 |
-
)
|
| 126 |
-
|
| 127 |
-
return pred_overlay
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
def predict_dev(image: gr.Image, prompts: str):
|
| 131 |
-
return image
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
if DEV_MODE:
|
| 135 |
-
init_model = init_model_dev
|
| 136 |
-
predict = predict_dev
|
| 137 |
-
else:
|
| 138 |
-
init_model = init_model_prod
|
| 139 |
-
predict = predict_prod
|
| 140 |
|
| 141 |
description = """Upload a biomedical image and enter prompts (separated by commas) to detect specific features.
|
| 142 |
|
|
@@ -161,21 +95,6 @@ This Space is based on the [BiomedParse model](https://microsoft.github.io/Biome
|
|
| 161 |
"""
|
| 162 |
|
| 163 |
|
| 164 |
-
def generate_colors(n):
|
| 165 |
-
cmap = plt.get_cmap("tab10")
|
| 166 |
-
colors = [tuple(int(255 * val) for val in cmap(i)[:3]) for i in range(n)]
|
| 167 |
-
return colors
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
def overlay_masks(image, masks, colors):
|
| 171 |
-
overlay = image.copy()
|
| 172 |
-
overlay = np.array(overlay, dtype=np.uint8)
|
| 173 |
-
for mask, color in zip(masks, colors):
|
| 174 |
-
overlay[mask > 0] = (overlay[mask > 0] * 0.4 + np.array(color) * 0.6).astype(
|
| 175 |
-
np.uint8
|
| 176 |
-
)
|
| 177 |
-
return Image.fromarray(overlay)
|
| 178 |
-
|
| 179 |
-
|
| 180 |
if __name__ == "__main__":
|
| 181 |
run()
|
|
|
|
|
|
| 11 |
# limitations under the License.
|
| 12 |
|
| 13 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# If True, then mock init_model() and predict() functions will be used.
|
| 16 |
+
DEV_MODE = True if os.getenv("DEV_MODE") else False
|
| 17 |
+
|
| 18 |
+
import gradio as gr
|
| 19 |
+
|
| 20 |
+
if DEV_MODE:
|
| 21 |
+
from inference_utils.init_predict_dev import init_model, predict
|
| 22 |
+
else:
|
| 23 |
+
from inference_utils.init_predict import init_model, predict
|
| 24 |
+
|
| 25 |
|
| 26 |
gr.set_static_paths(["assets"])
|
| 27 |
|
| 28 |
|
| 29 |
def run():
|
| 30 |
+
global model, demo
|
| 31 |
model = init_model()
|
| 32 |
|
| 33 |
demo = gr.Interface(
|
|
|
|
| 71 |
],
|
| 72 |
)
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
description = """Upload a biomedical image and enter prompts (separated by commas) to detect specific features.
|
| 76 |
|
|
|
|
| 95 |
"""
|
| 96 |
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
if __name__ == "__main__":
|
| 99 |
run()
|
| 100 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
pyproject.toml
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "biomedparse"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Python environment to develop the Gradio interface. Mocks the actual ML model."
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
requires-python = ">=3.9"
|
| 7 |
+
dependencies = [
|
| 8 |
+
"gradio==4.44.1",
|
| 9 |
+
]
|
uv.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|