Skip to content

Commit

Permalink
fix: cannot reset data while starting model
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-menlo committed Apr 11, 2024
1 parent 0d31f9e commit bddfab0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
40 changes: 17 additions & 23 deletions web/hooks/useActiveModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,33 +126,27 @@ export function useActiveModel() {
})
}

const stopModel = useCallback(
async (model?: Model) => {
const stoppingModel = activeModel || model
if (
!stoppingModel ||
(!model && stateModel.state === 'stop' && stateModel.loading)
)
return

setStateModel({ state: 'stop', loading: true, model: stoppingModel })
const engine = EngineManager.instance().get(stoppingModel.engine)
return engine
?.unloadModel(stoppingModel)
.catch()
.then(() => {
setActiveModel(undefined)
setStateModel({ state: 'start', loading: false, model: undefined })
loadModelController?.abort()
})
},
[activeModel, setActiveModel, setStateModel, stateModel]
)
const stopModel = useCallback(async () => {
const stoppingModel = activeModel || stateModel.model
if (!stoppingModel || (stateModel.state === 'stop' && stateModel.loading))
return

setStateModel({ state: 'stop', loading: true, model: stoppingModel })
const engine = EngineManager.instance().get(stoppingModel.engine)
return engine
?.unloadModel(stoppingModel)
.catch()
.then(() => {
setActiveModel(undefined)
setStateModel({ state: 'start', loading: false, model: undefined })
loadModelController?.abort()
})
}, [activeModel, setActiveModel, setStateModel, stateModel])

const stopInference = useCallback(async () => {
// Loading model
if (stateModel.loading) {
stopModel(stateModel.model)
stopModel()
return
}
if (!activeModel) return
Expand Down
12 changes: 5 additions & 7 deletions web/hooks/useFactoryReset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const factoryResetStateAtom = atom(FactoryResetState.Idle)

export default function useFactoryReset() {
const defaultJanDataFolder = useAtomValue(defaultJanDataFolderAtom)
const { activeModel, stopModel } = useActiveModel()
const { stopModel } = useActiveModel()
const setFactoryResetState = useSetAtom(factoryResetStateAtom)

const resetAll = useCallback(
Expand All @@ -44,11 +44,9 @@ export default function useFactoryReset() {
await window.core?.api?.updateAppConfiguration(configuration)
}

if (activeModel) {
setFactoryResetState(FactoryResetState.StoppingModel)
await stopModel()
await new Promise((resolve) => setTimeout(resolve, 4000))
}
setFactoryResetState(FactoryResetState.StoppingModel)
await stopModel()
await new Promise((resolve) => setTimeout(resolve, 4000))

setFactoryResetState(FactoryResetState.DeletingData)
await fs.rm(janDataFolderPath)
Expand All @@ -59,7 +57,7 @@ export default function useFactoryReset() {

await window.core?.api?.relaunch()
},
[defaultJanDataFolder, activeModel, stopModel, setFactoryResetState]
[defaultJanDataFolder, stopModel, setFactoryResetState]
)

return {
Expand Down

0 comments on commit bddfab0

Please sign in to comment.