RWKV-Runner/backend-python/routes/config.py

67 lines
1.9 KiB
Python
Raw Normal View History

2023-05-07 17:27:54 +08:00
import pathlib
2023-05-07 22:48:52 +08:00
from fastapi import APIRouter, HTTPException, Response, status
2023-05-07 17:27:54 +08:00
from pydantic import BaseModel
from langchain.llms import RWKV
from utils.rwkv import *
from utils.torch import *
import global_var
router = APIRouter()
2023-05-17 11:39:00 +08:00
class SwitchModelBody(BaseModel):
model: str
strategy: str
2023-05-07 17:27:54 +08:00
2023-05-17 11:39:00 +08:00
@router.post("/switch-model")
def switch_model(body: SwitchModelBody, response: Response):
if global_var.get(global_var.Model_Status) is global_var.ModelStatus.Loading:
2023-05-07 22:48:52 +08:00
response.status_code = status.HTTP_304_NOT_MODIFIED
return
2023-05-07 17:27:54 +08:00
global_var.set(global_var.Model_Status, global_var.ModelStatus.Offline)
global_var.set(global_var.Model, None)
torch_gc()
global_var.set(global_var.Model_Status, global_var.ModelStatus.Loading)
try:
2023-05-17 11:39:00 +08:00
global_var.set(
global_var.Model,
RWKV(
model=body.model,
strategy=body.strategy,
tokens_path=f"{pathlib.Path(__file__).parent.parent.resolve()}/20B_tokenizer.json",
),
)
2023-05-18 21:19:13 +08:00
except Exception as e:
print(e)
2023-05-07 17:27:54 +08:00
global_var.set(global_var.Model_Status, global_var.ModelStatus.Offline)
raise HTTPException(status.HTTP_500_INTERNAL_SERVER_ERROR, "failed to load")
2023-05-17 11:39:00 +08:00
if global_var.get(global_var.Model_Config) is None:
global_var.set(
global_var.Model_Config, get_rwkv_config(global_var.get(global_var.Model))
)
2023-05-07 17:27:54 +08:00
global_var.set(global_var.Model_Status, global_var.ModelStatus.Working)
return "success"
2023-05-17 11:39:00 +08:00
@router.post("/update-config")
def update_config(body: ModelConfigBody):
"""
Will not update the model config immediately, but set it when completion called to avoid modifications during generation
"""
print(body)
global_var.set(global_var.Model_Config, body)
return "success"
2023-05-19 15:59:04 +08:00
@router.get("/status")
def status():
return {"status": global_var.get(global_var.Model_Status)}