Skip to content

Commit 4bdfd84

Browse files
committed
Changed way to add environment variables - dotenv with .env file now supported. Also corrected the call to ConversationV1 - input and context are provided independently not as a single payload json object.
1 parent 5659e1c commit 4bdfd84

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
11
import json
2+
import os
3+
from dotenv import load_dotenv, find_dotenv
24
from watson_developer_cloud import ConversationV1
35
from watson_developer_cloud import ToneAnalyzerV3
46

57
# import tone detection
68
import tone_detection
79

8-
# replace with your own conversation credentials
10+
#
11+
load_dotenv(find_dotenv())
12+
13+
# replace with your own conversation credentials or put them in a .env file in the home directory
914
conversation = ConversationV1(
10-
username='YOUR SERVICE NAME', # YOUR SERVICE NAME
11-
password='YOUR PASSWORD',
15+
username=os.environ.get('CONVERSATION_USERNAME') or 'YOUR SERVICE NAME',
16+
password=os.environ.get('CONVERSATION_PASSWORD') or 'YOUR PASSWORD',
1217
version='2016-07-11')
1318

1419
# replace with your own tone analyzer credentials
1520
tone_analyzer = ToneAnalyzerV3(
16-
username='YOUR SERVICE NAME',
17-
password='YOUR PASSWORD',
21+
username=os.environ.get('TONE_ANALYZER_USERNAME') or 'YOUR SERVICE NAME',
22+
password=os.environ.get('TONE_ANALYZER_PASSWORD') or 'YOUR SERVICE NAME',
1823
version='2016-02-11')
1924

2025
# replace with your own workspace_id
21-
# the process.env probably won't work with python
22-
workspace_id = process.env.WORKSPACE_ID or 'YOUR WORKSPACE ID'
26+
workspace_id = os.environ.get('WORKSPACE_ID') or 'YOUR WORKSPACE ID'
2327

2428
# This example stores tone for each user utterance in conversation context.
2529
# Change this to false, if you do not want to maintain history
26-
maintainToneHistoryInContext = true
30+
maintainToneHistoryInContext = True
2731

2832
# Payload for the Watson Conversation Service
29-
# <workspace-id> and user input text required.
33+
# workspace_id and user input text required.
3034
payload = {
3135
'workspace_id':workspace_id,
3236
'input': {
33-
'text': "I am not happy today :("
37+
'text': "I am happy"
3438
}
3539
}
3640

@@ -45,10 +49,9 @@ def invokeToneConversation (payload, maintainToneHistoryInContext):
4549
4650
Note: as indicated below, the console.log statements can be replaced with application-specific code to process the err or data object returned by the Conversation Service.
4751
'''
48-
49-
tone = tone_analyzer.tone({'text': payload['input']['text']})
52+
tone = tone_analyzer.tone(text=payload['input']['text'])
5053
conversation_payload = tone_detection.updateUserTone(payload, tone, maintainToneHistoryInContext)
51-
response = conversation.message(workspace_id=workspace_id, message_input=conversation_payload)
54+
response = conversation.message(workspace_id=workspace_id, message_input=conversation_payload['input'], context=conversation_payload['context'])
5255
print(json.dumps(response, indent=2))
5356

5457
invokeToneConversation(payload,maintainToneHistoryInContext);

0 commit comments

Comments
 (0)