Skip to content

Commit 50a8551

Browse files
committed
Merge branch 'master' into video-inference
2 parents 083f14d + dfed71d commit 50a8551

File tree

5 files changed

+159
-88
lines changed

5 files changed

+159
-88
lines changed

.github/workflows/run_tests.yml

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ on:
2424

2525
jobs:
2626
build:
27-
2827
runs-on: ${{ matrix.os }}
2928
timeout-minutes: 20
3029
strategy:
@@ -51,6 +50,18 @@ jobs:
5150
pip install genbadge[coverage]
5251
- name: Run static analysis lint
5352
uses: pre-commit/action@v3.0.0
53+
- name: Check if Dockerfile.template changed
54+
id: check_dockerfile_change
55+
shell: bash
56+
run: |
57+
# We compare the files changed between the previous commit SHA (github.event.before)
58+
# and the current commit SHA (github.event.after).
59+
# If Dockerfile.template is in that list, we set changed=true
60+
if git diff --name-only ${{ github.event.before }} ${{ github.event.after }} | grep -q 'clarifai/runners/dockerfile_template/Dockerfile.template'; then
61+
echo "changed=true" >> $GITHUB_OUTPUT
62+
else
63+
echo "changed=false" >> $GITHUB_OUTPUT
64+
fi
5465
- name: Prepare the API keys & Run pytest
5566
env:
5667
CLARIFAI_USER_EMAIL_SECURE_HOSTING: ${{ secrets.CLARIFAI_USER_EMAIL_SECURE_HOSTING }}
@@ -71,11 +82,25 @@ jobs:
7182
export CLARIFAI_USER_ID="$(python scripts/key_for_tests.py --get-userid)"
7283
export CLARIFAI_PAT="$(python scripts/key_for_tests.py --create-pat)"
7384
if [ "${{ github.event.pull_request.head.repo.full_name }}" = "${{ github.repository }}" ]; then
74-
pytest --cov=. --cov-report=xml:coverage/coverage.cobertura.xml
85+
# Run coverage for all but the container test
86+
pytest --cov=. --cov-report=xml:coverage/coverage.cobertura.xml --ignore=tests/runners/test_model_run_locally-container.py
87+
88+
# Conditionally run the container test
89+
if [ "${{ steps.check_dockerfile_change.outputs.changed }}" = "true" ]; then
90+
pytest --cov=. --cov-append --cov-report=xml:coverage/coverage.cobertura.xml tests/runners/test_model_run_locally-container.py
91+
fi
7592
elif [ "${{ inputs.PERIODIC_CHECKS }}" = "true" ]; then
76-
pytest -m "not coverage_only" --cov=. --cov-report=xml:coverage/coverage.cobertura.xml
93+
pytest -m "not coverage_only" --cov=. --cov-report=xml:coverage/coverage.cobertura.xml --ignore=tests/runners/test_model_run_locally-container.py
94+
95+
if [ "${{ steps.check_dockerfile_change.outputs.changed }}" = "true" ]; then
96+
pytest -m "not coverage_only" --cov=. --cov-append --cov-report=xml:coverage/coverage.cobertura.xml tests/runners/test_model_run_locally-container.py
97+
fi
7798
else
78-
pytest -m "not requires_secrets" --cov=. --cov-report=xml:coverage/coverage.cobertura.xml
99+
pytest -m "not requires_secrets" --cov=. --cov-report=xml:coverage/coverage.cobertura.xml --ignore=tests/runners/test_model_run_locally-container.py
100+
101+
if [ "${{ steps.check_dockerfile_change.outputs.changed }}" = "true" ]; then
102+
pytest -m "not requires_secrets" --cov=. --cov-append --cov-report=xml:coverage/coverage.cobertura.xml tests/runners/test_model_run_locally-container.py
103+
fi
79104
fi
80105
- name: Code Coverage Report
81106
if: runner.os == 'Linux' && matrix.python-version == '3.11'

clarifai/runners/models/model_builder.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,8 @@ def get_model_version_proto(self):
477477
for concept in labels:
478478
concept_proto = json_format.ParseDict(concept, resources_pb2.Concept())
479479
model_version_proto.output_info.data.concepts.append(concept_proto)
480-
else:
480+
elif self.config.get("checkpoints") and HuggingFaceLoader.validate_concept(
481+
self.checkpoint_path):
481482
labels = HuggingFaceLoader.fetch_labels(self.checkpoint_path)
482483
logger.info(f"Found {len(labels)} concepts from the model checkpoints.")
483484
# sort the concepts by id and then update the config file

clarifai/runners/utils/loader.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,18 @@ def validate_config(checkpoint_path: str):
162162
return os.path.exists(checkpoint_path) and os.path.exists(
163163
os.path.join(checkpoint_path, 'config.json'))
164164

