Skip to content

Commit 37b3674

Browse files
Merge pull request #435 from watson-developer-cloud/develop
Merge develop to master for patch Version 1.2.2
2 parents b04f75c + 018af9c commit 37b3674

10 files changed

Lines changed: 428 additions & 110 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@ test/__init__.py
6464
*~
6565

6666
.sfdx/tools/apex.db
67+
.pytest_cache/

examples/microphone-speech-to-text.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# You need to install pyaudio to run this example
22
# pip install pyaudio
33

4+
# Note that you need to record just once. You will not be able to send
5+
# more audio after the initial recording.
6+
47
from __future__ import print_function
58
import pyaudio
69
import tempfile

examples/natural_language_classifier_v1.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from __future__ import print_function
22
import json
33
import os
4+
45
# from os.path import join, dirname
56
from watson_developer_cloud import NaturalLanguageClassifierV1
67

7-
88
natural_language_classifier = NaturalLanguageClassifierV1(
99
username='YOUR SERVICE USERNAME',
1010
password='YOUR SERVICE PASSWORD')
@@ -31,6 +31,12 @@
3131
'tomorrow?')
3232
print(json.dumps(classes, indent=2))
3333

34+
if status['status'] == 'Available':
35+
collection = ['{"text":"How hot will it be today?"}', '{"text":"Is it hot outside?"}']
36+
classes = natural_language_classifier.classify_collection(
37+
classifier_id, collection)
38+
print(json.dumps(classes, indent=2))
39+
3440
delete = natural_language_classifier.delete_classifier(classifier_id)
3541
print(json.dumps(delete, indent=2))
3642

examples/visual_recognition_v3.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from __future__ import print_function
22
import json
33
from os.path import join, dirname
4-
from watson_developer_cloud import VisualRecognitionV3
4+
from watson_developer_cloud import VisualRecognitionV3, WatsonApiException
55

66
test_url = 'https://www.ibm.com/ibm/ginni/images' \
77
'/ginni_bio_780x981_v4_03162016.jpg'
88

99
visual_recognition = VisualRecognitionV3('2016-05-20', api_key='YOUR API KEY')
10+
classifier_id = 'CarsvsTrucksxDO_NOT_DELETE_771019274'
1011

1112
# with open(join(dirname(__file__), '../resources/cars.zip'), 'rb') as cars, \
1213
# open(join(dirname(__file__), '../resources/trucks.zip'), 'rb') as
@@ -18,11 +19,22 @@
1819

1920
car_path = join(dirname(__file__), '../resources/cars.zip')
2021
with open(car_path, 'rb') as images_file:
21-
parameters = json.dumps({'threshold': 0.1, 'classifier_ids': ['CarsvsTrucks_931077904', 'default']})
22+
parameters = json.dumps({'threshold': 0.1, 'classifier_ids': [classifier_id, 'default']})
2223
car_results = visual_recognition.classify(images_file=images_file,
2324
parameters=parameters)
2425
print(json.dumps(car_results, indent=2))
2526

27+
# Example with no deprecated
28+
try:
29+
with open(car_path, 'rb') as images_file:
30+
car_results = visual_recognition.classify(
31+
images_file=images_file,
32+
threshold='0.1',
33+
classifier_ids=[classifier_id, 'default'])
34+
print(json.dumps(car_results, indent=2))
35+
except WatsonApiException as ex:
36+
print(ex.httpResponse.json())
37+
2638
# print(json.dumps(visual_recognition.get_classifier('YOUR CLASSIFIER ID'),
2739
# indent=2))
2840

