├── .gitignore ├── LICENSE ├── README.md ├── data └── patientdataV6.csv ├── doc └── images │ ├── api-1.png │ ├── api-2.png │ ├── application.png │ ├── architecture.png │ ├── auto-1.png │ ├── auto-2.png │ ├── auto-3.png │ ├── auto-4.png │ ├── auto-5.png │ ├── auto-6.png │ ├── auto-7.png │ ├── auto-8.png │ ├── auto-9.png │ ├── cf-1.png │ ├── cf-2.png │ ├── cp4d.png │ ├── deploy-1.png │ ├── deploy-2.png │ ├── deploy-3.png │ ├── deploy-4.png │ ├── deploy-5.png │ ├── deploy-6.png │ ├── deploy-7.png │ ├── pro-1.png │ ├── pro-2.png │ └── pro-3.png ├── manifest.yml └── server ├── app.js ├── env.example ├── package.json ├── public ├── favicon.ico ├── index.html ├── javascripts │ └── index.js └── stylesheets │ └── style.css └── routes └── classify.js /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | client/dist 5 | /tmp 6 | /out-tsc 7 | # Only exists if Bazel was run 8 | /bazel-out 9 | 10 | # server bits 11 | server/node_modules 12 | server/package-lock.json 13 | server/.env 14 | 15 | # dependencies 16 | /node_modules 17 | 18 | # profiling files 19 | chrome-profiler-events.json 20 | speed-measure-plugin.json 21 | 22 | # IDEs and editors 23 | /.idea 24 | .project 25 | .classpath 26 | .c9/ 27 | *.launch 28 | .settings/ 29 | *.sublime-workspace 30 | 31 | # IDE - VSCode 32 | .vscode/* 33 | !.vscode/settings.json 34 | !.vscode/tasks.json 35 | !.vscode/launch.json 36 | !.vscode/extensions.json 37 | .history/* 38 | 39 | # misc 40 | /.sass-cache 41 | /connect.lock 42 | /coverage 43 | /libpeerconnection.log 44 | npm-debug.log 45 | yarn-error.log 46 | testem.log 47 | /typings 48 | 49 | # System Files 50 | .DS_Store 51 | Thumbs.db 52 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Deploy a model to predict heart failure with Watson Machine Learning 2 | 3 | > **DISCLAIMER**: This application is used for demonstrative and illustrative purposes only and does not constitute an offering that has gone through regulatory review. 4 | 5 | This code pattern can be thought of as two distinct parts: 6 | 7 | 1. A predictive model will be built using AutoAI on IBM Cloud Pak for Data. The model is then deployed to the Watson Machine Learning service, where it can be accessed via a REST API. 8 | 9 | 2. A Node.js web app that allows a user to input some data to be scored against the previous model. 10 | 11 | When the reader has completed this Code Pattern, they will understand how to: 12 | 13 | * Build a predictive model with AutoAI on Cloud Pak for Data 14 | * Deploy the model to the IBM Watson Machine Learning service 15 | * Via a Node.js app, score some data against the model via an API call to the Watson Machine Learning service 16 | 17 | **Sample output** 18 | 19 | Here's an example of what the final web app looks like 20 | 21 | ![form](doc/images/application.png) 22 | 23 | ## Architecture 24 | 25 | 1. The developer creates a [Cloud Pak for Data](https://www.ibm.com/cloud/watson-studio) project. 26 | 1. A model is created with AutoAI by uploading some data. 27 | 1. Data is backed up and stored on Cloud Object Storage. 28 | 1. The model is deployed using the Watson Machine Learning service. 29 | 1. A [Node.js](https://nodejs.org/) web app is deployed on IBM Cloud. It calls the predictive model hosted on the Watson Machine Learning service. 30 | 1. A user visits the web app, enters their information, and the predictive model returns a response. 31 | 32 | !["architecture diagram"](doc/images/architecture.png) 33 | 34 | ## Prerequisites 35 | 36 | * An [IBM Cloud Account](https://cloud.ibm.com) 37 | * An account on [IBM Cloud Pak for Data](https://dataplatform.cloud.ibm.com/). 38 | 39 | > **NOTE**: As of 10/16/2020, the Watson Machine Learning service on IBM Cloud is only available in the Dallas, London, Frankfurt, or Tokyo regions. Not the Seoul, Frankfurt, or Sydney regions. 40 | 41 | ## Steps 42 | 43 | 1. [Create an IBM Cloud API key](#1-create-an-ibm-cloud-api-key) 44 | 1. [Create a new Cloud Pak for Data project](#2-create-a-new-cloud-pak-for-data-project) 45 | 1. [Build a model with AutoAI](#3-build-a-model-with-autoai) 46 | 1. [Deploy the model with WML](#4-deploy-the-model-with-wml) 47 | 1. [Run the Node.js application](#5-the-client-side) 48 | 49 | ### 1. Create an IBM Cloud API key 50 | 51 | To use the Watson Machine Learning service programmatically we'll need an API key. Even though this isn't used until later on, let's create one now. 52 | 53 | Navigate to and choose to create a new API key. 54 | 55 | ![create api key](doc/images/api-1.png) 56 | 57 | Give it a name and description, hit OK. Write down the API key somewhere. 58 | 59 | ![generated api key](doc/images/api-2.png) 60 | 61 | ### 2. Create a new Cloud Pak for Data project 62 | 63 | Log into IBM's [Cloud Pak for Data](https://dataplatform.cloud.ibm.com) service (formally known as Watson Studio). Once in, you'll land on the dashboard. 64 | 65 | Create a new project by clicking `Create a project`. 66 | 67 | ![new project](doc/images/cp4d.png) 68 | 69 | Choose an `Empty project`. 70 | 71 | ![empty project](doc/images/pro-1.png) 72 | 73 | Enter a `Name` and associate the project with a `Cloud Object Storage` service. 74 | 75 | ![empty project](doc/images/pro-2.png) 76 | 77 | > **NOTE**: By creating a project in Watson Studio a free tier `Object Storage` service will be created in your IBM Cloud account. Select the `Free` storage type to avoid fees. 78 | 79 | At the project dashboard click on the `Assets` tab and upload the data set associated with this repo. [`patientdataV6.csv`](https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/master/data/patientdataV6.csv) 80 | 81 | ![upload data](doc/images/pro-3.png) 82 | 83 | ### 3. Build a model with AutoAI 84 | 85 | Now we're going to build a model from the data using IBM's AutoAI. A tool that will automatically create multiple models and test them, giving us the best result. Data science made easy! 86 | 87 | Start by clicking on `Add to project` and choosing `AutoAI experiment`. 88 | 89 | ![Add to project](doc/images/auto-1.png) 90 | 91 | Give it a `Name` and specify a `Watson Machine Learning` instance. 92 | 93 | ![WML](doc/images/auto-2.png) 94 | 95 | Choose to use data from your project. 96 | 97 | ![Choose data](doc/images/auto-3.png) 98 | 99 | Choose the `patientdataV6.csv` option. 100 | 101 | ![data set](doc/images/auto-4.png) 102 | 103 | For the "What do you want to predict?" option, choose `HEARTFAILURE`. 104 | 105 | ![right column](doc/images/auto-5.png) 106 | 107 | The experiment will take a few minutes to run. Once completed hover over the top option to make the `Save as` button appear. Click it. 108 | 109 | ![experiment](doc/images/auto-6.png) 110 | 111 | Choose to save the experiment as a `Model`. You can optionally download a generated Jupyter Notebook that can be used to re-create the steps that were taken to create the model. 112 | 113 | ![save](doc/images/auto-7.png) 114 | 115 | You model will be saved. Click the dialog to view it in your project. 116 | 117 | ![dialog](doc/images/auto-8.png) 118 | 119 | Once you're at the model overview choose the button `Promote to deployment space`. 120 | 121 | ![promote](doc/images/auto-9.png) 122 | 123 | ### 4. Deploy the model with WML 124 | 125 | To promote the model to deployment you must specify a deployment space. If no space is created choose the `New space +` option to create one. This action will associate the model with the space. 126 | 127 | ![specify space](doc/images/deploy-1.png) 128 | 129 | Navigate to the space using the hamburger menu (`☰`) on the top right and choose to `View all spaces`. 130 | 131 | ![hamburger](doc/images/deploy-2.png) 132 | 133 | Click on the space you saved the model to. 134 | 135 | ![space](doc/images/deploy-3.png) 136 | 137 | Choose the deploy the model by clicking the rocket ship icon. 138 | 139 | ![deploy](doc/images/deploy-4.png) 140 | 141 | Choose the `Online` deployment option and give it a name. 142 | 143 | ![online](doc/images/deploy-5.png) 144 | 145 | Your new deployment will appear. 146 | 147 | ![new deployment](doc/images/deploy-6.png) 148 | 149 | Click on the `API reference` tab and save the `Endpoint`. We'll be using this in our application. 150 | 151 | ![endpoint](doc/images/deploy-7.png) 152 | 153 | ### 5. Run the Node.js application 154 | 155 | You can deploy this application as a Cloud Foundry application to IBM Cloud by simply clicking the button below. This option will create a deployment pipeline, complete with a hosted Git lab project and devops toolchain. 156 | 157 |

158 | 159 | Deploy to IBM Cloud 160 | 161 |

162 | 163 | You may be prompted for an *IBM Cloud API Key* during this process. Use the `Create (+)` button to auto-fill this field and the others. Click on the `Deploy` button to deploy the application. 164 | 165 | ![pipeline](doc/images/cf-1.png) 166 | 167 | Before using the application go to the `Runtime` section of the application and in the `Environment variables` tab add in your `API_KEY` and `DEPLOYMENT_URL` values from steps 1 and 4. 168 | 169 | > **TIP** Do *NOT* wrap these values with double quotes. 170 | 171 | Once updated your application will restart and you can visit the application by clicking on `Visit App URL`. 172 | 173 | ![env vars](doc/images/cf-2.png) 174 | 175 | The app is fairly self-explantory, simply fill in the data you want to score and click on the `Classify` button to test how those figures would score against our model. The model predicts that the risk of heart failure for a patient with these medical characteristics. 176 | 177 | ![risk](doc/images/application.png) 178 | 179 | ## License 180 | 181 | This code pattern is licensed under the Apache Software License, Version 2. Separate third party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the [Developer Certificate of Origin, Version 1.1 (DCO)](https://developercertificate.org/) and the [Apache Software License, Version 2](http://www.apache.org/licenses/LICENSE-2.0.txt). 182 | 183 | [Apache Software License (ASL) FAQ](http://www.apache.org/foundation/license-faq.html#WhatDoesItMEAN) 184 | -------------------------------------------------------------------------------- /doc/images/api-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/api-1.png -------------------------------------------------------------------------------- /doc/images/api-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/api-2.png -------------------------------------------------------------------------------- /doc/images/application.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/application.png -------------------------------------------------------------------------------- /doc/images/architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/architecture.png -------------------------------------------------------------------------------- /doc/images/auto-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/auto-1.png -------------------------------------------------------------------------------- /doc/images/auto-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/auto-2.png -------------------------------------------------------------------------------- /doc/images/auto-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/auto-3.png -------------------------------------------------------------------------------- /doc/images/auto-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/auto-4.png -------------------------------------------------------------------------------- /doc/images/auto-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/auto-5.png -------------------------------------------------------------------------------- /doc/images/auto-6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/auto-6.png -------------------------------------------------------------------------------- /doc/images/auto-7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/auto-7.png -------------------------------------------------------------------------------- /doc/images/auto-8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/auto-8.png -------------------------------------------------------------------------------- /doc/images/auto-9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/auto-9.png -------------------------------------------------------------------------------- /doc/images/cf-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/cf-1.png -------------------------------------------------------------------------------- /doc/images/cf-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/cf-2.png -------------------------------------------------------------------------------- /doc/images/cp4d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/cp4d.png -------------------------------------------------------------------------------- /doc/images/deploy-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/deploy-1.png -------------------------------------------------------------------------------- /doc/images/deploy-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/deploy-2.png -------------------------------------------------------------------------------- /doc/images/deploy-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/deploy-3.png -------------------------------------------------------------------------------- /doc/images/deploy-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/deploy-4.png -------------------------------------------------------------------------------- /doc/images/deploy-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/deploy-5.png -------------------------------------------------------------------------------- /doc/images/deploy-6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/deploy-6.png -------------------------------------------------------------------------------- /doc/images/deploy-7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/deploy-7.png -------------------------------------------------------------------------------- /doc/images/pro-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/pro-1.png -------------------------------------------------------------------------------- /doc/images/pro-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/pro-2.png -------------------------------------------------------------------------------- /doc/images/pro-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/doc/images/pro-3.png -------------------------------------------------------------------------------- /manifest.yml: -------------------------------------------------------------------------------- 1 | --- 2 | applications: 3 | - path: ./server 4 | name: watson-ml-tester 5 | command: npm start 6 | instances: 1 7 | memory: 256M 8 | env: 9 | API_KEY: placeholder 10 | DEPLOYMENT_URL: placeholder 11 | -------------------------------------------------------------------------------- /server/app.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2017 IBM Corp. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | /*jslint node: true*/ 18 | /*jslint es6 */ 19 | "use strict"; 20 | 21 | var express = require('express'); 22 | var path = require('path'); 23 | var favicon = require('serve-favicon'); 24 | var logger = require('morgan'); 25 | var cookieParser = require('cookie-parser'); 26 | var bodyParser = require('body-parser'); 27 | 28 | var classify = require('./routes/classify'); 29 | 30 | var app = express(); 31 | 32 | app.use(favicon(__dirname + '/public/favicon.ico')); 33 | app.use(logger('dev')); 34 | app.use(bodyParser.json()); 35 | app.use(bodyParser.urlencoded({ extended: false })); 36 | app.use(cookieParser()); 37 | app.use(express.static(path.join(__dirname, 'public'))); 38 | 39 | app.use('/classify', classify); 40 | 41 | // catch 404 and forward to error handler 42 | app.use(function(req, res, next) { 43 | var err = new Error('Not Found'); 44 | err.status = 404; 45 | next(err); 46 | }); 47 | 48 | // development error handler 49 | // will print stacktrace 50 | if (app.get('env') === 'development') { 51 | app.use(function(err, req, res, next) { 52 | res.status(err.status || 500); 53 | res.json({ 54 | message: err.message, 55 | error: err 56 | }); 57 | }); 58 | } 59 | 60 | // production error handler 61 | // no stacktraces leaked to user 62 | app.use(function(err, req, res, next) { 63 | res.status(err.status || 500); 64 | res.json({ 65 | message: err.message, 66 | error: {} 67 | }); 68 | }); 69 | 70 | 71 | const port = process.env.PORT || process.env.VCAP_APP_PORT || 3000; 72 | 73 | app.listen(port, function () { 74 | console.log("Server running on port: %d", port); 75 | }); 76 | module.exports = app; 77 | -------------------------------------------------------------------------------- /server/env.example: -------------------------------------------------------------------------------- 1 | # Replace the credentials here with your own. 2 | # Rename this file to .env before running 'npm start'. 3 | 4 | API_KEY= 5 | DEPLOYMENT_URL= 6 | -------------------------------------------------------------------------------- /server/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wml-heart-risk", 3 | "description": "An application using Watson Machine Learning to classify heart risk given some data", 4 | "version": "0.0.1", 5 | "main": "app.js", 6 | "scripts": { 7 | "start": "node app.js" 8 | }, 9 | "dependencies": { 10 | "body-parser": "^1.18.3", 11 | "cookie-parser": "~1.3.4", 12 | "debug": "^3.1.0", 13 | "dotenv": "^2.0.0", 14 | "express": "^4.16.3", 15 | "morgan": "^1.9.1", 16 | "request": "^2.61.0", 17 | "serve-favicon": "^2.5.0", 18 | "vcap_services": "^0.2.0", 19 | "xmlhttprequest": "^1.8.0" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /server/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IBM/predictive-model-on-watson-ml/416b67e58b39fd8bdf5b78f7f452c0cfafa20ff9/server/public/favicon.ico -------------------------------------------------------------------------------- /server/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | IBM Watson Machine Learning Heart Risk Example 8 | 9 | 10 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 |
23 |
24 | 29 |
30 |

31 | Use Watson Machine Learning to predict heart risk. Enter information below and see how it is classified. 32 |

33 |

34 | The Training Data: The data set used in this example was mocked up and is only for demonstrative and illustrative purposes only and does not constitute an offering that has gone through regulatory review. 35 |

36 |

37 | Fork the code in GitHub | 38 | WML Product Page | 39 | View the docs 40 |

41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 | 50 | 51 | 82 | 100 | 101 |
52 |
53 | 54 | 55 |
56 |
57 | 58 | 62 |
63 |
64 | 65 | 69 |
70 |
71 | 72 | 76 |
77 |
78 | 79 | 80 |
81 |
83 |
84 | 85 | 86 |
87 |
88 | 89 | 90 |
91 |
92 | 93 | 94 |
95 |
96 | 97 | 98 |
99 |
102 |
103 | 104 | 107 | 108 |
109 | 110 |
111 |
112 |
113 | 114 |
115 |
116 |
117 |

118 |

119 |

120 |

121 |
122 |
123 |
124 |
125 | 126 | 127 | 128 | 129 | 130 | -------------------------------------------------------------------------------- /server/public/javascripts/index.js: -------------------------------------------------------------------------------- 1 | (function() { 2 | 3 | $('.answers').hide(); 4 | $('.loading').hide(); 5 | $('#form').submit(onFormSubmit); 6 | $('#age').val('40'); 7 | $('#gender').val('M'); 8 | $('#familyhistory').val('N'); 9 | $('#smoker').val('N'); 10 | $('#exercise').val('60'); 11 | $('#cholesterol').val('10'); 12 | $('#bmi').val('20'); 13 | $('#heartbeats').val('60'); 14 | $('#palpitations').val('1'); 15 | 16 | function onFormSubmit() { 17 | var age = $('#age').val(); 18 | var gender = $('#gender').val(); 19 | var familyhistory = $('#familyhistory').val(); 20 | var smoker = $('#smoker').val(); 21 | var exercise = $('#exercise').val(); 22 | var cholesterol = $('#cholesterol').val(); 23 | var bmi = $('#bmi').val(); 24 | var heartbeats = $('#heartbeats').val(); 25 | var palpitations = $('#palpitations').val(); 26 | $('.loading').show(); 27 | $('.answers').hide(); 28 | $('.classify-btn').prop('disabled', true); 29 | $.post("/classify", {age: age, 30 | gender: gender, 31 | familyhistory: familyhistory, 32 | smoker: smoker, 33 | exercise: exercise, 34 | cholesterol: cholesterol, 35 | bmi: bmi, 36 | heartbeats: heartbeats, 37 | palpitations: palpitations 38 | }, function(data) { 39 | renderAnswer(data) 40 | }).fail(function(err) { 41 | renderAnswer(err); 42 | }); 43 | return false; 44 | } 45 | 46 | function renderAnswer(parsedResponse) { 47 | console.log(parsedResponse); 48 | 49 | if (parsedResponse.errors) { 50 | $('.answer').html('Something went wrong :-( ' + parsedResponse.errors[0].message); 51 | } else { 52 | var data = parsedResponse.predictions[0].values[0] 53 | var risk = data[0] 54 | var prediction = data[1][0] 55 | var probability = data[1][1] 56 | $('.risk').html('Heart Risk: '+risk); 57 | $('.prediction').html('Prediction: '+Math.floor(prediction*100 ).toFixed(0)+'%'); 58 | $('.probability').html('Probability: '+Math.floor(probability*100 ).toFixed(0)+'%'); 59 | } 60 | 61 | $('.classify-btn').prop('disabled', false); 62 | $('.answers').show(); 63 | $('.loading').hide(); 64 | } 65 | }()); 66 | -------------------------------------------------------------------------------- /server/public/stylesheets/style.css: -------------------------------------------------------------------------------- 1 | 2 | .container { 3 | margin-right: auto; 4 | margin-left: auto; 5 | padding-left: 15px; 6 | padding-right: 15px; 7 | } 8 | 9 | @media (min-width: 992px) { 10 | .container { 11 | width: 970px; 12 | } 13 | } 14 | 15 | .header { 16 | background: #F2FAFC; 17 | border-radius:none; 18 | border-bottom:1px solid #00B2EF; 19 | padding-top: 10px; 20 | padding-bottom:30px; 21 | margin:0 0 30px 0; 22 | } 23 | 24 | .page-header { 25 | padding-bottom: 8px; 26 | margin: 36px 0 18px; 27 | border-bottom: 1px solid #eeeeee; 28 | } 29 | 30 | .header h1 { 31 | color: #00B2EF; 32 | font-size: 50px; 33 | } 34 | 35 | .input-lg { 36 | height: 44px; 37 | } 38 | 39 | .btn-lg { 40 | border: solid 1px; 41 | } 42 | 43 | table { 44 | max-width: 100%; 45 | background-color: transparent; 46 | } 47 | th { 48 | text-align: left; 49 | } 50 | .table { 51 | width: 100%; 52 | margin-bottom: 18px; 53 | } 54 | td { 55 | padding: 8px; 56 | line-height: 1.428571429; 57 | vertical-align: top; 58 | border-top: 1px solid #eeeeee; 59 | } 60 | 61 | .row { 62 | padding-bottom: 20px; 63 | margin-left: -15px; 64 | margin-right: -15px; 65 | } 66 | 67 | .search-dropdown-menu { 68 | left: inherit; 69 | position: absolute; 70 | top: 100%; 71 | right: 0 !important; 72 | z-index: 1000; 73 | display: none; 74 | float: left; 75 | width: 800px; 76 | padding: 5px 0; 77 | margin: 2px 0 0; 78 | font-size: 14px; 79 | text-align: left; 80 | list-style: none; 81 | background-color: #fff; 82 | -webkit-background-clip: padding-box; 83 | background-clip: padding-box; 84 | border: 1px solid #ccc; 85 | border: 1px solid rgba(0,0,0,.15); 86 | border-radius: 4px; 87 | -webkit-box-shadow: 0 6px 12px rgba(0,0,0,.175); 88 | box-shadow: 0 6px 12px rgba(0,0,0,.175); 89 | } 90 | .search-dropdown-menu li { 91 | border-bottom: 1px solid #667 92 | } 93 | .search-dropdown-menu li, .search-dropdown-menu li a { 94 | overflow-wrap: break-word; 95 | word-wrap: break-word; 96 | white-space: normal; 97 | } 98 | 99 | .jumbotron { 100 | padding-top: 30px; 101 | padding-bottom: 30px; 102 | margin-bottom: 30px; 103 | padding-left: 20px; 104 | color: inherit; 105 | background-color: #eee; 106 | } 107 | 108 | @media screen and (min-width: 768px) { 109 | .container .jumbotron, .container-fluid .jumbotron { 110 | /* padding-right: 60px; */ 111 | padding-left: 20px; 112 | } 113 | } -------------------------------------------------------------------------------- /server/routes/classify.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2020 IBM Corp. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the 'License'); you may not 5 | * use this file except in compliance with the License. You may obtain a copy of 6 | * the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 | * License for the specific language governing permissions and limitations under 14 | * the License. 15 | */ 16 | 17 | 'use strict'; 18 | 19 | require('dotenv').config({ 20 | silent: true, 21 | }); 22 | 23 | const express = require('express'); 24 | const router = express.Router(); 25 | const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; 26 | 27 | const apiKey = process.env.API_KEY 28 | const deploymentURL = process.env.DEPLOYMENT_URL 29 | 30 | router.post('/', function(req, res, next) { 31 | classify(req, res); 32 | }); 33 | 34 | function classify(req, res) { 35 | console.log("Classifying:") 36 | console.log(req.body); 37 | 38 | getToken((req, res, err) => console.log(err), function () { 39 | let tokenResponse; 40 | try { 41 | tokenResponse = JSON.parse(this.responseText); 42 | } catch(ex) { 43 | console.log(ex) 44 | res.json({errors: [{message: "Cannot parse API token"}]}) 45 | } 46 | 47 | let values; 48 | const fields = ["AVGHEARTBEATSPERMIN","PALPITATIONSPERDAY","CHOLESTEROL","BMI","AGE","SEX","FAMILYHISTORY","SMOKERLAST5YRS","EXERCISEMINPERWEEK"] 49 | values = [[req.body.heartbeats, 50 | req.body.palpitations, 51 | req.body.cholesterol, 52 | req.body.bmi, 53 | req.body.age, 54 | req.body.gender, 55 | req.body.familyhistory, 56 | req.body.smoker, 57 | req.body.exercise]] 58 | let payload_json = {"input_data": [{"fields": fields, "values": values}]} 59 | let payload = JSON.stringify(payload_json); 60 | 61 | console.log("input data"); 62 | console.log(payload); 63 | 64 | apiPost(deploymentURL, tokenResponse.access_token, payload, function (resp) { 65 | let parsedPostResponse; 66 | try { 67 | parsedPostResponse = JSON.parse(this.responseText); 68 | } catch (ex) { 69 | console.log(ex) 70 | res.json({errors: [{message: "Cannot parse API token"}]}) 71 | } 72 | console.log("Scoring response"); 73 | res.json(parsedPostResponse) 74 | }, function (error) { 75 | console.log(error); 76 | res.json({errors: [{message: error}]}) 77 | }); 78 | }); 79 | 80 | } 81 | 82 | function getToken(errorCallback, loadCallback) { 83 | const req = new XMLHttpRequest(); 84 | req.addEventListener("load", loadCallback); 85 | req.addEventListener("error", errorCallback); 86 | req.open("POST", "https://iam.ng.bluemix.net/identity/token"); 87 | req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 88 | req.setRequestHeader("Accept", "application/json"); 89 | req.send("grant_type=urn:ibm:params:oauth:grant-type:apikey&apikey=" + apiKey); 90 | } 91 | 92 | function apiPost(deploymentURL, token, payload, loadCallback, errorCallback){ 93 | const oReq = new XMLHttpRequest(); 94 | oReq.addEventListener("load", loadCallback); 95 | oReq.addEventListener("error", errorCallback); 96 | oReq.open("POST", deploymentURL); 97 | oReq.setRequestHeader("Accept", "application/json"); 98 | oReq.setRequestHeader("Authorization", "Bearer " + token); 99 | oReq.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); 100 | oReq.send(payload); 101 | } 102 | 103 | module.exports = router; 104 | --------------------------------------------------------------------------------