Created
April 29, 2022 06:35
-
-
Save rtindru/d33fe10876c88a6fd0282ab93f97ac41 to your computer and use it in GitHub Desktop.
Step 6 Custom metric 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 custom metrics | |
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]/6197278", | |
integrations=[sentry_logging] | |
) | |
from prometheus_client import Summary | |
REQUEST_TIME = Summary('request_processing_time', 'Time spend processing request') | |
# Edit 1: Create a custom metric | |
REQUEST_TEXT_LEN = Summary('request_text_len', 'Length of texts array for inference') | |
@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", | |
} | |
@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 | |
predictions = self.artifacts.model.predict(texts) | |
res = [] | |
# Edit 2: Monitor request text lengt on the API | |
REQUEST_TEXT_LEN.observe(len(texts)) | |
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