-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
server.py
470 lines (416 loc) · 15.6 KB
/
server.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import sys, os, json, argparse
from threading import Timer, Lock
from flask import Flask, render_template, request
from flask import Response, stream_with_context
from waitress import serve
import webbrowser
import torch
from backend.models import update_model, load_models, get_model_info, list_models, remove_model, load_model, unload_model, get_loaded_model
from backend.config import set_config_dir, global_state
from backend.sessions import list_sessions, set_session, get_session, get_default_session_settings, new_session, delete_session, set_cancel_signal
from backend.notepads import list_notepads, set_notepad, get_notepad, get_default_notepad_settings, new_notepad, delete_notepad, set_notepad_cancel_signal
from backend.prompts import list_prompt_formats
from backend.settings import get_settings, set_settings
if os.name == "nt":
# Fix Windows inferring text/plain MIME type for static files
# https://stackoverflow.com/questions/59355194/
import mimetypes
mimetypes.add_type("application/javascript", ".js")
mimetypes.add_type("text/css", ".css")
app_dir = os.path.dirname(os.path.abspath(__file__))
app = Flask(
"ExUI",
template_folder = os.path.join(app_dir, "templates"),
static_folder = os.path.join(app_dir, "static")
)
api_lock = Lock()
api_lock_cancel = Lock()
parser = argparse.ArgumentParser(description="ExUI, chatbot UI for ExLlamaV2")
parser.add_argument("-host", "--host", type = str, help = "IP:PORT eg, 0.0.0.0:5000", default = "localhost:5000")
parser.add_argument("-d", "--dir", type = str, help = "Location for user data and sessions, default: ~/exui", default = "~/exui")
parser.add_argument("-v", "--verbose", action = "store_true", help = "Verbose (debug) mode")
parser.add_argument("-nb,", "--no_browser", action = "store_true", help = "Don't launch browser on startup")
args = parser.parse_args()
verbose = args.verbose
no_browser = args.no_browser
@app.route("/")
def home():
# global api_lock, verbose
if verbose: print("/")
# with api_lock:
return render_template("index.html")
@app.route("/api/list_models")
def api_list_models():
global api_lock, verbose
if verbose: print("/api/list_models")
with api_lock:
m, c = list_models()
result = { "result": "ok",
"models": m,
"current_model": c }
if verbose: print("->", result)
return json.dumps(result) + "\n"
@app.route("/api/get_model_info", methods=['POST'])
def api_get_model_info():
global api_lock, verbose
if verbose: print("/api/get_model_info")
with api_lock:
data = request.get_json()
if verbose: print("<-", data)
info = get_model_info(data)
if info: result = { "result": "ok",
"model_info": info }
else: result = { "result": "fail" }
if verbose: print("->", result)
return json.dumps(result) + "\n"
@app.route("/api/update_model", methods=['POST'])
def api_update_model():
global api_lock, verbose
if verbose: print("/api/update_model")
with api_lock:
data = request.get_json()
if verbose: print("<-", data)
i = update_model(data["model_info"])
result = { "result": "ok", "new_model_uuid": i }
if verbose: print("->", result)
return json.dumps(result) + "\n"
@app.route("/api/load_model", methods=['POST'])
def api_load_model():
global api_lock, verbose
if verbose: print("/api/load_model")
with api_lock:
data = request.get_json()
if verbose: print("<-", data)
if verbose: print("-> ...")
result = Response(stream_with_context(load_model(data)), mimetype = 'application/json')
if verbose: print("->", result)
return result
@app.route("/api/unload_model")
def api_unload_model():
global api_lock, verbose
if verbose: print("/api/unload_model")
with api_lock:
result = unload_model()
if verbose: print("->", result)
return json.dumps(result) + "\n"
@app.route("/api/list_sessions")
def api_list_sessions():
global api_lock, verbose
if verbose: print("/api/list_sessions")
with api_lock:
s, c = list_sessions()
result = { "result": "ok", "sessions": s, "current_session": c }
if verbose: print("-> (...)")
return json.dumps(result) + "\n"
@app.route("/api/get_default_settings")
def api_get_default_settings():
global api_lock, verbose
if verbose: print("/api/get_default_settings")
with api_lock:
result = { "result": "ok",
"session_settings": get_default_session_settings(),
"notepad_settings": get_default_notepad_settings(),
"prompt_formats": list_prompt_formats() }
return json.dumps(result) + "\n"
@app.route("/api/set_session", methods=['POST'])
def api_set_session():
global api_lock, verbose
if verbose: print("/api/set_session")
with api_lock:
data = request.get_json()
if verbose: print("<-", data)
session = set_session(data)
if session is not None:
result = { "result": "ok",
"session": session,
"prompt_formats": list_prompt_formats() }
if verbose: print("-> (...)")
else:
result = { "result": "fail" }
if verbose: print("->", result)
return json.dumps(result) + "\n"
@app.route("/api/new_session", methods=['POST'])
def api_new_session():
global api_lock, verbose
if verbose: print("/api/new_session")
with api_lock:
data = request.get_json()
if verbose: print("<-", data)
session = new_session()
if "settings" in data: get_session().update_settings(data["settings"])
if "user_input_text" in data: get_session().user_input(data)
if "new_name" in data: get_session().rename(data)
result = { "result": "ok", "session": session }
if verbose: print("-> (...)")
return json.dumps(result) + "\n"
@app.route("/api/rename_session", methods=['POST'])
def api_rename_session():
global api_lock, verbose
if verbose: print("/api/rename_session")
with api_lock:
data = request.get_json()
if verbose: print("<-", data)
s = get_session()
s.rename(data)
result = { "result": "ok" }
if verbose: print("->", result)
return json.dumps(result) + "\n"
@app.route("/api/update_settings", methods=['POST'])
def api_update_settings():
global api_lock, verbose
if verbose: print("/api/update_settings")
with api_lock:
s = get_session()
data = request.get_json()
if verbose: print("<-", data)
s.update_settings(data["settings"])
result = { "result": "ok" }
if verbose: print("->", result)
return json.dumps(result) + "\n"
@app.route("/api/user_input", methods=['POST'])
def api_user_input():
global api_lock, verbose
if verbose: print("/api/user_input")
with api_lock:
s = get_session()
data = request.get_json()
if verbose: print("<-", data)
new_block = s.user_input(data)
result = { "result": "ok", "new_block": new_block }
if verbose: print("->", result)
return json.dumps(result) + "\n"
@app.route("/api/list_prompt_formats")
def api_list_prompt_formats():
global api_lock, verbose
if verbose: print("/api/list_prompt_formats")
with api_lock:
result = {"result": "ok", "prompt_formats": list_prompt_formats()}
if verbose: print("->", result)
return json.dumps(result) + "\n"
@app.route("/api/delete_block", methods=['POST'])
def api_delete_block():
global api_lock, verbose
if verbose: print("/api/delete_block")
with api_lock:
s = get_session()
data = request.get_json()
if verbose: print("<-", data)
s.delete_block(data["block_uuid"], data["delete_from_here"])
result = { "result": "ok" }
if verbose: print("->", result)
return json.dumps(result) + "\n"
@app.route("/api/edit_block", methods=['POST'])
def api_edit_block():
global api_lock, verbose
if verbose: print("/api/edit_block")
with api_lock:
s = get_session()
data = request.get_json()
if verbose: print("<-", data)
s.edit_block(data["block"])
result = { "result": "ok" }
if verbose: print("->", result)
return json.dumps(result) + "\n"
@app.route("/api/generate", methods=['POST'])
def api_generate():
global api_lock, verbose
if verbose: print("/api/generate")
with api_lock:
data = request.get_json()
if verbose: print("<-", data)
s = get_session()
if verbose: print("-> ...");
result = Response(stream_with_context(s.generate(data)), mimetype = 'application/json')
if verbose: print("->", result)
return result
@app.route("/api/cancel_generate")
def api_cancel_generate():
global api_lock_cancel, verbose
if verbose: print("/api/cancel_generate")
with api_lock_cancel:
set_cancel_signal()
result = { "result": "ok" }
if verbose: print("->", result)
return result
@app.route("/api/delete_session", methods=['POST'])
def api_delete_session():
global api_lock, verbose
if verbose: print("/api/delete_session")
with api_lock:
data = request.get_json()
if verbose: print("<-", data)
delete_session(data["session_uuid"]);
result = { "result": "ok" }
if verbose: print("->", result)
return json.dumps(result) + "\n"
@app.route("/api/remove_model", methods=['POST'])
def api_remove_model():
global api_lock, verbose
if verbose: print("/api/remove_model")
with api_lock:
data = request.get_json()
if verbose: print("<-", data)
remove_model(data)
result = { "result": "ok" }
if verbose: print("->", result)
return json.dumps(result) + "\n"
@app.route("/api/get_settings")
def api_get_settings():
global api_lock, verbose
if verbose: print("/api/get_settings")
with api_lock:
settings = get_settings()
result = { "result": "ok",
"settings": settings }
if verbose: print("->", result)
return json.dumps(result) + "\n"
@app.route("/api/set_settings", methods=['POST'])
def api_set_settings():
global api_lock, verbose
if verbose: print("/api/set_settings")
with api_lock:
data = request.get_json()
if verbose: print("<-", data)
set_settings(data["settings"])
result = { "result": "ok" }
if verbose: print("->", result)
return json.dumps(result) + "\n"
@app.route("/api/list_notepads")
def api_list_notepads():
global api_lock, verbose
if verbose: print("/api/list_notepads")
with api_lock:
n, c = list_notepads()
result = { "result": "ok", "notepads": n, "current_notepad": c }
if verbose: print("-> (...)")
return json.dumps(result) + "\n"
@app.route("/api/set_notepad", methods=['POST'])
def api_set_notepad():
global api_lock, verbose
if verbose: print("/api/set_notepad")
with api_lock:
data = request.get_json()
if verbose: print("<-", data)
r = set_notepad(data)
if r["notepad"] is not None:
result = { "result": "ok",
"notepad": r["notepad"] }
if "tokenized_text" in r:
result["tokenized_text"] = r["tokenized_text"]
if verbose: print("-> (...)")
else:
result = { "result": "fail" }
if verbose: print("->", result)
return json.dumps(result) + "\n"
@app.route("/api/new_notepad", methods=['POST'])
def api_new_notepad():
global api_lock, verbose
if verbose: print("/api/new_notepad")
with api_lock:
data = request.get_json()
if verbose: print("<-", data)
notepad = new_notepad()
if "settings" in data: get_notepad().update_settings(data["settings"])
if "text" in data: get_notepad().set_text(data["text"])
if "new_name" in data: get_notepad().rename(data)
result = { "result": "ok", "notepad": notepad }
if verbose: print("-> (...)")
return json.dumps(result) + "\n"
@app.route("/api/rename_notepad", methods=['POST'])
def api_rename_notepad():
global api_lock, verbose
if verbose: print("/api/rename_notepad")
with api_lock:
data = request.get_json()
if verbose: print("<-", data)
s = get_notepad()
s.rename(data)
result = { "result": "ok" }
if verbose: print("->", result)
return json.dumps(result) + "\n"
@app.route("/api/delete_notepad", methods=['POST'])
def api_delete_notepad():
global api_lock, verbose
if verbose: print("/api/delete_notepad")
with api_lock:
data = request.get_json()
if verbose: print("<-", data)
delete_notepad(data["notepad_uuid"]);
result = { "result": "ok" }
if verbose: print("->", result)
return json.dumps(result) + "\n"
@app.route("/api/update_notepad_settings", methods=['POST'])
def api_update_notepad_settings():
global api_lock, verbose
if verbose: print("/api/update_notepad_settings")
with api_lock:
n = get_notepad()
data = request.get_json()
if verbose: print("<-", data)
n.update_settings(data["settings"])
result = { "result": "ok" }
if verbose: print("->", result)
return json.dumps(result) + "\n"
@app.route("/api/set_notepad_text", methods=['POST'])
def api_set_notepad_text():
global api_lock, verbose
if verbose: print("/api/set_notepad_text")
with api_lock:
n = get_notepad()
data = request.get_json()
if verbose: print("<-", data)
n.set_text(data["text"])
tokenized_text = n.get_tokenized_text()
result = { "result": "ok", "tokenized_text": tokenized_text }
if verbose: print("-> (...)")
return json.dumps(result) + "\n"
@app.route("/api/notepad_single_token", methods=['POST'])
def api_notepad_single_token():
global api_lock, verbose
if verbose: print("/api/notepad_single_token")
with api_lock:
n = get_notepad()
data = request.get_json()
if verbose: print("<-", data)
result = n.generate_single_token(data)
if verbose: print("-> (...)")
return json.dumps(result) + "\n"
@app.route("/api/notepad_generate", methods=['POST'])
def api_notepad_generate():
global api_lock, verbose
if verbose: print("/api/notepad_generate")
with api_lock:
data = request.get_json()
if verbose: print("<-", data)
n = get_notepad()
if verbose: print("-> ...");
result = Response(stream_with_context(n.generate(data)), mimetype = 'application/json')
if verbose: print("->", result)
return result
@app.route("/api/cancel_notepad_generate")
def api_cancel_notepad_generate():
global api_lock_cancel, verbose
if verbose: print("/api/cancel_notepad_generate")
with api_lock_cancel:
set_notepad_cancel_signal()
result = { "result": "ok" }
if verbose: print("->", result)
return result
# Prepare torch
# torch.cuda._lazy_init()
# Prepare config
print(f" -- User dir: {args.dir}")
set_config_dir(args.dir)
global_state.load()
load_models()
# Start server
machine = args.host
host, port = machine.split(":")
browser_start = False
if host == "localhost" and not no_browser:
Timer(1, lambda: webbrowser.open(f'http://{machine}/')).start()
browser_start = True
print(f" -- Starting server on {host} port {port}")
if browser_start:
print(f" -- Opening UI in default web browser")
serve(app, host = host, port = port, threads = 8)