Created
April 29, 2022 06:21
-
-
Save rtindru/756edd1542cb6aad980c4a5c8d5bbdfa to your computer and use it in GitHub Desktop.
Step 2: Package the model into an API service
This file contains hidden or 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
%%writefile sentiment_analysis_service.py | |
import bentoml | |
from bentoml.frameworks.sklearn import SklearnModelArtifact | |
from bentoml.service.artifacts.common import PickleArtifact | |
from bentoml.adapters import JsonInput | |
@bentoml.artifacts([PickleArtifact('model')]) # picke your trained model so that it can run on the server | |
@bentoml.env(pip_packages=["scikit-learn", "pandas"]) # specify the packages that your model depends on | |
class SKSentimentAnalysis(bentoml.BentoService): | |
sentiment_names = { | |
0: "very negative", | |
1: "somewhat negative", | |
2: "neutral", | |
3: "somewhat positive", | |
4: "very positive", | |
} | |
@bentoml.api(input=JsonInput()) | |
def predict(self, parsed_json): | |
""" | |
Sentiment prediction API service | |
Expected input format: | |
["Some text to predict the sentiment...", "some more text to predict sentiment"] | |
Output format: | |
{"sentiment_score": 4, "sentiment": "Very Positive", "tweet": "Tweet text to predict the sentiment..."} | |
""" | |
texts = parsed_json | |
predictions = self.artifacts.model.predict(texts) | |
res = [] | |
for idx, pred in enumerate(predictions): | |
res.append({ | |
"sentiment_score": pred, | |
"sentiment": self.sentiment_names[pred], | |
"text": texts[idx] | |
}) | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment