feat(ml): composable ml (#9973)

* modularize model classes

* various fixes

* expose port

* change response

* round coordinates

* simplify preload

* update server

* simplify interface

simplify

* update tests

* composable endpoint

* cleanup

fixes

remove unnecessary interface

support text input, cleanup

* ew camelcase

* update server

server fixes

fix typing

* ml fixes

update locustfile

fixes

* cleaner response

* better repo response

* update tests

formatting and typing

rename

* undo compose change

* linting

fix type

actually fix typing

* stricter typing

fix detection-only response

no need for defaultdict

* update spec file

update api

linting

* update e2e

* unnecessary dimension

* remove commented code

* remove duplicate code

* remove unused imports

* add batch dim
This commit is contained in:
Mert 2024-06-06 23:09:47 -04:00 committed by GitHub
parent 7a46f80ddc
commit 2b1b43a7e4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
39 changed files with 982 additions and 999 deletions

View file

@ -14,63 +14,31 @@ class CLIPConfig {
/// Returns a new [CLIPConfig] instance.
CLIPConfig({
required this.enabled,
this.mode,
required this.modelName,
this.modelType,
});
bool enabled;
///
/// Please note: This property should have been non-nullable! Since the specification file
/// does not include a default value (using the "default:" property), however, the generated
/// source code must fall back to having a nullable type.
/// Consider adding a "default:" property in the specification file to hide this note.
///
CLIPMode? mode;
String modelName;
///
/// Please note: This property should have been non-nullable! Since the specification file
/// does not include a default value (using the "default:" property), however, the generated
/// source code must fall back to having a nullable type.
/// Consider adding a "default:" property in the specification file to hide this note.
///
ModelType? modelType;
@override
bool operator ==(Object other) => identical(this, other) || other is CLIPConfig &&
other.enabled == enabled &&
other.mode == mode &&
other.modelName == modelName &&
other.modelType == modelType;
other.modelName == modelName;
@override
int get hashCode =>
// ignore: unnecessary_parenthesis
(enabled.hashCode) +
(mode == null ? 0 : mode!.hashCode) +
(modelName.hashCode) +
(modelType == null ? 0 : modelType!.hashCode);
(modelName.hashCode);
@override
String toString() => 'CLIPConfig[enabled=$enabled, mode=$mode, modelName=$modelName, modelType=$modelType]';
String toString() => 'CLIPConfig[enabled=$enabled, modelName=$modelName]';
Map<String, dynamic> toJson() {
final json = <String, dynamic>{};
json[r'enabled'] = this.enabled;
if (this.mode != null) {
json[r'mode'] = this.mode;
} else {
// json[r'mode'] = null;
}
json[r'modelName'] = this.modelName;
if (this.modelType != null) {
json[r'modelType'] = this.modelType;
} else {
// json[r'modelType'] = null;
}
return json;
}
@ -83,9 +51,7 @@ class CLIPConfig {
return CLIPConfig(
enabled: mapValueOfType<bool>(json, r'enabled')!,
mode: CLIPMode.fromJson(json[r'mode']),
modelName: mapValueOfType<String>(json, r'modelName')!,
modelType: ModelType.fromJson(json[r'modelType']),
);
}
return null;

View file

@ -1,85 +0,0 @@
//
// AUTO-GENERATED FILE, DO NOT MODIFY!
//
// @dart=2.18
// ignore_for_file: unused_element, unused_import
// ignore_for_file: always_put_required_named_parameters_first
// ignore_for_file: constant_identifier_names
// ignore_for_file: lines_longer_than_80_chars
part of openapi.api;
class CLIPMode {
/// Instantiate a new enum with the provided [value].
const CLIPMode._(this.value);
/// The underlying value of this enum member.
final String value;
@override
String toString() => value;
String toJson() => value;
static const vision = CLIPMode._(r'vision');
static const text = CLIPMode._(r'text');
/// List of all possible values in this [enum][CLIPMode].
static const values = <CLIPMode>[
vision,
text,
];
static CLIPMode? fromJson(dynamic value) => CLIPModeTypeTransformer().decode(value);
static List<CLIPMode> listFromJson(dynamic json, {bool growable = false,}) {
final result = <CLIPMode>[];
if (json is List && json.isNotEmpty) {
for (final row in json) {
final value = CLIPMode.fromJson(row);
if (value != null) {
result.add(value);
}
}
}
return result.toList(growable: growable);
}
}
/// Transformation class that can [encode] an instance of [CLIPMode] to String,
/// and [decode] dynamic data back to [CLIPMode].
class CLIPModeTypeTransformer {
factory CLIPModeTypeTransformer() => _instance ??= const CLIPModeTypeTransformer._();
const CLIPModeTypeTransformer._();
String encode(CLIPMode data) => data.value;
/// Decodes a [dynamic value][data] to a CLIPMode.
///
/// If [allowNull] is true and the [dynamic value][data] cannot be decoded successfully,
/// then null is returned. However, if [allowNull] is false and the [dynamic value][data]
/// cannot be decoded successfully, then an [UnimplementedError] is thrown.
///
/// The [allowNull] is very handy when an API changes and a new enum value is added or removed,
/// and users are still using an old app with the old code.
CLIPMode? decode(dynamic data, {bool allowNull = true}) {
if (data != null) {
switch (data) {
case r'vision': return CLIPMode.vision;
case r'text': return CLIPMode.text;
default:
if (!allowNull) {
throw ArgumentError('Unknown enum value to decode: $data');
}
}
}
return null;
}
/// Singleton [CLIPModeTypeTransformer] instance.
static CLIPModeTypeTransformer? _instance;
}

View file

@ -10,15 +10,14 @@
part of openapi.api;
class RecognitionConfig {
/// Returns a new [RecognitionConfig] instance.
RecognitionConfig({
class FacialRecognitionConfig {
/// Returns a new [FacialRecognitionConfig] instance.
FacialRecognitionConfig({
required this.enabled,
required this.maxDistance,
required this.minFaces,
required this.minScore,
required this.modelName,
this.modelType,
});
bool enabled;
@ -36,22 +35,13 @@ class RecognitionConfig {
String modelName;
///
/// Please note: This property should have been non-nullable! Since the specification file
/// does not include a default value (using the "default:" property), however, the generated
/// source code must fall back to having a nullable type.
/// Consider adding a "default:" property in the specification file to hide this note.
///
ModelType? modelType;
@override
bool operator ==(Object other) => identical(this, other) || other is RecognitionConfig &&
bool operator ==(Object other) => identical(this, other) || other is FacialRecognitionConfig &&
other.enabled == enabled &&
other.maxDistance == maxDistance &&
other.minFaces == minFaces &&
other.minScore == minScore &&
other.modelName == modelName &&
other.modelType == modelType;
other.modelName == modelName;
@override
int get hashCode =>
@ -60,11 +50,10 @@ class RecognitionConfig {
(maxDistance.hashCode) +
(minFaces.hashCode) +
(minScore.hashCode) +
(modelName.hashCode) +
(modelType == null ? 0 : modelType!.hashCode);
(modelName.hashCode);
@override
String toString() => 'RecognitionConfig[enabled=$enabled, maxDistance=$maxDistance, minFaces=$minFaces, minScore=$minScore, modelName=$modelName, modelType=$modelType]';
String toString() => 'FacialRecognitionConfig[enabled=$enabled, maxDistance=$maxDistance, minFaces=$minFaces, minScore=$minScore, modelName=$modelName]';
Map<String, dynamic> toJson() {
final json = <String, dynamic>{};
@ -73,38 +62,32 @@ class RecognitionConfig {
json[r'minFaces'] = this.minFaces;
json[r'minScore'] = this.minScore;
json[r'modelName'] = this.modelName;
if (this.modelType != null) {
json[r'modelType'] = this.modelType;
} else {
// json[r'modelType'] = null;
}
return json;
}
/// Returns a new [RecognitionConfig] instance and imports its values from
/// Returns a new [FacialRecognitionConfig] instance and imports its values from
/// [value] if it's a [Map], null otherwise.
// ignore: prefer_constructors_over_static_methods
static RecognitionConfig? fromJson(dynamic value) {
static FacialRecognitionConfig? fromJson(dynamic value) {
if (value is Map) {
final json = value.cast<String, dynamic>();
return RecognitionConfig(
return FacialRecognitionConfig(
enabled: mapValueOfType<bool>(json, r'enabled')!,
maxDistance: mapValueOfType<double>(json, r'maxDistance')!,
minFaces: mapValueOfType<int>(json, r'minFaces')!,
minScore: mapValueOfType<double>(json, r'minScore')!,
modelName: mapValueOfType<String>(json, r'modelName')!,
modelType: ModelType.fromJson(json[r'modelType']),
);
}
return null;
}
static List<RecognitionConfig> listFromJson(dynamic json, {bool growable = false,}) {
final result = <RecognitionConfig>[];
static List<FacialRecognitionConfig> listFromJson(dynamic json, {bool growable = false,}) {
final result = <FacialRecognitionConfig>[];
if (json is List && json.isNotEmpty) {
for (final row in json) {
final value = RecognitionConfig.fromJson(row);
final value = FacialRecognitionConfig.fromJson(row);
if (value != null) {
result.add(value);
}
@ -113,12 +96,12 @@ class RecognitionConfig {
return result.toList(growable: growable);
}
static Map<String, RecognitionConfig> mapFromJson(dynamic json) {
final map = <String, RecognitionConfig>{};
static Map<String, FacialRecognitionConfig> mapFromJson(dynamic json) {
final map = <String, FacialRecognitionConfig>{};
if (json is Map && json.isNotEmpty) {
json = json.cast<String, dynamic>(); // ignore: parameter_assignments
for (final entry in json.entries) {
final value = RecognitionConfig.fromJson(entry.value);
final value = FacialRecognitionConfig.fromJson(entry.value);
if (value != null) {
map[entry.key] = value;
}
@ -127,14 +110,14 @@ class RecognitionConfig {
return map;
}
// maps a json object with a list of RecognitionConfig-objects as value to a dart map
static Map<String, List<RecognitionConfig>> mapListFromJson(dynamic json, {bool growable = false,}) {
final map = <String, List<RecognitionConfig>>{};
// maps a json object with a list of FacialRecognitionConfig-objects as value to a dart map
static Map<String, List<FacialRecognitionConfig>> mapListFromJson(dynamic json, {bool growable = false,}) {
final map = <String, List<FacialRecognitionConfig>>{};
if (json is Map && json.isNotEmpty) {
// ignore: parameter_assignments
json = json.cast<String, dynamic>();
for (final entry in json.entries) {
map[entry.key] = RecognitionConfig.listFromJson(entry.value, growable: growable,);
map[entry.key] = FacialRecognitionConfig.listFromJson(entry.value, growable: growable,);
}
}
return map;

View file

@ -1,85 +0,0 @@
//
// AUTO-GENERATED FILE, DO NOT MODIFY!
//
// @dart=2.18
// ignore_for_file: unused_element, unused_import
// ignore_for_file: always_put_required_named_parameters_first
// ignore_for_file: constant_identifier_names
// ignore_for_file: lines_longer_than_80_chars
part of openapi.api;
class ModelType {
/// Instantiate a new enum with the provided [value].
const ModelType._(this.value);
/// The underlying value of this enum member.
final String value;
@override
String toString() => value;
String toJson() => value;
static const facialRecognition = ModelType._(r'facial-recognition');
static const clip = ModelType._(r'clip');
/// List of all possible values in this [enum][ModelType].
static const values = <ModelType>[
facialRecognition,
clip,
];
static ModelType? fromJson(dynamic value) => ModelTypeTypeTransformer().decode(value);
static List<ModelType> listFromJson(dynamic json, {bool growable = false,}) {
final result = <ModelType>[];
if (json is List && json.isNotEmpty) {
for (final row in json) {
final value = ModelType.fromJson(row);
if (value != null) {
result.add(value);
}
}
}
return result.toList(growable: growable);
}
}
/// Transformation class that can [encode] an instance of [ModelType] to String,
/// and [decode] dynamic data back to [ModelType].
class ModelTypeTypeTransformer {
factory ModelTypeTypeTransformer() => _instance ??= const ModelTypeTypeTransformer._();
const ModelTypeTypeTransformer._();
String encode(ModelType data) => data.value;
/// Decodes a [dynamic value][data] to a ModelType.
///
/// If [allowNull] is true and the [dynamic value][data] cannot be decoded successfully,
/// then null is returned. However, if [allowNull] is false and the [dynamic value][data]
/// cannot be decoded successfully, then an [UnimplementedError] is thrown.
///
/// The [allowNull] is very handy when an API changes and a new enum value is added or removed,
/// and users are still using an old app with the old code.
ModelType? decode(dynamic data, {bool allowNull = true}) {
if (data != null) {
switch (data) {
case r'facial-recognition': return ModelType.facialRecognition;
case r'clip': return ModelType.clip;
default:
if (!allowNull) {
throw ArgumentError('Unknown enum value to decode: $data');
}
}
}
return null;
}
/// Singleton [ModelTypeTypeTransformer] instance.
static ModelTypeTypeTransformer? _instance;
}

View file

@ -26,7 +26,7 @@ class SystemConfigMachineLearningDto {
bool enabled;
RecognitionConfig facialRecognition;
FacialRecognitionConfig facialRecognition;
String url;
@ -71,7 +71,7 @@ class SystemConfigMachineLearningDto {
clip: CLIPConfig.fromJson(json[r'clip'])!,
duplicateDetection: DuplicateDetectionConfig.fromJson(json[r'duplicateDetection'])!,
enabled: mapValueOfType<bool>(json, r'enabled')!,
facialRecognition: RecognitionConfig.fromJson(json[r'facialRecognition'])!,
facialRecognition: FacialRecognitionConfig.fromJson(json[r'facialRecognition'])!,
url: mapValueOfType<String>(json, r'url')!,
);
}