2023-04-23 20:52:43 +02:00
|
|
|
import json
|
2023-10-13 06:31:13 +02:00
|
|
|
import ssl
|
2023-04-23 20:52:43 +02:00
|
|
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
|
|
from threading import Thread
|
|
|
|
|
2023-05-10 03:49:39 +02:00
|
|
|
from extensions.api.util import build_parameters, try_start_cloudflared
|
2023-04-23 20:52:43 +02:00
|
|
|
from modules import shared
|
2023-05-20 23:42:17 +02:00
|
|
|
from modules.chat import generate_chat_reply
|
2023-06-08 16:34:36 +02:00
|
|
|
from modules.LoRA import add_lora_to_model
|
2023-06-09 05:30:22 +02:00
|
|
|
from modules.models import load_model, unload_model
|
2023-09-11 23:49:30 +02:00
|
|
|
from modules.models_settings import get_model_metadata, update_model_parameters
|
2023-07-25 23:49:56 +02:00
|
|
|
from modules.text_generation import (
|
|
|
|
encode,
|
|
|
|
generate_reply,
|
|
|
|
stop_everything_event
|
|
|
|
)
|
2023-06-08 16:34:36 +02:00
|
|
|
from modules.utils import get_available_models
|
2023-10-13 06:31:13 +02:00
|
|
|
from modules.logging_colors import logger
|
2023-06-08 16:34:36 +02:00
|
|
|
|
2023-06-09 05:30:22 +02:00
|
|
|
|
2023-06-08 16:34:36 +02:00
|
|
|
def get_model_info():
|
|
|
|
return {
|
|
|
|
'model_name': shared.model_name,
|
|
|
|
'lora_names': shared.lora_names,
|
|
|
|
# dump
|
|
|
|
'shared.settings': shared.settings,
|
|
|
|
'shared.args': vars(shared.args),
|
|
|
|
}
|
2023-04-23 20:52:43 +02:00
|
|
|
|
2023-06-17 00:00:37 +02:00
|
|
|
|
2023-04-23 20:52:43 +02:00
|
|
|
class Handler(BaseHTTPRequestHandler):
|
|
|
|
def do_GET(self):
|
|
|
|
if self.path == '/api/v1/model':
|
|
|
|
self.send_response(200)
|
|
|
|
self.end_headers()
|
|
|
|
response = json.dumps({
|
|
|
|
'result': shared.model_name
|
|
|
|
})
|
|
|
|
|
|
|
|
self.wfile.write(response.encode('utf-8'))
|
|
|
|
else:
|
|
|
|
self.send_error(404)
|
|
|
|
|
|
|
|
def do_POST(self):
|
|
|
|
content_length = int(self.headers['Content-Length'])
|
|
|
|
body = json.loads(self.rfile.read(content_length).decode('utf-8'))
|
|
|
|
|
|
|
|
if self.path == '/api/v1/generate':
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header('Content-Type', 'application/json')
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
prompt = body['prompt']
|
|
|
|
generate_params = build_parameters(body)
|
|
|
|
stopping_strings = generate_params.pop('stopping_strings')
|
2023-05-05 23:53:03 +02:00
|
|
|
generate_params['stream'] = False
|
2023-04-23 20:52:43 +02:00
|
|
|
|
|
|
|
generator = generate_reply(
|
2023-05-11 20:37:04 +02:00
|
|
|
prompt, generate_params, stopping_strings=stopping_strings, is_chat=False)
|
2023-04-23 20:52:43 +02:00
|
|
|
|
|
|
|
answer = ''
|
|
|
|
for a in generator:
|
2023-05-11 20:37:04 +02:00
|
|
|
answer = a
|
2023-04-23 20:52:43 +02:00
|
|
|
|
|
|
|
response = json.dumps({
|
|
|
|
'results': [{
|
2023-05-11 22:07:20 +02:00
|
|
|
'text': answer
|
2023-04-23 20:52:43 +02:00
|
|
|
}]
|
|
|
|
})
|
2023-05-20 23:42:17 +02:00
|
|
|
|
2023-04-23 20:52:43 +02:00
|
|
|
self.wfile.write(response.encode('utf-8'))
|
2023-05-20 23:42:17 +02:00
|
|
|
|
|
|
|
elif self.path == '/api/v1/chat':
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header('Content-Type', 'application/json')
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
user_input = body['user_input']
|
|
|
|
regenerate = body.get('regenerate', False)
|
|
|
|
_continue = body.get('_continue', False)
|
|
|
|
|
|
|
|
generate_params = build_parameters(body, chat=True)
|
|
|
|
generate_params['stream'] = False
|
|
|
|
|
|
|
|
generator = generate_chat_reply(
|
2023-07-05 02:36:47 +02:00
|
|
|
user_input, generate_params, regenerate=regenerate, _continue=_continue, loading_message=False)
|
2023-05-20 23:42:17 +02:00
|
|
|
|
2023-07-05 02:36:47 +02:00
|
|
|
answer = generate_params['history']
|
2023-05-20 23:42:17 +02:00
|
|
|
for a in generator:
|
|
|
|
answer = a
|
|
|
|
|
|
|
|
response = json.dumps({
|
|
|
|
'results': [{
|
|
|
|
'history': answer
|
|
|
|
}]
|
|
|
|
})
|
|
|
|
|
|
|
|
self.wfile.write(response.encode('utf-8'))
|
|
|
|
|
2023-05-31 03:03:40 +02:00
|
|
|
elif self.path == '/api/v1/stop-stream':
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header('Content-Type', 'application/json')
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
stop_everything_event()
|
|
|
|
|
|
|
|
response = json.dumps({
|
|
|
|
'results': 'success'
|
|
|
|
})
|
|
|
|
|
|
|
|
self.wfile.write(response.encode('utf-8'))
|
|
|
|
|
2023-06-08 16:34:36 +02:00
|
|
|
elif self.path == '/api/v1/model':
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header('Content-Type', 'application/json')
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
# by default return the same as the GET interface
|
|
|
|
result = shared.model_name
|
|
|
|
|
|
|
|
# Actions: info, load, list, unload
|
|
|
|
action = body.get('action', '')
|
|
|
|
|
|
|
|
if action == 'load':
|
|
|
|
model_name = body['model_name']
|
|
|
|
args = body.get('args', {})
|
|
|
|
print('args', args)
|
|
|
|
for k in args:
|
|
|
|
setattr(shared.args, k, args[k])
|
|
|
|
|
|
|
|
shared.model_name = model_name
|
|
|
|
unload_model()
|
|
|
|
|
2023-09-11 23:49:30 +02:00
|
|
|
model_settings = get_model_metadata(shared.model_name)
|
2023-09-12 05:13:10 +02:00
|
|
|
shared.settings.update({k: v for k, v in model_settings.items() if k in shared.settings})
|
2023-06-08 16:34:36 +02:00
|
|
|
update_model_parameters(model_settings, initial=True)
|
|
|
|
|
|
|
|
if shared.settings['mode'] != 'instruct':
|
|
|
|
shared.settings['instruction_template'] = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
shared.model, shared.tokenizer = load_model(shared.model_name)
|
|
|
|
if shared.args.lora:
|
2023-06-17 00:00:37 +02:00
|
|
|
add_lora_to_model(shared.args.lora) # list
|
2023-06-08 16:34:36 +02:00
|
|
|
|
|
|
|
except Exception as e:
|
2023-06-17 00:00:37 +02:00
|
|
|
response = json.dumps({'error': {'message': repr(e)}})
|
2023-06-08 16:34:36 +02:00
|
|
|
|
|
|
|
self.wfile.write(response.encode('utf-8'))
|
|
|
|
raise e
|
|
|
|
|
|
|
|
shared.args.model = shared.model_name
|
|
|
|
|
|
|
|
result = get_model_info()
|
|
|
|
|
|
|
|
elif action == 'unload':
|
|
|
|
unload_model()
|
|
|
|
shared.model_name = None
|
|
|
|
shared.args.model = None
|
|
|
|
result = get_model_info()
|
|
|
|
|
|
|
|
elif action == 'list':
|
|
|
|
result = get_available_models()
|
|
|
|
|
|
|
|
elif action == 'info':
|
|
|
|
result = get_model_info()
|
|
|
|
|
|
|
|
response = json.dumps({
|
|
|
|
'result': result,
|
|
|
|
})
|
|
|
|
|
|
|
|
self.wfile.write(response.encode('utf-8'))
|
|
|
|
|
2023-04-23 20:52:43 +02:00
|
|
|
elif self.path == '/api/v1/token-count':
|
|
|
|
self.send_response(200)
|
|
|
|
self.send_header('Content-Type', 'application/json')
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
tokens = encode(body['prompt'])[0]
|
|
|
|
response = json.dumps({
|
|
|
|
'results': [{
|
|
|
|
'tokens': len(tokens)
|
|
|
|
}]
|
|
|
|
})
|
2023-05-20 23:42:17 +02:00
|
|
|
|
2023-04-23 20:52:43 +02:00
|
|
|
self.wfile.write(response.encode('utf-8'))
|
|
|
|
else:
|
|
|
|
self.send_error(404)
|
|
|
|
|
2023-06-24 15:16:06 +02:00
|
|
|
def do_OPTIONS(self):
|
|
|
|
self.send_response(200)
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
def end_headers(self):
|
|
|
|
self.send_header('Access-Control-Allow-Origin', '*')
|
|
|
|
self.send_header('Access-Control-Allow-Methods', '*')
|
|
|
|
self.send_header('Access-Control-Allow-Headers', '*')
|
|
|
|
self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate')
|
|
|
|
super().end_headers()
|
|
|
|
|
2023-04-23 20:52:43 +02:00
|
|
|
|
2023-08-09 03:20:27 +02:00
|
|
|
def _run_server(port: int, share: bool = False, tunnel_id=str):
|
2023-04-23 20:52:43 +02:00
|
|
|
address = '0.0.0.0' if shared.args.listen else '127.0.0.1'
|
|
|
|
server = ThreadingHTTPServer((address, port), Handler)
|
|
|
|
|
2023-10-13 06:31:13 +02:00
|
|
|
ssl_certfile = shared.args.ssl_certfile
|
|
|
|
ssl_keyfile = shared.args.ssl_keyfile
|
|
|
|
ssl_verify = True if (ssl_keyfile and ssl_certfile) else False
|
|
|
|
if ssl_verify:
|
|
|
|
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
|
|
|
context.load_cert_chain(ssl_certfile, ssl_keyfile)
|
|
|
|
server.socket = context.wrap_socket(server.socket, server_side=True)
|
|
|
|
|
2023-04-23 20:52:43 +02:00
|
|
|
def on_start(public_url: str):
|
2023-10-22 06:11:07 +02:00
|
|
|
logger.info(f'Blocking API URL: \n\n{public_url}/api\n')
|
2023-04-23 20:52:43 +02:00
|
|
|
|
|
|
|
if share:
|
|
|
|
try:
|
2023-08-09 03:20:27 +02:00
|
|
|
try_start_cloudflared(port, tunnel_id, max_attempts=3, on_start=on_start)
|
2023-04-23 20:52:43 +02:00
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
else:
|
2023-10-13 06:31:13 +02:00
|
|
|
if ssl_verify:
|
2023-10-22 06:11:07 +02:00
|
|
|
logger.info(f'Blocking API URL: \n\nhttps://{address}:{port}/api\n')
|
2023-10-13 06:31:13 +02:00
|
|
|
else:
|
2023-10-22 06:11:07 +02:00
|
|
|
logger.info(f'Blocking API URL: \n\nhttp://{address}:{port}/api\n')
|
2023-04-23 20:52:43 +02:00
|
|
|
|
|
|
|
server.serve_forever()
|
|
|
|
|
|
|
|
|
2023-08-09 03:20:27 +02:00
|
|
|
def start_server(port: int, share: bool = False, tunnel_id=str):
|
|
|
|
Thread(target=_run_server, args=[port, share, tunnel_id], daemon=True).start()
|