|
1 |
| -from typing import Any |
| 1 | +""" |
| 2 | +This module was created as an alias for `transformers_js_py` |
| 3 | +due to a historical reason that the original package name was `transformers_js`. |
| 4 | +Ref: https://github.com/whitphx/transformers.js.py/issues/26 |
| 5 | +""" |
2 | 6 |
|
3 |
| -import js |
4 |
| -import pyodide.code |
5 |
| -import pyodide.ffi |
6 |
| -import pyodide.webloop |
7 |
| - |
8 |
| -from .url import as_url |
9 |
| - |
10 |
| - |
11 |
| -class TjsModuleProxy: |
12 |
| - def __init__(self, js_obj: pyodide.ffi.JsProxy): |
13 |
| - if not isinstance(js_obj, pyodide.ffi.JsProxy) or js_obj.typeof != "object": |
14 |
| - raise TypeError("js_obj must be a JS module object") |
15 |
| - self.js_obj = js_obj |
16 |
| - |
17 |
| - def __getattr__(self, name: str) -> Any: |
18 |
| - res = getattr(self.js_obj, name) |
19 |
| - if isinstance(res, pyodide.ffi.JsProxy): |
20 |
| - return TjsProxy(res) |
21 |
| - return res |
22 |
| - |
23 |
| - def __repr__(self) -> str: |
24 |
| - return "TjsModuleProxy({})".format(", ".join(self.js_obj.object_keys())) |
25 |
| - |
26 |
| - |
27 |
| -class TjsProxy: |
28 |
| - def __init__(self, js_obj: pyodide.ffi.JsProxy): |
29 |
| - self._js_obj = js_obj |
30 |
| - |
31 |
| - def __call__(self, *args: Any, **kwds: Any) -> Any: |
32 |
| - if hasattr(self._js_obj, "_call"): |
33 |
| - args = pyodide.ffi.to_js(args) |
34 |
| - kwds = pyodide.ffi.to_js(kwds) |
35 |
| - |
36 |
| - # Transformers.js uses a custom _call() method |
37 |
| - # to make the JS classes callable. |
38 |
| - # https://github.com/xenova/transformers.js/blob/2.4.1/src/utils/core.js#L45-L77 |
39 |
| - res = self._js_obj._call(*args, **kwds) |
40 |
| - else: |
41 |
| - res = self._js_obj(*args, **kwds) |
42 |
| - |
43 |
| - return wrap_or_unwrap_proxy_object(res) |
44 |
| - |
45 |
| - def __getattr__(self, name: str) -> Any: |
46 |
| - res = getattr(self._js_obj, name) |
47 |
| - return wrap_or_unwrap_proxy_object(res) |
48 |
| - |
49 |
| - def __getitem__(self, key: Any) -> Any: |
50 |
| - res = self._js_obj[key] |
51 |
| - return wrap_or_unwrap_proxy_object(res) |
52 |
| - |
53 |
| - def __setitem__(self, key: Any, value: Any) -> None: |
54 |
| - self._js_obj[key] = value |
55 |
| - |
56 |
| - def __setattr__(self, __name: str, __value: Any) -> None: |
57 |
| - if __name == "_js_obj": |
58 |
| - super().__setattr__("_js_obj", __value) |
59 |
| - else: |
60 |
| - setattr(self._js_obj, __name, __value) |
61 |
| - |
62 |
| - |
63 |
| -def wrap_or_unwrap_proxy_object(obj): |
64 |
| - if isinstance(obj, pyodide.ffi.JsProxy): |
65 |
| - if obj.typeof == "object": |
66 |
| - return obj.to_py() |
67 |
| - return TjsProxy(obj) |
68 |
| - elif isinstance(obj, pyodide.webloop.PyodideFuture): |
69 |
| - return obj.then(wrap_or_unwrap_proxy_object) |
70 |
| - return obj |
71 |
| - |
72 |
| - |
73 |
| -async def import_transformers_js(version: str = "latest"): |
74 |
| - pyodide.code.run_js( |
75 |
| - """ |
76 |
| - async function loadTransformersJs(version) { |
77 |
| - const isBrowserMainThread = typeof window !== 'undefined'; |
78 |
| - const isWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope; |
79 |
| - const isBrowser = isBrowserMainThread || isWorker; |
80 |
| - const transformers = await import(isBrowser ? 'https://cdn.jsdelivr.net/npm/@xenova/transformers@' + version : '@xenova/transformers'); |
81 |
| -
|
82 |
| - transformers.env.allowLocalModels = false; |
83 |
| -
|
84 |
| - globalThis._transformers = { // Convert a module to an object. |
85 |
| - ...transformers, |
86 |
| - }; |
87 |
| - } |
88 |
| - """ # noqa: E501 |
89 |
| - ) |
90 |
| - loadTransformersJsFn = js.loadTransformersJs |
91 |
| - await loadTransformersJsFn(version) |
92 |
| - |
93 |
| - transformers = js._transformers |
94 |
| - return TjsModuleProxy(transformers) |
95 |
| - |
96 |
| - |
97 |
| -__all__ = ["as_url", "import_transformers_js"] |
| 7 | +from transformers_js_py import * # noqa: F401, F403 |
0 commit comments