Skip to content

Commit 2c7c82f

Browse files
authoredMay 17, 2020
Update pytorch/sentiment-analysis example (#1064)
1 parent c07865f commit 2c7c82f

File tree

4 files changed

+9
-145
lines changed

4 files changed

+9
-145
lines changed
 

‎examples/pytorch/sentiment-analyzer/cortex.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
predictor:
55
type: python
66
path: predictor.py
7-
config:
8-
model: s3://cortex-examples/pytorch/sentiment-analyzer/export.pkl
97
compute:
108
cpu: 1
9+
# gpu: 1 # This is optional, since the API will run on CPUs (albeit slower)

‎examples/pytorch/sentiment-analyzer/fastai_sentiment_ulmfit.ipynb

-127
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
# WARNING: you are on the master branch, please refer to the examples on the branch that matches your `cortex version`
22

3-
import os
4-
import re
5-
import boto3
6-
from botocore import UNSIGNED
7-
from botocore.client import Config
8-
from fastai.text import load_learner
3+
import torch
4+
from transformers import pipeline
95

106

117
class PythonPredictor:
128
def __init__(self, config):
13-
# download the model
14-
bucket, key = re.match("s3://(.+?)/(.+)", config["model"]).groups()
15-
s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED))
16-
os.mkdir("/tmp/model")
17-
s3.download_file(bucket, key, "/tmp/model/export.pkl")
9+
device = 0 if torch.cuda.is_available() else -1
10+
print(f"using device: {'cuda' if device == 0 else 'cpu'}")
1811

19-
self.predictor = load_learner("/tmp/model")
12+
self.analyzer = pipeline(task="sentiment-analysis", device=device)
2013

2114
def predict(self, payload):
22-
prediction = self.predictor.predict(payload["text"])
23-
return prediction[0].obj
15+
return self.analyzer(payload["text"])[0]
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
fastai==1.0.60
2-
torch==1.3.1
1+
torch
2+
transformers

0 commit comments

Comments
 (0)