Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
129 changes: 129 additions & 0 deletions example_notebooks/Churn-Prediction/inference.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Import necessary libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from autogluon.tabular import TabularPredictor\n",
"from sqlalchemy import create_engine"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Specify the path to the test dataset"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cloud_data_path = \"s3://your-aws-bucket/example\"\n",
"test_data_path = f\"{cloud_data_path}/Telco_customer_churn_test.csv\""
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Load the trained model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import s3fs\n",
"\n",
"def download_model():\n",
" fs = s3fs.S3FileSystem()\n",
"\n",
" local_file_path = 'models'\n",
" remote_file_path =f\"s3://your-aws-bucket/demo/{local_file_path}\"\n",
"\n",
" # Download the file\n",
" fs.get(remote_file_path, local_file_path)\n",
"\n",
" return 'success'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"download_model()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Generate predictions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"predictor = TabularPredictor.load('./models/',require_version_match=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"engine = create_engine('postgresql://superset:superset@example/superset')"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Write the predictions to the database"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_data_filter.to_sql('churn_predictions', con=engine , if_exists = 'replace')"
]
}
],
"metadata": {
"language_info": {
"name": "python"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
7 changes: 7 additions & 0 deletions example_notebooks/Churn-Prediction/pipeline.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pipeline:
name: "Churn_Prediction_Pipeline"
tasks:
- name: "train"
type: "jupyter notebook"
notebook_path: "Churn-Prediction/inference.ipynb"
notebook_output_path: "inference_output.ipynb"
1 change: 1 addition & 0 deletions example_notebooks/Churn-Prediction/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dataset is detailed (here)[https://community.ibm.com/community/user/businessanalytics/blogs/steven-macko/2019/07/11/telco-customer-churn-1113]
12 changes: 12 additions & 0 deletions example_notebooks/Churn-Prediction/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
seaborn==0.12.2
mxnet==1.8.0
torch==1.13.1+cpu
torchvision==0.14.1+cpu
autogluon==0.7.0
imblearn==0.0
openpyxl==3.1.2
fastapi==0.70.0
uvicorn==0.15.0
boto3==1.17.106
autogluon.eda==0.7.0
numpy==1.24.3
4,070 changes: 4,070 additions & 0 deletions example_notebooks/Churn-Prediction/train.ipynb

Large diffs are not rendered by default.

49 changes: 49 additions & 0 deletions example_notebooks/Churn-Prediction/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
helper function

"""
def download_from_cloud(local_file_name, remote_file_name):
"""
Upload a file to gcs bucket or s3 bucket.
"""
import os

cloud_name = remote_file_name.split('://')[0]
if cloud_name =='gs':
import gcsfs
fs = gcsfs.GCSFileSystem(project=os.environ['GCP_PROJECT'])
elif cloud_name =='s3':
import s3fs
fs = s3fs.S3FileSystem()
else:
raise NameError(f'cloud name {cloud_name} unknown')
try:
print(f'Downloading from {remote_file_name} to {local_file_name}')
fs.get(remote_file_name, local_file_name)
print("done downloading!")
except Exception as exp:
print(f"Download failed: {exp}")
return

def upload_to_cloud(local_file_name, remote_file_name):
"""
Upload a file to gcs bucket or s3 bucket.
"""
import os
cloud_name = remote_file_name.split('://')[0]
if cloud_name =='gs':
import gcsfs
fs = gcsfs.GCSFileSystem(project=os.environ['GCP_PROJECT'])
elif cloud_name =='s3':
import s3fs
fs = s3fs.S3FileSystem()
else:
raise NameError(f'cloud name {cloud_name} unknown')
try:
print(f'Uploading from {local_file_name} to {remote_file_name}')
fs.put(local_file_name, remote_file_name)
print("done uploading!")
except Exception as exp:
print(f"Uploading failed: {exp}")

return fs