Skip to content

Commit

Permalink
adds a/sync options for classify method
Browse files Browse the repository at this point in the history
  • Loading branch information
craiglabenz committed Oct 31, 2023
1 parent 4bb3ba7 commit cb4dde3
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@ export 'universal_text_classifier.dart'

/// Channel to analyze text via MediaPipe's text classification task.
abstract class BaseTextClassifier {
/// Generative constructor.
BaseTextClassifier({required this.options});

/// Configuration object for tasks completed by this classifier.
/// Configuration wobject for tasks completed by this classifier.
final TextClassifierOptions options;

/// Sends a `String` value to MediaPipe for classification.
TextClassifierResult classify(String text);
/// Sends a `String` value to MediaPipe for classification. Uses an Isolate
/// on mobile and desktop, and a web worker on web, to add concurrency and avoid
/// blocking the UI thread while this task completes.
///
/// See also:
/// * [classify_sync] for a synchronous alternative
Future<TextClassifierResult> classify(String text);

/// Sends a `String` value to MediaPipe for classification. Blocks the main
/// event loop for the duration of the task, so use this with caution.
///
/// See also:
/// * [classify] for a concurrent alternative
TextClassifierResult classifySync(String text);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:mediapipe_core/mediapipe_core.dart';

Expand All @@ -11,10 +12,26 @@ final _log = Logger('TextClassifier');

/// TextClassifier implementation able to use FFI and `dart:io`.
class TextClassifier extends BaseTextClassifier {
/// Generative constructor.
TextClassifier({required super.options});

/// Sends a `String` value to MediaPipe for classification. Uses an Isolate
/// on mobile and desktop, and a web worker on web, to add concurrency and avoid
/// blocking the UI thread while this task completes.
///
/// See also:
/// * [classify_sync] for a synchronous alternative
@override
TextClassifierResult classify(String text) {
Future<TextClassifierResult> classify(String text) async =>
compute<String, TextClassifierResult>(classifySync, text);

/// Sends a `String` value to MediaPipe for classification. Blocks the main
/// event loop for the duration of the task, so use this with caution.
///
/// See also:
/// * [classify] for a concurrent alternative
@override
TextClassifierResult classifySync(String text) {
// Allocate and hydrate the configuration object
final optionsPtr = options.toStruct();
_log.finest('optionsPtr: $optionsPtr');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,29 @@ import 'package:mediapipe_text/mediapipe_text.dart';

/// Channel to classify text via MediaPipe's "classifyText" Task.
class TextClassifier extends BaseTextClassifier {
/// Generative constructor.
TextClassifier({required super.options}) {
throw Exception('Must use the web or FFI implementations');
}

/// Sends a `String` value to MediaPipe for classification.
/// Sends a `String` value to MediaPipe for classification. Uses an Isolate
/// on mobile and desktop, and a web worker on web, to add concurrency and avoid
/// blocking the UI thread while this task completes.
///
/// See also:
/// * [classify_sync] for a synchronous alternative
@override
TextClassifierResult classify(String text) {
Future<TextClassifierResult> classify(String text) async {
throw Exception('Must use the web or FFI implementations');
}

/// Sends a `String` value to MediaPipe for classification. Blocks the main
/// event loop for the duration of the task, so use this with caution.
///
/// See also:
/// * [classify] for a concurrent alternative
@override
TextClassifierResult classifySync(String text) {
throw Exception('Must use the web or FFI implementations');
}
}
43 changes: 25 additions & 18 deletions packages/mediapipe-task-text/test/text_classifier_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,32 @@ void main() {
);

test('return results from C', () {
final result = classifier.classify('some text');
expect(result.timestamp, const Duration(milliseconds: 300));
expect(result.classifications.length, 1);

final classifications = result.classifications.first;
expect(classifications.headName, 'Whatever');
expect(classifications.headIndex, 1);
expect(classifications.categories.length, 2);

final categories = result.classifications.first.categories;
expect(categories.first.index, 0);
expect(categories.first.score, closeTo(0.9, 0.001));
expect(categories.first.categoryName, 'some text');
expect(categories.first.displayName, 'some text');
_verifyResults(classifier.classifySync('some text'));
});

expect(categories.last.index, 1);
expect(categories.last.score, closeTo(0.7, 0.001));
expect(categories.last.categoryName, 'some text');
expect(categories.last.displayName, 'some text');
test('return results from C asynchronously', () async {
_verifyResults(await classifier.classify('some text'));
});
});
}

void _verifyResults(TextClassifierResult result) {
expect(result.timestamp, const Duration(milliseconds: 300));
expect(result.classifications.length, 1);

final classifications = result.classifications.first;
expect(classifications.headName, 'Whatever');
expect(classifications.headIndex, 1);
expect(classifications.categories.length, 2);

final categories = result.classifications.first.categories;
expect(categories.first.index, 0);
expect(categories.first.score, closeTo(0.9, 0.001));
expect(categories.first.categoryName, 'some text');
expect(categories.first.displayName, 'some text');

expect(categories.last.index, 1);
expect(categories.last.score, closeTo(0.7, 0.001));
expect(categories.last.categoryName, 'some text');
expect(categories.last.displayName, 'some text');
}

0 comments on commit cb4dde3

Please sign in to comment.