165+
@staticmethod
166+
def validate_concept(checkpoint_path: str):
167+
# check if downloaded concept exists in hf model
168+
config_path = os.path.join(checkpoint_path, 'config.json')
169+
with open(config_path, 'r') as f:
170+
config = json.load(f)
171+
172+
labels = config.get('id2label', None)
173+
if labels:
174+
return True
175+
return False
176+
165177
@staticmethod
166178
def fetch_labels(checkpoint_path: str):
167179
# Fetch labels for classification, detection and segmentation models
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import os
2+
import shutil
3+
import subprocess
4+
import sys
5+
6+
import pytest
7+
from clarifai.runners.models.model_run_locally import ModelRunLocally
8+
9+
CLARIFAI_USER_ID = os.environ["CLARIFAI_USER_ID"]
10+
CLARIFAI_PAT = os.environ["CLARIFAI_PAT"]
11+
12+
13+
@pytest.fixture
14+
def model_run_locally(dummy_models_path):
15+
"""
16+
Fixture that instantiates the ModelRunLocally class
17+
with the dummy model_path that already exists.
18+
"""
19+
return ModelRunLocally(dummy_models_path)
20+
21+
22+
@pytest.fixture
23+
def hf_model_run_locally(dummy_hf_models_path):
24+
"""
25+
Fixture that instantiates the ModelRunLocally class
26+
with the dummy model_path that already exists.
27+
"""
28+
return ModelRunLocally(dummy_hf_models_path)
29+
30+
31+
@pytest.mark.skipif(shutil.which("docker") is None, reason="Docker not installed or not in PATH.")
32+
@pytest.mark.skipif(
33+
sys.platform not in ["linux", "darwin"],
34+
reason="Test only runs on Linux and macOS because base image only supports those platforms.")
35+
def test_docker_build_and_test_container(model_run_locally):
36+
"""
37+
Test building a Docker image and running a container test using the dummy model.
38+
This test will be skipped if Docker is not installed.
39+
"""
40+
41+
# Test if Docker is installed
42+
assert model_run_locally.is_docker_installed(), "Docker not installed, skipping."
43+
44+
# Build or re-build the Docker image
45+
model_run_locally.builder.create_dockerfile()
46+
image_tag = model_run_locally._docker_hash()
47+
image_name = f"{model_run_locally.config['model']['id']}:{image_tag}"
48+
49+
if not model_run_locally.docker_image_exists(image_name):
50+
model_run_locally.build_docker_image(image_name=image_name)
51+
52+
# Run tests inside the container
53+
try:
54+
model_run_locally.test_model_container(
55+
image_name=image_name,
56+
container_name="test_clarifai_model_container",
57+
env_vars={
58+
'CLARIFAI_PAT': CLARIFAI_PAT,
59+
'CLARIFAI_API_BASE': os.environ.get('CLARIFAI_API_BASE', 'https://api.clarifai.com')
60+
})
61+
except subprocess.CalledProcessError:
62+
pytest.fail("Failed to test the model inside the docker container.")
63+
finally:
64+
# Clean up the container if it still exists
65+
if model_run_locally.container_exists("test_clarifai_model_container"):
66+
model_run_locally.stop_docker_container("test_clarifai_model_container")
67+
model_run_locally.remove_docker_container("test_clarifai_model_container")
68+
69+
# Remove the image
70+
model_run_locally.remove_docker_image(image_name)
71+
72+
73+
# Skip the test if Docker is not installed or if the platform is not Linux/macOS
74+
@pytest.mark.skipif(shutil.which("docker") is None, reason="Docker not installed or not in PATH.")
75+
@pytest.mark.skipif(
76+
sys.platform not in ["linux", "darwin"],
77+
reason="Test only runs on Linux and macOS because base image only supports those platforms.")
78+
def test_hf_docker_build_and_test_container(hf_model_run_locally):
79+
"""
80+
Test building a Docker image and running a container test using the dummy model.
81+
This test will be skipped if Docker is not installed.
82+
"""
83+
84+
# Download the checkpoints for the model
85+
hf_model_run_locally.builder.download_checkpoints()
86+
87+
# Test if Docker is installed
88+
assert hf_model_run_locally.is_docker_installed(), "Docker not installed, skipping."
89+
90+
# Build or re-build the Docker image
91+
hf_model_run_locally.builder.create_dockerfile()
92+
image_tag = hf_model_run_locally._docker_hash()
93+
image_name = f"{hf_model_run_locally.config['model']['id']}:{image_tag}"
94+
95+
if not hf_model_run_locally.docker_image_exists(image_name):
96+
hf_model_run_locally.build_docker_image(image_name=image_name)
97+
98+
# Run tests inside the container
99+
try:
100+
hf_model_run_locally.test_model_container(
101+
image_name=image_name,
102+
container_name="test_clarifai_model_container",
103+
env_vars={
104+
'CLARIFAI_PAT': CLARIFAI_PAT,
105+
'CLARIFAI_API_BASE': os.environ.get('CLARIFAI_API_BASE', 'https://api.clarifai.com')
106+
})
107+
except subprocess.CalledProcessError:
108+
pytest.fail("Failed to test the model inside the docker container.")
109+
finally:
110+
# Clean up the container if it still exists
111+
if hf_model_run_locally.container_exists("test_clarifai_model_container"):
112+
hf_model_run_locally.stop_docker_container("test_clarifai_model_container")
113+
hf_model_run_locally.remove_docker_container("test_clarifai_model_container")
114+
115+
# Remove the image
116+
hf_model_run_locally.remove_docker_image(image_name)

