Skip to content

Instantly share code, notes, and snippets.

@edoakes
Last active June 25, 2020 20:48
Show Gist options
  • Save edoakes/64b5082ef0b6689b3094d8c3a20d19d2 to your computer and use it in GitHub Desktop.
Save edoakes/64b5082ef0b6689b3094d8c3a20d19d2 to your computer and use it in GitHub Desktop.
import ray
from ray import serve
# Connect to the running Ray Serve instance.
ray.init(address='auto', ignore_reinit_error=True)
serve.init()
# Deploy the model.
serve.create_backend("sklearn_backend", SKLearnBackend)
serve.create_endpoint("sentiment_endpoint", backend="sklearn_backend", route="/sentiment")
import ray
from ray import serve
ray.init(address="auto", ignore_reinit_error=True)
serve.init()
serve.create_backend("pytorch_backend", PyTorchBackend)
serve.set_traffic("sentiment_endpoint", {"pytorch_backend": 1.0})
import joblib
import s3fs
class SKLearnBackend:
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)
def __call__(self, request):
vectorized = self.vectorizer.transform([request.data])
transformed = self.preprocessor.transform(vectorized)
[result] = self.classifier.predict(transformed)
if result == 1:
return 'POSITIVE'
else:
return 'NEGATIVE'
import ray
from ray import serve
ray.init(address='auto') # Connect to the running Ray cluster.
serve.init() # Start the Ray Serve processes within the Ray cluster.
import requests
input_text = "Ray Serve eases the pain of model serving"
result = requests.get("http://127.0.0.1:8000/sentiment", data=input_text).text
print("Result for '{}': {}".format(input_text, result))
from transformers import pipeline
class PyTorchBackend:
def __init__(self):
self.classifier = pipeline("sentiment-analysis")
def __call__(self, request):
[result] = self.classifier(str(request.data))
return result["label"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment