Skip to content

BERT + sentiment classifier example #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/sentiment/cortex.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- kind: deployment
name: sentiment

- kind: api
name: sentiment
model: s3://cortex-examples/sentiment/model.zip
request_handler: sentiment.py
2 changes: 2 additions & 0 deletions examples/sentiment/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tensorflow_hub==0.5.0
bert-tensorflow==1.0.1
7 changes: 7 additions & 0 deletions examples/sentiment/samples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"samples": [
{
"input": "Great movie!"
}
]
}
41 changes: 41 additions & 0 deletions examples/sentiment/sentiment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
import bert
from bert import run_classifier
from bert import tokenization


def create_tokenizer_from_hub_module():
"""Get the vocab file and casing info from the Hub module."""
with tf.Graph().as_default():
bert_module = hub.Module("https://tfhub.dev/google/bert_uncased_L-12_H-768_A-12/1")
tokenization_info = bert_module(signature="tokenization_info", as_dict=True)
with tf.Session() as sess:
vocab_file, do_lower_case = sess.run(
[tokenization_info["vocab_file"], tokenization_info["do_lower_case"]]
)

return bert.tokenization.FullTokenizer(vocab_file=vocab_file, do_lower_case=do_lower_case)


tokenizer = create_tokenizer_from_hub_module()
labels = ["Negative", "Positive"]


def pre_inference(sample, metadata):
input_feature = run_classifier.convert_single_example(
0, # example Idx
run_classifier.InputExample(guid="", text_a=sample["input"], text_b=None, label=0),
[0, 1], # label IDs
128, # max len
tokenizer,
)
return {"input_ids": input_feature.input_ids}


def post_inference(prediction, metadata):
return {
"sentiment": labels[prediction["response"]["labels"][0]],
"probabilities": prediction["response"]["probabilities"],
}
Loading