Skip to content

Instantly share code, notes, and snippets.

View rtindru's full-sized avatar

Rajtilak Indrajit rtindru

View GitHub Profile
@rtindru
rtindru / step_6_custom_metric.py
Created April 29, 2022 06:35
Step 6 Custom metric with Prometheus
%%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
@rtindru
rtindru / step_5_latency.py
Created April 29, 2022 06:30
Step 5: Measure latency with Prometheus
%%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
@rtindru
rtindru / step_4_error_handling.py
Created April 29, 2022 06:27
Step 4: Handle errors with sentry
%%writefile sentiment_analysis_service.py
# Now let's modify our service to use sentry
import bentoml
from bentoml.frameworks.sklearn import SklearnModelArtifact
from bentoml.service.artifacts.common import PickleArtifact
from bentoml.adapters import JsonInput
# Edit 1: Import sentry
@rtindru
rtindru / step_3_package_service.py
Created April 29, 2022 06:24
Step 3: Package the model service
# 1) import the custom BentoService defined above
from sentiment_analysis_service import SKSentimentAnalysis
# 2) `pack` it with required artifacts, i.e. the trained model from step 1
bento_service = SKSentimentAnalysis()
bento_service.pack('model', sentiment_lr)
# 3) save your BentoSerivce to file archive
saved_path = bento_service.save()
@rtindru
rtindru / step_2_package_api.py
Created April 29, 2022 06:21
Step 2: Package the model into an API service
%%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
@rtindru
rtindru / step_1_build_model.py
Created February 10, 2022 08:22
Step 1: Build a pandas model for sentiment classification
import numpy as np
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, roc_auc_score, roc_curve
from sklearn.pipeline import Pipeline
%%bash
@rtindru
rtindru / gaussian_float.py
Created May 21, 2021 00:07
Gaussian Floats - imagining a programming language that represents the uncertainties of the real-world.
from scipy import stats
"""
Imagining a programming language where basic types are represented as a Gaussian distribution. Would make for some pretty interesting (read: hair-pulling) programming experience, lol.
. The default assumptions in Deep Learning these days. But in a way, this may be how we should model the world around us. There are no statics or constants, everything is a probability distribution function.
"""
class GaussianFloat(object):
def __init__(self, val, scale=None):
@rtindru
rtindru / tic_tac_toe.py
Last active October 29, 2020 16:20
Python code for a game of Tic Tac Toe
BOARD = dict()
EMPTY_CHAR = "*"
WIN_CHAR = "@"
# Store all possible win combinations
WIN_COMBOS = [
(1, 2, 3),
(4, 5, 6),
(7, 8, 9),
(1, 4, 7),