# Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from PIL import Image from huggingface_hub import hf_hub_download import matplotlib.pyplot as plt import numpy as np from inference_utils.inference import interactive_infer_image from main import model import gradio as gr from modeling import build_model from modeling.BaseModel import BaseModel from utilities.arguments import load_opt_from_config_files from utilities.constants import BIOMED_CLASSES from utilities.distributed import init_distributed def generate_colors(n): cmap = plt.get_cmap("tab10") colors = [tuple(int(255 * val) for val in cmap(i)[:3]) for i in range(n)] return colors def overlay_masks(image, masks, colors): overlay = image.copy() overlay = np.array(overlay, dtype=np.uint8) for mask, color in zip(masks, colors): overlay[mask > 0] = (overlay[mask > 0] * 0.4 + np.array(color) * 0.6).astype( np.uint8 ) return Image.fromarray(overlay) def predict(image: gr.Image, prompts: str): if not prompts: return None # Convert string input to list prompts = [p.strip() for p in prompts.split(",")] # Convert to RGB if needed if image.mode != "RGB": image = image.convert("RGB") # Get predictions pred_mask = interactive_infer_image(model, image, prompts) # Generate visualization colors = generate_colors(len(prompts)) pred_overlay = overlay_masks( image, [1 * (pred_mask[i] > 0.5) for i in range(len(prompts))], colors ) return pred_overlay def init_model(): # Download model model_file = hf_hub_download( repo_id="microsoft/BiomedParse", filename="biomedparse_v1.pt", token=os.getenv("HF_TOKEN"), ) # Initialize model conf_files = "configs/biomedparse_inference.yaml" opt = load_opt_from_config_files([conf_files]) opt = init_distributed(opt) model = BaseModel(opt, build_model(opt)).from_pretrained(model_file).eval().cuda() with torch.no_grad(): model.model.sem_seg_head.predictor.lang_encoder.get_text_embeddings( BIOMED_CLASSES + ["background"], is_eval=True ) return model