-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathask_watson.py
69 lines (47 loc) · 1.83 KB
/
ask_watson.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import logging
from random import randint
from flask import Flask, render_template
from flask_ask import Ask, statement, question, session
import json
app = Flask(__name__)
logging.getLogger("flask_ask").setLevel(logging.DEBUG)
#Setup Logging to a file
logging.basicConfig(filename='watson.log',level=logging.DEBUG)
# Setup Credentials on initialization to conversation service
from watson_developer_cloud import AuthorizationV1
from watson_developer_cloud import ConversationV1
workspace_id = '7f749a89-5fa8-4893-b6a1-b14abc718c8a'
conv_username = 'a482ff6b-ef54-4025-aad6-fb10d19bc792'
conv_password = 'x5t01eec8Gt2'
# Call Conversation Service
conversation = ConversationV1(
username = conv_username,
password = conv_password,
version='2016-09-20')
logging.debug('Checking Conversation Service is working')
response = conversation.message(workspace_id=workspace_id, message_input={'text': 'Hello'})
print(json.dumps(response, indent=2))
ask = Ask(app, "/")
@ask.launch
def launch_watson():
welcome_msg = render_template('welcome')
return question(welcome_msg)
@ask.intent("HelloIntent", convert={'Phrase': str})
def hellowatson(Phrase):
response = conversation.message(workspace_id=workspace_id, message_input={'text': Phrase})
return question(json.dumps(response['output']['text'])).reprompt("Sorry Shiva I didn't understand what you said")
@ask.intent('ColorIntent',default={'color': 'blue'})
def favcolor(color):
session.attributes['favcolor']=color
msg = render_template('favcolor',color=color)
return statement(msg)
@ask.session_ended
def session_ended():
return "{}", 200
@ask.on_session_started
def new_session():
logging.debug('New session started')
if __name__ == '__main__':
print("APP RUNNING : 0.0.0.0:5000")
app.run(host='0.0.0.0', port=5000, debug=True)