chore: fix responsible issue when importing models (cortex.cpp does not support download in parallel yet)

This commit is contained in:
Louis 2024-10-30 10:34:42 +07:00
parent 8837b872af
commit 8c759676d9
No known key found for this signature in database
GPG Key ID: 44FA9F4D33C37DE2
5 changed files with 44 additions and 26 deletions

View File

@ -261,8 +261,13 @@ const ModelDropdown = ({
}, []) }, [])
const findByEngine = filteredDownloadedModels const findByEngine = filteredDownloadedModels
.filter((x) => !inActiveEngineProvider.includes(x.engine)) .map((x) => {
.map((x) => x.engine) // Legacy engine support - they will be grouped under Cortex LlamaCPP
if (x.engine === InferenceEngine.nitro)
return InferenceEngine.cortex_llamacpp
return x.engine
})
.filter((x) => !inActiveEngineProvider.includes(x))
const groupByEngine = findByEngine const groupByEngine = findByEngine
.filter(function (item, index) { .filter(function (item, index) {

View File

@ -18,7 +18,11 @@ import { snackbar } from '@/containers/Toast'
import { FilePathWithSize } from '@/utils/file' import { FilePathWithSize } from '@/utils/file'
import { extensionManager } from '@/extension' import { extensionManager } from '@/extension'
import { importingModelsAtom } from '@/helpers/atoms/Model.atom' import {
addDownloadingModelAtom,
importingModelsAtom,
removeDownloadingModelAtom,
} from '@/helpers/atoms/Model.atom'
export type ImportModelStage = export type ImportModelStage =
| 'NONE' | 'NONE'
@ -49,11 +53,25 @@ export type ModelUpdate = {
const useImportModel = () => { const useImportModel = () => {
const setImportModelStage = useSetAtom(setImportModelStageAtom) const setImportModelStage = useSetAtom(setImportModelStageAtom)
const setImportingModels = useSetAtom(importingModelsAtom) const setImportingModels = useSetAtom(importingModelsAtom)
const addDownloadingModel = useSetAtom(addDownloadingModelAtom)
const removeDownloadingModel = useSetAtom(removeDownloadingModelAtom)
const importModels = useCallback( const importModels = useCallback(
(models: ImportingModel[], optionType: OptionType) => (models: ImportingModel[], optionType: OptionType) => {
localImportModels(models, optionType), models
[] .filter((e) => !!e.modelId)
.map((model) => {
if (model.modelId) {
const modelId = model.modelId
addDownloadingModel(modelId)
extensionManager
.get<ModelExtension>(ExtensionTypeEnum.Model)
?.importModel(model.modelId, model.path)
.finally(() => removeDownloadingModel(modelId))
}
})
},
[addDownloadingModel, removeDownloadingModel]
) )
const updateModelInfo = useCallback( const updateModelInfo = useCallback(
@ -101,21 +119,6 @@ const useImportModel = () => {
return { importModels, updateModelInfo, sanitizeFilePaths } return { importModels, updateModelInfo, sanitizeFilePaths }
} }
const localImportModels = async (
models: ImportingModel[],
// TODO: @louis - We will set this option when cortex.cpp supports it
optionType: OptionType
): Promise<void> => {
await models
.filter((e) => !!e.modelId)
.map((model) => {
if (model.modelId)
extensionManager
.get<ModelExtension>(ExtensionTypeEnum.Model)
?.importModel(model.modelId, model.path)
})
}
const localUpdateModelInfo = async ( const localUpdateModelInfo = async (
modelInfo: Partial<Model> modelInfo: Partial<Model>
): Promise<Model | undefined> => ): Promise<Model | undefined> =>

View File

@ -21,7 +21,10 @@ import { mainViewStateAtom } from '@/helpers/atoms/App.atom'
import { assistantsAtom } from '@/helpers/atoms/Assistant.atom' import { assistantsAtom } from '@/helpers/atoms/Assistant.atom'
import { importHuggingFaceModelStageAtom } from '@/helpers/atoms/HuggingFace.atom' import { importHuggingFaceModelStageAtom } from '@/helpers/atoms/HuggingFace.atom'
import { downloadedModelsAtom } from '@/helpers/atoms/Model.atom' import {
downloadedModelsAtom,
getDownloadingModelAtom,
} from '@/helpers/atoms/Model.atom'
type Props = { type Props = {
index: number index: number
@ -42,11 +45,13 @@ const ModelDownloadRow: React.FC<Props> = ({
const { downloadModel, abortModelDownload } = useDownloadModel() const { downloadModel, abortModelDownload } = useDownloadModel()
const allDownloadStates = useAtomValue(modelDownloadStateAtom) const allDownloadStates = useAtomValue(modelDownloadStateAtom)
const downloadState: DownloadState | undefined = allDownloadStates[fileName] const downloadState: DownloadState | undefined = allDownloadStates[fileName]
const downloadingModels = useAtomValue(getDownloadingModelAtom)
const { requestCreateNewThread } = useCreateNewThread() const { requestCreateNewThread } = useCreateNewThread()
const setMainViewState = useSetAtom(mainViewStateAtom) const setMainViewState = useSetAtom(mainViewStateAtom)
const assistants = useAtomValue(assistantsAtom) const assistants = useAtomValue(assistantsAtom)
const downloadedModel = downloadedModels.find((md) => md.id === fileName) const downloadedModel = downloadedModels.find((md) => md.id === fileName)
const isDownloading = downloadingModels.some((md) => md === fileName)
const setHfImportingStage = useSetAtom(importHuggingFaceModelStageAtom) const setHfImportingStage = useSetAtom(importHuggingFaceModelStageAtom)
@ -114,7 +119,7 @@ const ModelDownloadRow: React.FC<Props> = ({
> >
Use Use
</Button> </Button>
) : downloadState != null ? ( ) : isDownloading ? (
<Button variant="soft"> <Button variant="soft">
<div className="flex items-center space-x-2"> <div className="flex items-center space-x-2">
<span className="inline-block" onClick={onAbortDownloadClick}> <span className="inline-block" onClick={onAbortDownloadClick}>
@ -129,7 +134,7 @@ const ModelDownloadRow: React.FC<Props> = ({
} }
/> />
<span className="tabular-nums"> <span className="tabular-nums">
{formatDownloadPercentage(downloadState.percent)} {formatDownloadPercentage(downloadState?.percent)}
</span> </span>
</div> </div>
</Button> </Button>

View File

@ -116,7 +116,12 @@ const MyModels = () => {
getAllSettings() getAllSettings()
}, []) }, [])
const findByEngine = filteredDownloadedModels.map((x) => x.engine) const findByEngine = filteredDownloadedModels.map((x) => {
// Legacy engine support - they will be grouped under Cortex LlamaCPP
if (x.engine === InferenceEngine.nitro)
return InferenceEngine.cortex_llamacpp
return x.engine
})
const groupByEngine = findByEngine const groupByEngine = findByEngine
.filter(function (item, index) { .filter(function (item, index) {
if (findByEngine.indexOf(item) === index) return item if (findByEngine.indexOf(item) === index) return item

View File

@ -19,7 +19,7 @@ export const formatDownloadPercentage = (
options?: { hidePercentage?: boolean } options?: { hidePercentage?: boolean }
) => { ) => {
if (options?.hidePercentage) return input <= 1 ? input * 100 : input if (options?.hidePercentage) return input <= 1 ? input * 100 : input
return (input <= 1 ? input * 100 : input).toFixed(2) + '%' return (input <= 1 ? input * 100 : (input ?? 0)).toFixed(2) + '%'
} }
export const formatDownloadSpeed = (input: number | undefined) => { export const formatDownloadSpeed = (input: number | undefined) => {