Skip to content

Commit b14bf61

Browse files
Merge pull request #390 from andrewfulton9/ci-lint
Ci lint
2 parents 1266976 + e7ec424 commit b14bf61

File tree

28 files changed

+1833
-1606
lines changed

28 files changed

+1833
-1606
lines changed

.github/actions/setup-env/action.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,3 @@ runs:
2525
run: |
2626
python -m pip install --upgrade pip
2727
pip install ${{ inputs.package-root-dir }}[test]
28-

.github/workflows/ci-lint.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: pre-commit
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [master]
7+
8+
jobs:
9+
pre-commit:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4.1.7
13+
with:
14+
# Ensure the full history is fetched
15+
# This is required to run pre-commit on a specific set of commits
16+
# TODO: Remove this when all the pre-commit issues are fixed
17+
fetch-depth: 0
18+
- uses: actions/setup-python@v5.1.1
19+
with:
20+
python-version: 3.13
21+
- uses: pre-commit/action@v3.0.1

.pre-commit-config.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# pre-commit is a tool to perform a predefined set of tasks manually and/or
2+
# automatically before git commits are made.
3+
#
4+
# Config reference: https://pre-commit.com/#pre-commit-configyaml---top-level
5+
#
6+
# Common tasks
7+
#
8+
# - Register git hooks: pre-commit install --install-hooks
9+
# - Run on all files: pre-commit run --all-files
10+
#
11+
# These pre-commit hooks are run as CI.
12+
#
13+
# NOTE: if it can be avoided, add configs/args in pyproject.toml or below instead of creating a new `.config.file`.
14+
# https://pre-commit.ci/#configuration
15+
ci:
16+
autoupdate_schedule: monthly
17+
autofix_commit_msg: |
18+
[pre-commit.ci] Apply automatic pre-commit fixes
19+
20+
repos:
21+
# general
22+
- repo: https://github.com/pre-commit/pre-commit-hooks
23+
rev: v4.6.0
24+
hooks:
25+
- id: end-of-file-fixer
26+
exclude: '\.svg$'
27+
- id: trailing-whitespace
28+
exclude: '\.svg$'
29+
- id: check-json
30+
- id: check-yaml
31+
args: [--allow-multiple-documents, --unsafe]
32+
- id: check-toml
33+
34+
- repo: https://github.com/astral-sh/ruff-pre-commit
35+
rev: v0.5.6
36+
hooks:
37+
- id: ruff
38+
args: ["--fix"]
39+
- id: ruff-format

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ options {
6464
- ![Fairness Indicators on the TensorFlow blog](images/tf_full_color_primary_icon.svg)
6565

6666
### [Fairness Indicators on the TensorFlow blog](https://blog.tensorflow.org/2019/12/fairness-indicators-fair-ML-systems.html)
67-
67+
6868
---
6969

7070
[Read on the TensorFlow blog](https://blog.tensorflow.org/2019/12/fairness-indicators-fair-ML-systems.html)

docs/javascripts/mathjax.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ window.MathJax = {
1111
}
1212
};
1313

14-
document$.subscribe(() => {
14+
document$.subscribe(() => {
1515
MathJax.startup.output.clearCache()
1616
MathJax.typesetClear()
1717
MathJax.texReset()

docs/tutorials/_toc.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@ toc:
1313
path: /responsible_ai/fairness_indicators/tutorials/Fairness_Indicators_Pandas_Case_Study
1414
- title: FaceSSD example Colab
1515
path: /responsible_ai/fairness_indicators/tutorials/Facessd_Fairness_Indicators_Example_Colab
16-

fairness_indicators/example_model.py

Lines changed: 80 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121

2222
from typing import Any
2323

24-
from fairness_indicators import fairness_indicators_metrics # pylint: disable=unused-import
25-
from tensorflow import keras
2624
import tensorflow.compat.v1 as tf
2725
import tensorflow_model_analysis as tfma
26+
from tensorflow import keras
2827

28+
from fairness_indicators import fairness_indicators_metrics # noqa: F401
2929

30-
TEXT_FEATURE = 'comment_text'
31-
LABEL = 'toxicity'
32-
SLICE = 'slice'
30+
TEXT_FEATURE = "comment_text"
31+
LABEL = "toxicity"
32+
SLICE = "slice"
3333
FEATURE_MAP = {
3434
LABEL: tf.io.FixedLenFeature([], tf.float32),
3535
TEXT_FEATURE: tf.io.FixedLenFeature([], tf.string),
@@ -38,74 +38,75 @@
3838

3939

4040
class ExampleParser(keras.layers.Layer):
41-
"""A Keras layer that parses the tf.Example."""
41+
"""A Keras layer that parses the tf.Example."""
42+
43+
def __init__(self, input_feature_key):
44+
self._input_feature_key = input_feature_key
45+
self.input_spec = keras.layers.InputSpec(shape=(1,), dtype=tf.string)
46+
super().__init__()
4247

43-
def __init__(self, input_feature_key):
44-
self._input_feature_key = input_feature_key
45-
self.input_spec = keras.layers.InputSpec(shape=(1,), dtype=tf.string)
46-
super().__init__()
48+
def compute_output_shape(self, input_shape: Any):
49+
return [1, 1]
4750

48-
def compute_output_shape(self, input_shape: Any):
49-
return [1, 1]
51+
def call(self, serialized_examples):
52+
def get_feature(serialized_example):
53+
parsed_example = tf.io.parse_single_example(
54+
serialized_example, features=FEATURE_MAP
55+
)
56+
return parsed_example[self._input_feature_key]
5057

51-
def call(self, serialized_examples):
52-
def get_feature(serialized_example):
53-
parsed_example = tf.io.parse_single_example(
54-
serialized_example, features=FEATURE_MAP
55-
)
56-
return parsed_example[self._input_feature_key]
57-
serialized_examples = tf.cast(serialized_examples, tf.string)
58-
return tf.map_fn(get_feature, serialized_examples)
58+
serialized_examples = tf.cast(serialized_examples, tf.string)
59+
return tf.map_fn(get_feature, serialized_examples)
5960

6061

6162
class Reshaper(keras.layers.Layer):
62-
"""A Keras layer that reshapes the input."""
63+
"""A Keras layer that reshapes the input."""
6364

64-
def call(self, inputs):
65-
return tf.reshape(inputs, (1, 32))
65+
def call(self, inputs):
66+
return tf.reshape(inputs, (1, 32))
6667

6768

6869
class Caster(keras.layers.Layer):
69-
"""A Keras layer that reshapes the input."""
70+
"""A Keras layer that reshapes the input."""
7071

71-
def call(self, inputs):
72-
return tf.cast(inputs, tf.float32)
72+
def call(self, inputs):
73+
return tf.cast(inputs, tf.float32)
7374

7475

7576
def get_example_model(input_feature_key: str):
76-
"""Returns a Keras model for testing."""
77-
parser = ExampleParser(input_feature_key)
78-
text_vectorization = keras.layers.TextVectorization(
79-
max_tokens=32,
80-
output_mode='int',
81-
output_sequence_length=32,
82-
)
83-
text_vectorization.adapt(
84-
['nontoxic', 'toxic comment', 'test comment', 'abc', 'abcdef', 'random']
85-
)
86-
dense1 = keras.layers.Dense(
87-
32,
88-
activation=None,
89-
use_bias=True,
90-
kernel_initializer='glorot_uniform',
91-
bias_initializer='zeros',
92-
)
93-
dense2 = keras.layers.Dense(
94-
1,
95-
activation=None,
96-
use_bias=False,
97-
kernel_initializer='glorot_uniform',
98-
bias_initializer='zeros',
99-
)
100-
101-
inputs = tf.keras.Input(shape=(), dtype=tf.string)
102-
parsed_example = parser(inputs)
103-
text_vector = text_vectorization(parsed_example)
104-
text_vector = Reshaper()(text_vector)
105-
text_vector = Caster()(text_vector)
106-
output1 = dense1(text_vector)
107-
output2 = dense2(output1)
108-
return tf.keras.Model(inputs=inputs, outputs=output2)
77+
"""Returns a Keras model for testing."""
78+
parser = ExampleParser(input_feature_key)
79+
text_vectorization = keras.layers.TextVectorization(
80+
max_tokens=32,
81+
output_mode="int",
82+
output_sequence_length=32,
83+
)
84+
text_vectorization.adapt(
85+
["nontoxic", "toxic comment", "test comment", "abc", "abcdef", "random"]
86+
)
87+
dense1 = keras.layers.Dense(
88+
32,
89+
activation=None,
90+
use_bias=True,
91+
kernel_initializer="glorot_uniform",
92+
bias_initializer="zeros",
93+
)
94+
dense2 = keras.layers.Dense(
95+
1,
96+
activation=None,
97+
use_bias=False,
98+
kernel_initializer="glorot_uniform",
99+
bias_initializer="zeros",
100+
)
101+
102+
inputs = tf.keras.Input(shape=(), dtype=tf.string)
103+
parsed_example = parser(inputs)
104+
text_vector = text_vectorization(parsed_example)
105+
text_vector = Reshaper()(text_vector)
106+
text_vector = Caster()(text_vector)
107+
output1 = dense1(text_vector)
108+
output2 = dense2(output1)
109+
return tf.keras.Model(inputs=inputs, outputs=output2)
109110

110111

111112
def evaluate_model(
@@ -114,23 +115,23 @@ def evaluate_model(
114115
tfma_eval_result_path,
115116
eval_config,
116117
):
117-
"""Evaluate Model using Tensorflow Model Analysis.
118-
119-
Args:
120-
classifier_model_path: Trained classifier model to be evaluted.
121-
validate_tf_file_path: File containing validation TFRecordDataset.
122-
tfma_eval_result_path: Path to export tfma-related eval path.
123-
eval_config: tfma eval_config.
124-
"""
125-
126-
eval_shared_model = tfma.default_eval_shared_model(
127-
eval_saved_model_path=classifier_model_path, eval_config=eval_config
128-
)
129-
130-
# Run the fairness evaluation.
131-
tfma.run_model_analysis(
132-
eval_shared_model=eval_shared_model,
133-
data_location=validate_tf_file_path,
134-
output_path=tfma_eval_result_path,
135-
eval_config=eval_config,
136-
)
118+
"""Evaluate Model using Tensorflow Model Analysis.
119+
120+
Args:
121+
----
122+
classifier_model_path: Trained classifier model to be evaluted.
123+
validate_tf_file_path: File containing validation TFRecordDataset.
124+
tfma_eval_result_path: Path to export tfma-related eval path.
125+
eval_config: tfma eval_config.
126+
"""
127+
eval_shared_model = tfma.default_eval_shared_model(
128+
eval_saved_model_path=classifier_model_path, eval_config=eval_config
129+
)
130+
131+
# Run the fairness evaluation.
132+
tfma.run_model_analysis(
133+
eval_shared_model=eval_shared_model,
134+
data_location=validate_tf_file_path,
135+
output_path=tfma_eval_result_path,
136+
eval_config=eval_config,
137+
)

0 commit comments

Comments
 (0)