# Path: tensorflow_model_remediation/tools/tutorials_utils/min_diff_keras_utils.py
# TEXT_FEATURE = 'comment_text'
# LABEL = 'toxicity'
# def download_and_process_civil_comments_data():
# def _create_embedding_layer(hub_url):
# def create_keras_sequential_model(
#     hub_url='https://tfhub.dev/google/tf2-preview/nnlm-en-dim128/1',
#     cnn_filter_sizes=[128, 128, 128],
#     cnn_kernel_sizes=[5, 5, 5],
#     cnn_pooling_sizes=[5, 5, 40]):


import csv
import os
import tempfile
import unittest.mock as mock
import tensorflow as tf
from tensorflow_model_remediation.tools.tutorials_utils import min_diff_keras_utils

  def _write_csv(self, examples):
    filename = os.path.join(tempfile.mkdtemp(), 'input.csv')
    with open(filename, 'w', newline='') as csvfile:
      csvwriter = csv.writer(csvfile, delimiter=',')
      for example in examples:
        csvwriter.writerow(example)

    return filename

  @mock.patch(
      'tensorflow_model_remediation.tools.tutorials_utils.min_diff_keras_utils._create_embedding_layer',
      autospec=True)
  @mock.patch('tensorflow.keras.utils.get_file', autospec=True)
  def test_download_and_process_civil_comments_data_and_create_model(
      self, mock_get_file, mock__create_embedding_layer):

    # First test download_and_process_civil_comments_data.  Mock out the
    # download.
    filename = self._write_csv(
        self._create_example_csv(use_fake_embedding=True))
    mock_get_file.return_value = filename
    data_train, _, _, labels_train, _ = min_diff_keras_utils.download_and_process_civil_comments_data(
    )

    self.assertEqual(mock_get_file.call_count, 3)

    # Undo the string interpretation of the text_feature, since we are mocking
    # out the embedding layer in the following model testing.
    data_train[min_diff_keras_utils.TEXT_FEATURE] = data_train[

# Path: tensorflow_model_remediation/tools/tutorials_utils/min_diff_keras_utils.py
# TEXT_FEATURE = 'comment_text'
# LABEL = 'toxicity'
# def download_and_process_civil_comments_data():
# def _create_embedding_layer(hub_url):
# def create_keras_sequential_model(
#     hub_url='https://tfhub.dev/google/tf2-preview/nnlm-en-dim128/1',
#     cnn_filter_sizes=[128, 128, 128],
#     cnn_kernel_sizes=[5, 5, 5],
#     cnn_pooling_sizes=[5, 5, 40]):


# Path: tensorflow_model_remediation/tools/tutorials_utils/min_diff_keras_utils_test.py
import csv
import os
import tempfile
import unittest.mock as mock
import tensorflow as tf
from tensorflow_model_remediation.tools.tutorials_utils import min_diff_keras_utils


  def _write_csv(self, examples):
    filename = os.path.join(tempfile.mkdtemp(), 'input.csv')
    with open(filename, 'w', newline='') as csvfile:
      csvwriter = csv.writer(csvfile, delimiter=',')
      for example in examples:
        csvwriter.writerow(example)

    return filename

  @mock.patch(
      'tensorflow_model_remediation.tools.tutorials_utils.min_diff_keras_utils._create_embedding_layer',
      autospec=True)
  @mock.patch('tensorflow.keras.utils.get_file', autospec=True)
  def test_download_and_process_civil_comments_data_and_create_model(
      self, mock_get_file, mock__create_embedding_layer):

    # First test download_and_process_civil_comments_data.  Mock out the
    # download.
    filename = self._write_csv(
        self._create_example_csv(use_fake_embedding=True))
    mock_get_file.return_value = filename
    data_train, _, _, labels_train, _ = min_diff_keras_utils.download_and_process_civil_comments_data(
    )

    self.assertEqual(mock_get_file.call_count, 3)

    # Undo the string interpretation of the text_feature, since we are mocking
    # out the embedding layer in the following model testing.
    data_train[min_diff_keras_utils.TEXT_FEATURE] = data_train[

        min_diff_keras_utils.TEXT_FEATURE].astype(float)