Repository navigation
Add versioned image classification and embedded RAW previews #46
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| // Child-process fixture: mock only the model boundary, keeping real Sharp decoding and handlers. | ||
| import { mock } from "bun:test"; | ||
| import sharp from "sharp"; | ||
|
|
||
| const scenario = Bun.argv[2]; | ||
| const loadStarted = Promise.withResolvers<void>(); | ||
| const pendingLoad = Promise.withResolvers<typeof infer>(); | ||
| const inferenceStarted = Promise.withResolvers<void>(); | ||
| const pendingInference = Promise.withResolvers<void>(); | ||
| let loads = 0; | ||
| let inferences = 0; | ||
| let pixels: object | undefined; | ||
|
|
||
| async function infer(_image: unknown, labels: string[]) { | ||
| inferences++; | ||
| if (scenario === "inference-abort" && inferences === 1) { | ||
| inferenceStarted.resolve(); | ||
| await pendingInference.promise; | ||
| } | ||
| return labels.map((label) => ({ label, score: 1 / labels.length })); | ||
| } | ||
|
|
||
| mock.module("@huggingface/transformers", () => ({ | ||
| env: {}, | ||
| pipeline: async () => { | ||
| loads++; | ||
| if (scenario.startsWith("load-") && loads === 1) { | ||
| loadStarted.resolve(); | ||
| return pendingLoad.promise; | ||
| } | ||
| return infer; | ||
| }, | ||
| RawImage: class { | ||
| constructor(data: Uint8ClampedArray, width: number, height: number, channels: number) { | ||
| pixels = { width, height, channels, bytes: data.length }; | ||
| } | ||
| }, | ||
| })); | ||
|
|
||
| const bytes = await sharp({ create: { width: 32, height: 16, channels: 3, background: "red" } }) | ||
| .jpeg() | ||
| .toBuffer(); | ||
| const options = { | ||
| classifierId: "clip-image-labels", | ||
| classifierVersion: "1", | ||
| labels: ["landscape", "receipt"], | ||
| }; | ||
| const expectedSha256 = new Bun.CryptoHasher("sha256").update(bytes).digest("hex"); | ||
|
|
||
| if (scenario === "handler-success") { | ||
| const { classifyImageHandler } = | ||
| await import("../packages/server/src/images/classify-image-handler.ts"); | ||
| const response = await classifyImageHandler( | ||
| new Request("http://m4k/images/classify", { | ||
| method: "POST", | ||
| body: bytes, | ||
| headers: { "X-Options": JSON.stringify(options) }, | ||
| }), | ||
| ); | ||
| console.log( | ||
| JSON.stringify({ | ||
| status: response.status, | ||
| body: await response.json(), | ||
| expectedSha256, | ||
| pixels, | ||
| loads, | ||
| inferences, | ||
| }), | ||
| ); | ||
| } else { | ||
| const { classifyImage } = await import("../packages/core/src/images/classify-image.ts"); | ||
| const ctrl = new AbortController(); | ||
| let settled = false; | ||
| const first = classifyImage(bytes, options, { signal: ctrl.signal }).then( | ||
| () => { | ||
| settled = true; | ||
| return "completed"; | ||
| }, | ||
| (error: Error) => { | ||
| settled = true; | ||
| return error.name; | ||
| }, | ||
| ); | ||
| if (scenario.startsWith("load-")) { | ||
| await loadStarted.promise; | ||
| ctrl.abort(); | ||
| const aborted = await first; | ||
| if (scenario === "load-abort") { | ||
| const retry = classifyImage(bytes, options); | ||
| const competing = await classifyImage(bytes, options).then( | ||
| () => "unexpected", | ||
| (error: Error) => error.message, | ||
| ); | ||
| pendingLoad.resolve(infer); | ||
| console.log( | ||
| JSON.stringify({ | ||
| aborted, | ||
| competing, | ||
| retry: await retry, | ||
| loads, | ||
| inferences, | ||
| expectedSha256, | ||
| pixels, | ||
| }), | ||
| ); | ||
| } else { | ||
| pendingLoad.reject(new Error("background load failed")); | ||
| const retry = await classifyImage(bytes, options); | ||
| console.log(JSON.stringify({ aborted, retry, loads, inferences, expectedSha256, pixels })); | ||
| } | ||
| } else if (scenario === "inference-abort") { | ||
| await inferenceStarted.promise; | ||
| ctrl.abort(); | ||
| const competing = await classifyImage(bytes, options).then( | ||
| () => "unexpected", | ||
| (error: Error) => error.message, | ||
| ); | ||
| const settledBeforeInferenceEnded = settled; | ||
| pendingInference.resolve(); | ||
| const aborted = await first; | ||
| const retry = await classifyImage(bytes, options); | ||
| console.log( | ||
| JSON.stringify({ | ||
| aborted, | ||
| competing, | ||
| settledBeforeInferenceEnded, | ||
| retry, | ||
| loads, | ||
| inferences, | ||
| expectedSha256, | ||
| pixels, | ||
| }), | ||
| ); | ||
| } else { | ||
| throw new Error(`Unknown scenario: ${scenario}`); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /** Synthetic little-endian TIFF/DNG metadata with an IFD1 JPEG, not a camera RAW decoder fixture. */ | ||
| export function embeddedPreviewFixture(jpeg: Uint8Array, orientation = 6): Buffer { | ||
| const offset = 8 + 2 + 2 * 12 + 4; | ||
| const dataOffset = offset + 2 + 5 * 12 + 4; | ||
| const bytes = Buffer.alloc(dataOffset + jpeg.length); | ||
| bytes.write("II"); | ||
| bytes.writeUInt16LE(42, 2); | ||
| bytes.writeUInt32LE(8, 4); | ||
| bytes.writeUInt16LE(2, 8); | ||
| const entry = (at: number, tag: number, type: number, count: number, value: number) => { | ||
| bytes.writeUInt16LE(tag, at); | ||
| bytes.writeUInt16LE(type, at + 2); | ||
| bytes.writeUInt32LE(count, at + 4); | ||
| bytes.writeUInt32LE(value, at + 8); | ||
| }; | ||
| entry(10, 0x112, 3, 1, orientation); | ||
| entry(22, 0xc612, 1, 4, 0x00000401); | ||
| bytes.writeUInt32LE(offset, 34); | ||
| bytes.writeUInt16LE(5, offset); | ||
| entry(offset + 2, 0x100, 4, 1, 32); | ||
| entry(offset + 14, 0x101, 4, 1, 16); | ||
| entry(offset + 26, 0x103, 3, 1, 6); | ||
| entry(offset + 38, 0x201, 4, 1, dataOffset); | ||
| entry(offset + 50, 0x202, 4, 1, jpeg.length); | ||
| bytes.set(jpeg, dataOffset); | ||
| return bytes; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /** Isolate model mocks from the other package suites and from real-container smoke tests. */ | ||
| export async function runClassificationScenario(scenario: string) { | ||
| const child = Bun.spawn( | ||
| [ | ||
| process.execPath, | ||
| new URL("./classification-lifecycle.ts", import.meta.url).pathname, | ||
| scenario, | ||
| ], | ||
| { | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }, | ||
| ); | ||
| const timeout = setTimeout(() => child.kill(), 4_000); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| try { | ||
| const [stdout, stderr, code] = await Promise.all([ | ||
| new Response(child.stdout).text(), | ||
| new Response(child.stderr).text(), | ||
| child.exited, | ||
| ]); | ||
| if (code !== 0) throw new Error(`Classification fixture failed (${code}): ${stderr}`); | ||
| return JSON.parse(stdout); | ||
| } finally { | ||
| clearTimeout(timeout); | ||
| if (child.exitCode === null) child.kill(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: sv2dev/m4k
Length of output: 1208
Script executed:
Repository: sv2dev/m4k
Length of output: 1568
Web query:
Bun.spawn cmd array argv passed as-is documentation Bun shell spawn arguments not URL decodedResult:
Convert the file URL with
fileURLToPathbefore spawning.pathnamekeeps URL escapes, so a checkout path with a space becomes%20.Bun.spawnpasses that string to the child unchanged. The child cannot open the fixture, sorunClassificationScenariothrows for every caller.Suggested fix