Skip to content

Add parseProgressFromLogs helper function #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,10 @@ declare module "replicate" {
},
secret: string
): boolean;

export function parseProgress(logs: Prediction | string): {
percentage: number;
current: number;
total: number;
};
}
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const ApiError = require("./lib/error");
const ModelVersionIdentifier = require("./lib/identifier");
const { Stream } = require("./lib/stream");
const { withAutomaticRetries, validateWebhook } = require("./lib/util");
const {
withAutomaticRetries,
validateWebhook,
parseProgress,
} = require("./lib/util");

const accounts = require("./lib/accounts");
const collections = require("./lib/collections");
Expand Down Expand Up @@ -375,3 +379,4 @@ class Replicate {

module.exports = Replicate;
module.exports.validateWebhook = validateWebhook;
module.exports.parseProgress = parseProgress;
110 changes: 86 additions & 24 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Replicate, {
Model,
Prediction,
validateWebhook,
parseProgress,
} from "replicate";
import nock from "nock";
import fetch from "cross-fetch";
Expand Down Expand Up @@ -888,63 +889,124 @@ describe("Replicate client", () => {
});

describe("run", () => {
test("Calls the correct API routes for a version", async () => {
const firstPollingRequest = true;

test("Calls the correct API routes", async () => {
nock(BASE_URL)
.post("/predictions")
.reply(201, {
id: "ufawqhfynnddngldkgtslldrkq",
status: "starting",
logs: null,
})
.get("/predictions/ufawqhfynnddngldkgtslldrkq")
.twice()
.reply(200, {
id: "ufawqhfynnddngldkgtslldrkq",
status: "processing",
logs: [
"Using seed: 12345",
"0%| | 0/5 [00:00<?, ?it/s]",
"20%|██ | 1/5 [00:00<00:01, 21.38it/s]",
"40%|████▍ | 2/5 [00:01<00:01, 22.46it/s]",
].join("\n"),
})
.get("/predictions/ufawqhfynnddngldkgtslldrkq")
.reply(200, {
id: "ufawqhfynnddngldkgtslldrkq",
status: "processing",
logs: [
"Using seed: 12345",
"0%| | 0/5 [00:00<?, ?it/s]",
"20%|██ | 1/5 [00:00<00:01, 21.38it/s]",
"40%|████▍ | 2/5 [00:01<00:01, 22.46it/s]",
"60%|████▍ | 3/5 [00:01<00:01, 22.46it/s]",
"80%|████████ | 4/5 [00:01<00:00, 22.86it/s]",
].join("\n"),
})
.get("/predictions/ufawqhfynnddngldkgtslldrkq")
.reply(200, {
id: "ufawqhfynnddngldkgtslldrkq",
status: "succeeded",
output: "Goodbye!",
logs: [
"Using seed: 12345",
"0%| | 0/5 [00:00<?, ?it/s]",
"20%|██ | 1/5 [00:00<00:01, 21.38it/s]",
"40%|████▍ | 2/5 [00:01<00:01, 22.46it/s]",
"60%|████▍ | 3/5 [00:01<00:01, 22.46it/s]",
"80%|████████ | 4/5 [00:01<00:00, 22.86it/s]",
"100%|██████████| 5/5 [00:02<00:00, 22.26it/s]",
].join("\n"),
});

const progress = jest.fn();
const callback = jest.fn();

const output = await client.run(
"owner/model:5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa",
{
input: { text: "Hello, world!" },
wait: { interval: 1 },
},
progress
(prediction) => {
const progress = parseProgress(prediction);
callback(prediction, progress);
}
);

expect(output).toBe("Goodbye!");

expect(progress).toHaveBeenNthCalledWith(1, {
id: "ufawqhfynnddngldkgtslldrkq",
status: "starting",
});
expect(callback).toHaveBeenNthCalledWith(
1,
{
id: "ufawqhfynnddngldkgtslldrkq",
status: "starting",
logs: null,
},
null
);

expect(progress).toHaveBeenNthCalledWith(2, {
id: "ufawqhfynnddngldkgtslldrkq",
status: "processing",
});
expect(callback).toHaveBeenNthCalledWith(
2,
{
id: "ufawqhfynnddngldkgtslldrkq",
status: "processing",
logs: expect.any(String),
},
{
percentage: 0.4,
current: 2,
total: 5,
}
);

expect(progress).toHaveBeenNthCalledWith(3, {
id: "ufawqhfynnddngldkgtslldrkq",
status: "processing",
});
expect(callback).toHaveBeenNthCalledWith(
3,
{
id: "ufawqhfynnddngldkgtslldrkq",
status: "processing",
logs: expect.any(String),
},
{
percentage: 0.8,
current: 4,
total: 5,
}
);

expect(progress).toHaveBeenNthCalledWith(4, {
id: "ufawqhfynnddngldkgtslldrkq",
status: "succeeded",
output: "Goodbye!",
});
expect(callback).toHaveBeenNthCalledWith(
4,
{
id: "ufawqhfynnddngldkgtslldrkq",
status: "succeeded",
logs: expect.any(String),
output: "Goodbye!",
},
{
percentage: 1.0,
current: 5,
total: 5,
}
);

expect(progress).toHaveBeenCalledTimes(4);
expect(callback).toHaveBeenCalledTimes(4);
});

test("Calls the correct API routes for a model", async () => {
Expand Down
36 changes: 35 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,38 @@ function isPlainObject(value) {
);
}

module.exports = { transformFileInputs, validateWebhook, withAutomaticRetries };
/**
* Parse progress from prediction logs.
* @param {object|string} input - A prediction object or string.
* @returns {object} - An object with the percentage, current, and total.
*/
function parseProgress(input) {
const logs = typeof input === "object" && input.logs ? input.logs : input;
if (!logs || typeof logs !== "string") {
return null;
}

const pattern = /^\s*(\d+)%\s*\|.+?\|\s*(\d+)\/(\d+)/;
const lines = logs.split("\n").reverse();

for (const line of lines) {
const matches = line.match(pattern);

if (matches && matches.length === 4) {
return {
percentage: parseInt(matches[1], 10) / 100,
current: parseInt(matches[2], 10),
total: parseInt(matches[3], 10),
};
}
}

return null;
}

module.exports = {
transformFileInputs,
validateWebhook,
withAutomaticRetries,
parseProgress,
};