Silver Rubanza
12/30/2024, 1:47 PMimport numpy as np
from numpy import random
from tqdm import tqdm
import fastbook
fastbook.setup_book()
from fastbook import *
from fastai.tabular.all import *
import pandas as pd
import matplotlib.pyplot as plt
from fastai.imports import *
np.set_printoptions(linewidth=130)
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from pathlib import Path
import os
import xgboost as xgb
from xgboost import plot_importance
from xgboost import XGBClassifier
import warnings
import gc
import pickle
from joblib import dump, load
import typing as t
import bentoml
torch.manual_seed(42)
path = Path('data/')
train_df = pd.read_csv(path/'train.csv',index_col='id')
test_df = pd.read_csv(path/'test.csv',index_col='id')
sub_df = pd.read_csv(path/'sample_submission.csv')
cont_names,cat_names = cont_cat_split(train_df, dep_var='Depression')
splits = RandomSplitter(valid_pct=0.2)(range_of(train_df))
to = TabularPandas(train_df, procs=[Categorify, FillMissing,Normalize],
#to = TabularPandas(train_df, procs=[Categorify,Normalize],
cat_names = cat_names,
cont_names = cont_names,
y_names='Depression',
y_block=CategoryBlock(),
splits=splits)
dls = to.dataloaders(bs=64)
#dls = to.dataloaders(bs=1024)
test_dl = dls.test_dl(test_df)
X_train, y_train = to.train.xs, to.train.ys.values.ravel()
X_test, y_test = to.valid.xs, to.valid.ys.values.ravel()
xgb_model = xgb.XGBClassifier()
xgb_model = xgb_model.fit(X_train, y_train)
#xgb_preds = tensor(xgb_model.predict(test_dl.xs))
#xgb_preds_x = tensor(xgb_model.predict(X_test))
bentoml.xgboost.save_model("mental_health_v1", xgb_model)
My test.py file which returns
tensor([0, 0, 0, ..., 0, 1, 0]) when i run python test.py on my terminal
Looks like below
import bentoml
import xgboost as xgb
import pandas as pd
from pathlib import Path
import fastbook
fastbook.setup_book()
from fastbook import *
from fastai.tabular.all import *
# Load the model by setting the model tag
booster = bentoml.xgboost.load_model("mental_health_v1:hsohdawgwkno6aav")
path = Path('data/')
test_df = pd.read_csv(path/'test.csv',index_col='id')
train_df = pd.read_csv(path/'train.csv',index_col='id')
cont_names,cat_names = cont_cat_split(train_df, dep_var='Depression')
splits = RandomSplitter(valid_pct=0.2)(range_of(train_df))
to = TabularPandas(train_df, procs=[Categorify, FillMissing,Normalize],
#to = TabularPandas(train_df, procs=[Categorify,Normalize],
cat_names = cat_names,
cont_names = cont_names,
y_names='Depression',
y_block=CategoryBlock(),
splits=splits)
dls = to.dataloaders(bs=64)
test_dl = dls.test_dl(test_df)
res = tensor(booster.predict(test_dl.xs))
print(res)
I have tried various ways of building my predict method in my service.py file with the simplest version being something like
import os
import numpy as np
import bentoml
import fastbook
fastbook.setup_book()
from fastbook import *
from fastai.tabular.all import *
from typing import Annotated # Python 3.9 or above
from typing_extensions import Annotated # Older than 3.9
import pandas as pd
from bentoml.validators import DataframeSchema
from fastai.tabular.all import *
import xgboost as xgb
@bentoml.service(
resources={"cpu": "8"},
traffic={"timeout": 10},
)
class MentalHealthClassifier:
#retrieve the latest version of the model from the BentoML model store
bento_model = bentoml.models.get("mental_health_v1:latest")
#bento_model = BentoModel('mental_health_v1:q5kcqtf5ys3qoaav')
def __init__(self):
self.model = bentoml.xgboost.load_model(self.bento_model)
@bentoml.api
def predict(self, data:pd.DataFrame):
return bento_model.predict(data)
but nothing seems to work when i pass in some data for example
{
"Name": "Shivam",
"Gender": "Male",
"Age": 53,
"City": "Visakhapatnam",
"Working Professional or Student": "Working Professional",
"Profession": "Judge",
"Academic Pressure": 5,
"Work Pressure": 4,
"CGPA": 7.34,
"Study Satisfaction": 3,
"Job Satisfaction": 2,
"Sleep Duration": "Less than 5 hours",
"Dietary Habits": "Moderate",
"Degree": "LLB",
"Have you ever had suicidal thoughts ?": "Yes",
"Work/Study Hours": 93,
"Financial Stress": 4,
"Family History of Mental Illness": "No"
}
Any ideas on how to help with this would be highly appreciated.