-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
393 lines (327 loc) · 10.8 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
# -*- coding: utf-8 -*-
import time, random, re, sys, os, signal, json, tqdm
from quart import Quart, websocket, request
from hypercorn.config import Config
from hypercorn.asyncio import serve
import asyncio
from rwkv import (
RWKVChater,
RWKVNicknameGener,
RWKVGroupChater,
RWKVInterruptException,
process_default_state,
)
from app_util import prxxx, gen_echo, clean_symbols
from typing import Dict, Tuple
from config import MODEL_STATE_NAME, APP_BIND, APP_AUTOSAVE_TIME, APP_TEST_MESSAGE
from show_state import show_state_delta
with open("help.min.html", "r") as f:
flask_help = f.read()
random.seed(time.time())
chaters: Dict[str, RWKVChater] = {}
group_chaters: Dict[str, RWKVGroupChater] = {}
nicknameGener = RWKVNicknameGener()
app = Quart(__name__)
def restart():
# app.shutdown()
python = sys.executable
prxxx("### Restart ! ###")
os.execl(python, python, *sys.argv)
def stop(signal=None, frame=None):
# app.shutdown()
prxxx("### STOP ! ###")
sys.exit()
async def save_chaters_state():
for id in tqdm.tqdm(chaters, desc="Save chater", leave=False, unit="chr"):
await asyncio.sleep(0)
await chaters[id].save_state(id, q=True)
for id in tqdm.tqdm(
group_chaters, desc="Save grpup chater", leave=False, unit="chr"
):
await asyncio.sleep(0)
await group_chaters[id].save_state(id, q=False)
def save_chaters_state_sync():
for id in tqdm.tqdm(chaters, desc="Save chater", leave=False, unit="chr"):
if chaters[id].need_save:
chaters[id].state.save_sync(id)
prxxx(f"Save state name: {id}")
for id in tqdm.tqdm(
group_chaters, desc="Save grpup chater", leave=False, unit="chr"
):
if group_chaters[id].need_save:
group_chaters[id].state.save_sync(id)
prxxx(f"Save state name: {id}")
async def time_to_save():
while True:
for i in range(APP_AUTOSAVE_TIME): # 防止卡服务器关闭
await asyncio.sleep(1)
await save_chaters_state()
prxxx("Autosave all chater")
async def chat(
message: str,
id: str = "-b2bi0JgEhJru87HTcRjh9vdT",
user: str = "木子",
nickname: str = "墨子",
state: str = MODEL_STATE_NAME,
debug=False,
echo=None,
) -> Tuple[str, bool]:
id = clean_symbols(id)
echo = gen_echo()
if not id in chaters:
prxxx()
chaters[id] = RWKVChater(id, state_name=state)
await chaters[id].init_state()
prxxx()
prxxx(f" # Chat id: {id} | user: {user} | echo: {echo}")
prxxx(f" # -M->[{message}]-{echo}")
answer, original, is_want_to_say = await chaters[id].chat(
message=message, chatuser=user, nickname=nickname, debug=debug
)
prxxx()
prxxx(f" # Chat id: {id} | nickname: {nickname} | echo: {echo}")
prxxx(f" # {echo}-[{original}][{'FY'[is_want_to_say]}]<-O-")
prxxx(f" # {echo}-[{answer}]<-A-")
# 如果接受到的内容为空,则给出相应的回复
if answer.isspace() or len(answer) == 0:
answer = "喵喵喵?"
return answer, is_want_to_say
async def group_chat_send(
message: str,
id: str = "-b2bi0JgEhJru87HTcRjh9vdT",
user: str = "木子",
state: str = MODEL_STATE_NAME,
echo=None,
) -> None: # -> Tuple[str, bool]:
id = clean_symbols(id)
if len(message) == 0:
return
echo = gen_echo()
if not id in group_chaters:
prxxx()
group_chaters[id] = RWKVGroupChater(id, state_name=state)
await group_chaters[id].init_state()
prxxx()
prxxx(f" # Send Gchat id: {id} | user: {user} | echo: {echo}")
prxxx(f" # -M->[{message}]-{echo}")
await group_chaters[id].send_message(message=message, chatuser=user)
async def group_chat_get(
id: str = "-b2bi0JgEhJru87HTcRjh9vdT",
nickname: str = "墨子",
state: str = MODEL_STATE_NAME,
echo=None,
) -> Tuple[str, bool]:
id = clean_symbols(id)
echo = gen_echo()
if not id in group_chaters:
prxxx()
group_chaters[id] = RWKVGroupChater(id, state_name=state)
await group_chaters[id].init_state()
answer, original, is_want_to_say = await group_chaters[id].get_answer(
nickname=nickname
)
prxxx()
prxxx(f" # Get gchat id: {id} | nickname: {nickname} | echo: {echo}")
prxxx(f" # {echo}-[{original}][{'FY'[is_want_to_say]}]<-O-")
prxxx(f" # {echo}-[{answer}]<-A-")
# 如果接受到的内容为空,则给出相应的回复
if answer.isspace() or len(answer) == 0:
answer = "喵喵喵?"
return answer, is_want_to_say
async def gen_nickname(name: str, echo=None):
echo = gen_echo()
prxxx()
prxxx(f" # GenNickname echo: {echo}")
prxxx(f" # -N->[{name}]-{echo}")
nickname, _ = await nicknameGener.gen_nickname(name)
prxxx()
prxxx(f" # GenNickname echo: {echo}")
prxxx(f" # {echo}-[{nickname}]<-N-")
# 如果接受到的内容为空,则给出相应的回复
if nickname.isspace() or len(nickname) == 0 or nickname == "None":
nickname = name
return nickname
async def reset_state(id: str, echo=None):
id = clean_symbols(id)
flag = False
if id in chaters:
await chaters[id].reset_state()
flag = True
if id in group_chaters:
await group_chaters[id].reset_state()
flag = True
return flag
@app.route("/chat", methods=["POST", "GET"])
async def R_chat():
if request.method == "GET":
kwargs = request.args
elif request.method == "POST":
kwargs = await request.form
try:
answer, is_want_to_say = await chat(**kwargs)
return {"message": answer, "is_want_to_say": is_want_to_say, "state": "ok"}
except RWKVInterruptException:
return {"state": "interrupted"}
@app.route("/group_chat_send", methods=["POST", "GET"])
async def R_group_chat_send():
if request.method == "GET":
kwargs = request.args
elif request.method == "POST":
kwargs = await request.form
await group_chat_send(**kwargs)
return {"state": "ok"}
@app.route("/group_chat_get", methods=["POST", "GET"])
async def R_group_chat_get():
if request.method == "GET":
kwargs = request.args
elif request.method == "POST":
kwargs = await request.form
answer, is_want_to_say = await group_chat_get(**kwargs)
return {"message": answer, "is_want_to_say": is_want_to_say, "state": "ok"}
@app.route("/nickname", methods=["POST", "GET"])
async def R_nickname():
if request.method == "GET":
kwargs = request.args
elif request.method == "POST":
kwargs = await request.form
nickname = await gen_nickname(**kwargs)
return {"nickname": nickname, "state": "ok"}
@app.route("/reset_state", methods=["GET"])
async def R_reset_state():
if request.method == "GET":
kwargs = request.args
elif request.method == "POST":
kwargs = await request.form
flag = await reset_state(**kwargs)
return {"state": "ok" if flag else "a?"}
@app.route("/save_state", methods=["GET"])
async def R_save_state():
if request.method == "GET":
kwargs = request.args
elif request.method == "POST":
kwargs = await request.form
flag = await save_chaters_state(**kwargs)
return {"state": "ok"}
@app.route("/restart", methods=["GET"])
async def R_restart():
if request.args["passwd_gkd"] == "ihAVEcODE":
await app.shutdown()
restart()
return {"state": "fuck you!"}
@app.route("/stop", methods=["GET"])
async def R_stop():
if request.args["passwd_gkd"] == "ihAVEcODE":
await app.shutdown()
stop()
return {"state": "fuck you!"}
@app.route("/", methods=["GET"])
async def R_index():
return flask_help
@app.websocket("/chat")
async def W_chat():
while True:
data = json.loads(await websocket.receive())
"""
data{
id
message
username
nickname*
default_state*
echo*
debug*
}
"""
try:
answer, is_want_to_say = await chat(**data)
await websocket.send(
json.dumps(
{
"message": answer,
"is_want_to_say": is_want_to_say,
"state": "OK",
"echo": data.get("echo", ""),
}
)
)
except RWKVInterruptException:
await websocket.send(
json.dumps({"state": "interrupted", "echo": data.get("echo", "")})
)
@app.websocket("/group_chat")
async def W_group_chat():
while True:
data = json.loads(await websocket.receive())
"""
data{
action
id
message+
username+
nickname*
default_state*
echo*
}
"""
if data["action"] == "send":
await group_chat_send(**data)
await websocket.send(
json.dumps({"state": "OK", "echo": data.get("echo", "")})
)
elif data["action"] == "get":
answer, is_want_to_say = await group_chat_get(**data)
await websocket.send(
json.dumps(
{
"message": answer,
"is_want_to_say": is_want_to_say,
"state": "OK",
"echo": data.get("echo", ""),
}
)
)
else:
await websocket.send(
json.dumps({"state": "A?", "echo": data.get("echo", "")})
)
# @app.before_serving
async def before_serving():
# app.add_background_task(time_to_save)
await process_default_state()
await nicknameGener.init_state()
init = RWKVChater("init")
chaters["init"] = init
await init.init_state()
prxxx(f"State size: {init.state.state.size}")
await init.reset_state()
await chat(
**{
"id": "init",
"message": APP_TEST_MESSAGE,
"user": "测试者",
}
)
prxxx()
prxxx(" *#* RWKV!高性能ですから! *#*")
prxxx()
prxxx("Web api server start!\a")
prxxx(f"API bind: {APP_BIND}")
@app.after_serving
async def after_serving():
save_chaters_state_sync()
global chaters, group_chaters
del chaters, group_chaters
prxxx("### STOP ! ###")
async def main():
await before_serving() # fix: timeout wen shutup
config = Config()
config.bind = APP_BIND
config.use_reloader = True
config.loglevel = "debug"
"""
for i in tqdm.trange(99999):
await group_chat_send({"id":"ggtgg","message":"uuuu","user":"yyyyy"})
"""
await serve(app, config)
if __name__ == "__main__":
asyncio.run(main())