Created
April 29, 2022 06:30
-
-
Save rtindru/0d894971befe746249c7c85cf5c2b90a to your computer and use it in GitHub Desktop.
Step 5: Measure latency with Prometheus
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 | |
# Now let's modify our service to use prometheus to measure latency | |
import bentoml | |
from bentoml.frameworks.sklearn import SklearnModelArtifact | |
from bentoml.service.artifacts.common import PickleArtifact | |
from bentoml.adapters import JsonInput | |
import sentry_sdk | |
import logging | |
from sentry_sdk.integrations.logging import LoggingIntegration | |
# All of this is already happening by default! | |
sentry_logging = LoggingIntegration( | |
level=logging.INFO, # Capture info and above as breadcrumbs | |
event_level=logging.ERROR # Send errors as events | |
) | |
sentry_sdk.init( | |
dsn="https://[email protected]/934290", | |
integrations=[sentry_logging] | |
) | |
# Edit 1: Import prometheus | |
from prometheus_client import Summary | |
REQUEST_TIME = Summary('request_processing_time', 'Time spend processing request') | |
@bentoml.artifacts([PickleArtifact('model')]) | |
@bentoml.env(infer_pip_packages=True) | |
class SKSentimentAnalysis(bentoml.BentoService): | |
sentiment_names = { | |
0: "very negative", | |
1: "somewhat negative", | |
2: "neutral", | |
3: "somewhat positive", | |
4: "very positive", | |
} | |
# Edit 2: Monitor request time on the API | |
@REQUEST_TIME.time() | |
@bentoml.api(input=JsonInput()) | |
def predict(self, parsed_json): | |
""" | |
Sentiment prediction API service | |
Expected input format: | |
{"tweet": "Tweet text to predict the sentiment..."} | |
Output format: | |
{"sentiment_score": 4, "sentiment": "Very Positive", "tweet": "Tweet text to predict the sentiment..."} | |
""" | |
try: | |
texts = parsed_json | |
if len(texts) == 12: | |
import time | |
time.sleep(5) | |
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 | |
except: | |
sentry_sdk.capture_exception() | |
return "error" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment