Skip to content

Instantly share code, notes, and snippets.

View Steboss89's full-sized avatar

Stefano Bosisio Steboss89

View GitHub Profile
@Steboss89
Steboss89 / matmul.py
Created October 14, 2022 15:20
Simple matrix multiplication in Python
def matmul(mat1, mat2, mat3):
r""" Function to multiply mat1 and mat2
returns mat3
Parameters
---------
mat1: np.array, matrix A
mat2: np.array, matrix B
mat3: np.array, empty matrix C
Return
@Steboss89
Steboss89 / dashboard.py
Created September 19, 2022 08:58
Streamlit Dashboard for calling the SageMaker endpoint
import streamlit as st
# define a block where the tweet can be written
text = st.text_input("Insert tweet here")
# codify the input text in a compatible SageMaker json
input_df = pd.DataFrame({"text":text}, index=[0]).to_json(orient="split")
# here we are printing out results
st.subheader("Sentiment:")
@Steboss89
Steboss89 / local_call_endpoint.py
Created September 18, 2022 22:25
Call the SageMaker endpoint from local
import pandas as pd
import json
import boto3
# app name and region are global variables
global app_name
global region
app_name = 'NaiveBayesTest'
region = "eu-west-1"
@Steboss89
Steboss89 / mlflow_deploy_sagemaker.py
Created September 18, 2022 21:47
Deploy to a SageMaker endpoint
import mlflow
import os
# PARSE YOUR MLFLOW INFO
# image build and push by mlflow
image_uri = "230178520806.dkr.ecr.eu-west-1.amazonaws.com/mlflow-pyfunc:1.28.0"
# the model uri
model_uri = "models:/SageMaker1/Staging"
# your region
@Steboss89
Steboss89 / register_model.py
Created September 18, 2022 20:58
Register a model to MLflow registry
import mlflow
run_id= "ade1040e1fd94a74b1da5ec0583fba63"
artifact_name = "model"
model_name = "NAME OF YOU MODEL"
mlflow.register_model(f"runs:/{run_id}/{artifact_name}", model_name)
@Steboss89
Steboss89 / multiple_models.sh
Created September 18, 2022 17:53
Run multiple models
#!/bin/bash
echo "Submit NB with CountVectorizer"
venv/bin/python multiple_models.py --run_name "naivebayes" --exp_name "sentiment_comparison" --model "naivebayes" --vectorizer "countvectorizer"
echo "Submit NB with TFIDF"
venv/bin/python multiple_models.py --run_name "naivebayes_tfidf" --exp_name "sentiment_comparison" --model "naivebayes" --vectorizer "tfidf"
echo "Submit LogReg with CountVectorizer"
venv/bin/python multiple_models.py --run_name "logreg" --exp_name "sentiment_comparison" --model "logreg" --vectorizer "countvectorizer"
@Steboss89
Steboss89 / general_mlflow.py
Created September 18, 2022 17:50
Set up for MLflow for general models
import mlflow
from mlflow.tracking.client import MlflowClient
# CLASS AND PIPELINE
# MAIN
# argparse ...
# set up the tracking and define the input arguments
mlflow_client = MlflowClient(tracking_uri=mlflow_tracking_uri)
@Steboss89
Steboss89 / setup_argparse.py
Created September 18, 2022 17:46
Set up a parser for input arguments
import argparse
# MAIN
parser = argparse.ArgumentParser(description='Input arguments for MLflow testing in general')
parser.add_argument('--run_name', type=str,
help='Name of the run within the experiment family')
parser.add_argument('--exp_name', type=str,
help='Name of the family experiment')
parser.add_argument('--model', type=str,
help='Model to be used: naivebayes, logreg, randomforest')
@Steboss89
Steboss89 / function_pipeline.py
Created September 18, 2022 17:40
Wrap model and preprocess to a sklearn pipeline
def training_process(model:str,
vectorizer:str):
r""" Function to create the training pipeline with cleaner and model
Parameters
----------
model: str, type of model we want to run, see get_model function
vectorizer: str, type of vectorizer, `countvectorizer` or `tfidf`
Return
@Steboss89
Steboss89 / use_mlflow.py
Last active September 18, 2022 17:01
MLflow set up
import mlflow
# all imports
# PREPROCESSING FUNCTION
# input dataframe
# clean dataframe
# set up train and test
# count vectorizer