-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteractive_assistant.py
201 lines (157 loc) · 7.58 KB
/
Interactive_assistant.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python
# coding: utf-8
"""
Interactive Assistant Service - From scratch
============================================
Voice Assistant Prototype:
Webservice bot that interacts with user in front
of the screen.
Server side
Note:
Structure:
interactive_assistant.py
_copyright_ = 'Copyright (c) 2019 J.W. - Everis',
_license_ = GNU General Public License
"""
# Load libraries
from flask import Flask, request, jsonify
from flask_restful import Resource, Api
from flask_assistant import Assistant, ask, tell
import random
import time
import win32com.client as wincl
import win32api
app = Flask(__name__)
api = Api(app)
session_active = True
class ShowroomAssistant(Resource):
def __init__(self):
""" Initialize some common phrases and Microsoft Voice Api """
self.sentiment = ['happiness', 'surprise', 'sadness', 'anger', 'disgust', 'fear']
self.congrats = ["Muy bien! excelente trabajo.", "¡Excelente!", "¡Bien hecho!"]
self.again = ["A ver, inténtalo nuevamente", "Vamos a intentarlo de nuevo con otra expresión"]
import pythoncom
pythoncom.CoInitialize()
self.speak = wincl.Dispatch("SAPI.SpVoice")
def get(self):
return {'Default response': 'Use POST method'}
def post(self):
response = ""
if request.headers['Content-Type'] == 'application/json; charset=UTF-8':
req = dict(request.json)
action = req['action']
if action == 'input.welcome':
print("<input.welcome>")
name = req['name']
msg = 'Hola ' + name + '!'
print(msg)
self.speak.Speak(msg)
msg = "Te presentamos un sistema que implementa diversos modelos de inteligencia artificial."
print(msg)
self.speak.Speak(msg)
msg = "Para mostrarte cómo funcionan estos modelos, hagamos algunas pruebas o retos."
print(msg)
self.speak.Speak(msg)
response = { "session":"active", "reto": "", "detected":"Yes", "state":"welcome"}
reply = {
"fulfillmentText": response,
}
return jsonify(reply)
if action == 'input.challenge':
print("<input.challenge>")
reto = req['reto']
listReto = req['listReto']
remainingSent = sorted(set(self.sentiment) - set(listReto))
if len(remainingSent) > 0:
#challenge = random.choice(self.sentiment)
challenge = random.choice(remainingSent)
if challenge == "happiness":
msg = "Muéstranos una sonrisa"
print(msg)
self.speak.Speak(msg)
response = { "session":"active", "reto": challenge, "detected":"Yes", "state":"challenge"}
if challenge == "surprise":
msg = "Danos tu mejor cara de asombro."
print(msg)
self.speak.Speak(msg)
response = { "session":"active", "reto": challenge, "detected":"Yes", "state":"challenge"}
if challenge == "sadness":
msg = "¿Qué tal una cara de tristeza?"
print(msg)
self.speak.Speak(msg)
response = { "session":"active", "reto": challenge, "detected":"Yes", "state":"challenge"}
if challenge == "anger":
msg = "¿Cuál es tu cara de enojo?"
print(msg)
self.speak.Speak(msg)
response = { "session":"active", "reto": challenge, "detected":"Yes", "state":"challenge"}
if challenge == "disgust":
msg = "¿Cómo es tu cara de desagrado?"
print(msg)
self.speak.Speak(msg)
response = { "session":"active", "reto": challenge, "detected":"Yes", "state":"challenge"}
if challenge == "fear":
msg = "¿Muéstranos una cara de miedo?"
print(msg)
self.speak.Speak(msg)
response = { "session":"active", "reto": challenge, "detected":"Yes", "state":"challenge"}
else:
msg = "Gracias por participar"
print(msg)
self.speak.Speak(msg)
response = {"session":"end", "reto": "ninguno", "detected":"No", "state":"completed"}
reply = {
"fulfillmentText": response,
}
return jsonify(reply)
if action == 'input.feedback':
print("<input.feedback>")
score = req['score']
reto = req['reto']
if score == "positive":
msg = random.choice(self.congrats)
print(msg)
self.speak.Speak(msg)
repeat = int(req['repeat'])
if repeat <= 3:
if repeat == 1:
msg = "Por eso te has ganado " + str(repeat) + " punto en tu monedero virtual"
print(msg)
self.speak.Speak(msg)
else:
msg = "Por eso te has ganado " + str(repeat) + " puntos en tu monedero virtual"
print(msg)
self.speak.Speak(msg)
if repeat == 3:
msg = "Muchas gracias por visitarnos"
print(msg)
self.speak.Speak(msg)
response = {'session':'end', "reto": "ninguno", "detected":"No", "state":"completed"}
else:
response = {"session":"active", "reto": "", "detected":"Yes", "state":"welcome"}
else:
fail = int(req['fail'])
if fail <= 4:
msg = random.choice(self.again)
print(msg)
self.speak.Speak(msg)
response = {"session":"active", "reto": reto, "detected":"Yes", "state":"welcome"}
else:
msg = "Empecemos de nuevo"
print(msg)
self.speak.Speak(msg)
response = {"session":"end", "reto": "ninguno", "detected":"No", "state":"terminate"}
reply = { "fulfillmentText": response, }
return jsonify(reply)
if action == "input.terminate":
print("<input.terminate>")
msg = "** Persona abandonó el juego"
print(msg)
response = {'session':'end', "reto": "ninguno", "detected":"No", "state":"terminate"}
reply = { "fulfillmentText": response, }
return jsonify(reply)
else:
return jsonify({"Message": "Invalid post request",})
api.add_resource(ShowroomAssistant, '/test')
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')