2023-09-11 17:49:30 -04:00
|
|
|
from modules import shared
|
2023-11-15 19:13:36 -05:00
|
|
|
from modules.logging_colors import logger
|
2023-11-18 22:35:22 -05:00
|
|
|
from modules.LoRA import add_lora_to_model
|
2023-11-07 23:58:06 -05:00
|
|
|
from modules.models import load_model, unload_model
|
|
|
|
from modules.models_settings import get_model_metadata, update_model_parameters
|
2023-11-18 22:35:22 -05:00
|
|
|
from modules.utils import get_available_loras, get_available_models
|
2023-07-11 17:50:08 -04:00
|
|
|
|
2023-07-12 14:33:25 -04:00
|
|
|
|
2023-11-07 21:59:02 -05:00
|
|
|
def get_current_model_info():
|
|
|
|
return {
|
|
|
|
'model_name': shared.model_name,
|
2024-04-18 23:24:46 -04:00
|
|
|
'lora_names': shared.lora_names,
|
|
|
|
'loader': shared.args.loader
|
2023-11-07 21:59:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-07 22:59:27 -05:00
|
|
|
def list_models():
|
2023-11-18 22:35:22 -05:00
|
|
|
return {'model_names': get_available_models()[1:]}
|
|
|
|
|
|
|
|
|
|
|
|
def list_dummy_models():
|
2023-11-07 22:59:27 -05:00
|
|
|
result = {
|
2023-07-11 17:50:08 -04:00
|
|
|
"object": "list",
|
2023-11-07 22:59:27 -05:00
|
|
|
"data": []
|
2023-07-11 17:50:08 -04:00
|
|
|
}
|
|
|
|
|
2023-11-18 22:35:22 -05:00
|
|
|
# these are expected by so much, so include some here as a dummy
|
|
|
|
for model in ['gpt-3.5-turbo', 'text-embedding-ada-002']:
|
2023-11-07 22:59:27 -05:00
|
|
|
result["data"].append(model_info_dict(model))
|
2023-07-11 17:50:08 -04:00
|
|
|
|
2023-11-07 22:59:27 -05:00
|
|
|
return result
|
2023-07-11 17:50:08 -04:00
|
|
|
|
2023-11-07 22:59:27 -05:00
|
|
|
|
|
|
|
def model_info_dict(model_name: str) -> dict:
|
2023-07-11 17:50:08 -04:00
|
|
|
return {
|
|
|
|
"id": model_name,
|
|
|
|
"object": "model",
|
2023-11-07 22:59:27 -05:00
|
|
|
"created": 0,
|
|
|
|
"owned_by": "user"
|
2023-07-11 17:50:08 -04:00
|
|
|
}
|
2023-11-07 22:59:27 -05:00
|
|
|
|
|
|
|
|
2023-11-07 23:58:06 -05:00
|
|
|
def _load_model(data):
|
|
|
|
model_name = data["model_name"]
|
|
|
|
args = data["args"]
|
|
|
|
settings = data["settings"]
|
|
|
|
|
|
|
|
unload_model()
|
|
|
|
model_settings = get_model_metadata(model_name)
|
2023-11-08 20:41:12 -05:00
|
|
|
update_model_parameters(model_settings)
|
2023-11-07 23:58:06 -05:00
|
|
|
|
|
|
|
# Update shared.args with custom model loading settings
|
|
|
|
if args:
|
|
|
|
for k in args:
|
2023-11-08 00:34:13 -05:00
|
|
|
if hasattr(shared.args, k):
|
2023-11-07 23:58:06 -05:00
|
|
|
setattr(shared.args, k, args[k])
|
|
|
|
|
|
|
|
shared.model, shared.tokenizer = load_model(model_name)
|
2023-11-10 10:34:27 -05:00
|
|
|
|
2023-11-07 23:58:06 -05:00
|
|
|
# Update shared.settings with custom generation defaults
|
|
|
|
if settings:
|
|
|
|
for k in settings:
|
|
|
|
if k in shared.settings:
|
|
|
|
shared.settings[k] = settings[k]
|
2023-11-15 19:13:36 -05:00
|
|
|
if k == 'truncation_length':
|
|
|
|
logger.info(f"TRUNCATION LENGTH (UPDATED): {shared.settings['truncation_length']}")
|
|
|
|
elif k == 'instruction_template':
|
|
|
|
logger.info(f"INSTRUCTION TEMPLATE (UPDATED): {shared.settings['instruction_template']}")
|
2023-11-18 22:35:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
def list_loras():
|
|
|
|
return {'lora_names': get_available_loras()[1:]}
|
|
|
|
|
|
|
|
|
|
|
|
def load_loras(lora_names):
|
|
|
|
add_lora_to_model(lora_names)
|
|
|
|
|
|
|
|
|
|
|
|
def unload_all_loras():
|
|
|
|
add_lora_to_model([])
|