Add files

This commit is contained in:
Andras Schmelczer 2022-08-07 14:14:58 +02:00
commit 9630436d6b
No known key found for this signature in database
GPG key ID: 0EA1BC97D0AB076E
5 changed files with 6333 additions and 0 deletions

137
deploy_done.ipynb Normal file
View file

@ -0,0 +1,137 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# todo: export the model from train.ipynb\n",
"\n",
"# todo: copy import from train.ipynb\n",
"# todo: copy normalize() from train.ipynb\n",
"# todo: copy predict() from train.ipynb\n",
"\n",
"# todo: inject saved model into predict()\n",
"# todo: turn predict() into a GreatAI service\n",
"\n",
"# todo: log prediction into output trace"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from great_ai.utilities import clean\n",
"from great_ai import use_model, GreatAI, log_metric\n",
"import re\n",
"\n",
"def normalize(text: str) -> str:\n",
" cleaned = clean(text, convert_to_ascii=True).lower()\n",
" return re.sub(r\"[^a-z]+\", \" \", cleaned)\n",
"\n",
"@GreatAI.create\n",
"@use_model('financial-sentiment')\n",
"def predict_financial_sentiment(text: str, model):\n",
" \"\"\"Classify news articles into Negative, Neutral, or Positive classes using a linear SVM.\"\"\"\n",
" text = normalize(text)\n",
" features = model.named_steps[\"tfidfvectorizer\"].transform([text])\n",
" prediction = model.named_steps[\"sgdclassifier\"].predict(features)[0]\n",
"\n",
" explanation = [\n",
" (feature_name, weight)\n",
" for weight, feature_name in sorted(\n",
" (\n",
" (feature_weight * feature, feature_name)\n",
" for feature_name, feature_weight, feature in zip(\n",
" model.named_steps[\"tfidfvectorizer\"].get_feature_names_out(),\n",
" model.named_steps[\"sgdclassifier\"].coef_[list(model.named_steps[\"sgdclassifier\"].classes_).index(prediction)],\n",
" features.toarray()[0],\n",
" )\n",
" if feature * feature_weight != 0\n",
" ),\n",
" reverse=True,\n",
" )\n",
" ][:10]\n",
"\n",
" log_metric('prediction', prediction, disable_logging=True)\n",
"\n",
" return prediction, explanation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# todo: add integration test (copy metric derivation from train.ipynb)\n",
"\n",
"if __name__ == '__main__':\n",
" from great_ai import query_ground_truth\n",
" import matplotlib.pyplot as plt\n",
" from sklearn import metrics\n",
"\n",
" test_split = query_ground_truth('test', return_max_count=200)\n",
"\n",
" traces = predict.process_batch([t.input for t in test_split])\n",
" y_predicted = [t.output[0] for t in traces]\n",
" y_test = [t.output for t in test_split]\n",
"\n",
" %matplotlib inline\n",
" plt.rcParams[\"figure.figsize\"] = (10, 10)\n",
" plt.rcParams[\"font.size\"] = 16\n",
"\n",
" print(metrics.classification_report(y_test, y_predicted))\n",
" metrics.ConfusionMatrixDisplay.from_predictions(\n",
" y_true=y_test,\n",
" y_pred=y_predicted,\n",
" xticks_rotation=\"vertical\",\n",
" normalize=\"pred\",\n",
" values_format=\".2f\",\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# todo: serve prediction model\n",
"# todo: open dashboard\n",
"\n",
"!great-ai deploy.ipynb"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.4 64-bit",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}