-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel-downloader.ts
60 lines (55 loc) · 2.04 KB
/
model-downloader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { Logger } from '@nestjs/common';
import { PipelineType, pipeline, env } from '@huggingface/transformers';
import { isRemoteModel } from './const';
import { UniversalStatusManager } from './universal-status';
import { getModelsDir } from 'codefox-common';
env.allowLocalModels = true;
env.localModelPath = getModelsDir();
export class UniversalDownloader {
readonly logger = new Logger(UniversalDownloader.name);
private static instance: UniversalDownloader;
private readonly statusManager = UniversalStatusManager.getInstance();
public static getInstance(): UniversalDownloader {
if (!UniversalDownloader.instance) {
UniversalDownloader.instance = new UniversalDownloader();
}
return UniversalDownloader.instance;
}
async getPipeline(task: string, model: string, path: string): Promise<any> {
if (isRemoteModel(model)) {
this.logger.log(`Remote model detected: ${model}, marking as downloaded`);
console.log(this.statusManager);
this.statusManager.updateStatus(model, true);
return null;
}
this.logger.log(`Starting download for local model: ${model}`);
try {
console.log(path);
const pipelineInstance = await pipeline(task as PipelineType, model, {
cache_dir: path,
});
this.logger.log(`Successfully downloaded local model: ${model}`);
this.statusManager.updateStatus(model, true);
return pipelineInstance;
} catch (error) {
this.logger.error(`Failed to download model ${model}: ${error.message}`);
this.statusManager.updateStatus(model, false);
return null;
}
}
public async getLocalModel(task: string, model: string): Promise<any> {
try {
const pipelineInstance = await pipeline(task as PipelineType, model, {
local_files_only: true,
revision: 'local',
cache_dir: getModelsDir(),
});
return pipelineInstance;
} catch (error) {
this.logger.error(
`Failed to load local model: ${model} with error: ${error.message || error}`,
);
return null;
}
}
}