File size: 3,357 Bytes
a03b3ba |
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
import { test, describe, assert } from "vitest";
import { readFileSync } from "fs";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
import { Blob } from "node:buffer";
const __dirname = dirname(fileURLToPath(import.meta.url));
const image_path = join(
__dirname,
"..",
"..",
"..",
"demo",
"kitchen_sink",
"files",
"lion.jpg"
);
import { walk_and_store_blobs, client, handle_blob } from "./client";
describe.skip("extract blob parts", () => {
test("convert Buffer to Blob", async () => {
const image = readFileSync(image_path);
await client("gradio/hello_world_main");
const parts = walk_and_store_blobs({
data: {
image
}
});
assert.isTrue(parts[0].blob instanceof Blob);
});
test("leave node Blob as Blob", async () => {
const image = new Blob([readFileSync(image_path)]);
await client("gradio/hello_world_main");
const parts = walk_and_store_blobs({
data: {
image
}
});
assert.isTrue(parts[0].blob instanceof Blob);
});
test("handle deep structures", async () => {
const image = new Blob([readFileSync(image_path)]);
await client("gradio/hello_world_main");
const parts = walk_and_store_blobs({
a: {
b: {
data: {
image
}
}
}
});
assert.isTrue(parts[0].blob instanceof Blob);
});
test("handle deep structures with arrays", async () => {
const image = new Blob([readFileSync(image_path)]);
await client("gradio/hello_world_main");
const parts = walk_and_store_blobs({
a: [
{
b: [
{
data: [
{
image
}
]
}
]
}
]
});
assert.isTrue(parts[0].blob instanceof Blob);
});
test("handle deep structures with arrays 2", async () => {
const image = new Blob([readFileSync(image_path)]);
await client("gradio/hello_world_main");
const obj = {
a: [
{
b: [
{
data: [[image], image, [image, [image]]]
}
]
}
]
};
const parts = walk_and_store_blobs(obj);
function map_path(
obj: Record<string, any>,
parts: { path: string[]; blob: any }[]
) {
const { path, blob } = parts[parts.length - 1];
let ref = obj;
path.forEach((p) => (ref = ref[p]));
return ref === blob;
}
assert.isTrue(parts[0].blob instanceof Blob);
// assert.isTrue(map_path(obj, parts));
});
});
describe("handle_blob", () => {
test("handle blobs", async () => {
const image = new Blob([readFileSync(image_path)]);
const app = await client("gradio/hello_world_main");
const obj = [
{
a: [
{
b: [
{
data: [[image], image, [image, [image]]]
}
]
}
]
}
];
const parts = await handle_blob(app.config.root, obj, undefined);
//@ts-ignore
// assert.isString(parts.data[0].a[0].b[0].data[0][0]);
});
});
describe.skip("private space", () => {
test("can access a private space", async () => {
const image = new Blob([readFileSync(image_path)]);
const app = await client("pngwn/hello_world", {
hf_token: "hf_"
});
console.log(app);
const obj = [
{
a: [
{
b: [
{
data: [[image], image, [image, [image]]]
}
]
}
]
}
];
const parts = await handle_blob(app.config.root, obj, "hf_");
//@ts-ignore
assert.isString(parts.data[0].a[0].b[0].data[0][0]);
});
});
|