forked from aws/amazon-sagemaker-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature]Add Online Explainability notebooks for SageMaker Clarify (a…
…ws#3613) * Add Online Explainability notebooks for SageMaker Clarify * Correcting text in clean-up sections of online explainability example notebooks * Updating install commands for captum and sagemaker pypy packages * debug captum installation * change instance type Co-authored-by: Aaron Markham <[email protected]> Co-authored-by: atqy <[email protected]> Co-authored-by: atqy <[email protected]>
- Loading branch information
1 parent
56d5be1
commit bc29bb7
Showing
6 changed files
with
2,326 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
sagemaker-clarify/online_explainability/natural_language_processing/code/inference.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from io import StringIO | ||
import numpy as np | ||
import os | ||
import pandas as pd | ||
import json | ||
from transformers import AutoTokenizer, AutoModelForSequenceClassification | ||
import torch | ||
from typing import Any, Dict, List | ||
|
||
|
||
def model_fn(model_dir: str) -> Dict[str, Any]: | ||
""" | ||
Load the model for inference | ||
""" | ||
model_path = os.path.join(model_dir, "model") | ||
|
||
# Load HuggingFace tokenizer. | ||
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased") | ||
|
||
# Load HuggingFace model from disk. | ||
model = AutoModelForSequenceClassification.from_pretrained(model_path, local_files_only=True) | ||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | ||
model.to(device) | ||
model.eval() | ||
model_dict = {"model": model, "tokenizer": tokenizer} | ||
return model_dict | ||
|
||
|
||
def predict_fn(input_data: List, model: Dict) -> np.ndarray: | ||
""" | ||
Apply model to the incoming request | ||
""" | ||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | ||
tokenizer = model["tokenizer"] | ||
huggingface_model = model["model"] | ||
|
||
encoded_input = tokenizer(input_data, truncation=True, padding=True, max_length=128, return_tensors="pt") | ||
encoded_input = {k: v.to(device) for k, v in encoded_input.items()} | ||
with torch.no_grad(): | ||
output = huggingface_model(input_ids=encoded_input["input_ids"], attention_mask=encoded_input["attention_mask"]) | ||
res = torch.nn.Softmax(dim=1)(output.logits).detach().cpu().numpy()[:, 1] | ||
return res | ||
|
||
|
||
def input_fn(request_body: str, request_content_type: str) -> List[str]: | ||
""" | ||
Deserialize and prepare the prediction input | ||
""" | ||
if request_content_type == "application/json": | ||
sentences = [json.loads(request_body)] | ||
|
||
elif request_content_type == "text/csv": | ||
# We have a single column with the text. | ||
sentences = list(pd.read_csv(StringIO(request_body), header=None).values[:, 0].astype(str)) | ||
else: | ||
sentences = request_body | ||
return sentences |
3 changes: 3 additions & 0 deletions
3
sagemaker-clarify/online_explainability/natural_language_processing/code/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
transformers==4.2.1 | ||
torch==1.7.1 | ||
pandas |
Oops, something went wrong.