Comments (7)
I works !!
void main() async {
try {
final transformer = await importModule(transformers).toDart;
final env = transformer.getProperty('env'.toJS) as JSObject;
env.getProperty('allowLocalModels'.toJS);
env.setProperty('allowLocalModels'.toJS, false.toJS);
env.setProperty('useBrowserCache'.toJS, true.toJS);
final pipe = await ((transformer.callMethod('pipeline'.toJS, 'sentiment-analysis'.toJS, 'Xenova/distilbert-base-uncased-finetuned-sst-2-english'.toJS)) as JSPromise<Callable>).toDart;
final out = await (pipe.customCall('I like wine'.toJS) as JSPromise<JSAny>).toDart;
print("RES: ${stringify(out)}");
} catch (e) {
print("Error: $e");
}
runApp(const MyApp());
}
extension type Callable._(JSObject _) implements JSObject {
@JS('_call')
external JSAny? customCall(JSAny? a);
}
@JS('JSON.stringify')
external String stringify(JSAny obj);
Debug service listening on ws://127.0.0.1:64705/8EYvmhS7W0s=/ws
[object Object]
Pipe ok
RES: [{"label":"POSITIVE","score":0.9996498823165894}]
from sdk.
You can dynamically import the module use importModule
in combination with callAsFunction
to call the JS functions. For example (I'm assuming pipeline
and pipe
are JS functions):
import 'dart:js_interop';
const transformers = 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]';
void main() async {
final pipeline = (await importModule(transformers).toDart) as JSFunction;
final pipe = await (pipeline.callAsFunction(null, 'sentiment-analysis'.toJS) as JSPromise<JSFunction>).toDart;
final out = await (pipe.callAsFunction(null, 'I like wine'.toJS) as JSPromise<JSAny?>).toDart;
}
from sdk.
I get this error: TypeError: Instance of 'LegacyJavaScriptObject': type 'LegacyJavaScriptObject' is not a subtype of type 'JavaScriptFunction'
From sourceCode: https://github.com/xenova/transformers.js/blob/main/src/pipelines.js
Pipeline extends a Callable
const transformers = 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]';
void main() async {
try {
final pipeline = (await importModule(transformers).toDart) as JSFunction;
print("Pipeline: $pipeline");
final pipe = await (pipeline.callAsFunction(null, 'sentiment-analysis'.toJS) as JSPromise<JSFunction>).toDart;
final out = await (pipe.callAsFunction(null, 'I like wine'.toJS) as JSPromise<JSAny?>).toDart;
print(out);
} catch (e) {
print(e);
}
}
Callable:
export const Callable = /** @type {any} */ (class {
/**
* Creates a new instance of the Callable class.
*/
constructor() {
/**
* Creates a closure that delegates to a private method '_call' with the given arguments.
* @type {any}
* @param {...any} args Zero or more arguments to pass to the '_call' method.
* @returns {*} The result of calling the '_call' method.
*/
let closure = function (...args) {
return closure._call(...args)
}
return Object.setPrototypeOf(closure, new.target.prototype)
}
/**
* This method should be implemented in subclasses to provide the
* functionality of the callable object.
*
* @param {any[]} args
* @throws {Error} If the subclass does not implement the `_call` method.
*/
_call(...args) {
throw Error('Must implement _call method in subclass')
}
});
from sdk.
Hmm, it looks like it isn't a JS function (hence the type error) and they use their own custom callable type instead, so we'd need JSObject
. If _call
is going to be called in the subclass eventually, we could just use that and avoid the ()
since we don't have an interop way yet to call arbitrary objects.
import 'dart:js_interop';
const transformers = 'https://cdn.jsdelivr.net/npm/@xenova/[email protected]';
extension type Callable._(JSObject _) implements JSObject {
@JS('_call')
external JSAny? customCall(String arg);
}
void main() async {
final pipeline = (await importModule(transformers).toDart) as Callable;
final pipe = await (pipeline.customCall('sentiment-analysis') as JSPromise<Callable>).toDart;
final out = await (pipe.customCall('I like wine') as JSPromise<JSAny?>).toDart;
}
from sdk.
Tx for your support !
It seems we are missing something...
I have continued to understand the source code and the pipeline is an (export) async function who is returning the right Pipeline regarding the task parameter (sentiment-analysis in our case).
I have updated the code to get a async function but I do not know how to call the method
Quick question: how do we know the import function with are importing with the importModule ? (the js file as multiple exports and we want to import the pipeline function)
try {
final pipeline = ((await importModule(transformers).toDart) as JSPromise<JSFunction>).toDart;
final pipe = await <how to call pipeline function>('sentiment-analysis'.toJS) as Callable;
final out = await (pipe.customCall('I like wine'.toJS) as JSPromise<JSAny?>).toDart;
print(out);
} catch (e) {
print(e);
}
export async function pipeline(
task,
model = null,
{
quantized = true,
progress_callback = null,
config = null,
cache_dir = null,
local_files_only = false,
revision = 'main',
} = {}
) {
// Helper method to construct pipeline
// Apply aliases
// @ts-ignore
task = TASK_ALIASES[task] ?? task;
// Get pipeline info
const pipelineInfo = SUPPORTED_TASKS[task.split('_', 1)[0]];
if (!pipelineInfo) {
throw Error(`Unsupported pipeline: ${task}. Must be one of [${Object.keys(SUPPORTED_TASKS)}]`)
}
// Use model if specified, otherwise, use default
if (!model) {
model = pipelineInfo.default.model
console.log(`No model specified. Using default model: "${model}".`);
}
const pretrainedOptions = {
quantized,
progress_callback,
config,
cache_dir,
local_files_only,
revision,
}
const classes = new Map([
['tokenizer', pipelineInfo.tokenizer],
['model', pipelineInfo.model],
['processor', pipelineInfo.processor],
]);
// Load model, tokenizer, and processor (if they exist)
const results = await loadItems(classes, model, pretrainedOptions);
results.task = task;
dispatchCallback(progress_callback, {
'status': 'ready',
'task': task,
'model': model,
});
const pipelineClass = pipelineInfo.pipeline;
return new pipelineClass(results);
}
from sdk.
I have updated the code to get a async function but I do not know how to call the method
If pipeline
is indeed a JS function (which it looks like it is), then you can just use callAsFunction
like in the example above. callAsFunction
lowers to something like <function>.call(...)
which should work with JS functions. I'm confused on what the Callable
class is for then if pipeline
is a function. Maybe pipe
is a Callable
? That would be consistent with your latest Dart code.
Quick question: how do we know the import function with are importing with the importModule ? (the js file as multiple exports and we want to import the pipeline function)
Can you elaborate what you mean here? importModule
calls the dynamic import
function with the given module name.
from sdk.
Cool! Looks like the result of pipeline
was indeed a Callable
. I'll go ahead and mark this as resolved.
from sdk.
Related Issues (20)
- Improve `dart:developer` Timeline API HOT 1
- Even though the types match, the analyzer still reports a type mismatch, but there are no errors at runtime HOT 3
- [vm/core] Inlining `_GrowableList.add` bloats program too much HOT 4
- vm/dart/isolates/shared_primitives_test changing from flaky to RTE on vm-reload-linux-debug-x64 HOT 2
- [diagnostic] don't report `DUPLICATE_FIELD_NAME` if the name is invalid
- Refactoring to convert params/args between positional and named HOT 2
- Analyzer exception doesn't specify affected file HOT 3
- VM always prints "<isolate> has no debugger is attached and is paused"
- [dart2wasm] UnimplementedError when calling Flutter's Matrix4.translate with an integer argument HOT 2
- `where` can't accept an async predicate HOT 6
- [Augmentations] Wrong error in the analyzer when augmenting enum member
- [Augmentations] No error in the analyzer when augmenting a function type with no augmented type
- [Augmentations] No error in analyzer for mixin and extension when augmentation is before original declaration HOT 1
- [Augmentations] Analyzer crash on co19/LanguageFeatures/Augmentation-libraries/augmenting_types_A01_t01
- [Augmentations] Analyzer error when augmenting representation variable
- [Augmentations] Omitted type bound is not inherited
- [Augmentations] No error in the analyzer if augmentation declares a different return type HOT 2
- The integer negative zero formats as "-0.0" HOT 7
- [dart2js] bug in global inference with `call` getter HOT 1
- DevTools URI opens to a white screen
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sdk.