From 4b1bcd9298992bf8cabb0c9901c6f22533a98ff1 Mon Sep 17 00:00:00 2001 From: Hartorn Date: Mon, 18 Sep 2023 10:24:22 +0200 Subject: [PATCH] Handle missing tensorflow for integration tests --- python-client/tests/fixtures/imdb.py | 4 ++- .../tests/models/tensorflow/test_mnist.py | 13 ++++---- .../tensorflow/test_text_classification.py | 24 +++++++------- .../test_text_classification_tfhub.py | 2 +- .../test_tf_auto_model_as_embedding_layer.py | 31 +++++++++---------- 5 files changed, 38 insertions(+), 36 deletions(-) diff --git a/python-client/tests/fixtures/imdb.py b/python-client/tests/fixtures/imdb.py index 934789e769..f9bd91e547 100644 --- a/python-client/tests/fixtures/imdb.py +++ b/python-client/tests/fixtures/imdb.py @@ -1,7 +1,6 @@ from pathlib import Path import pytest -from keras.utils import text_dataset_from_directory, get_file batch_size = 32 seed = 42 @@ -9,6 +8,9 @@ @pytest.fixture() def imdb_data(): + # Import put here to avoid failure when resolving tests + from keras.utils import text_dataset_from_directory, get_file + dataset = get_file( "aclImdb", "https://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz", diff --git a/python-client/tests/models/tensorflow/test_mnist.py b/python-client/tests/models/tensorflow/test_mnist.py index 27f53237c1..3982884825 100644 --- a/python-client/tests/models/tensorflow/test_mnist.py +++ b/python-client/tests/models/tensorflow/test_mnist.py @@ -1,18 +1,19 @@ -import tensorflow as tf -from tensorflow import keras import pytest -from giskard import Dataset + import tests.utils +from giskard import Dataset from giskard.models.tensorflow import TensorFlowModel +tf = pytest.importorskip("tensorflow") + # Define a simple sequential model def create_model(): model = tf.keras.Sequential( [ - keras.layers.Dense(512, activation="relu", input_shape=(784,)), - keras.layers.Dropout(0.2), - keras.layers.Dense(10, activation="softmax"), + tf.keras.layers.Dense(512, activation="relu", input_shape=(784,)), + tf.keras.layers.Dropout(0.2), + tf.keras.layers.Dense(10, activation="softmax"), ] ) diff --git a/python-client/tests/models/tensorflow/test_text_classification.py b/python-client/tests/models/tensorflow/test_text_classification.py index ac961c6c77..27bb59e567 100644 --- a/python-client/tests/models/tensorflow/test_text_classification.py +++ b/python-client/tests/models/tensorflow/test_text_classification.py @@ -1,15 +1,15 @@ import pandas as pd import pytest -from tensorflow.keras import layers -import tensorflow as tf import tests.utils from giskard import Dataset from giskard.models.tensorflow import TensorFlowModel +tf = pytest.importorskip("tensorflow") @pytest.mark.slow def test_text_classification_1d_output(imdb_data): + raw_train_ds, raw_test_ds = imdb_data test_dataset = {"Review": [], "Label": []} @@ -36,11 +36,11 @@ def test_text_classification_1d_output(imdb_data): model = tf.keras.Sequential( [ - layers.Embedding(max_features + 1, embedding_dim), - layers.Dropout(0.2), - layers.GlobalAveragePooling1D(), - layers.Dropout(0.2), - layers.Dense(1), + tf.keras.layers.Embedding(max_features + 1, embedding_dim), + tf.keras.layers.Dropout(0.2), + tf.keras.layers.GlobalAveragePooling1D(), + tf.keras.layers.Dropout(0.2), + tf.keras.layers.Dense(1), ] ) @@ -101,11 +101,11 @@ def test_text_classification_2d_output(imdb_data): model = tf.keras.Sequential( [ - layers.Embedding(max_features + 1, embedding_dim), - layers.Dropout(0.2), - layers.GlobalAveragePooling1D(), - layers.Dropout(0.2), - layers.Dense(2), + tf.keras.layers.Embedding(max_features + 1, embedding_dim), + tf.keras.layers.Dropout(0.2), + tf.keras.layers.GlobalAveragePooling1D(), + tf.keras.layers.Dropout(0.2), + tf.keras.layers.Dense(2), tf.keras.layers.Softmax(), ] ) diff --git a/python-client/tests/models/tensorflow/test_text_classification_tfhub.py b/python-client/tests/models/tensorflow/test_text_classification_tfhub.py index cc30afb2ca..f7a1f02544 100644 --- a/python-client/tests/models/tensorflow/test_text_classification_tfhub.py +++ b/python-client/tests/models/tensorflow/test_text_classification_tfhub.py @@ -2,13 +2,13 @@ import pandas as pd import pytest -import tensorflow as tf from sklearn import model_selection import tests.utils from giskard import Dataset from giskard.models.tensorflow import TensorFlowModel +tf = pytest.importorskip("tensorflow") def test_text_classification_tfhub(): hub = pytest.importorskip("tensorflow_hub") diff --git a/python-client/tests/models/tensorflow/test_tf_auto_model_as_embedding_layer.py b/python-client/tests/models/tensorflow/test_tf_auto_model_as_embedding_layer.py index 0e2d272059..47dc191f93 100644 --- a/python-client/tests/models/tensorflow/test_tf_auto_model_as_embedding_layer.py +++ b/python-client/tests/models/tensorflow/test_tf_auto_model_as_embedding_layer.py @@ -1,16 +1,16 @@ -from transformers import AutoTokenizer, TFAutoModel - -from giskard import Dataset +import logging -import pandas as pd -import tensorflow as tf import numpy as np -import logging +import pandas as pd import pytest +from transformers import AutoTokenizer, TFAutoModel import tests.utils +from giskard import Dataset from giskard.models.tensorflow import TensorFlowModel +tf = pytest.importorskip("tensorflow") + logging.basicConfig(level=logging.INFO) @@ -96,16 +96,6 @@ def get_inputs(tokenizer, sentences, max_length): return inputs -pd.set_option("display.max_colwidth", None) - -models = {"complaints": "comp_debiased_10"} -special_tokens = [] -max_length = {"complaints": 64} -intent = "complaints" -tokenizer, transformer_model = load_transformer_models("distilbert-base-multilingual-cased", special_tokens) -model = get_model(max_length.get(intent), transformer_model, num_labels=1, name_model=models.get(intent)) - - @pytest.mark.skip( reason="Loading must be customised to take care of loading the TF and embedded huggingFace model correctly" ) @@ -126,6 +116,15 @@ def test_tf_auto_model_as_embedding_layer(): "The app developers are homosexual": 0, } + pd.set_option("display.max_colwidth", None) + + models = {"complaints": "comp_debiased_10"} + special_tokens = [] + max_length = {"complaints": 64} + intent = "complaints" + tokenizer, transformer_model = load_transformer_models("distilbert-base-multilingual-cased", special_tokens) + model = get_model(max_length.get(intent), transformer_model, num_labels=1, name_model=models.get(intent)) + data = pd.DataFrame(columns=["text", "label"]) data.loc[:, "text"] = data_dict.keys() data.loc[:, "label"] = data_dict.values()