From b9dd9e9eb8dc8d4e77b80d2067107ff44e6dde95 Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Fri, 14 Nov 2025 09:44:37 -0500 Subject: [PATCH 01/14] update docker compose --- label_studio_ml/examples/tesseract/docker-compose.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/label_studio_ml/examples/tesseract/docker-compose.yml b/label_studio_ml/examples/tesseract/docker-compose.yml index 8f5e71a8..aab064c9 100644 --- a/label_studio_ml/examples/tesseract/docker-compose.yml +++ b/label_studio_ml/examples/tesseract/docker-compose.yml @@ -22,13 +22,13 @@ services: # Do not use 'localhost' as it does not work within Docker containers. # Use prefix 'http://' or 'https://' for the URL always. # Determine the actual IP using 'ifconfig' (Linux/Mac) or 'ipconfig' (Windows). - - LABEL_STUDIO_HOST= + - LABEL_STUDIO_HOST=http://app.heartex.com # specify the access token for the label studio if you use file upload - LABEL_STUDIO_ACCESS_TOKEN= # set these variables to use Minio as a storage backend - - AWS_ACCESS_KEY_ID=your-MINIO_ROOT_USER - - AWS_SECRET_ACCESS_KEY=your-MINIO_ROOT_PASSWORD - - AWS_ENDPOINT=http://host.docker.internal:9000 + - AWS_ACCESS_KEY_ID= + - AWS_SECRET_ACCESS_KEY= + - AWS_ENDPOINT extra_hosts: - "host.docker.internal:host-gateway" # for macos and unix From b0e1f7a03fa22c551a47b368c3583641d49cc69a Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Fri, 14 Nov 2025 09:48:34 -0500 Subject: [PATCH 02/14] switch to official minio image --- label_studio_ml/examples/tesseract/docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/label_studio_ml/examples/tesseract/docker-compose.yml b/label_studio_ml/examples/tesseract/docker-compose.yml index aab064c9..3f638c1a 100644 --- a/label_studio_ml/examples/tesseract/docker-compose.yml +++ b/label_studio_ml/examples/tesseract/docker-compose.yml @@ -34,7 +34,7 @@ services: minio: container_name: minio - image: bitnami/minio:latest + image: minio/minio:latest environment: - MINIO_ROOT_USER= - MINIO_ROOT_PASSWORD= From 59e69a1544bd1a86757d167ba7f156afbda0276d Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Fri, 14 Nov 2025 09:53:26 -0500 Subject: [PATCH 03/14] fix langchain tool import --- label_studio_ml/examples/langchain_search_agent/model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/label_studio_ml/examples/langchain_search_agent/model.py b/label_studio_ml/examples/langchain_search_agent/model.py index 8a551786..a9ae2c1c 100644 --- a/label_studio_ml/examples/langchain_search_agent/model.py +++ b/label_studio_ml/examples/langchain_search_agent/model.py @@ -4,7 +4,7 @@ from uuid import uuid4 from typing import List, Dict, Optional, Any from label_studio_ml.model import LabelStudioMLBase -from langchain.tools import Tool +from langchain.tools import tool from langchain.utilities import GoogleSearchAPIWrapper from langchain.callbacks.base import BaseCallbackHandler from langchain.agents import initialize_agent From 7820e34e292b2187b8134bd7ad5946cd5bdd6f39 Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Fri, 14 Nov 2025 10:15:17 -0500 Subject: [PATCH 04/14] Fix mmdetection imports --- label_studio_ml/examples/mmdetection-3/Dockerfile | 12 ++++++++---- .../examples/mmdetection-3/requirements.txt | 1 - 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/label_studio_ml/examples/mmdetection-3/Dockerfile b/label_studio_ml/examples/mmdetection-3/Dockerfile index 9e462d4d..ae1ecfa4 100644 --- a/label_studio_ml/examples/mmdetection-3/Dockerfile +++ b/label_studio_ml/examples/mmdetection-3/Dockerfile @@ -8,8 +8,11 @@ ARG TEST_ENV WORKDIR /app # To fix GPG key error when running apt-get update -RUN apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/3bf863cc.pub \ - && apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/7fa2af80.pub +RUN apt-get update && apt-get install -y --no-install-recommends wget gnupg ca-certificates \ + && rm -rf /var/lib/apt/lists/* + +RUN wget -qO - https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/3bf863cc.pub | gpg --dearmor -o /usr/share/keyrings/nvidia.gpg \ + && wget -qO - https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/7fa2af80.pub | gpg --dearmor -o /usr/share/keyrings/nvidia-ml.gpg # Update the base OS RUN --mount=type=cache,target="/var/cache/apt",sharing=locked \ @@ -57,8 +60,9 @@ RUN --mount=type=cache,target=${PIP_CACHE_DIR},sharing=locked \ fi RUN mim install mmengine==0.10.3 -RUN mim install mmdet==3.3.0 -RUN mim download mmdet --config yolov3_mobilenetv2_8xb24-320-300e_coco --dest . +RUN mim install mmcv==2.1.0 +RUN mim install mmdet==3.3.0 +RUN mim download mmdet --config yolov3_mobilenetv2_8xb24-320-300e_coco --dest . COPY . . diff --git a/label_studio_ml/examples/mmdetection-3/requirements.txt b/label_studio_ml/examples/mmdetection-3/requirements.txt index b456271f..b2ee5cd4 100644 --- a/label_studio_ml/examples/mmdetection-3/requirements.txt +++ b/label_studio_ml/examples/mmdetection-3/requirements.txt @@ -1,6 +1,5 @@ boto3>=1.26.103,<2.0.0 openmim~=0.3.9 -mmcv>=2.0.0rc4,<2.2.0 numpy~=1.26 From fca2266b87c5c5289d69fa5d78dabd4f23946b71 Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Fri, 14 Nov 2025 10:34:52 -0500 Subject: [PATCH 05/14] fix segment anything model --- .../examples/segment_anything_model/Dockerfile | 8 ++++++-- .../examples/segment_anything_model/requirements.txt | 7 +++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/label_studio_ml/examples/segment_anything_model/Dockerfile b/label_studio_ml/examples/segment_anything_model/Dockerfile index e660289e..4ff6ac90 100644 --- a/label_studio_ml/examples/segment_anything_model/Dockerfile +++ b/label_studio_ml/examples/segment_anything_model/Dockerfile @@ -23,17 +23,21 @@ RUN --mount=type=cache,target="/var/cache/apt",sharing=locked \ apt-get update; \ apt-get upgrade -y; \ apt install --no-install-recommends -y \ - wget git libopencv-dev python3-opencv cmake protobuf-compiler; \ + wget git libopencv-dev cmake protobuf-compiler; \ apt-get autoremove -y # Copy and run the model download script COPY download_models.sh . RUN bash /app/download_models.sh +# Install numpy first to avoid conflicts with system numpy +RUN --mount=type=cache,target=${PIP_CACHE_DIR},sharing=locked \ + pip install --upgrade pip && \ + pip install "numpy>=2,<2.3.0" + # install base requirements COPY requirements-base.txt . RUN --mount=type=cache,target=${PIP_CACHE_DIR},sharing=locked \ - pip install --upgrade pip && \ pip install -r requirements-base.txt # install custom requirements diff --git a/label_studio_ml/examples/segment_anything_model/requirements.txt b/label_studio_ml/examples/segment_anything_model/requirements.txt index 95fcddda..55d56639 100644 --- a/label_studio_ml/examples/segment_anything_model/requirements.txt +++ b/label_studio_ml/examples/segment_anything_model/requirements.txt @@ -1,7 +1,8 @@ +numpy>=2,<2.3.0 label_studio_converter -opencv-python +opencv-python-headless>=4.12.0,<5.0.0 onnxruntime==1.15.1 -onnx +onnx==1.12.0 torch==2.0.1 torchvision==0.15.2 gunicorn==22.0.0 @@ -11,5 +12,3 @@ timm==0.4.12 segment_anything @ git+https://github.com/facebookresearch/segment-anything.git mobile-sam @ git+https://github.com/ChaoningZhang/MobileSAM.git label-studio-ml @ git+https://github.com/heartexlabs/label-studio-ml-backend.git - -numpy<2 From d82d24e4876d07e30394527dd1ebabcdead13ace Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Fri, 14 Nov 2025 10:36:40 -0500 Subject: [PATCH 06/14] fix mmdetection nummpy error --- label_studio_ml/examples/mmdetection-3/Dockerfile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/label_studio_ml/examples/mmdetection-3/Dockerfile b/label_studio_ml/examples/mmdetection-3/Dockerfile index ae1ecfa4..48272a0e 100644 --- a/label_studio_ml/examples/mmdetection-3/Dockerfile +++ b/label_studio_ml/examples/mmdetection-3/Dockerfile @@ -41,6 +41,10 @@ ENV PYTHONUNBUFFERED=1 \ RUN --mount=type=cache,target=$PIP_CACHE_DIR \ pip install -U pip +# Install numpy early to avoid dependency conflicts +RUN --mount=type=cache,target=${PIP_CACHE_DIR},sharing=locked \ + pip install "numpy~=1.26" + # Install base requirements COPY requirements-base.txt . RUN --mount=type=cache,target=${PIP_CACHE_DIR},sharing=locked \ From 7a527a6410283bd5e2b88a539f5767931e2277c2 Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Fri, 14 Nov 2025 10:44:21 -0500 Subject: [PATCH 07/14] fix sam test import error --- label_studio_ml/examples/segment_anything_model/Dockerfile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/label_studio_ml/examples/segment_anything_model/Dockerfile b/label_studio_ml/examples/segment_anything_model/Dockerfile index 4ff6ac90..80ffe8d2 100644 --- a/label_studio_ml/examples/segment_anything_model/Dockerfile +++ b/label_studio_ml/examples/segment_anything_model/Dockerfile @@ -23,7 +23,7 @@ RUN --mount=type=cache,target="/var/cache/apt",sharing=locked \ apt-get update; \ apt-get upgrade -y; \ apt install --no-install-recommends -y \ - wget git libopencv-dev cmake protobuf-compiler; \ + wget git libopencv-dev cmake protobuf-compiler execstack; \ apt-get autoremove -y # Copy and run the model download script @@ -45,6 +45,10 @@ COPY requirements.txt . RUN --mount=type=cache,target=${PIP_CACHE_DIR},sharing=locked \ pip install -r requirements.txt +# Fix executable stack issue with onnxruntime shared library +RUN PYTHON_VER=$(python3 -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')") && \ + find /usr/local/lib/python${PYTHON_VER}/site-packages/onnxruntime/capi -name "onnxruntime_pybind11_state*.so" -exec execstack -c {} \; 2>/dev/null || true + # install test requirements if needed COPY requirements-test.txt . # build only when TEST_ENV="true" From 5afba0b291857d2f25e4860af2c44e2c4370ff9b Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Fri, 14 Nov 2025 10:48:44 -0500 Subject: [PATCH 08/14] update mmdetection 3 test numpy error --- .../examples/mmdetection-3/Dockerfile | 16 ++++++++++++---- .../examples/mmdetection-3/requirements-test.txt | 1 + .../examples/mmdetection-3/test_model.py | 6 ++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/label_studio_ml/examples/mmdetection-3/Dockerfile b/label_studio_ml/examples/mmdetection-3/Dockerfile index 48272a0e..85a19a55 100644 --- a/label_studio_ml/examples/mmdetection-3/Dockerfile +++ b/label_studio_ml/examples/mmdetection-3/Dockerfile @@ -55,7 +55,7 @@ COPY requirements.txt . RUN --mount=type=cache,target=${PIP_CACHE_DIR},sharing=locked \ pip install -r requirements.txt -# Install test requirements if needed +# Install test requirements if needed (install before mim to ensure numpy is available) COPY requirements-test.txt . # build only when TEST_ENV="true" RUN --mount=type=cache,target=${PIP_CACHE_DIR},sharing=locked \ @@ -63,13 +63,21 @@ RUN --mount=type=cache,target=${PIP_CACHE_DIR},sharing=locked \ pip install -r requirements-test.txt; \ fi -RUN mim install mmengine==0.10.3 -RUN mim install mmcv==2.1.0 -RUN mim install mmdet==3.3.0 +# Ensure numpy is available and pinned before mim installs (mim packages may depend on it) +RUN python -c "import numpy; print(f'numpy version: {numpy.__version__}')" || pip install "numpy~=1.26" + +# Install mim packages, but prevent numpy from being upgraded +RUN mim install mmengine==0.10.3 && \ + mim install mmcv==2.1.0 && \ + mim install mmdet==3.3.0 && \ + pip install --force-reinstall --no-deps "numpy~=1.26" || true RUN mim download mmdet --config yolov3_mobilenetv2_8xb24-320-300e_coco --dest . COPY . . +# Final verification that numpy is available (important for tests) +RUN python -c "import numpy; print(f'✓ numpy {numpy.__version__} is available')" || (echo "ERROR: numpy is not available!" && exit 1) + EXPOSE 9090 CMD gunicorn --preload --bind :$PORT --workers $WORKERS --threads $THREADS --timeout 0 _wsgi:app diff --git a/label_studio_ml/examples/mmdetection-3/requirements-test.txt b/label_studio_ml/examples/mmdetection-3/requirements-test.txt index 9955decc..759e647e 100644 --- a/label_studio_ml/examples/mmdetection-3/requirements-test.txt +++ b/label_studio_ml/examples/mmdetection-3/requirements-test.txt @@ -1,2 +1,3 @@ pytest pytest-cov +numpy~=1.26 diff --git a/label_studio_ml/examples/mmdetection-3/test_model.py b/label_studio_ml/examples/mmdetection-3/test_model.py index 33db0b37..c30b4265 100644 --- a/label_studio_ml/examples/mmdetection-3/test_model.py +++ b/label_studio_ml/examples/mmdetection-3/test_model.py @@ -1,5 +1,11 @@ import requests +# Ensure numpy is available before importing mmdetection (which depends on mmdet) +try: + import numpy +except ImportError: + raise ImportError("numpy is not available. Please install numpy before running tests.") + from mmdetection import MMDetection from label_studio_ml.utils import compare_nested_structures From 603c898c57bf42a65c722206a1c6ef5fa794b888 Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Fri, 14 Nov 2025 10:52:01 -0500 Subject: [PATCH 09/14] fix --- .../examples/langchain_search_agent/model.py | 22 ++++++++++++++----- .../langchain_search_agent/requirements.txt | 1 + 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/label_studio_ml/examples/langchain_search_agent/model.py b/label_studio_ml/examples/langchain_search_agent/model.py index a9ae2c1c..88ac5c9a 100644 --- a/label_studio_ml/examples/langchain_search_agent/model.py +++ b/label_studio_ml/examples/langchain_search_agent/model.py @@ -4,12 +4,22 @@ from uuid import uuid4 from typing import List, Dict, Optional, Any from label_studio_ml.model import LabelStudioMLBase -from langchain.tools import tool -from langchain.utilities import GoogleSearchAPIWrapper -from langchain.callbacks.base import BaseCallbackHandler -from langchain.agents import initialize_agent -from langchain.agents import AgentType -from langchain.llms import OpenAI + +# Try new langchain imports first (v0.1.0+), fall back to old imports +try: + from langchain_community.utilities import GoogleSearchAPIWrapper + from langchain_core.callbacks import BaseCallbackHandler + from langchain.agents import initialize_agent, AgentType + from langchain_openai import OpenAI + from langchain.tools import Tool +except ImportError: + # Fall back to old imports for older langchain versions + from langchain.utilities import GoogleSearchAPIWrapper + from langchain.callbacks.base import BaseCallbackHandler + from langchain.agents import initialize_agent, AgentType + from langchain.llms import OpenAI + from langchain.tools import Tool + from label_studio_ml.utils import match_labels logger = logging.getLogger(__name__) diff --git a/label_studio_ml/examples/langchain_search_agent/requirements.txt b/label_studio_ml/examples/langchain_search_agent/requirements.txt index 2bfb036d..1ea2922f 100644 --- a/label_studio_ml/examples/langchain_search_agent/requirements.txt +++ b/label_studio_ml/examples/langchain_search_agent/requirements.txt @@ -1,5 +1,6 @@ langchain langchain_community +langchain_openai google-api-python-client openai From 6f22a4764b3d09f07b4cf18e87a1df401bb1a6fe Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Fri, 14 Nov 2025 10:57:39 -0500 Subject: [PATCH 10/14] fix sam dockerfile --- label_studio_ml/examples/segment_anything_model/Dockerfile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/label_studio_ml/examples/segment_anything_model/Dockerfile b/label_studio_ml/examples/segment_anything_model/Dockerfile index 80ffe8d2..ac11bbbb 100644 --- a/label_studio_ml/examples/segment_anything_model/Dockerfile +++ b/label_studio_ml/examples/segment_anything_model/Dockerfile @@ -23,7 +23,7 @@ RUN --mount=type=cache,target="/var/cache/apt",sharing=locked \ apt-get update; \ apt-get upgrade -y; \ apt install --no-install-recommends -y \ - wget git libopencv-dev cmake protobuf-compiler execstack; \ + wget git libopencv-dev cmake protobuf-compiler binutils; \ apt-get autoremove -y # Copy and run the model download script @@ -46,8 +46,9 @@ RUN --mount=type=cache,target=${PIP_CACHE_DIR},sharing=locked \ pip install -r requirements.txt # Fix executable stack issue with onnxruntime shared library +# Remove the .note.GNU-stack section which marks the stack as executable RUN PYTHON_VER=$(python3 -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')") && \ - find /usr/local/lib/python${PYTHON_VER}/site-packages/onnxruntime/capi -name "onnxruntime_pybind11_state*.so" -exec execstack -c {} \; 2>/dev/null || true + find /usr/local/lib/python${PYTHON_VER}/site-packages/onnxruntime/capi -name "onnxruntime_pybind11_state*.so" -exec sh -c 'objcopy --remove-section .note.GNU-stack "$1" 2>/dev/null || true' _ {} \; || true # install test requirements if needed COPY requirements-test.txt . From 05ae5c8476c91d65f5448da267aa18916cd3d297 Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Fri, 14 Nov 2025 11:06:34 -0500 Subject: [PATCH 11/14] fix langchain search agent container' --- .../examples/langchain_search_agent/model.py | 58 +++++++++++-------- .../langchain_search_agent/requirements.txt | 1 + 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/label_studio_ml/examples/langchain_search_agent/model.py b/label_studio_ml/examples/langchain_search_agent/model.py index 88ac5c9a..f360029c 100644 --- a/label_studio_ml/examples/langchain_search_agent/model.py +++ b/label_studio_ml/examples/langchain_search_agent/model.py @@ -5,20 +5,14 @@ from typing import List, Dict, Optional, Any from label_studio_ml.model import LabelStudioMLBase -# Try new langchain imports first (v0.1.0+), fall back to old imports -try: - from langchain_community.utilities import GoogleSearchAPIWrapper - from langchain_core.callbacks import BaseCallbackHandler - from langchain.agents import initialize_agent, AgentType - from langchain_openai import OpenAI - from langchain.tools import Tool -except ImportError: - # Fall back to old imports for older langchain versions - from langchain.utilities import GoogleSearchAPIWrapper - from langchain.callbacks.base import BaseCallbackHandler - from langchain.agents import initialize_agent, AgentType - from langchain.llms import OpenAI - from langchain.tools import Tool + +# Import langchain components - use new API (v1.0+) +from langchain_community.utilities import GoogleSearchAPIWrapper +from langchain_core.callbacks import BaseCallbackHandler +from langchain.agents import create_agent +from langchain_openai import ChatOpenAI +from langchain_core.tools import Tool + from label_studio_ml.utils import match_labels @@ -92,17 +86,16 @@ def predict(self, tasks: List[Dict], context: Optional[Dict] = None, **kwargs) - func=search.run, callbacks=[search_results] )] - llm = OpenAI( + llm = ChatOpenAI( temperature=0, - model_name='gpt-3.5-turbo-instruct' + model="gpt-3.5-turbo" ) - agent = initialize_agent( - tools, - llm, - agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, - verbose=True, - max_iterations=3, - early_stopping_method="generate", + + # Use new agent API (langchain 1.0+) + agent = create_agent( + model=llm, + tools=tools, + debug=True ) labels = self.parsed_label_config[from_name]['labels'] @@ -131,7 +124,24 @@ def predict(self, tasks: List[Dict], context: Optional[Dict] = None, **kwargs) - text = self.preload_task_data(task, task['data'][value]) full_prompt = self.PROMPT_TEMPLATE.format(prompt=prompt, text=text) logger.info(f'Full prompt: {full_prompt}') - llm_result = agent.run(full_prompt) + # Invoke the agent with the prompt + result = agent.invoke({"messages": [("user", full_prompt)]}) + # Extract the response from the agent result + if isinstance(result, dict) and "messages" in result: + # Get the last message which should be the agent's response + messages = result["messages"] + if messages: + last_message = messages[-1] + if hasattr(last_message, 'content'): + llm_result = last_message.content + elif isinstance(last_message, dict) and 'content' in last_message: + llm_result = last_message['content'] + else: + llm_result = str(last_message) + else: + llm_result = str(result) + else: + llm_result = str(result) output_classes = match_labels(llm_result, labels) snippets = search_results.snippets logger.debug(f'LLM result: {llm_result}') diff --git a/label_studio_ml/examples/langchain_search_agent/requirements.txt b/label_studio_ml/examples/langchain_search_agent/requirements.txt index 1ea2922f..04194b56 100644 --- a/label_studio_ml/examples/langchain_search_agent/requirements.txt +++ b/label_studio_ml/examples/langchain_search_agent/requirements.txt @@ -1,5 +1,6 @@ langchain langchain_community +langchain_core langchain_openai google-api-python-client openai From 99b76ce4b8c4982da70a34a5ced342ce01753171 Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Fri, 14 Nov 2025 11:20:34 -0500 Subject: [PATCH 12/14] fix sam --- label_studio_ml/examples/segment_anything_model/Dockerfile | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/label_studio_ml/examples/segment_anything_model/Dockerfile b/label_studio_ml/examples/segment_anything_model/Dockerfile index ac11bbbb..403722a0 100644 --- a/label_studio_ml/examples/segment_anything_model/Dockerfile +++ b/label_studio_ml/examples/segment_anything_model/Dockerfile @@ -23,7 +23,7 @@ RUN --mount=type=cache,target="/var/cache/apt",sharing=locked \ apt-get update; \ apt-get upgrade -y; \ apt install --no-install-recommends -y \ - wget git libopencv-dev cmake protobuf-compiler binutils; \ + wget git libopencv-dev cmake protobuf-compiler binutils patchelf; \ apt-get autoremove -y # Copy and run the model download script @@ -45,10 +45,9 @@ COPY requirements.txt . RUN --mount=type=cache,target=${PIP_CACHE_DIR},sharing=locked \ pip install -r requirements.txt -# Fix executable stack issue with onnxruntime shared library -# Remove the .note.GNU-stack section which marks the stack as executable +# Fix executable stack issue with onnxruntime shared library using patchelf RUN PYTHON_VER=$(python3 -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')") && \ - find /usr/local/lib/python${PYTHON_VER}/site-packages/onnxruntime/capi -name "onnxruntime_pybind11_state*.so" -exec sh -c 'objcopy --remove-section .note.GNU-stack "$1" 2>/dev/null || true' _ {} \; || true + find /usr/local/lib/python${PYTHON_VER}/site-packages/onnxruntime/capi -name "onnxruntime_pybind11_state*.so" -exec sh -c 'patchelf --clear-execstack "$1" 2>/dev/null || true' _ {} \; || true # install test requirements if needed COPY requirements-test.txt . From af65c81db426a85547184990dcb55b2f481dd4d2 Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Fri, 14 Nov 2025 12:10:06 -0500 Subject: [PATCH 13/14] fix sam --- .../segment_anything_model/Dockerfile | 4 +- .../segment_anything_model/onnxconverter.py | 27 +++++++---- .../segment_anything_model/requirements.txt | 4 +- .../segment_anything_model/sam_predictor.py | 48 +++++++++++++++++++ 4 files changed, 71 insertions(+), 12 deletions(-) diff --git a/label_studio_ml/examples/segment_anything_model/Dockerfile b/label_studio_ml/examples/segment_anything_model/Dockerfile index 403722a0..63b32a8a 100644 --- a/label_studio_ml/examples/segment_anything_model/Dockerfile +++ b/label_studio_ml/examples/segment_anything_model/Dockerfile @@ -59,8 +59,8 @@ RUN --mount=type=cache,target=${PIP_CACHE_DIR},sharing=locked \ COPY . . -# Add ONNX model -RUN python3 onnxconverter.py +# Add ONNX model (skip if it fails - not critical for basic functionality) +RUN python3 onnxconverter.py || echo "Warning: ONNX conversion failed, but continuing build" EXPOSE 9090 diff --git a/label_studio_ml/examples/segment_anything_model/onnxconverter.py b/label_studio_ml/examples/segment_anything_model/onnxconverter.py index 3def6c76..9e89d142 100644 --- a/label_studio_ml/examples/segment_anything_model/onnxconverter.py +++ b/label_studio_ml/examples/segment_anything_model/onnxconverter.py @@ -55,13 +55,24 @@ def convert(checkpoint_path): dynamic_axes=dynamic_axes, ) - quantize_dynamic( - model_input=onnx_model_path, - model_output=onnx_model_quantized_path, - optimize_model=True, - per_channel=False, - reduce_range=False, - weight_type=QuantType.QUInt8, - ) + # Newer versions of onnxruntime don't have optimize_model parameter + try: + quantize_dynamic( + model_input=onnx_model_path, + model_output=onnx_model_quantized_path, + optimize_model=True, + per_channel=False, + reduce_range=False, + weight_type=QuantType.QUInt8, + ) + except TypeError: + # Fallback for newer onnxruntime versions without optimize_model + quantize_dynamic( + model_input=onnx_model_path, + model_output=onnx_model_quantized_path, + per_channel=False, + reduce_range=False, + weight_type=QuantType.QUInt8, + ) convert(VITH_CHECKPOINT) diff --git a/label_studio_ml/examples/segment_anything_model/requirements.txt b/label_studio_ml/examples/segment_anything_model/requirements.txt index 55d56639..8f6fbf50 100644 --- a/label_studio_ml/examples/segment_anything_model/requirements.txt +++ b/label_studio_ml/examples/segment_anything_model/requirements.txt @@ -1,8 +1,8 @@ numpy>=2,<2.3.0 label_studio_converter opencv-python-headless>=4.12.0,<5.0.0 -onnxruntime==1.15.1 -onnx==1.12.0 +onnxruntime>=1.18.0 +onnx>=1.15.0 torch==2.0.1 torchvision==0.15.2 gunicorn==22.0.0 diff --git a/label_studio_ml/examples/segment_anything_model/sam_predictor.py b/label_studio_ml/examples/segment_anything_model/sam_predictor.py index 1a6a7f0f..b95f6d3b 100644 --- a/label_studio_ml/examples/segment_anything_model/sam_predictor.py +++ b/label_studio_ml/examples/segment_anything_model/sam_predictor.py @@ -9,6 +9,52 @@ from label_studio_ml.utils import InMemoryLRUDictCache from label_studio_sdk._extensions.label_studio_tools.core.utils.io import get_local_path +# Monkey-patch torch.as_tensor to handle numpy 2.x compatibility +_original_as_tensor = torch.as_tensor +def _patched_as_tensor(data, dtype=None, device=None): + """Patched version of torch.as_tensor that handles numpy 2.x compatibility""" + if isinstance(data, np.ndarray): + # For numpy 2.x compatibility, ensure arrays are properly converted + if dtype is None and data.dtype == np.uint8: + # Explicitly convert uint8 arrays + return _original_as_tensor(data.copy(), dtype=torch.uint8, device=device) + elif dtype is not None: + # If dtype is specified, ensure the array is compatible + if data.dtype == np.float32 and dtype == torch.int: + # Convert float32 to int properly + return _original_as_tensor(data.astype(np.int32), dtype=dtype, device=device) + return _original_as_tensor(data, dtype=dtype, device=device) +torch.as_tensor = _patched_as_tensor + +# Also patch tensor.numpy() to handle numpy 2.x compatibility +_original_tensor_numpy = torch.Tensor.numpy +def _patched_tensor_numpy(self, *args, **kwargs): + """Patched version of tensor.numpy() that handles numpy 2.x compatibility""" + try: + return _original_tensor_numpy(self, *args, **kwargs) + except RuntimeError as e: + if "Numpy is not available" in str(e): + # Fallback: manually convert tensor to numpy array + # This is a workaround for numpy 2.x compatibility issues + arr = self.detach().cpu().contiguous() + # Convert to list first, then to numpy array + if arr.dim() == 0: + return np.array(arr.item()) + else: + # Map torch dtypes to numpy dtypes + dtype_map = { + torch.float32: np.float32, + torch.float64: np.float64, + torch.int32: np.int32, + torch.int64: np.int64, + torch.uint8: np.uint8, + torch.bool: np.bool_, + } + np_dtype = dtype_map.get(arr.dtype, None) + return np.array(arr.tolist(), dtype=np_dtype) + raise +torch.Tensor.numpy = _patched_tensor_numpy + logger = logging.getLogger(__name__) _MODELS_DIR = pathlib.Path(__file__).parent / "models" @@ -91,6 +137,8 @@ def set_image(self, img_path, calculate_embeddings=True, task=None): ) image = cv2.imread(image_path) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) + # Ensure image is contiguous and properly typed for numpy 2.x compatibility + image = np.ascontiguousarray(image, dtype=np.uint8) self.predictor.set_image(image) payload = {'image_shape': image.shape[:2]} logger.debug(f'Finished set_image({img_path}) in `IN_MEM_CACHE`: image shape {image.shape[:2]}') From e6d8c97c37c03122d5b92c3935fdd5961eafdb54 Mon Sep 17 00:00:00 2001 From: Micaela Kaplan Date: Fri, 14 Nov 2025 12:34:47 -0500 Subject: [PATCH 14/14] update spacy --- label_studio_ml/examples/spacy/Dockerfile | 9 +++++++-- label_studio_ml/examples/spacy/requirements.txt | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/label_studio_ml/examples/spacy/Dockerfile b/label_studio_ml/examples/spacy/Dockerfile index 4174ddb0..cf37e0fa 100644 --- a/label_studio_ml/examples/spacy/Dockerfile +++ b/label_studio_ml/examples/spacy/Dockerfile @@ -21,7 +21,10 @@ RUN --mount=type=cache,target="/var/cache/apt",sharing=locked \ apt-get update; \ apt-get upgrade -y; \ apt install --no-install-recommends -y \ - git; \ + git \ + build-essential \ + gcc \ + g++; \ apt-get autoremove -y # install base requirements @@ -32,7 +35,9 @@ RUN --mount=type=cache,target=${PIP_CACHE_DIR},sharing=locked \ # install custom requirements COPY requirements.txt . RUN --mount=type=cache,target=${PIP_CACHE_DIR},sharing=locked \ - pip install -r requirements.txt + pip install -r requirements.txt && \ + apt-get purge -y build-essential gcc g++ && \ + apt-get autoremove -y # install test requirements if needed COPY requirements-test.txt . diff --git a/label_studio_ml/examples/spacy/requirements.txt b/label_studio_ml/examples/spacy/requirements.txt index 05e7beb1..12e764e9 100644 --- a/label_studio_ml/examples/spacy/requirements.txt +++ b/label_studio_ml/examples/spacy/requirements.txt @@ -1,6 +1,6 @@ gunicorn==23.0.0 spacy~=3.6 label-studio-ml @ git+https://github.com/HumanSignal/label-studio-ml-backend.git@master -numpy~=1.26 +numpy>=2,<2.3.0