Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
38 changes: 26 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Python client library to quickly get started with the various [Watson APIs][wdc]
* [Changes for v2.0](#changes-for-v20)
* [Migration](#migration)
* [Configuring the http client](#configuring-the-http-client-supported-from-v110)
* [Disable SSL certificate verification](#disable-ssl-certificate-verification)
* [Sending request headers](#sending-request-headers)
* [Parsing HTTP response info](#parsing-http-response-info)
* [Dependencies](#dependencies)
Expand Down Expand Up @@ -106,40 +107,42 @@ You supply either an IAM service **API key** or an **access token**:

```python
# In the constructor, letting the SDK manage the IAM token
discovery = DiscoveryV1(version='2017-10-16',
discovery = DiscoveryV1(version='2018-08-01',
url='<url_as_per_region>',
iam_apikey='<iam_apikey>',
iam_url='<iam_url>') # optional - the default value is https://iam.bluemix.net/identity/token
```

```python
# after instantiation, letting the SDK manage the IAM token
discovery = DiscoveryV1(version='2017-10-16')
discovery = DiscoveryV1(version='2018-08-01', url='<url_as_per_region>')
discovery.set_iam_apikey('<iam_apikey>')
```

#### Supplying the access token
```python
# in the constructor, assuming control of managing IAM token
discovery = DiscoveryV1(version='2017-10-16',
discovery = DiscoveryV1(version='2018-08-01',
url='<url_as_per_region>',
iam_access_token='<iam_access_token>')
```

```python
# after instantiation, assuming control of managing IAM token
discovery = DiscoveryV1(version='2017-10-16')
discovery = DiscoveryV1(version='2018-08-01', url='<url_as_per_region>')
discovery.set_iam_access_token('<access_token>')
```

### Username and password
```python
from watson_developer_cloud import DiscoveryV1
# In the constructor
discovery = DiscoveryV1(version='2017-10-16', username='<username>', password='<password>')
discovery = DiscoveryV1(version='2018-08-01', url='<url_as_per_region>', username='<username>', password='<password>')
```

```python
# After instantiation
discovery = DiscoveryV1(version='2017-10-16')
discovery = DiscoveryV1(version='2018-08-01', url='<url_as_per_region>')
discovery.set_username_and_password('<username>', '<password>')
```

Expand All @@ -150,12 +153,12 @@ discovery.set_username_and_password('<username>', '<password>')
```python
from watson_developer_cloud import VisualRecognitionV3
# In the constructor
visual_recognition = VisualRecognitionV3(version='2018-05-22', api_key='<api_key>')
visual_recognition = VisualRecognitionV3(version='2018-03-19', url='<url_as_per_region>', api_key='<api_key>')
```

```python
# After instantiation
visual_recognition = VisualRecognitionV3(version='2018-05-22')
visual_recognition = VisualRecognitionV3(version='2018-03-19')
visual_recognition.set_api_key('<api_key>')
```

Expand All @@ -174,7 +177,8 @@ from watson_developer_cloud import AssistantV1
assistant = AssistantV1(
username='xxx',
password='yyy',
version='2017-04-21')
url='<url_as_per_region>',
version='2018-07-10')

response = assistant.list_workspaces(headers={'Custom-Header': 'custom_value'})
print(response.get_result())
Expand All @@ -195,14 +199,22 @@ from watson_developer_cloud import AssistantV1
assistant = AssistantV1(
username='xxx',
password='yyy',
version='2017-04-21')
url='<url_as_per_region>',
version='2018-07-10')

assistant.set_http_config({'timeout': 100})
response = assistant.message(workspace_id=workspace_id, input={
'text': 'What\'s the weather like?'}).get_result()
print(json.dumps(response, indent=2))
```

## Disable SSL certificate verification
For ICP(IBM Cloud Private), you can disable the SSL certificate verification by:

```python
service.disable_SSL_verification()
```

## Sending request headers
Custom headers can be passed in any request in the form of a `dict` as:
```python
Expand All @@ -218,7 +230,8 @@ from watson_developer_cloud import AssistantV1
assistant = AssistantV1(
username='xxx',
password='yyy',
version='2017-04-21')
url='<url_as_per_region>',
version='2018-07-10')

response = assistant.list_workspaces(headers={'Custom-Header': 'custom_value'}).get_result()
```
Expand All @@ -231,7 +244,8 @@ from watson_developer_cloud import AssistantV1
assistant = AssistantV1(
username='xxx',
password='yyy',
version='2017-04-21')
url='<url_as_per_region>',
version='2018-07-10')

assistant.set_detailed_response(True)
response = assistant.list_workspaces(headers={'Custom-Header': 'custom_value'}).get_result()
Expand Down
4 changes: 2 additions & 2 deletions examples/assistant_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# If service instance provides API key authentication
# assistant = AssistantV1(
# version='2017-04-21',
# version='2018-07-10',
# ## url is optional, and defaults to the URL below. Use the correct URL for your region.
# url='https://gateway.watsonplatform.net/assistant/api',
# iam_apikey='iam_apikey')
Expand All @@ -14,7 +14,7 @@
password='YOUR SERVICE PASSWORD',
## url is optional, and defaults to the URL below. Use the correct URL for your region.
# url='https://gateway.watsonplatform.net/assistant/api',
version='2017-04-21')
version='2018-07-10')

#########################
# Workspaces
Expand Down
41 changes: 41 additions & 0 deletions examples/assistant_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from __future__ import print_function
import json
from watson_developer_cloud import AssistantV2

# If service instance provides API key authentication
# assistant = AssistantV2(
# version='2017-04-21',
# ## url is optional, and defaults to the URL below. Use the correct URL for your region.
# url='https://gateway.watsonplatform.net/assistant/api',
# iam_apikey='iam_apikey')

assistant = AssistantV2(
username='YOUR SERVICE USERNAME',
password='YOUR SERVICE PASSWORD',
## url is optional, and defaults to the URL below. Use the correct URL for your region.
url='https://gateway.watsonplatform.net/assistant/api',
version='2017-04-21')

#########################
# Sessions
#########################

session = assistant.create_session("<YOUR ASSISTANT ID>").get_result()
print(json.dumps(session, indent=2))

assistant.delete_session("<YOUR ASSISTANT ID>", "<YOUR SESSION ID>").get_result()

#########################
# Message
#########################

message = assistant.message(
"<YOUR ASSISTANT ID>",
"<YOUR SESSION ID>",
input={'text': 'What\'s the weather like?'},
context={
'metadata': {
'deployment': 'myDeployment'
}
}).get_result()
print(json.dumps(message, indent=2))
4 changes: 2 additions & 2 deletions examples/conversation_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## If service instance provides API key authentication
# conversation = ConversationV1(
# version='2018-02-16',
# version='2018-07-10',
# ## url is optional, and defaults to the URL below. Use the correct URL for your region.
# url='https://gateway.watsonplatform.net/conversation/api',
# iam_apikey='iam_apikey')
Expand All @@ -14,7 +14,7 @@
password='YOUR SERVICE PASSWORD',
## url is optional, and defaults to the URL below. Use the correct URL for your region.
# url='https://gateway.watsonplatform.net/conversation/api',
version='2018-02-16')
version='2018-07-10')

# When you send multiple requests for the same conversation, include the
# context object from the previous response.
Expand Down
4 changes: 2 additions & 2 deletions examples/discovery_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

# If service instance provides API key authentication
# discovery = DiscoveryV1(
# version='2017-10-16',
# version='2018-08-01',
# ## url is optional, and defaults to the URL below. Use the correct URL for your region.
# url='https://gateway.watsonplatform.net/discovery/api',
# iam_apikey='iam_apikey')

discovery = DiscoveryV1(
version='2017-10-16',
version='2018-08-01',
## url is optional, and defaults to the URL below. Use the correct URL for your region.
# url='https://gateway.watsonplatform.net/discovery/api',
username='YOUR SERVICE USERNAME',
Expand Down
4 changes: 2 additions & 2 deletions examples/language_translator_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
from watson_developer_cloud import LanguageTranslatorV3

# language_translator = LanguageTranslatorV3(
# version='2018-05-31',
# version='2018-05-01.',
# ### url is optional, and defaults to the URL below. Use the correct URL for your region.
# # url='https://gateway.watsonplatform.net/language-translator/api',
# iam_apikey='your_apikey')

# Authenticate with username/password if your service instance doesn't provide an API key
language_translator = LanguageTranslatorV3(
version='2018-05-31',
version='2018-05-01.',
username='YOUR SERVICE USERNAME',
password='YOUR SERVICE PASSWORD')

Expand Down
3 changes: 2 additions & 1 deletion examples/microphone-speech-to-text.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def recognize_using_weboscket(*args):
mycallback = MyRecognizeCallback()
speech_to_text.recognize_using_websocket(audio=audio_source,
content_type='audio/l16; rate=44100',
recognize_callback=mycallback)
recognize_callback=mycallback,
interim_results=True)

###############################################
#### Prepare the for recording using Pyaudio ##
Expand Down
4 changes: 2 additions & 2 deletions examples/natural_language_understanding_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

# If service instance provides API key authentication
# service = NaturalLanguageUnderstandingV1(
# version='2017-02-27',
# version='2018-03-16',
# ## url is optional, and defaults to the URL below. Use the correct URL for your region.
# url='https://gateway.watsonplatform.net/natural-language-understanding/api',
# iam_apikey='your_apikey')

service = NaturalLanguageUnderstandingV1(
version='2017-02-27',
version='2018-03-16',
## url is optional, and defaults to the URL below. Use the correct URL for your region.
# url='https://gateway.watsonplatform.net/natural-language-understanding/api',
username='YOUR SERVICE USERNAME',
Expand Down
27 changes: 25 additions & 2 deletions examples/personality_insights_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@
import json
from os.path import join, dirname
from watson_developer_cloud import PersonalityInsightsV3
import csv

# # If service instance provides API key authentication
# service = PersonalityInsightsV3(
# version='2016-10-20',
# version='2017-10-13',
# ## url is optional, and defaults to the URL below. Use the correct URL for your region.
# url='https://gateway.watsonplatform.net/personality-insights/api',
# iam_apikey='your_apikey')

service = PersonalityInsightsV3(
version='2016-10-20',
version='2017-10-13',
## url is optional, and defaults to the URL below. Use the correct URL for your region.
# url='https://gateway.watsonplatform.net/personality-insights/api',
username='YOUR SERVICE USERNAME',
password='YOUR SERVICE PASSWORD')

############################
# Profile with JSON output #
############################

with open(join(dirname(__file__), '../resources/personality-v3.json')) as \
profile_json:
profile = service.profile(
Expand All @@ -30,3 +35,21 @@
consumption_preferences=True).get_result()

print(json.dumps(profile, indent=2))

###########################
# Profile with CSV output #
###########################

with open(join(dirname(__file__), '../resources/personality-v3.json')) as \
profile_json:
response = service.profile(
profile_json.read(),
content_type='application/json',
accept="text/csv",
csv_headers=True).get_result()

profile = response.content
cr = csv.reader(profile.splitlines())
my_list = list(cr)
for row in my_list:
print(row)
4 changes: 2 additions & 2 deletions examples/tone_analyzer_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
# service = ToneAnalyzerV3(
# ## url is optional, and defaults to the URL below. Use the correct URL for your region.
# url='https://gateway.watsonplatform.net/tone-analyzer/api',
# version='2017-09-26',
# version='2017-09-21',
# iam_apikey='your_apikey')

service = ToneAnalyzerV3(
## url is optional, and defaults to the URL below. Use the correct URL for your region.
# url='https://gateway.watsonplatform.net/tone-analyzer/api',
username='YOUR SERVICE USERNAME',
password='YOUR SERVICE PASSWORD',
version='2017-09-26')
version='2017-09-21')

print("\ntone_chat() example 1:\n")
utterances = [{
Expand Down
4 changes: 2 additions & 2 deletions examples/visual_recognition_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

# # If service instance provides IAM API key authentication
# service = VisualRecognitionV3(
# '2016-05-20',
# '2018-03-19',
# ## url is optional, and defaults to the URL below. Use the correct URL for your region.
# url='https://gateway.watsonplatform.net/visual-recognition/api',
# iam_apikey='iam_apikey')

service = VisualRecognitionV3('2016-05-20',
service = VisualRecognitionV3('2018-03-19',
## url is optional, and defaults to the URL below. Use the correct URL for your region.
# url='https://gateway.watsonplatform.net/visual-recognition/api',
api_key='YOUR API KEY')
Expand Down
3 changes: 2 additions & 1 deletion test/integration/test_discovery_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class Discoveryv1(TestCase):
def setUp(self):
self.discovery = watson_developer_cloud.DiscoveryV1(
version='2017-10-16',
version='2018-08-01',
username="YOUR SERVICE USERNAME",
password="YOUR SERVICE PASSWORD")
self.discovery.set_default_headers({
Expand Down Expand Up @@ -117,6 +117,7 @@ def test_queries(self):
return_fields='extracted_metadata.sha1').get_result()
assert query_results is not None

@pytest.mark.skip(reason="Temporary skipping because update_credentials fails")
def test_credentials(self):
credential_details = {
'credential_type': 'username_password',
Expand Down
2 changes: 1 addition & 1 deletion test/integration/test_visual_recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class IntegrationTestVisualRecognitionV3(TestCase):
@classmethod
def setup_class(cls):
cls.visual_recognition = watson_developer_cloud.VisualRecognitionV3(
'2016-05-20', api_key='YOUR API KEY')
'2018-03-19', api_key='YOUR API KEY')
cls.visual_recognition.set_default_headers({
'X-Watson-Learning-Opt-Out':
'1',
Expand Down
Loading