-
Notifications
You must be signed in to change notification settings - Fork 10
/
runWebsite.py
256 lines (204 loc) · 8.06 KB
/
runWebsite.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
import socketserver
import json
import http.server
import webbrowser
from rwkvChatPersonalities import Personaities
import numpy as np
import os
from typing import Dict as dict
import gc
try:
import inquirer
except:
install = not input(
"inquirer not installed. Would you like to install it? (Y/n)") in ["n", "N"]
if install:
os.system("pip3 install inquirer")
import inquirer
else:
print("Exiting...")
exit()
try:
from scipy.special import softmax
except:
install = inquirer.prompt([inquirer.Confirm(
'install',
message="scipy not installed. Would you like to install it?",
default=True,
)])["install"]
if install:
os.system("pip3 install scipy")
from scipy.special import softmax
else:
print("Exiting...")
exit()
try:
import torch
except:
install = inquirer.prompt([inquirer.Confirm(
'install',
message="torch not installed. Would you like to install it? NOTE: AMD GPU users should double check https://pytorch.org/get-started/locally/",
default=True,
)])["install"]
if install:
# test if amd gpu
os.system(
"pip3 install torch torchvision torchaudio")
import torch
else:
print("Exiting...")
exit()
# check if yarn installed
if os.system("yarn --version"):
install = inquirer.prompt([inquirer.Confirm(
'install',
message="yarn not installed. Would you like to install it?",
default=True,
)])["install"]
if install:
if os.system("npm install -g yarn"):
print("Failed to install yarn using npm. you need to install nodejs manually from https://nodejs.org/en/download/")
exit()
else:
print("Exiting...")
exit()
from rwkvstic.rwkvMaster import RWKVMaster
async def runWebsite(model: RWKVMaster):
os.chdir("web-interface")
os.system("yarn")
os.system("yarn build")
os.chdir("..")
files = os.listdir()
# filter by ending in .pth
files = [f for f in files if f.endswith(".pth")]
###### A good prompt for chatbot ######
# context = "hello world! I am your supreme overlord!"
########################################################################################################
print(f'\nOptimizing speed...')
gc.collect()
torch.cuda.empty_cache()
########################################################################################################
print(
"Note: currently the first run takes a while if your prompt is long, as we are using RNN to preprocess the prompt. Use GPT to build the hidden state for better speed.\n"
)
print("torch.cuda.memory_allocated: %fGB" %
(torch.cuda.memory_allocated(0)/1024/1024/1024))
print("torch.cuda.memory_reserved: %fGB" %
(torch.cuda.memory_reserved(0)/1024/1024/1024))
print("torch.cuda.max_memory_reserved: %fGB" %
(torch.cuda.max_memory_reserved(0)/1024/1024/1024))
people: dict[str, str] = {}
progress = {}
for P, personality in Personaities.items():
people[P] = model.loadContext(
ctx="\n", newctx=personality, statex=model.emptyState)[1]
class S(http.server.SimpleHTTPRequestHandler):
def do_OPTIONS(self):
self.send_response(200, "ok")
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods',
'GET, POST, OPTIONS')
self.send_header("Access-Control-Allow-Headers",
"X-Requested-With, Content-Type")
self.end_headers()
def do_GET(self) -> None:
self.send_response(200)
self.send_header('Access-Control-Allow-Origin', '*')
if "css" in self.path:
self.send_header('Content-type', 'text/css')
elif "js" in self.path:
self.send_header('Content-type', 'text/javascript')
else:
self.send_header('Content-type', 'text/html')
self.end_headers()
if "ping" in self.path:
self.wfile.write("pong".encode('utf-8'))
return
if (self.path == "/"):
self.path = "/index.html"
if (self.path == "/personalities.json"):
self.wfile.write(json.dumps(
list(people.keys())).encode('utf-8'))
return
if ("/progress" in self.path):
self.wfile.write(
json.dumps(progress.get(self.path.split("/")[-1])).encode('utf-8'))
return
# self._set_response()
self.wfile.write(
open("web-interface/build/"+self.path, "rb").read())
def do_POST(self):
self.send_response(200)
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Content-type', 'text/html')
if (self.path == "/progress"):
state = json.loads(self.rfile.read(
int(self.headers['Content-Length'])).decode('utf-8'))
progress[state["key"]] = state["state"]
self.wfile.write("ok".encode('utf-8'))
return
self.end_headers()
# Get body
content_length = int(self.headers['Content-Length'])
body = self.rfile.read(content_length)
body = body.decode('utf-8')
# get json
body = json.loads(body)
print(body["message"])
character = body.get("character", list(people.keys())[0])
body["state"] = progress.get(
body.get("state", body["key"]), None)
if body["state"] is None:
body["state"] = people[character]
else:
body["state"] = (
list(map(lambda xx: model.initTensor(torch.tensor(xx)), body["state"])))
progresskey = body["key"]
tokens = (
body["message"]+f"\n\nLong Expert Full Response:")
def updateProgress(x):
# send current chunk
# the eyes emoji
emoj = "👀"
self.wfile.write(
json.dumps({"response": emoj, "done": False, "progress": len(model.tokenizer.decode(x))}).encode('utf-8'))
model.loadContext(
ctx="\nUser", newctx=tokens, statex=body["state"], progressCallBack=updateProgress)
currentData = (tokens, model.getState())
ln = len(currentData[0])
response = ""
for i in range(400):
x = model.forward(state=currentData[1])["output"]
response += x
self.wfile.write(
json.dumps({"done": False, "response": response.replace("\nUser", ""), "progress": -1}).encode('utf-8'))
currentData = (currentData[0]+x, model.getState())
if (currentData[0]).strip().endswith(("\nUser")):
break
tokens = currentData[0][ln:]
# flatten
print(tokens)
out = {}
out["response"] = tokens + ":"
out["done"] = True
out["progress"] = -1
out["state"] = progresskey
# recursively convert state to number from tensor
state = currentData[1]
ns = []
for i in range(len(state)):
try:
ns += [np.array(state[i].float().cpu()).tolist()]
except:
ns += [np.array(state[i]).tolist()]
progress[progresskey] = ns
# set content length
out = json.dumps(out).encode("utf8")
self.wfile.write(out)
# end connection
self.close_connection = True
httpd = socketserver.ThreadingTCPServer(('', int(input("Port:"))), S)
# open browser
if (input("Open browser? (y/N)") == "y"):
webbrowser.open("http://localhost:"+str(httpd.server_address[1]))
httpd.serve_forever()