-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
418 lines (311 loc) · 10.5 KB
/
app.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
import base64
import csv
import json
import logging
import os
import os.path
# import preview as pre
import random
import wave
from base64 import b64encode
from cgi import print_arguments
from io import StringIO
from mimetypes import guess_extension
from os import path
import flask
import numpy as np
import requests
# from flask_session import Session
from engineio.payload import Payload
from flask import (
Flask,
abort,
current_app,
jsonify,
make_response,
redirect,
render_template,
request,
session,
url_for,
)
from flask_socketio import SocketIO, emit, join_room, leave_room
from werkzeug.utils import secure_filename
import chatGPTTest
import event_handler
import GlobalData as GD
import layout_module
#import load_extensions
import plotlyExamples as PE
import search
import spam_protection as spam
# load audio and pad/trim it to fit 30 seconds
import TextToSpeech
import uploader
import uploaderGraph
import util
import websocket_functions as webfunc
from extensions import load_extensions
import pandas as pd
import plotly
import plotly.express as px
#### Settings
spam_protector = spam.SpamProtector()
log = logging.getLogger("werkzeug")
log.setLevel(logging.ERROR)
Payload.max_decode_packets = 50
app = Flask(__name__)
app.debug = False
app.config["SECRET_KEY"] = "secret"
app.config["SESSION_TYPE"] = "filesystem"
socketio = SocketIO(app, manage_session=False)
app, extensions = load_extensions.load(app)
### HTML ROUTES ###
### Execute code before first request ###
@app.before_first_request
def execute_before_first_request():
uploader.check_ProjectFolder() # checks if GD.json exists
util.create_dynamic_links(app)
GD.checkProjectGDexists() #check if project in GD.json exist / if not exists, using demo project
GD.loadGD()
GD.loadPFile()
GD.loadPD()
GD.loadColor()
# C_DEBUG test
GD.loadXYZTex()
GD.loadLinks()
GD.load_annotations()
def index():
return flask.redirect("/home")
@app.route("/preview")
def preview():
return render_template("preview.html", extensions=extensions)
@app.route("/main", methods=["GET"])
def main():
username = util.generate_username()
project = GD.data["actPro"] # flask.request.args.get("project")
if project is None:
project = GD.data["actPro"]
return "no project selected in GD.json"
if flask.request.method == "GET":
room = 1
# Store the data in session
flask.session["username"] = username
flask.session["room"] = room
# prolist = uploader.listProjects()
return render_template("main.html", user=username, extensions=extensions)
else:
return "error"
myusers = [{'uid': 4, 'links': [2, 2, 2, 2, 2, 133, 666, 666, 666, 666, 125, 125]}, {'uid': 666, 'links': [133]}, {'uid': 133, 'links': [666]}, {'uid': 555, 'links': [666, 133, 4, 123, 124, 125, 125, 125]}, {'uid': 125, 'links': [555, 128]}, {'uid': 128, 'links': [555]}, {'uid': 130, 'links': [555]}]
import plotlyExamples
@app.route('/evilAI')
def evilAI():
nodes=[]
links=[]
annot=[]
# Create graphJSON
#graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
if request.cookies.get('userID'): # has cookie, add to links
if request.args.get('uid'):
uid = request.args.get('uid')
name = request.cookies.get('userID')
for i in myusers:
if i['uid'] == int(name):
i['links'].append(int(uid))
break
for i in myusers:
if i['uid'] == uid:
i['links'].append(int(name))
break
for i in myusers:
nodes.append(int(i['uid']))
annot.append("agent"+ str(i['uid']))
for i in myusers:
start = -1
try:
start = nodes.index(i['uid'])
except ValueError:
start = -1
for l in i['links']:
try:
end = nodes.index(l)
links.append((start,end))
except ValueError:
end = -1
print(links)
print(nodes)
print(annot)
print(myusers)
graphJSON = plotlyExamples.networkGraphRT(nodes, annot , links)
#return '<h1>welcome ' + name + '</h1><br>You just met Agent ' + uid
return render_template("evilAi.html", user=name, data=myusers, graphJSON=graphJSON)
#login set cookie
else:
if request.args.get('uid'):
uid = request.args.get('uid')
exists = False
for i in myusers:
if i['uid'] == uid:
exists = True
if not exists:
thisUser = {}
thisUser["uid"] = int(uid)
thisUser["links"] = []
myusers.append(thisUser)
resp = make_response(render_template('evilAi.html',user=uid, data=myusers) + uid)
resp.set_cookie('userID', uid)
return resp
else:
return '<h1>QR code already claimed</h1>'
#return render_template('login.html')
@app.route('/setcookie', methods = ['POST', 'GET'])
def setcookie():
#if request.method == 'POST':
user = request.form['nm']
resp = make_response(render_template('readcookie.html'))
#resp = make_response(render_template('readcookie.html'))
resp.set_cookie('userID', user)
return resp
@app.route('/getcookie')
def getcookie():
if request.cookies.get('userID'):
name = request.cookies.get('userID')
return '<h1>welcome ' + name + '</h1>'
else:
return '<h1>no cookie</h1>'
@app.route("/GPT", methods=["POST"])
def GPT():
result = {}
if request.method == "POST":
data = flask.request.get_json()
answer = chatGPTTest.NewGPTrequest(data.get("text"))
fname = TextToSpeech.makeogg(answer, 0)
print(answer)
return {"text": answer, "audiofile": fname + ".ogg"}
@app.route("/TTS", methods=["POST", "GET"])
def TTS():
result = {}
if request.method == "GET":
text = flask.request.args.get("text")
voice = int(flask.request.args.get("voice"))
result["text"] = TextToSpeech.makeogg(text, voice)
return result
@app.route("/nodepanel", methods=["GET"])
def nodepanel():
data = []
return render_template("nodepanel.html", data)
@app.route("/uploadOLD", methods=["GET"])
def upload():
prolist = GD.listProjects()
return render_template(
"upload.html",
namespaces=prolist,
extensions=extensions,
sessionData=json.dumps(GD.data),
)
@app.route("/upload", methods=["GET"])
def uploadNew():
prolist = GD.listProjects()
return render_template(
"uploadNew.html",
namespaces=prolist,
extensions=extensions,
sessionData=json.dumps(GD.data),
)
@app.route("/uploadJSON", methods=["GET"])
def uploadJSON():
prolist = GD.listProjects()
return render_template(
"uploadGRAPH.html",
namespaces=prolist,
extensions=extensions,
sessionData=json.dumps(GD.data),
)
@app.route("/uploadfiles", methods=["GET", "POST"])
def upload_files():
return uploader.upload_files(flask.request)
@app.route("/uploadfilesNew", methods=["GET", "POST"])
def upload_filesNew():
return uploader.upload_filesNew(flask.request)
@app.route("/uploadfilesJSON", methods=["GET", "POST"])
def upload_filesJSON():
return uploaderGraph.upload_filesJSON(flask.request)
@app.route("/delpro", methods=["GET", "POST"])
def delete_project():
return util.delete_project(flask.request)
# gets information about a specific node (project must be provided as argument)
@app.route("/node", methods=["GET", "POST"])
def nodeinfo():
id = flask.request.args.get("id")
key = flask.request.args.get("key")
name = "static/projects/" + str(flask.request.args.get("project")) + "/nodes"
nodestxt = open(name + ".json", "r")
nodes = json.load(nodestxt)
nlength = len(nodes["nodes"]) - len(nodes["labels"])
print(nlength)
if key:
return str(nodes["nodes"][int(id)].get(key))
else:
if int(id) > nlength:
# is label
print(nodes["labels"][int(id) - nlength])
return nodes["nodes"][int(id)]
@app.route("/home")
def home():
if not flask.session.get("username"):
flask.session["username"] = util.generate_username()
flask.session["room"] = 1
return render_template("home.html", sessionData=json.dumps(GD.data))
### DATA ROUTES###
@app.route("/load_all_projects", methods=["GET", "POST"])
def loadAllProjectsR():
return jsonify(projects=GD.listProjects())
@app.route("/load_project/<name>", methods=["GET", "POST"])
def loadProjectInfoR(name):
return uploader.loadProjectInfo(name)
@app.route("/projectAnnotations/<name>", methods=["GET"])
def loadProjectAnnotations(name):
return uploader.loadAnnotations(name)
###SocketIO ROUTES###
@socketio.on("join", namespace="/main")
def join(message):
for func in GD.functions["join"]:
func(message)
room = flask.session.get("room")
join_room(room)
print(message["usr"])
print(
webfunc.bcolors.WARNING
+ message["usr"]
+ " has entered the room."
+ webfunc.bcolors.ENDC
)
emit("status", {"usr": message["usr"], "msg": " has entered the room."}, room=room)
@socketio.on("ex", namespace="/main")
@spam_protector
def ex(message):
for func in GD.functions["ex"]:
func(message)
room = flask.session.get("room")
project = GD.data["actPro"]
# print(webfunc.bcolors.WARNING+ flask.session.get("username")+ "ex: "+ json.dumps(message)+ webfunc.bcolors.ENDC)
# message["usr"] = flask.session.get("username")
print("incoming " + str(message))
event_handler.handle_socket_execute(message, room, project)
@socketio.on("left", namespace="/main")
def left(message):
for func in GD.functions["left"]:
func(message)
room = flask.session.get("room")
username = flask.session.get("username")
leave_room(room)
flask.session.clear()
emit("status", {"msg": username + " has left the room."}, room=room)
print(
webfunc.bcolors.WARNING
+ flask.session.get("username")
+ " has left the room."
+ webfunc.bcolors.ENDC
)
if __name__ == "__main__":
socketio.run(app, debug=True)