Skip to content

Commit 3b31254

Browse files
committed
ci/d
1 parent 948db9f commit 3b31254

File tree

7 files changed

+52
-18
lines changed

7 files changed

+52
-18
lines changed

.github/workflows/python-package.yml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@ jobs:
1919
python-version: ["3.10", "3.11"]
2020

2121
steps:
22-
- uses: actions/checkout@v3
22+
- uses: actions/checkout@v4
2323
- name: Set up Python ${{ matrix.python-version }}
24-
uses: actions/setup-python@v3
24+
uses: actions/setup-python@v4
2525
with:
2626
python-version: ${{ matrix.python-version }}
2727
- name: Install dependencies
2828
run: |
2929
python -m pip install --upgrade pip
30-
python -m pip install flake8 pytest
31-
pip install -e .
30+
pip install -e .[test]
3231
- name: Lint with flake8
3332
run: |
3433
# stop the build if there are Python syntax errors or undefined names
@@ -37,7 +36,4 @@ jobs:
3736
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
3837
- name: Test with pytest
3938
run: |
40-
cd piedomains/tests
41-
pwd
42-
ls -ltr
43-
pytest
39+
pytest piedomains/tests -v -m "not ml"

.github/workflows/tests-macos.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
python-version: ["3.10", "3.11"]
1111

1212
steps:
13-
- uses: actions/checkout@v3
13+
- uses: actions/checkout@v4
1414

1515
- name: Set up Python ${{ matrix.python-version }}
1616
uses: actions/setup-python@v4
@@ -21,6 +21,6 @@ jobs:
2121
run: |
2222
python -m pip install --upgrade pip
2323
pip install -e .[test]
24-
pytest piedomains/tests -v
24+
pytest piedomains/tests -v -m "not ml"
2525
- name: Upload coverage report
2626
run: bash <(curl -s https://codecov.io/bash)

.github/workflows/tests-ubuntu.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
python-version: ["3.10", "3.11"]
1111

1212
steps:
13-
- uses: actions/checkout@v3
13+
- uses: actions/checkout@v4
1414

1515
- name: Set up Python ${{ matrix.python-version }}
1616
uses: actions/setup-python@v4
@@ -26,6 +26,6 @@ jobs:
2626
run: |
2727
python -m pip install --upgrade pip
2828
pip install -e .[test]
29-
pytest piedomains/tests -v
29+
pytest piedomains/tests -v -m "not ml"
3030
- name: Upload coverage report
3131
run: bash <(curl -s https://codecov.io/bash)

.github/workflows/tests-windows.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
# TOXENV: asyncio
1919

2020
steps:
21-
- uses: actions/checkout@v3
21+
- uses: actions/checkout@v4
2222

2323
- name: Set up Python ${{ matrix.python-version }}
2424
uses: actions/setup-python@v4

piedomains/piedomain.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,40 @@ def extract_html_text(cls, offline: bool, input: str, html_path: str):
503503
f.close()
504504
return domains, content
505505

506+
@classmethod
507+
def _predict_with_model(cls, model, input_data):
508+
"""
509+
Universal prediction method that handles both Keras models and TFSMLayer.
510+
511+
Args:
512+
model: Either a Keras model or TFSMLayer instance
513+
input_data: Input tensor for prediction
514+
515+
Returns:
516+
Prediction results
517+
"""
518+
tf = _get_tensorflow()
519+
if hasattr(model, 'predict'):
520+
return model.predict(input_data)
521+
else:
522+
# Handle TFSMLayer which is callable but doesn't have predict method
523+
# TFSMLayer expects keyword arguments and may return a dict
524+
if isinstance(input_data, (list, str)):
525+
# For text input (list of strings), convert to tensor and pass as keyword argument
526+
tf = _get_tensorflow()
527+
input_tensor = tf.constant(input_data)
528+
results = model(input_tensor)
529+
else:
530+
# For tensor input (images), pass as positional argument
531+
results = model(input_data)
532+
533+
# TFSMLayer may return a dict, extract the tensor if needed
534+
if isinstance(results, dict):
535+
# Get the first (and likely only) tensor value from the dict
536+
results = list(results.values())[0]
537+
538+
return results
539+
506540
@classmethod
507541
def load_model(cls, model_file_name: str, latest: bool = False):
508542
"""
@@ -706,14 +740,14 @@ def pred_shalla_cat_with_text(
706740
all_results = []
707741
for i in range(0, len(content), config.batch_size):
708742
batch_content = content[i:i + config.batch_size]
709-
batch_results = cls.model.predict(batch_content)
743+
batch_results = cls._predict_with_model(cls.model, batch_content)
710744
all_results.append(batch_results)
711745
# Clear intermediate results to free memory
712746
del batch_results
713747
results = np.concatenate(all_results, axis=0)
714748
del all_results # Free memory
715749
else:
716-
results = cls.model.predict(content)
750+
results = cls._predict_with_model(cls.model, content)
717751

718752
tf = _get_tensorflow()
719753
probs = tf.nn.softmax(results)
@@ -808,7 +842,11 @@ def pred_shalla_cat_with_images(
808842

809843
logger.info("Processing image tensors")
810844
# Extract domains for file lookups
811-
domain_list = [cls.parse_url_to_domain(item) for item in urls_or_domains]
845+
if offline_images and len(urls_or_domains) == 0:
846+
# In offline mode with no input, process all images in directory
847+
domain_list = []
848+
else:
849+
domain_list = [cls.parse_url_to_domain(item) for item in urls_or_domains]
812850
images = cls.extract_image_tensor(offline_images, domain_list, image_path)
813851
img_domains = list(images.keys())
814852
logger.info(f"Successfully processed images for {len(img_domains)} domains")
@@ -833,7 +871,7 @@ def pred_shalla_cat_with_images(
833871
for i in range(0, len(img_tensors_list), config.batch_size):
834872
batch_tensors = img_tensors_list[i:i + config.batch_size]
835873
batch_tensor_stack = tf.stack(batch_tensors)
836-
batch_results = cls.model_cv.predict(batch_tensor_stack)
874+
batch_results = cls._predict_with_model(cls.model_cv, batch_tensor_stack)
837875
all_results.append(batch_results)
838876

839877
# Clear intermediate tensors to free memory
@@ -844,7 +882,7 @@ def pred_shalla_cat_with_images(
844882
else:
845883
tf = _get_tensorflow()
846884
img_tensors = tf.stack(img_tensors_list)
847-
results = cls.model_cv.predict(img_tensors)
885+
results = cls._predict_with_model(cls.model_cv, img_tensors)
848886
del img_tensors # Free memory
849887

850888
# Clear the images dict to free memory
687 Bytes
Loading
686 Bytes
Loading

0 commit comments

Comments
 (0)