Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 88 additions & 35 deletions cortex-js/src/infrastructure/controllers/models.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
Get,
Post,
Body,
Patch,
Param,
Delete,
HttpCode,
Expand All @@ -23,9 +22,7 @@ import { StartModelSuccessDto } from '@/infrastructure/dtos/models/start-model-s
import { TransformInterceptor } from '../interceptors/transform.interceptor';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { ModelSettingsDto } from '../dtos/models/model-settings.dto';
import {
EventName,
} from '@/domain/telemetry/telemetry.interface';
import { EventName } from '@/domain/telemetry/telemetry.interface';
import { TelemetryUsecases } from '@/usecases/telemetry/telemetry.usecases';
import { CommonResponseDto } from '../dtos/common/common-response.dto';
import { HuggingFaceRepoSibling } from '@/domain/models/huggingface.interface';
Expand Down Expand Up @@ -118,23 +115,53 @@ export class ModelsController {
},
],
})

@ApiParam({
name: 'modelId',
required: true,
description: 'The unique identifier of the model.',
})
@ApiParam({
name: 'fileName',
required: false,
description: 'The file name of the model to download.',
})
@ApiParam({
name: 'persistedModelId',
required: false,
description: 'The unique identifier of the model in your local storage.',
})
@Get('download/:modelId(*)')
downloadModel(@Param('modelId') modelId: string, @Query('fileName') fileName: string, @Query('persistedModelId') persistedModelId?: string) {
this.modelsUsecases.pullModel(modelId, false, (files) => {
return new Promise<HuggingFaceRepoSibling>(async (resolve, reject) => {
const file = files
.find((e) => e.quantization && e.rfilename === fileName)
if(!file) {
return reject(new BadRequestException('File not found'));
}
return resolve(file);
});
}, persistedModelId).then(() => this.telemetryUsecases.addEventToQueue({
name: EventName.DOWNLOAD_MODEL,
modelId,
})
);
downloadModel(
@Param('modelId') modelId: string,
@Query('fileName') fileName: string,
@Query('persistedModelId') persistedModelId?: string,
) {
this.modelsUsecases
.pullModel(
modelId,
false,
(files) => {
return new Promise<HuggingFaceRepoSibling>(
async (resolve, reject) => {
const file = files.find(
(e) =>
e.quantization && (!fileName || e.rfilename === fileName),
);
if (!file) {
return reject(new BadRequestException('File not found'));
}
return resolve(file);
},
);
},
persistedModelId,
)
.then(() =>
this.telemetryUsecases.addEventToQueue({
name: EventName.DOWNLOAD_MODEL,
modelId,
}),
);
return {
message: 'Download model started successfully.',
};
Expand Down Expand Up @@ -173,22 +200,48 @@ export class ModelsController {
required: true,
description: 'The unique identifier of the model.',
})
@ApiParam({
name: 'fileName',
required: false,
description: 'The file name of the model to download.',
})
@ApiParam({
name: 'persistedModelId',
required: false,
description: 'The unique identifier of the model in your local storage.',
})
@Get('pull/:modelId(*)')
pullModel(@Param('modelId') modelId: string, @Query('fileName') fileName: string, @Query('persistedModelId') persistedModelId?: string) {
this.modelsUsecases.pullModel(modelId, false, (files) => {
return new Promise<HuggingFaceRepoSibling>(async (resolve, reject) => {
const file = files
.find((e) => e.quantization && e.rfilename === fileName)
if(!file) {
return reject(new BadRequestException('File not found'));
}
return resolve(file);
});
}, persistedModelId).then(() => this.telemetryUsecases.addEventToQueue({
name: EventName.DOWNLOAD_MODEL,
modelId,
})
);
pullModel(
@Param('modelId') modelId: string,
@Query('fileName') fileName?: string,
@Query('persistedModelId') persistedModelId?: string,
) {
this.modelsUsecases
.pullModel(
modelId,
false,
(files) => {
return new Promise<HuggingFaceRepoSibling>(
async (resolve, reject) => {
const file = files.find(
(e) =>
e.quantization && (!fileName || e.rfilename === fileName),
);
if (!file) {
return reject(new BadRequestException('File not found'));
}
return resolve(file);
},
);
},
persistedModelId,
)
.then(() =>
this.telemetryUsecases.addEventToQueue({
name: EventName.DOWNLOAD_MODEL,
modelId,
}),
);
return {
message: 'Download model started successfully.',
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,6 @@ export class FileManagerService {
}
async writeFile(filePath: string, data: any): Promise<void> {
try {
const dirPath = filePath.split('/').slice(0, -1).join('/');
await this.createFolderIfNotExistInDataFolder(dirPath);
return promises.writeFile(filePath, data, {
encoding: 'utf8',
flag: 'w+',
Expand Down