@@ -53,3 +65,9 @@
5365
with open(face_path, 'rb') as image_file:
5466
face_result = visual_recognition.detect_faces(images_file=image_file)
5567
print(json.dumps(face_result, indent=2))
68+
69+
#Core ml model example
70+
# model_name = '{0}.mlmodel'.format(classifier_id)
71+
# core_ml_model = visual_recognition.get_core_ml_model(classifier_id)
72+
# with open('/tmp/{0}'.format(model_name), 'wb') as fp:
73+
# fp.write(core_ml_model.content)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# coding: utf-8
2+
from unittest import TestCase
3+
import os
4+
import watson_developer_cloud
5+
import pytest
6+
import json
7+
import time
8+
9+
FIVE_SECONDS = 5
10+
11+
@pytest.mark.skipif(os.getenv('VCAP_SERVICES') is None, reason='requires VCAP_SERVICES')
12+
class TestNaturalLanguageClassifierV1(TestCase):
13+
def setUp(self):
14+
self.natural_language_classifier = watson_developer_cloud.NaturalLanguageClassifierV1(
15+
username='YOUR SERVICE USERNAME',
16+
password='YOUR SERVICE PASSWORD')
17+
self.natural_language_classifier.set_default_headers({
18+
'X-Watson-Learning-Opt-Out': '1',
19+
'X-Watson-Test': '1'
20+
})
21+
22+
# Create a classifier
23+
with open(os.path.join(os.path.dirname(__file__), '../../resources/weather_data_train.csv'), 'rb') as training_data:
24+
metadata = json.dumps({'name': 'my-classifier', 'language': 'en'})
25+
classifier = self.natural_language_classifier.create_classifier(
26+
metadata=metadata,
27+
training_data=training_data
28+
)
29+
self.classifier_id = classifier['classifier_id']
30+
31+
def tearDown(self):
32+
self.natural_language_classifier.delete_classifier(self.classifier_id)
33+
34+
def test_list_classifier(self):
35+
list_classifiers = self.natural_language_classifier.list_classifiers()
36+
assert list_classifiers is not None
37+
38+
@pytest.mark.skip(reason="The classifier takes more than a minute")
39+
def test_classify_text(self):
40+
iterations = 0
41+
while iterations < 15:
42+
status = self.natural_language_classifier.get_classifier(self.classifier_id)
43+
iterations += 1
44+
if status['status'] != 'Available':
45+
time.sleep(FIVE_SECONDS)
46+
47+
if status['status'] != 'Available':
48+
assert False, 'Classifier is not available'
49+
50+
classes = self.natural_language_classifier.classify(self.classifier_id, 'How hot will it be tomorrow?')
51+
assert classes is not None
52+
53+
collection = ['{"text":"How hot will it be today?"}', '{"text":"Is it hot outside?"}']
54+
classes = self.natural_language_classifier.classify_collection(
55+
self.classifier_id, collection)
56+
assert classes is not None

test/integration/test_visual_recognition.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ def setUp(self):
1919
'X-Watson-Test':
2020
'1'
2121
})
22+
self.classifier_id = 'CarsvsTrucksxDO_NOT_DELETE_771019274'
2223

2324
def test_classify(self):
2425
car_path = join(dirname(__file__), '../../resources/cars.zip')
2526
with open(car_path, 'rb') as images_file:
2627
parameters = json.dumps({
2728
'threshold':
2829
0.1,
29-
'classifier_ids': ['CarsvsTrucks_931077904', 'default']
30+
'classifier_ids': [self.classifier_id, 'default']
3031
})
3132
car_results = self.visual_recognition.classify(
3233
images_file=images_file, parameters=parameters)
@@ -46,7 +47,8 @@ def test_custom_classifier(self):
4647
classifier = self.visual_recognition.create_classifier(
4748
'Cars vs Trucks',
4849
cars_positive_examples=cars,
49-
negative_examples=trucks)
50+
negative_examples=trucks,
51+
)
5052

5153
assert classifier is not None
5254

@@ -58,3 +60,7 @@ def test_custom_classifier(self):
5860
assert classifiers is not None
5961

6062
output = self.visual_recognition.delete_classifier(classifier_id)
63+
64+
def test_core_ml_model(self):
65+
core_ml_model = self.visual_recognition.get_core_ml_model(self.classifier_id)
66+
assert core_ml_model.ok

test/unit/test_natural_language_classifier_v1.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,53 @@ def test_success():
8282
assert responses.calls[4].response.text == remove_response
8383

8484
assert len(responses.calls) == 5
85+
86+
@responses.activate
87+
def test_classify_collection():
88+
natural_language_classifier = watson_developer_cloud.NaturalLanguageClassifierV1(username="username",
89+
password="password")
90+
classify_collection_url = 'https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/497EF2-nlc-00/classify_collection'
91+
classify_collection_response = '{ \
92+
"classifier_id": "497EF2-nlc-00", \
93+
"url": "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/10D41B-nlc-1", \
94+
"collection": [ \
95+
{ \
96+
"text": "How hot will it be today?", \
97+
"top_class": "temperature", \
98+
"classes": [ \
99+
{ \
100+
"class_name": "temperature", \
101+
"confidence": 0.9930558798985937 \
102+
}, \
103+
{ \
104+
"class_name": "conditions", \
105+
"confidence": 0.006944120101406304 \
106+
} \
107+
] \
108+
}, \
109+
{ \
110+
"text": "Is it hot outside?", \
111+
"top_class": "temperature", \
112+
"classes": [ \
113+
{ \
114+
"class_name": "temperature", \
115+
"confidence": 1 \
116+
}, \
117+
{ \
118+
"class_name": "conditions", \
119+
"confidence": 0 \
120+
} \
121+
] \
122+
} \
123+
] \
124+
}'
125+
responses.add(responses.POST, classify_collection_url,
126+
body=classify_collection_response, status=200,
127+
content_type='application/json')
128+
129+
classifier_id = '497EF2-nlc-00'
130+
collection = ['{"text":"How hot will it be today?"}', '{"text":"Is it hot outside?"}']
131+
natural_language_classifier.classify_collection(classifier_id, collection)
132+
133+
assert responses.calls[0].request.url == classify_collection_url
134+
assert responses.calls[0].response.text == classify_collection_response

0 commit comments

Comments
 (0)