File size: 5,467 Bytes
fcbc617 fbee800 fcbc617 fbee800 fcbc617 95af55b 8b59fc6 fbee800 fcbc617 fbee800 fcbc617 fbee800 fcbc617 fbee800 fcbc617 fbee800 fcbc617 fbee800 fcbc617 fbee800 fcbc617 fbee800 fcbc617 fbee800 fcbc617 fbee800 fcbc617 fbee800 fcbc617 fbee800 fcbc617 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
import os
import sklearn
import xgboost
import dill as pickle
import pandas as pd
from pathlib import Path
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.pipeline import Pipeline
from sklearn.metrics import classification_report, f1_score, accuracy_score, roc_auc_score, RocCurveDisplay, roc_curve, auc, confusion_matrix, ConfusionMatrixDisplay
from skops import hub_utils
from skops import card
# load in in the Suicide Detection dataset
# accessible at https://www.kaggle.com/datasets/nikhileswarkomati/suicide-watch
df = pd.read_csv(
"Suicide_Detection.csv",
usecols=["text", "class"],
dtype= {"text":str,"class":str}
)
# separate text and target class
X = df['text'].to_list()
y = df['class'].apply(lambda x: 1 if x == 'suicide' else 0).to_list()
# construct training and testing splits
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
def preprocessor(s):
"""preprocessor for the tfidf vectorizer"""
from sklearn.feature_extraction.text import ENGLISH_STOP_WORDS
stopwords_set = set(ENGLISH_STOP_WORDS)
def filter(text):
if text == None:
return ""
words = str(text).split()
filtered_words = [word for word in words if word and word.lower() not in stopwords_set]
return " ".join(filtered_words)
return filter(s)
# construct the model pipeline
model = Pipeline([
('tfidf', TfidfVectorizer(preprocessor=preprocessor, ngram_range=(1, 3), min_df=100)),
('classifier', xgboost.XGBClassifier())
], verbose=True)
# fit the model using the training split
model.fit(X_train, y_train)
# use the trained model to make predictions on the testing set
y_pred = model.predict(X_test)
y_pred_proba = model.predict_proba(X_test)[:, 1]
# save the trained model
model_filename = "model.pkl"
with open(model_filename, mode="bw") as f:
pickle.dump(model, file=f)
local_repo = Path("suicide-detector")
# construct the hugging face page
hub_utils.init(
model=model_filename,
requirements=[f"scikit-learn={sklearn.__version__}", f"xgboost={xgboost.__version__}"],
dst=str(local_repo),
task="text-classification",
data=X_test,
)
# made a header card from the metadata
model_card = card.Card(model, metadata=card.metadata_from_config(local_repo))
# add license
model_card.metadata.license = "mit"
model_description = """
Suicide Detection text classification model.
PYTHON 3.10 ONLY
"""
model_card.add(**{"Model description": model_description})
model_card.delete("Model description/Intended uses & limitations")
model_card.delete("Model Card Contact")
model_card.delete("Citation")
# model_card.delete("Evaluation Results")
model_card.delete("Model Card Authors")
training_procedure = """
Trained using 0.7 of the the Suicide and Depression Detection dataset (https://www.kaggle.com/datasets/nikhileswarkomati/suicide-watch)
The model vectorises each text using a trained tfidf vectorizer and then classifies using xgboost.
See main.py for further details.
"""
model_card.add(**{"Model description/Training Procedure": training_procedure})
# add description of how the model was evaluated
eval_descr = (
"The model was evaluated on a 0.3 holdout split using f1 score, accuracy, confusion matrix and ROC curves."
)
model_card.add(**{"Model Evaluation": eval_descr})
# compute model evaluation metrics and add details to the hugging face model card
accuracy = accuracy_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred, average="micro")
cm = confusion_matrix(y_test, y_pred, labels=model.classes_)
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=model.classes_)
disp.plot()
disp.figure_.savefig(local_repo / "confusion_matrix.png")
fpr, tpr, thresholds = roc_curve(y_test, y_pred_proba)
roc_auc = auc(fpr, tpr)
disp = RocCurveDisplay(fpr=fpr, tpr=tpr, roc_auc=roc_auc)
disp.plot()
disp.figure_.savefig(local_repo / "roc_curve.png")
clf_report = classification_report(
y_test, y_pred, output_dict=True, target_names=["not suicide", "suicide"]
)
model_card.add_metrics(**{"accuracy": accuracy, "f1 score": f1,"ROC AUC": roc_auc})
model_card.add_plot(**{"Model Evaluation/Confusion matrix": "confusion_matrix.png"})
model_card.add_plot(**{"Model Evaluation/ROC Curve": "roc_curve.png"})
clf_report = pd.DataFrame(clf_report).T.reset_index()
model_card.add_table(
**{
"Classification Report": clf_report,
},
)
get_started_code = """
```python
import sklearn
import dill as pickle
from skops import hub_utils
from pathlib import Path
suicide_detector_repo = Path("./suicide-detector")
hub_utils.download(
repo_id="AndyJamesTurner/suicideDetector",
dst=suicide_detector_repo
)
with open(suicide_detector_repo / "model.pkl", 'rb') as file:
clf = pickle.load(file)
classification = clf.predict(["I want to kill myself"])[0]
```
"""
authors = """
This model was created by the following authors:
* Andy Turner
"""
# add additional details to the page including
# model description, getting started guide, and author
model_card.add(**{
"How to Get Started with the Model": get_started_code,
"Model Authors": authors
}
)
# construct a readme from the model card
model_card.save(local_repo / "README.md")
# add this file to the repo to document how it was constructed
hub_utils.add_files(
os.path.realpath(__file__),
dst=str(local_repo),
) |