refac: models endpoint
This commit is contained in:
@@ -35,7 +35,7 @@ log = logging.getLogger(__name__)
|
|||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
def validate_model_id(model_id: str) -> bool:
|
def is_valid_model_id(model_id: str) -> bool:
|
||||||
return model_id and len(model_id) <= 256
|
return model_id and len(model_id) <= 256
|
||||||
|
|
||||||
|
|
||||||
@@ -90,7 +90,7 @@ async def create_new_model(
|
|||||||
detail=ERROR_MESSAGES.MODEL_ID_TAKEN,
|
detail=ERROR_MESSAGES.MODEL_ID_TAKEN,
|
||||||
)
|
)
|
||||||
|
|
||||||
if not validate_model_id(form_data.id):
|
if not is_valid_model_id(form_data.id):
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_400_BAD_REQUEST,
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
detail=ERROR_MESSAGES.MODEL_ID_TOO_LONG,
|
detail=ERROR_MESSAGES.MODEL_ID_TOO_LONG,
|
||||||
@@ -157,7 +157,7 @@ async def import_models(
|
|||||||
# Here, you can add logic to validate model_data if needed
|
# Here, you can add logic to validate model_data if needed
|
||||||
model_id = model_data.get("id")
|
model_id = model_data.get("id")
|
||||||
|
|
||||||
if model_id and validate_model_id(model_id):
|
if model_id and is_valid_model_id(model_id):
|
||||||
existing_model = Models.get_model_by_id(model_id)
|
existing_model = Models.get_model_by_id(model_id)
|
||||||
if existing_model:
|
if existing_model:
|
||||||
# Update existing model
|
# Update existing model
|
||||||
@@ -203,6 +203,10 @@ async def sync_models(
|
|||||||
###########################
|
###########################
|
||||||
|
|
||||||
|
|
||||||
|
class ModelIdForm(BaseModel):
|
||||||
|
id: str
|
||||||
|
|
||||||
|
|
||||||
# Note: We're not using the typical url path param here, but instead using a query parameter to allow '/' in the id
|
# Note: We're not using the typical url path param here, but instead using a query parameter to allow '/' in the id
|
||||||
@router.get("/model", response_model=Optional[ModelResponse])
|
@router.get("/model", response_model=Optional[ModelResponse])
|
||||||
async def get_model_by_id(id: str, user=Depends(get_verified_user)):
|
async def get_model_by_id(id: str, user=Depends(get_verified_user)):
|
||||||
@@ -296,12 +300,10 @@ async def toggle_model_by_id(id: str, user=Depends(get_verified_user)):
|
|||||||
|
|
||||||
@router.post("/model/update", response_model=Optional[ModelModel])
|
@router.post("/model/update", response_model=Optional[ModelModel])
|
||||||
async def update_model_by_id(
|
async def update_model_by_id(
|
||||||
id: str,
|
|
||||||
form_data: ModelForm,
|
form_data: ModelForm,
|
||||||
user=Depends(get_verified_user),
|
user=Depends(get_verified_user),
|
||||||
):
|
):
|
||||||
model = Models.get_model_by_id(id)
|
model = Models.get_model_by_id(form_data.id)
|
||||||
|
|
||||||
if not model:
|
if not model:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
@@ -327,9 +329,9 @@ async def update_model_by_id(
|
|||||||
############################
|
############################
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/model/delete", response_model=bool)
|
@router.post("/model/delete", response_model=bool)
|
||||||
async def delete_model_by_id(id: str, user=Depends(get_verified_user)):
|
async def delete_model_by_id(form_data: ModelIdForm, user=Depends(get_verified_user)):
|
||||||
model = Models.get_model_by_id(id)
|
model = Models.get_model_by_id(form_data.id)
|
||||||
if not model:
|
if not model:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
@@ -346,7 +348,7 @@ async def delete_model_by_id(id: str, user=Depends(get_verified_user)):
|
|||||||
detail=ERROR_MESSAGES.UNAUTHORIZED,
|
detail=ERROR_MESSAGES.UNAUTHORIZED,
|
||||||
)
|
)
|
||||||
|
|
||||||
result = Models.delete_model_by_id(id)
|
result = Models.delete_model_by_id(form_data.id)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -192,17 +192,14 @@ export const toggleModelById = async (token: string, id: string) => {
|
|||||||
export const updateModelById = async (token: string, id: string, model: object) => {
|
export const updateModelById = async (token: string, id: string, model: object) => {
|
||||||
let error = null;
|
let error = null;
|
||||||
|
|
||||||
const searchParams = new URLSearchParams();
|
const res = await fetch(`${WEBUI_API_BASE_URL}/models/model/update`, {
|
||||||
searchParams.append('id', id);
|
|
||||||
|
|
||||||
const res = await fetch(`${WEBUI_API_BASE_URL}/models/model/update?${searchParams.toString()}`, {
|
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
authorization: `Bearer ${token}`
|
authorization: `Bearer ${token}`
|
||||||
},
|
},
|
||||||
body: JSON.stringify(model)
|
body: JSON.stringify({ ...model, id })
|
||||||
})
|
})
|
||||||
.then(async (res) => {
|
.then(async (res) => {
|
||||||
if (!res.ok) throw await res.json();
|
if (!res.ok) throw await res.json();
|
||||||
@@ -228,16 +225,14 @@ export const updateModelById = async (token: string, id: string, model: object)
|
|||||||
export const deleteModelById = async (token: string, id: string) => {
|
export const deleteModelById = async (token: string, id: string) => {
|
||||||
let error = null;
|
let error = null;
|
||||||
|
|
||||||
const searchParams = new URLSearchParams();
|
const res = await fetch(`${WEBUI_API_BASE_URL}/models/model/delete`, {
|
||||||
searchParams.append('id', id);
|
method: 'POST',
|
||||||
|
|
||||||
const res = await fetch(`${WEBUI_API_BASE_URL}/models/model/delete?${searchParams.toString()}`, {
|
|
||||||
method: 'DELETE',
|
|
||||||
headers: {
|
headers: {
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
authorization: `Bearer ${token}`
|
authorization: `Bearer ${token}`
|
||||||
}
|
},
|
||||||
|
body: JSON.stringify({ id })
|
||||||
})
|
})
|
||||||
.then(async (res) => {
|
.then(async (res) => {
|
||||||
if (!res.ok) throw await res.json();
|
if (!res.ok) throw await res.json();
|
||||||
|
|||||||
Reference in New Issue
Block a user