enh: reset models

This commit is contained in:
Timothy Jaeryang Baek 2024-11-19 11:03:36 -08:00
parent d76e1319ff
commit 81bb2750e0
5 changed files with 92 additions and 5 deletions

View File

@ -257,5 +257,15 @@ class ModelsTable:
except Exception: except Exception:
return False return False
def delete_all_models(self) -> bool:
try:
with get_db() as db:
db.query(Model).delete()
db.commit()
return True
except Exception:
return False
Models = ModelsTable() Models = ModelsTable()

View File

@ -180,3 +180,9 @@ async def delete_model_by_id(id: str, user=Depends(get_verified_user)):
result = Models.delete_model_by_id(id) result = Models.delete_model_by_id(id)
return result return result
@router.delete("/delete/all", response_model=bool)
async def delete_all_models(user=Depends(get_admin_user)):
result = Models.delete_all_models()
return result

View File

@ -231,3 +231,36 @@ export const deleteModelById = async (token: string, id: string) => {
return res; return res;
}; };
export const deleteAllModels = async (token: string) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/models/delete/all`, {
method: 'DELETE',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.then((json) => {
return json;
})
.catch((err) => {
error = err;
console.log(err);
return null;
});
if (error) {
throw error;
}
return res;
};

View File

@ -9,6 +9,7 @@
import { WEBUI_NAME, config, mobile, models as _models, settings, user } from '$lib/stores'; import { WEBUI_NAME, config, mobile, models as _models, settings, user } from '$lib/stores';
import { import {
createNewModel, createNewModel,
deleteAllModels,
getBaseModels, getBaseModels,
toggleModelById, toggleModelById,
updateModelById updateModelById
@ -22,6 +23,7 @@
import ModelEditor from '$lib/components/workspace/Models/ModelEditor.svelte'; import ModelEditor from '$lib/components/workspace/Models/ModelEditor.svelte';
import { toast } from 'svelte-sonner'; import { toast } from 'svelte-sonner';
import ConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
let importFiles; let importFiles;
let modelsImportInputElement: HTMLInputElement; let modelsImportInputElement: HTMLInputElement;
@ -32,8 +34,8 @@
let baseModels = null; let baseModels = null;
let filteredModels = []; let filteredModels = [];
let selectedModelId = null; let selectedModelId = null;
let showResetModal = false;
$: if (models) { $: if (models) {
filteredModels = models.filter( filteredModels = models.filter(
@ -126,6 +128,19 @@
}); });
</script> </script>
<ConfirmDialog
title={$i18n.t('Delete All Models')}
message={$i18n.t('This will delete all models including custom models and cannot be undone.')}
bind:show={showResetModal}
onConfirm={async () => {
const res = deleteAllModels(localStorage.token);
if (res) {
toast.success($i18n.t('All models deleted successfully'));
init();
}
}}
/>
{#if models !== null} {#if models !== null}
{#if selectedModelId === null} {#if selectedModelId === null}
<div class="flex flex-col gap-1 mt-1.5 mb-2"> <div class="flex flex-col gap-1 mt-1.5 mb-2">
@ -137,6 +152,22 @@
>{filteredModels.length}</span >{filteredModels.length}</span
> >
</div> </div>
<div>
<Tooltip content={$i18n.t('This will delete all models including custom models')}>
<button
class=" px-2.5 py-1 rounded-full flex gap-1 items-center"
type="button"
on:click={() => {
showResetModal = true;
}}
>
<div class="text-sm flex-shrink-0">
{$i18n.t('Reset')}
</div>
</button>
</Tooltip>
</div>
</div> </div>
<div class=" flex flex-1 items-center w-full space-x-2"> <div class=" flex flex-1 items-center w-full space-x-2">

View File

@ -12,6 +12,8 @@
export let cancelLabel = $i18n.t('Cancel'); export let cancelLabel = $i18n.t('Cancel');
export let confirmLabel = $i18n.t('Confirm'); export let confirmLabel = $i18n.t('Confirm');
export let onConfirm = () => {};
export let input = false; export let input = false;
export let inputPlaceholder = ''; export let inputPlaceholder = '';
export let inputValue = ''; export let inputValue = '';
@ -29,11 +31,17 @@
if (event.key === 'Enter') { if (event.key === 'Enter') {
console.log('Enter'); console.log('Enter');
show = false; confirmHandler();
dispatch('confirm', inputValue);
} }
}; };
const confirmHandler = async () => {
show = false;
await onConfirm();
dispatch('confirm', inputValue);
};
onMount(() => { onMount(() => {
mounted = true; mounted = true;
}); });
@ -110,8 +118,7 @@
<button <button
class="bg-gray-900 hover:bg-gray-850 text-gray-100 dark:bg-gray-100 dark:hover:bg-white dark:text-gray-800 font-medium w-full py-2.5 rounded-lg transition" class="bg-gray-900 hover:bg-gray-850 text-gray-100 dark:bg-gray-100 dark:hover:bg-white dark:text-gray-800 font-medium w-full py-2.5 rounded-lg transition"
on:click={() => { on:click={() => {
show = false; confirmHandler();
dispatch('confirm', inputValue);
}} }}
type="button" type="button"
> >