11import json
2+ import os
3+ from dotenv import load_dotenv , find_dotenv
24from watson_developer_cloud import ConversationV1
35from watson_developer_cloud import ToneAnalyzerV3
46
57# import tone detection
68import 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
914conversation = 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
1520tone_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.
3034payload = {
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
5457invokeToneConversation (payload ,maintainToneHistoryInContext );
0 commit comments