Skip to content

Instantly share code, notes, and snippets.

@edoakes
Last active August 25, 2021 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save edoakes/90731fdc168ea2b54c7eb28bbd79b522 to your computer and use it in GitHub Desktop.
Save edoakes/90731fdc168ea2b54c7eb28bbd79b522 to your computer and use it in GitHub Desktop.
import joblib
import s3fs
import sklearn
@serve.deployment(route_prefix="/sentiment", name="sentiment-deployment")
class SentimentDeployment:
def __init__(self):
fs = s3fs.S3FileSystem(anon=True)
with fs.open('ray-serve-blog/unigram_vectorizer.joblib', 'rb') as f:
self.vectorizer = joblib.load(f)
with fs.open('ray-serve-blog/unigram_tf_idf_transformer.joblib', 'rb') as f:
self.preprocessor = joblib.load(f)
with fs.open('ray-serve-blog/unigram_tf_idf_classifier.joblib', 'rb') as f:
self.classifier = joblib.load(f)
async def __call__(self, request):
data = await request.body()
vectorized = self.vectorizer.transform([str(data)])
transformed = self.preprocessor.transform(vectorized)
[result] = self.classifier.predict(transformed)
if result == 1:
return 'POSITIVE'
else:
return 'NEGATIVE'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment