Skip to content

Commit 4fd8b33

Browse files
authored
update to latest watson sdk (#45)
* update to latest watson sdk * make linting happy * use predefined service name for key * drop authenticator call * Update .travis.yml
1 parent 4f879f3 commit 4fd8b33

File tree

7 files changed

+28
-40
lines changed

7 files changed

+28
-40
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
language: python
22
python:
3-
- "2.7"
43
- "3.6"
54
install:
65
- "pip install -r requirements.txt"

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ Follow the steps below for deploying the application:
131131

132132
* Once viewing the application, click the `Runtime` option on the menu and navigate to the `Environment Variables` section.
133133

134-
* Update the `CLASSIFIER_ID`, and `NATURAL_LANGUAGE_CLASSIFIER_IAM_APIKEY` variables with your `Model ID` from [Step 4](#4-train-the-nlc-model) and NLC API key from [Step 2](#2-create-ibm-cloud-services). Click `Save`.
134+
* Update the `CLASSIFIER_ID`, and `NATURAL_LANGUAGE_CLASSIFIER_APIKEY` variables with your `Model ID` from [Step 4](#4-train-the-nlc-model) and NLC API key from [Step 2](#2-create-ibm-cloud-services). Click `Save`.
135135

136136
![env vars](doc/source/images/nlc-envvars.png)
137137

@@ -172,10 +172,8 @@ The general recommendation for Python development is to use a virtual environmen
172172
# Comment out the unset environment variables
173173
# Rename this file to .env before running app.py.
174174

175-
# NATURAL_LANGUAGE_CLASSIFIER_USERNAME=<add_NLC_username>
176-
# NATURAL_LANGUAGE_CLASSIFIER_PASSWORD=<add_NLC_password>
177-
178-
NATURAL_LANGUAGE_CLASSIFIER_IAM_APIKEY=<add_NLC_iam_apikey>
175+
CLASSIFIER_ID=<add_nlc_classifier_id>
176+
NATURAL_LANGUAGE_CLASSIFIER_APIKEY=<add_nlc_apikey>
179177
```
180178

181179
* Install the app dependencies by running:

app.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,24 @@
1616

1717
from dotenv import load_dotenv
1818
from flask import Flask, render_template, request
19+
1920
from ibm_watson import NaturalLanguageClassifierV1
2021

2122
DEBUG = True
2223
app = Flask(__name__)
2324

2425
load_dotenv(os.path.join(os.path.dirname(__file__), ".env"))
25-
26-
nlc_username = os.environ.get("NATURAL_LANGUAGE_CLASSIFIER_USERNAME")
27-
nlc_password = os.environ.get("NATURAL_LANGUAGE_CLASSIFIER_PASSWORD")
28-
nlc_iam_apikey = os.environ.get("NATURAL_LANGUAGE_CLASSIFIER_IAM_APIKEY")
2926
classifier_id = os.environ.get("CLASSIFIER_ID")
3027

31-
# Use provided credentials from environment or pull from IBM Cloud VCAP
32-
if nlc_iam_apikey != "placeholder":
33-
NLC_SERVICE = NaturalLanguageClassifierV1(
34-
iam_apikey=nlc_iam_apikey
35-
)
36-
elif nlc_username != "placeholder":
37-
NLC_SERVICE = NaturalLanguageClassifierV1(
38-
username=nlc_username,
39-
password=nlc_password
40-
)
41-
else:
42-
NLC_SERVICE = NaturalLanguageClassifierV1()
28+
NLC_SERVICE = NaturalLanguageClassifierV1()
4329

4430

4531
@app.route('/')
4632
def default():
4733
classifier_info = "cannot detect classifier"
4834
if NLC_SERVICE:
49-
classifier_info = "classifier detected, using API: " + NLC_SERVICE.url
35+
classifier_info = ("classifier detected, using API: " +
36+
NLC_SERVICE.service_url)
5037
return render_template(
5138
'index.html',
5239
classifier_info=classifier_info,
@@ -58,12 +45,21 @@ def default():
5845
@app.route('/classifyhandler', methods=['GET', 'POST'])
5946
def classify_text():
6047
inputtext = request.form['classifierinput']
61-
classifier_info = NLC_SERVICE.get_classifier(classifier_id)
62-
classifier_output = NLC_SERVICE.classify(classifier_id,
63-
inputtext).get_result()
64-
icd_code, icd_output = _get_ICD_code_info(classifier_output)
65-
classifier_output = json.dumps(classifier_output, indent=4)
66-
icd_output = json.dumps(icd_output, indent=4)
48+
49+
try:
50+
classifier_info = NLC_SERVICE.get_classifier(classifier_id)
51+
classifier_output = NLC_SERVICE.classify(classifier_id,
52+
inputtext).get_result()
53+
icd_code, icd_output = _get_ICD_code_info(classifier_output)
54+
classifier_output = json.dumps(classifier_output, indent=4)
55+
icd_output = json.dumps(icd_output, indent=4)
56+
except Exception:
57+
classifier_info = ("error from classifier service, "
58+
"check if credentials are set")
59+
classifier_output = ""
60+
icd_code = ""
61+
icd_output = ""
62+
6763
return render_template(
6864
'index.html',
6965
classifier_info=classifier_info,

doc/source/images/nlc-envvars.png

142 KB
Loading

env.example

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,5 @@
22
# Comment out the unset environment variables
33
# Rename this file to .env before running app.py.
44

5-
CLASSIFIER_ID=<add_NLC_classifier_id>
6-
NATURAL_LANGUAGE_CLASSIFIER_USERNAME=<add_NLC_username>
7-
NATURAL_LANGUAGE_CLASSIFIER_PASSWORD=<add_NLC_password>
8-
9-
NATURAL_LANGUAGE_CLASSIFIER_IAM_APIKEY=<add_NLC_iam_apikey>
5+
CLASSIFIER_ID=<add_nlc_classifier_id>
6+
NATURAL_LANGUAGE_CLASSIFIER_APIKEY=<add_nlc_apikey>

manifest.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@ applications:
88
buildpack: python_buildpack
99
env:
1010
CLASSIFIER_ID: placeholder
11-
NATURAL_LANGUAGE_CLASSIFIER_USERNAME: placeholder
12-
NATURAL_LANGUAGE_CLASSIFIER_PASSWORD: placeholder
13-
NATURAL_LANGUAGE_CLASSIFIER_IAM_APIKEY: placeholder
11+
NATURAL_LANGUAGE_CLASSIFIER_APIKEY: placeholder

requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
python-dotenv
2-
Flask
3-
ibm-watson
1+
Flask==1.1.1
2+
ibm-watson==4.2.1
3+
python-dotenv==0.10.5

0 commit comments

Comments
 (0)