Skip to content

Commit

Permalink
cleanup after review
Browse files Browse the repository at this point in the history
  • Loading branch information
craiglabenz committed Oct 31, 2023
1 parent 0f2cd63 commit 4bb3ba7
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/mediapipe-task-text/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include: package:flutter_lints/flutter.yaml
include: ../analysis_options.yaml

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
import 'package:mediapipe_text/mediapipe_text.dart';

export 'universal_text_classifier.dart'
if (dart.library.html) 'web_text_classifier.dart'
if (dart.library.io) 'ffi_text_classifier.dart';
if (dart.library.html) 'text_classifier_web.dart'
if (dart.library.io) 'text_classifier_io.dart';

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

/// Configuration object passed into C or JavaScript to influence how
/// MediaPipe completes the requested operation.
/// Configuration object for tasks completed by this classifier.
final TextClassifierOptions options;

/// Location for where to find the means to communicate with the MediaPipe SDK.
///
/// For mobile and desktop targets, this should be the location on the local
/// filesystem of the C wrappers.
///
/// For web builds, this should be the namespace off the global `window`
/// object where the JavaScript wrapper can be found.
// final String sdkPath;

/// Sends a `String` value to MediaPipe for classification.
TextClassifierResult classify(String text);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,22 @@ final _log = Logger('TextClassifier');

/// TextClassifier implementation able to use FFI and `dart:io`.
class TextClassifier extends BaseTextClassifier {
TextClassifier({required super.options, required super.sdkPath}) {
_bindings = bindings.MediaPipeTextBindings(DynamicLibrary.open(sdkPath));
}

late bindings.MediaPipeTextBindings _bindings;
TextClassifier({required super.options});

@override
TextClassifierResult classify(String text) {
// Allocate and hydrate the configuration object
final optionsPtr = options.toStruct();
_log.finest('optionsPtr: $optionsPtr');
final classifierPtr = _bindings.text_classifier_create(optionsPtr);
final classifierPtr = bindings.text_classifier_create(optionsPtr);
_log.finest('classifierPtr: $classifierPtr');

// Allocate the container for our results
final resultsPtr = calloc<bindings.TextClassifierResult>();
_log.finest('resultsPtr: $resultsPtr');

// Actually classify the text
_bindings.text_classifier_classify(
bindings.text_classifier_classify(
classifierPtr,
prepareString(text),
resultsPtr,
Expand All @@ -39,7 +35,7 @@ class TextClassifier extends BaseTextClassifier {
// Convert the results into pure-Dart objects and free all memory
final result = TextClassifierResult.fromStruct(resultsPtr.ref);
_log.fine('Text classification result: $result');
_bindings.text_classifier_close(classifierPtr);
bindings.text_classifier_close(classifierPtr);
TextClassifierResult.freeStruct(resultsPtr);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:mediapipe_text/mediapipe_text.dart';

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:mediapipe_text/mediapipe_text.dart';
void main() {
group('FFI-based TextClassifier.classify should', () {
final classifier = TextClassifier(
sdkPath: 'test/c/fake_text_classifier.dylib',
options: TextClassifierOptions.fromAssetPath('fake'),
);

Expand Down

0 comments on commit 4bb3ba7

Please sign in to comment.