fix: stop server if failed to load model (#2807)
fix: load model error start server state not update Co-authored-by: James <james@jan.ai>
This commit is contained in:
parent
a1fd987567
commit
7d4cc67aa1
@ -25,7 +25,7 @@ export const stateModelAtom = atom<ModelState>({
|
|||||||
model: undefined,
|
model: undefined,
|
||||||
})
|
})
|
||||||
|
|
||||||
export let loadModelController: AbortController | undefined
|
const pendingModelLoadAtom = atom<boolean>(false)
|
||||||
|
|
||||||
export function useActiveModel() {
|
export function useActiveModel() {
|
||||||
const [activeModel, setActiveModel] = useAtom(activeModelAtom)
|
const [activeModel, setActiveModel] = useAtom(activeModelAtom)
|
||||||
@ -33,6 +33,7 @@ export function useActiveModel() {
|
|||||||
const [stateModel, setStateModel] = useAtom(stateModelAtom)
|
const [stateModel, setStateModel] = useAtom(stateModelAtom)
|
||||||
const downloadedModels = useAtomValue(downloadedModelsAtom)
|
const downloadedModels = useAtomValue(downloadedModelsAtom)
|
||||||
const setLoadModelError = useSetAtom(loadModelErrorAtom)
|
const setLoadModelError = useSetAtom(loadModelErrorAtom)
|
||||||
|
const [pendingModelLoad, setPendingModelLoad] = useAtom(pendingModelLoadAtom)
|
||||||
|
|
||||||
const downloadedModelsRef = useRef<Model[]>([])
|
const downloadedModelsRef = useRef<Model[]>([])
|
||||||
|
|
||||||
@ -40,7 +41,7 @@ export function useActiveModel() {
|
|||||||
downloadedModelsRef.current = downloadedModels
|
downloadedModelsRef.current = downloadedModels
|
||||||
}, [downloadedModels])
|
}, [downloadedModels])
|
||||||
|
|
||||||
const startModel = async (modelId: string) => {
|
const startModel = async (modelId: string, abortable: boolean = true) => {
|
||||||
if (
|
if (
|
||||||
(activeModel && activeModel.id === modelId) ||
|
(activeModel && activeModel.id === modelId) ||
|
||||||
(stateModel.model?.id === modelId && stateModel.loading)
|
(stateModel.model?.id === modelId && stateModel.loading)
|
||||||
@ -48,7 +49,7 @@ export function useActiveModel() {
|
|||||||
console.debug(`Model ${modelId} is already initialized. Ignore..`)
|
console.debug(`Model ${modelId} is already initialized. Ignore..`)
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
}
|
}
|
||||||
loadModelController = new AbortController()
|
setPendingModelLoad(true)
|
||||||
|
|
||||||
let model = downloadedModelsRef?.current.find((e) => e.id === modelId)
|
let model = downloadedModelsRef?.current.find((e) => e.id === modelId)
|
||||||
|
|
||||||
@ -107,15 +108,16 @@ export function useActiveModel() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
if (loadModelController?.signal.aborted)
|
|
||||||
return Promise.reject(new Error('aborted'))
|
|
||||||
|
|
||||||
setStateModel(() => ({
|
setStateModel(() => ({
|
||||||
state: 'start',
|
state: 'start',
|
||||||
loading: false,
|
loading: false,
|
||||||
model,
|
model,
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
if (!pendingModelLoad && abortable) {
|
||||||
|
return Promise.reject(new Error('aborted'))
|
||||||
|
}
|
||||||
|
|
||||||
toaster({
|
toaster({
|
||||||
title: 'Failed!',
|
title: 'Failed!',
|
||||||
description: `Model ${model.id} failed to start.`,
|
description: `Model ${model.id} failed to start.`,
|
||||||
@ -139,9 +141,15 @@ export function useActiveModel() {
|
|||||||
.then(() => {
|
.then(() => {
|
||||||
setActiveModel(undefined)
|
setActiveModel(undefined)
|
||||||
setStateModel({ state: 'start', loading: false, model: undefined })
|
setStateModel({ state: 'start', loading: false, model: undefined })
|
||||||
loadModelController?.abort()
|
setPendingModelLoad(false)
|
||||||
})
|
})
|
||||||
}, [activeModel, setActiveModel, setStateModel, stateModel])
|
}, [
|
||||||
|
activeModel,
|
||||||
|
setActiveModel,
|
||||||
|
setStateModel,
|
||||||
|
setPendingModelLoad,
|
||||||
|
stateModel,
|
||||||
|
])
|
||||||
|
|
||||||
const stopInference = useCallback(async () => {
|
const stopInference = useCallback(async () => {
|
||||||
// Loading model
|
// Loading model
|
||||||
|
|||||||
@ -155,12 +155,12 @@ const LocalServerScreen = () => {
|
|||||||
isCorsEnabled,
|
isCorsEnabled,
|
||||||
isVerboseEnabled,
|
isVerboseEnabled,
|
||||||
})
|
})
|
||||||
await startModel(selectedModel.id)
|
|
||||||
if (isStarted) setServerEnabled(true)
|
if (isStarted) setServerEnabled(true)
|
||||||
if (firstTimeVisitAPIServer) {
|
if (firstTimeVisitAPIServer) {
|
||||||
localStorage.setItem(FIRST_TIME_VISIT_API_SERVER, 'false')
|
localStorage.setItem(FIRST_TIME_VISIT_API_SERVER, 'false')
|
||||||
setFirstTimeVisitAPIServer(false)
|
setFirstTimeVisitAPIServer(false)
|
||||||
}
|
}
|
||||||
|
startModel(selectedModel.id, false).catch((e) => console.error(e))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
toaster({
|
toaster({
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user