tests/runners/test_model_run_locally.py

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -165,46 +165,6 @@ def test_test_model_success(model_run_locally):
165165
model_run_locally.clean_up()
166166

167167

168-
# @pytest.mark.skipif(shutil.which("docker") is None, reason="Docker not installed or not in PATH.")
169-
@pytest.mark.skip(reason="Will add later after new clarifai package is released")
170-
def test_docker_build_and_test_container(model_run_locally):
171-
"""
172-
Test building a Docker image and running a container test using the dummy model.
173-
This test will be skipped if Docker is not installed.
174-
"""
175-
176-
# Test if Docker is installed
177-
assert model_run_locally.is_docker_installed(), "Docker not installed, skipping."
178-
179-
# Build or re-build the Docker image
180-
model_run_locally.builder.create_dockerfile()
181-
image_tag = model_run_locally._docker_hash()
182-
image_name = f"{model_run_locally.config['model']['id']}:{image_tag}"
183-
184-
if not model_run_locally.docker_image_exists(image_name):
185-
model_run_locally.build_docker_image(image_name=image_name)
186-
187-
# Run tests inside the container
188-
try:
189-
model_run_locally.test_model_container(
190-
image_name=image_name,
191-
container_name="test_clarifai_model_container",
192-
env_vars={
193-
'CLARIFAI_PAT': CLARIFAI_PAT,
194-
'CLARIFAI_API_BASE': os.environ.get('CLARIFAI_API_BASE', 'https://api.clarifai.com')
195-
})
196-
except subprocess.CalledProcessError:
197-
pytest.fail("Failed to test the model inside the docker container.")
198-
finally:
199-
# Clean up the container if it still exists
200-
if model_run_locally.container_exists("test_clarifai_model_container"):
201-
model_run_locally.stop_docker_container("test_clarifai_model_container")
202-
model_run_locally.remove_docker_container("test_clarifai_model_container")
203-
204-
# Remove the image
205-
model_run_locally.remove_docker_image(image_name)
206-
207-
208168
def test_hf_test_model_success(hf_model_run_locally):
209169
"""
210170
Test that test_model succeeds with the dummy model.
@@ -225,46 +185,3 @@ def test_hf_test_model_success(hf_model_run_locally):
225185
finally:
226186
# Clean up
227187
hf_model_run_locally.clean_up()
228-
229-
230-
# @pytest.mark.skipif(shutil.which("docker") is None, reason="Docker not installed or not in PATH.")
231-
@pytest.mark.skip(reason="Will add later after new clarifai package is released")
232-
def test_hf_docker_build_and_test_container(hf_model_run_locally):
233-
"""
234-
Test building a Docker image and running a container test using the dummy model.
235-
This test will be skipped if Docker is not installed.
236-
"""
237-
238-
# Download the checkpoints for the model
239-
hf_model_run_locally.builder.download_checkpoints()
240-
241-
# Test if Docker is installed
242-
assert hf_model_run_locally.is_docker_installed(), "Docker not installed, skipping."
243-
244-
# Build or re-build the Docker image
245-
hf_model_run_locally.builder.create_dockerfile()
246-
image_tag = hf_model_run_locally._docker_hash()
247-
image_name = f"{hf_model_run_locally.config['model']['id']}:{image_tag}"
248-
249-
if not hf_model_run_locally.docker_image_exists(image_name):
250-
hf_model_run_locally.build_docker_image(image_name=image_name)
251-
252-
# Run tests inside the container
253-
try:
254-
hf_model_run_locally.test_model_container(
255-
image_name=image_name,
256-
container_name="test_clarifai_model_container",
257-
env_vars={
258-
'CLARIFAI_PAT': CLARIFAI_PAT,
259-
'CLARIFAI_API_BASE': os.environ.get('CLARIFAI_API_BASE', 'https://api.clarifai.com')
260-
})
261-
except subprocess.CalledProcessError:
262-
pytest.fail("Failed to test the model inside the docker container.")
263-
finally:
264-
# Clean up the container if it still exists
265-
if hf_model_run_locally.container_exists("test_clarifai_model_container"):
266-
hf_model_run_locally.stop_docker_container("test_clarifai_model_container")
267-
hf_model_run_locally.remove_docker_container("test_clarifai_model_container")
268-
269-
# Remove the image
270-
hf_model_run_locally.remove_docker_image(image_name)

0 commit comments

Comments
 (0)