Skip to content

Commit

Permalink
add progress to run command
Browse files Browse the repository at this point in the history
  • Loading branch information
marknguyen1302 authored and louis-jan committed Jul 25, 2024
1 parent 9591463 commit b654a74
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Engines } from '../types/engine.interface';
import { CortexUsecases } from '@/usecases/cortex/cortex.usecases';
import { BaseCommand } from '../base.command';
import { Presets, SingleBar } from 'cli-progress';
import { downloadModelProgress } from '@/utils/pull-model';

@SubCommand({
name: 'pull',
Expand Down Expand Up @@ -56,42 +57,7 @@ export class ModelPullCommand extends BaseCommand {
exit(1);
});

const response = await this.cortex.events.downloadEvent();

const rl = require('readline').createInterface({
input: stdin,
output: stdout,
});

rl.on('SIGINT', () => {
console.log('\nStopping download...');
process.emit('SIGINT');
});
process.on('SIGINT', async () => {
await this.cortex.models.abortDownload(modelId);
exit(1);
});

const progressBar = new SingleBar({}, Presets.shades_classic);
progressBar.start(100, 0);

for await (const stream of response) {
if (stream.length) {
const data = stream[0] as any;

if (data.status === 'downloaded') break;

let totalBytes = 0;
let totalTransferred = 0;
data.children.forEach((child: any) => {
totalBytes += child.size.total;
totalTransferred += child.size.transferred;
});
progressBar.update(Math.floor((totalTransferred / totalBytes) * 100));
}
}
progressBar.stop();
rl.close();
await downloadModelProgress(this.cortex, modelId);

const existingModel = await this.cortex.models.retrieve(modelId);
const engine = existingModel?.engine || Engines.llamaCPP;
Expand Down
2 changes: 2 additions & 0 deletions cortex-js/src/infrastructure/commanders/run.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { checkModelCompatibility } from '@/utils/model-check';
import { BaseCommand } from './base.command';
import { isRemoteEngine } from '@/utils/normalize-model-id';
import { ChatClient } from './services/chat-client';
import { downloadModelProgress } from '@/utils/pull-model';

type RunOptions = {
threadId?: string;
Expand Down Expand Up @@ -62,6 +63,7 @@ export class RunCommand extends BaseCommand {
checkingSpinner.fail(e.message ?? e);
exit(1);
});
await downloadModelProgress(this.cortex, modelId);
}

// Second check if model is available
Expand Down
42 changes: 42 additions & 0 deletions cortex-js/src/utils/pull-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Presets, SingleBar } from "cli-progress";
import { Cortex } from "cortexso-node";
import { exit, stdin, stdout } from 'node:process';

export const downloadModelProgress = async (cortex: Cortex, modelId: string) => {
const response = await cortex.events.downloadEvent();

const rl = require('readline').createInterface({
input: stdin,
output: stdout,
});

rl.on('SIGINT', () => {
console.log('\nStopping download...');
process.emit('SIGINT');
});
process.on('SIGINT', async () => {
await cortex.models.abortDownload(modelId);
exit(1);
});

const progressBar = new SingleBar({}, Presets.shades_classic);
progressBar.start(100, 0);

for await (const stream of response) {
if (stream.length) {
const data = stream[0] as any;

if (data.status === 'downloaded') break;

let totalBytes = 0;
let totalTransferred = 0;
data.children.forEach((child: any) => {
totalBytes += child.size.total;
totalTransferred += child.size.transferred;
});
progressBar.update(Math.floor((totalTransferred / (totalBytes || 1)) * 100));
}
}
progressBar.stop();
rl.close();
};

0 comments on commit b654a74

Please sign in to comment.