forked from ViperX7/Alpaca-Turbo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
244 lines (193 loc) · 6.97 KB
/
api.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
#!/bin/python
"""
▄▄▄· ▄▄▌ ▄▄▄· ▄▄▄· ▄▄· ▄▄▄· ▄▄▄▄▄▄• ▄▌▄▄▄ ▄▄▄▄·
▐█ ▀█ ██• ▐█ ▄█▐█ ▀█ ▐█ ▌▪▐█ ▀█ •██ █▪██▌▀▄ █·▐█ ▀█▪▪
▄█▀▀█ ██▪ ██▀·▄█▀▀█ ██ ▄▄▄█▀▀█ ▐█.▪█▌▐█▌▐▀▀▄ ▐█▀▀█▄ ▄█▀▄
▐█ ▪▐▌▐█▌▐▌▐█▪·•▐█ ▪▐▌▐███▌▐█ ▪▐▌ ▐█▌·▐█▄█▌▐█•█▌██▄▪▐█▐█▌.▐▌
▀ ▀ .▀▀▀ .▀ ▀ ▀ ·▀▀▀ ▀ ▀ ▀▀▀ ▀▀▀ .▀ ▀·▀▀▀▀ ▀█▄▀▪
https;//github.comViperX7/Alpaca-Turbo
"""
import mimetypes
import psutil
from alpaca_turbo import Assistant
from flask import Flask, jsonify, render_template, request
from flask_cors import CORS
from flask_socketio import SocketIO, emit
from helpers.prompts import Personas
mimetypes.add_type('application/javascript', '.js')
mimetypes.add_type('text/css', '.css')
app = Flask(
__name__,
static_url_path="",
static_folder="templates",
)
assistant = Assistant()
################################
CORS(app)
socketio = SocketIO(app, cors_allowed_origins="*")
personas = Personas("./prompts.json")
@socketio.on("connect")
def test_connect():
print("Socket connected")
@socketio.on("disconnect")
def test_disconnect():
print("socket disconnected")
@socketio.on("send_input")
def send_conv(data):
print("-----------")
print(data)
print("-----------")
inp = data["inp"]
fmt = data.get("fmt", "")
preprompt = data.get("pre", "")
if "\n" not in fmt:
fmt = fmt.replace("###","\n\n###")
fmt = fmt.replace(":",":\n\n")
fmt = fmt.strip("\n")
output = assistant.send_conv(preprompt, fmt, inp)
print("Attempting to GENERATE======================")
buffer = ""
for value in output: # (output, "Generating"):
buffer += value
emit("data", value)
print("=====LOG=====")
print(buffer)
print("=====LOG=====")
@app.route("/unload")
def unload_model():
result = assistant.safe_kill()
return jsonify({"result": result})
@app.route("/remove_all_chat")
def remove_all_chat():
result = assistant.remove_all_chat()
return jsonify({"result": result})
########################
@app.route("/list_models")
def list_models():
models = assistant.list_available_models(assistant.models_directory)
return jsonify(models)
@app.route("/load_model/<int:model_idx>")
def load_model(model_idx):
assistant.model_idx = model_idx
assistant.unload_model()
resp = assistant.load_model()
return jsonify({"status": resp})
@app.route("/stop")
def stop():
res = assistant.stop_generation()
return jsonify({"status": res})
@app.route("/status")
def status():
cpu_percent = psutil.cpu_percent()
ram_usage = psutil.virtual_memory().percent
total_ram = psutil.virtual_memory().total / (1024**3) # convert to GB
total_cores = psutil.cpu_count(logical=False)
total_threads = psutil.cpu_count(logical=True)
# threads_above_80 = len(
# [
# thread
# for thread in psutil.process_iter(attrs=["pid", "name", "cpu_percent"])
# if thread.info["cpu_percent"] > 80
# ]
# )
return jsonify(
{
"cpu_percent": cpu_percent,
"ram_usage": ram_usage,
"total_ram": total_ram,
"total_cores": total_cores,
"total_threads": total_threads,
# "threads_above_80": threads_above_80,
"is_model_loaded": assistant.is_loaded,
"turbo_status": assistant.current_state,
}
)
@app.route("/config", methods=["GET"])
def get_config():
return jsonify(
{
"threads": assistant.threads,
"top_k": assistant.top_k,
"top_p": assistant.top_p,
"temp": assistant.temp,
"repeat_penalty": assistant.repeat_penalty,
"seed": assistant.seed,
"n_predict": assistant.n_predict,
"repeat_last_n": assistant.repeat_last_n,
"batch_size": assistant.batch_size,
"antiprompt": assistant.antiprompt,
"models_directory": assistant.models_directory,
}
)
@app.route("/config", methods=["POST"])
def set_config():
data = request.get_json()
assistant.threads = data.get("threads", assistant.threads)
assistant.top_k = data.get("top_k", assistant.top_k)
assistant.top_p = data.get("top_p", assistant.top_p)
assistant.temp = data.get("temp", assistant.temp)
assistant.repeat_penalty = data.get("repeat_penalty", assistant.repeat_penalty)
assistant.seed = data.get("seed", assistant.seed)
assistant.n_predict = data.get("n_predict", assistant.n_predict)
assistant.repeat_last_n = data.get("repeat_last_n", assistant.repeat_last_n)
assistant.batch_size = data.get("batch_size", assistant.batch_size)
assistant.antiprompt = data.get("antiprompt", assistant.antiprompt)
assistant.models_directory = data.get("models_directory", assistant.models_directory)
return jsonify({"success": True})
@app.route("/load_chat/<string:id>", methods=["GET"])
def load_chat(id):
result = assistant.load_chat(id)
try:
new_res = [res.to_json() for res in result]
except AttributeError:
new_res = result
return jsonify(new_res)
@app.route("/save_chat")
def save_chat():
result = assistant.save_chat()
return jsonify(result)
@app.route("/get_conv_logs", methods=["GET"])
def get_conv_logs():
result = assistant.get_conv_logs()
return jsonify(result)
@app.route("/clear_chat", methods=["GET"])
def clear_chat():
result = assistant.clear_chat()
return jsonify(result)
@app.route("/personas", methods=["GET"])
def get_personas():
personas_list = personas.get_all()
return jsonify(personas_list)
@app.route("/personas/<string:name>", methods=["GET"])
def get_persona(name):
persona_data = personas.get(name)
if persona_data:
return jsonify(persona_data)
else:
return jsonify({"error": "Persona not found."})
@app.route("/personas", methods=["POST"])
def add_persona():
data = request.json
if "name" not in data or "data" not in data:
return jsonify({"error": "Name and data fields are required."})
name = data["name"]
persona_data = data["data"]
personas.add(name, persona_data)
return jsonify({"success": True})
@app.route("/personas/<string:name>", methods=["PUT"])
def update_persona(name):
data = request.json
persona_data = data["data"]
personas.update(name, persona_data)
return jsonify({"success": True})
@app.route("/")
def index():
return render_template("index.html")
if __name__ == "__main__":
socketio.run(
app,
host="0.0.0.0",
port=7887,
allow_unsafe_werkzeug=True,
debug=True,
)