-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
421 lines (361 loc) · 13 KB
/
main.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import json
import pythonosc
import argparse
import math
import datetime
from pythonosc import dispatcher, osc_server, udp_client, osc_message_builder
import requests
from collections import OrderedDict
from statistics import mean
## added variables to change the ip and port easily
## testing if Git works with ST
ip_osc = '192.168.1.255'
##ip_osc = '192.168.0.255'
ip_osc_server='0.0.0.0'
ip_osc_editor='196.168.1.255'
## ip_osc = '10.253.0.255'
port_server = 7007
port_client = 7007
port_client_editor = 7007
api_url = "http://frankenstein.hunterowens.net/"
## Some comments
current_state = OrderedDict()
current_state["/state"] = "calm"
current_state["/action"] = "start"
current_state["/sentiment"] = 0.0
current_state["/energy"] = 0.0
current_state["/focus"] = 0.0
def change_state(current_state, new_state):
"""
Change the current state dict to
reflect state param: new_state
return current_state
"""
current_state['/state'] = new_state
print("New State Set to {0}".format(current_state))
return current_state
def send_surface_state_to_ai(sentiment, energy, focus):
"""
sents the state / new talking as JSON to the AI
focus, energy, and sentiment are floats; unit is a string; words and parts are arrays of strings where the indexes correspond, so words[0] goes with parts[0]
"""
print("AI State is: {0} focus, {1} energy, and {2} sentiment".format(current_focus, current_energy, current_sentiment))
data = {
'focus': focus,
'sentiment': sentiment,
'energy': energy
}
r = requests.post(api_url + 'interact-surface', data = data)
return r
def send_answer_to_ai(answer):
"""
Sents an answer to the AI
"""
print("Answer sending ", answer)
headers = {
"content-type": "application/json"
}
r = requests.post(api_url + 'interact',
json={'string': answer},
headers=headers)
return r
def get_api_interact_data():
"""
Gets state from AI, transforms into sentiment.
Returns a string of JSON
"""
print("Getting Data from AI")
r = requests.get(api_url + 'interact')
if r.status_code == 200:
data = r.json()
else:
data = pickle.load(open('./default-api-response.p','rb'))
print("Using Default Data: {}".format(data))
current_state['/state'] = data['state']
current_state['/sentiment'] = data['sentiment']
current_state['/focus'] = data['focus']
current_state['/energy'] = data['energy']
print('state updated')
return data
def setup():
"""
sets AI in waiting state
"""
r = requests.get(api_url + "reset")
print("AI Init State Waiting")
current_state = get_api_interact_data()
##pull text from AI
return None
def osc_dispatch(addr, msg, ip=ip_osc, port=port_client):
"""
Dispatches a message in state change over OSC to all listeners
"""
client = udp_client.UDPClient(ip, port,1)
## SimpleOSCClientRedux(client)
## client._sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
print("Sent {0} with {1} to {2} at {3}".format(addr, msg, ip, port))
builder = osc_message_builder.OscMessageBuilder(address=addr)
builder.add_arg(msg)
client.send(builder.build())
## print(client(addr, msg))
return None
def broadcast_state(state=current_state, ip=ip_osc, port=port_client):
"""
Broadcasts state
"""
print("Called Broadcast State Function")
#client = udp_client.UDPClient(ip, port,1)
#builder = osc_message_builder.OscMessageBuilder(address='/status')
#for k,v in state.items():
# builder.add_arg(v)
#client.send(builder.build())
#print("sent {0} to {1}:{2}".format(builder.args, ip, port))
return None
def broadcast_text(AItext):
"""
send a fixed piece of text from the AI
add delay into this OSC as second args
get text somehow
"""
osc_dispatch('/textnoquest', AItext, port=port_client_editor)
print("Updating State")
broadcast_state()
return None
def send_questions_to_line_editor():
"""
Sends data for display to Line Editor
"""
data = get_api_interact_data()['questions']
print("Called send question to the line editor")
#client = udp_client.UDPClient(ip_osc_editor, port_client_editor,1)
#builder = osc_message_builder.OscMessageBuilder(address='/textques')
#for k,v in data.items():
# builder.add_arg(v)
#builder.add_arg(.75)
#print('builder ', builder.address)
#client.send(builder.build())
#osc_dispatch('/textquest', .75, ip=ip_osc_server, port=port_client_editor)
# print("sent {0} to {1}:{2}".format(builder.args, ip_osc_editor, port_client_editor))
ip=ip_osc
port=port_client_editor
client = udp_client.UDPClient(ip, port,1)
print("Send Data to Line Editor {}:{}", ip, port)
builder = osc_message_builder.OscMessageBuilder(address='/textques')
for k,v in data.items():
print(k,v)
builder.add_arg(v)
client.send(builder.build())
print("sent {0} to {1}:{2}".format(builder.args, ip, port))
broadcast_state()
return None
surface_data = []
def surface_handler(unused_addr, args):
"""
Handles the surface messages, alts sentiment
Surface argument to be OSC String Formatted as followed
"sentiment: value; focus: value; energy: value"
"""
print("Got Surface Message")
try:
vals = json.loads(args)
## surfaces need to be directed to pi, look in js/machineConfiguration.json
except ValueError:
print("Unable to decode JSON from Surface")
exit()
current_sentiment = vals['sentiment']
current_focus = vals['focus']
current_energy = vals['energy']
current_unit = vals['unit']
print("From Surface Unit {0}".format(current_unit))
current_words = vals['words']
current_parts = vals['parts']
surface_data.append(vals)
return None
def reset_handler(unused_addr, args):
"""
Handles the reset from Editor
"""
## TODO: Implement
print("reset handler")
setup()
surface_data = []
current_state.update({'/action': 'start'})
broadcast_state()
current_state.update({'/action': 'expectant'})
return None
def answer_handler(unused_addr, args):
"""
Starts answering
"""
print("send answer to ai")
send_answer_to_ai(args)
current_state.update({'/action': 'thinking'})
broadcast_state()
## Call line editor
send_questions_to_line_editor()
return None
def refresh_handler(unused_addr, args):
"""
Refresh text
"""
print("Refreshing text")
send_questions_to_line_editor()
return None
def talking_handler(unused_addr, args):
"""
Starts talking
"""
print("talking handler")
current_state.update({'/action': 'talking'})
broadcast_state()
send_questions_to_line_editor()
return None
def question_handler(unused_addr, args):
"""
shuts the machine up
"""
print('question handler')
current_state.update({'/action': 'question'})
broadcast_state()
return None
def thinking_handler(unsused_addr, args):
"""
shuts the machine up
"""
print('thinking handler')
current_state.update({'/action': 'thinking'})
broadcast_state()
return None
def silent_handler(unused_addr, args):
"""
silences the system after TTS
"""
print("silence handles")
current_state.update({'/action': 'expectant'})
broadcast_state()
return None
def surfacestart_handler(unused_addr, args):
"""
blasts start to the surfaces
"""
print("Blasting Start to the Surfaces")
osc_dispatch('/start-surface', 1)
def surfacereset_handler(unused_addr, args):
"""
blasts reset to surface
"""
print("Blasting Reset to the Surface")
osc_dispatch('/reset-surface', 1)
def surfaceclose_handler(unused_addr, args):
"""
blasts close to surface
"""
print("Blasting Close to the Surface")
osc_dispatch('/close-surface', 1)
sentiment = mean([d['sentiment'] for d in surface_data])
energy = mean([d['energy'] for d in surface_data])
focus = mean([d['focus'] for d in surface_data])
send_surface_state_to_ai(sentiment, energy, focus)
def end_handler(unused_addr, args):
"""
ends the show
"""
print("end of show")
current_state.update({'/action': 'end'})
broadcast_state()
return
print("some stupid stuff")
def osc_server(ip=ip_osc_server, port=port_server):
"""
sets up and runs the OSC server.
"""
dispatch = dispatcher.Dispatcher()
dispatch.map("/surface-sentiments", surface_handler)
dispatch.map("/reset", reset_handler)
dispatch.map("/silent", silent_handler)
dispatch.map("/answer", answer_handler)
dispatch.map("/refresh", refresh_handler)
dispatch.map("/talking", talking_handler)
dispatch.map("/end", end_handler)
dispatch.map("/question", question_handler)
dispatch.map("/thinking", thinking_handler)
dispatch.map("/startsurface", surfacestart_handler)
dispatch.map("/closesurface", surfaceclose_handler)
dispatch.map("/resetsurface", surfacereset_handler)
## TODO: Talk State - > triger from AI to get new words/questions etc from teh AI on the server and then broadcast
server = pythonosc.osc_server.ThreadingOSCUDPServer(
(ip, port), dispatch)
print("Serving on {}".format(server.server_address))
server.serve_forever()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--ip", default=ip_osc,
help="The ip of the OSC server")
parser.add_argument("--port", type=int, default=port_server,
help="The port the OSC server is listening on")
parser.add_argument('--server', action='store_true', default=False,
help="Run in server mode")
parser.add_argument('--text', action='store_true', default=False,
help="broadcast the text questions")
parser.add_argument('--silent', action='store_true', default=False, help="end talking cue")
parser.add_argument('--talking', action='store_true', default=False, help="get talking cue")
parser.add_argument('--answer', action='store_true', default=False, help="get answer")
parser.add_argument('--reset', action='store_true', default=False, help="start over")
parser.add_argument('--refresh', action='store_true', default=False, help="refresh questions")
parser.add_argument('--end', action='store_true', default=False, help="end experience")
parser.add_argument('--question', action='store_true', default=False, help='test question handler')
parser.add_argument('--thinking', action='store_true', default=False, help='test thinking handler')
parser.add_argument('--surface', action='store_true', default=False, help="send dummy surface data")
parser.add_argument( "--set-state", dest="new-state", default='guarded',
help="set teh new state", metavar="STATE")
parser.add_argument('--startsurface', action='store_true', default=False, help="test surface start")
parser.add_argument('--resetsurface', action='store_true', default=False, help="test surface reset")
parser.add_argument('--closesurface', action='store_true', default=False, help="test surface stop")
args = parser.parse_args()
print("Got argument: {}".format(args))
if args.server:
print("Sending Server")
osc_server()
elif args.text:
print("Sending Text")
broadcast_questions()
elif args.silent:
print("Sending OSC Test Message")
osc_dispatch('/silent', 1)
elif args.talking:
print("Sending Talking")
osc_dispatch('/talking', "answer")
elif args.answer:
print("Sending Answer") ## verified with web app
osc_dispatch('/answer', "answer")
elif args.reset:
print("Reseting") ## verified with web app
osc_dispatch('/reset', 1)
elif args.refresh:
print("Refreshing questions")
osc_dispatch('/refresh', 1)
elif args.end:
print("End experience")
osc_dispatch('/end', 1)
elif args.question:
print("Sending a question")
osc_dispatch('/question', 1)
elif args.thinking:
print("Setting thinking")
osc_dispatch('/thinking', 1)
elif args.startsurface:
print("Telling surfaces to turn on")
osc_dispatch('/startsurface', 1)
elif args.closesurface:
print("Telling surfaces to close")
osc_dispatch('/closesurface', 1)
elif args.resetsurface:
print("Telling surfaces to start over")
osc_dispatch('/resetsurface', 1)
elif args.surface:
print("Sending Surface Message")
## foo = json.loads('{"number": 1.0, "other": 4.3}')
osc_dispatch('/surface-sentiments', '{"sentiment": 0.15, "focus": 0.65, "energy": -0.3, "unit": "test", "words": ["inspired", "anxious", "understanding"], "parts": ["hand", "eye", "head"]}')
elif vars(args)['new-state']:
print('changing state')
change_state(current_state, vars(args)['new-state'])