Skip to content

Commit 8eee8d7

Browse files
authored
Fix TjsProxy.__call__ to unwrap the TjsProxy instances passed in the arguments (#34)
1 parent 5936aea commit 8eee8d7

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

pyodide-e2e/src/tests/pipeline.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,40 @@ output = await depth_estimator(as_url("/tmp/bread_small.png"))
7575

7676
await pyodide.runPythonAsync(`
7777
output["depth"].save('/tmp/depth.png')
78+
`);
79+
const depthImage: Uint8Array = pyodide.FS.readFile("/tmp/depth.png", { encoding: "binary" });
80+
// TODO: How to assert the depth image? Image snapshot is not available in the browser env.
81+
})
82+
83+
test("depth-estimation with a RawImage input", async () => {
84+
await pyodide.runPythonAsync(`
85+
from transformers_js_py import import_transformers_js, as_url
86+
87+
transformers = await import_transformers_js()
88+
89+
pipeline = transformers.pipeline
90+
RawImage = transformers.RawImage
91+
92+
depth_estimator = await pipeline('depth-estimation', 'Xenova/depth-anything-small-hf');
93+
94+
image = await RawImage.fromURL('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/db8bd36/bread_small.png')
95+
96+
output = await depth_estimator(image)
97+
`);
98+
const outputMap = await pyodide.globals.get("output").toJs() // Python's dict to JS's Map
99+
const output = Object.fromEntries(outputMap);
100+
101+
const depth = output.depth.toJs();
102+
const predictedDepth = output.predicted_depth.toJs();
103+
104+
// API reference: https://huggingface.co/Xenova/depth-anything-small-hf
105+
expect(depth.width).toBe(640)
106+
expect(depth.height).toBe(424)
107+
expect(depth.channels).toBe(1)
108+
expect(predictedDepth).toBeDefined()
109+
110+
await pyodide.runPythonAsync(`
111+
output["depth"].save('/tmp/depth.png')
78112
`);
79113
const depthImage: Uint8Array = pyodide.FS.readFile("/tmp/depth.png", { encoding: "binary" });
80114
// TODO: How to assert the depth image? Image snapshot is not available in the browser env.

transformers_js_py/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ def __init__(self, js_obj: pyodide.ffi.JsProxy):
4646
) # Ref: https://stackoverflow.com/a/30760236/13103190
4747

4848
def __call__(self, *args: Any, **kwds: Any) -> Any:
49+
args = [arg._js_obj if isinstance(arg, TjsProxy) else arg for arg in args]
50+
kwds = {k: v._js_obj if isinstance(v, TjsProxy) else v for k, v in kwds.items()}
4951
args = pyodide.ffi.to_js(args)
5052
kwds = pyodide.ffi.to_js(kwds)
5153

0 commit comments

Comments
 (0)