Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions lib/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,57 @@
}

class Config {
static Hints hints = Hints();
static String _proxy = '';
static bool _proxyEnabled = false;
static ThemeMode _mode = ThemeMode.system;
static Envvars envvars = Envvars();

static String get proxy => _proxy;
static setProxy(String value) async {
static final Config _instance = Config._internal();
Config._internal();

factory Config () {
return _instance;
}

final List<String> _externalModels = [];
List<String> get externalModels => _externalModels;
addExternalModel(String path) async {
_externalModels.add(path);
await _save('externalModels', _externalModels);

Check warning on line 34 in lib/config.dart

View check run for this annotation

Codecov / codecov/patch

lib/config.dart#L32-L34

Added lines #L32 - L34 were not covered by tests
}
removeExternalModel(String path) async {
_externalModels.remove(path);
await _save('externalModels', _externalModels);

Check warning on line 38 in lib/config.dart

View check run for this annotation

Codecov / codecov/patch

lib/config.dart#L36-L38

Added lines #L36 - L38 were not covered by tests
}

Hints hints = Hints();
String _proxy = '';
bool _proxyEnabled = false;
ThemeMode _mode = ThemeMode.system;
Envvars envvars = Envvars();

String get proxy => _proxy;
setProxy(String value) async {
_proxy = value;
await _save('proxy', value);
}

static bool get proxyEnabled => _proxyEnabled;
static setProxyEnabled(bool value) async {
bool get proxyEnabled => _proxyEnabled;
setProxyEnabled(bool value) async {
_proxyEnabled = value;
await _save('proxyEnabled', value);
}

static ThemeMode get themeMode => _mode;
static setThemeMode(ThemeMode value) async {
ThemeMode get themeMode => _mode;
setThemeMode(ThemeMode value) async {
_mode = value;
await _save('mode', value.index);
}

static void reset() {
void reset() {
hints = Hints();
_proxy = '';
_proxyEnabled = false;
_mode = ThemeMode.system;
envvars = Envvars();
}

static Future<String> _getProxy() async {
Future<String> _getProxy() async {
final dio = Dio(BaseOptions(connectTimeout: const Duration(seconds: 10)));
dio.httpClientAdapter = IOHttpClientAdapter(
createHttpClient: () {
Expand All @@ -79,7 +97,7 @@
return '';
}

static Future<void> loadFromFile() async {
Future<void> loadFromFile() async {
final directory = await getApplicationSupportDirectory();
final file = File('${directory.path}/config.json');
if (await file.exists()) {
Expand All @@ -92,10 +110,11 @@
_proxy = await _getProxy();
}
_mode = ThemeMode.values[json['mode'] ?? 0];
_externalModels.addAll(List<String>.from(json['externalModels'] ?? []));
}
}

static Future<void> _save(String key, dynamic value) async {
Future<void> _save(String key, dynamic value) async {
final directory = await getApplicationSupportDirectory();
final file = File('${directory.path}/config.json');
Map<String, dynamic> json = {};
Expand All @@ -104,7 +123,8 @@
json = jsonDecode(contents);
}
json[key] = value;
await file.writeAsString(jsonEncode(json));
const encoder = JsonEncoder.withIndent(" ");
await file.writeAsString(encoder.convert(json));
}

}
21 changes: 11 additions & 10 deletions lib/deployment_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import 'dart:convert';
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:inference/config.dart';
import 'package:inference/importers/manifest_importer.dart';
import 'package:inference/migration/migration_manager.dart';
import 'package:inference/migration/migrations/migration_1.0.0_to_25.0.1.dart';
import 'package:inference/project.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart';
Expand Down Expand Up @@ -35,31 +35,32 @@
final directory = await getApplicationSupportDirectory();
final manifest = ManifestImporter("assets/manifest.json");
await manifest.loadManifest();
final migrationManager = MigrationManager(
final migrationManager = MigrationManager.withMigrations(
destinationVersion: currentApplicationVersion,
manifest: manifest.allModels,
migrations: [
MigrationV1ToV2501(),
]
);

return List.from(directory.listSync()
final modelPaths = [
Config().externalModels,
directory.listSync().map((m) => m.path),
].expand((l) => l);

return List.from(modelPaths
.map((projectFolder) {
if (!Directory(projectFolder.path).existsSync()) {
if (!Directory(projectFolder).existsSync()) {

Check warning on line 50 in lib/deployment_processor.dart

View check run for this annotation

Codecov / codecov/patch

lib/deployment_processor.dart#L50

Added line #L50 was not covered by tests
return null;
}
final platformContext = Context(style: Style.platform);
try {
final projectFile = File(platformContext.join(projectFolder.path, "project.json"));
final projectFile = File(platformContext.join(projectFolder, "project.json"));

Check warning on line 55 in lib/deployment_processor.dart

View check run for this annotation

Codecov / codecov/patch

lib/deployment_processor.dart#L55

Added line #L55 was not covered by tests
final content = projectFile.readAsStringSync();
var jsonContent = jsonDecode(content);
if (migrationManager.eligible((jsonContent))) {
print("Migrating ${projectFolder.path}");
jsonContent = migrationManager.migrate(jsonContent);
const encoder = JsonEncoder.withIndent(" ");
projectFile.writeAsStringSync(encoder.convert(jsonContent));
}
final project = Project.fromJson(jsonContent, projectFolder.path);
final project = Project.fromJson(jsonContent, projectFolder);

Check warning on line 63 in lib/deployment_processor.dart

View check run for this annotation

Codecov / codecov/patch

lib/deployment_processor.dart#L63

Added line #L63 was not covered by tests
project.loaded.complete();
return project;
} catch (exception, stack) {
Expand Down
3 changes: 2 additions & 1 deletion lib/importers/geti_deployment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import 'package:inference/interop/utils.dart';
import 'package:inference/importers/importer.dart';
import 'package:inference/project.dart';
import 'package:inference/utils.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:uuid/uuid.dart';
Expand Down Expand Up @@ -63,7 +64,7 @@
await processTask(task);
}
const encoder = JsonEncoder.withIndent(" ");
project!.size = project!.calculateDiskUsage();
project!.size = calculateDiskUsage(project!.storagePath);

Check warning on line 67 in lib/importers/geti_deployment.dart

View check run for this annotation

Codecov / codecov/patch

lib/importers/geti_deployment.dart#L67

Added line #L67 was not covered by tests
File(platformContext.join(project!.storagePath, "project.json"))
.writeAsString(encoder.convert(project!.toMap()));
project!.loaded.complete();
Expand Down
60 changes: 60 additions & 0 deletions lib/importers/model_directory_importer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'dart:convert';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import 'dart:convert';
// Copyright (c) 2024 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
import 'dart:convert';

import 'dart:io';

import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:inference/config.dart';
import 'package:inference/importers/importer.dart';
import 'package:inference/migration/migration_manager.dart';
import 'package:inference/project.dart';
import 'package:path/path.dart';

class ModelDirImporter extends Importer {
final String directory;
Project? project;

ModelDirImporter(this.directory);

Check warning on line 16 in lib/importers/model_directory_importer.dart

View check run for this annotation

Codecov / codecov/patch

lib/importers/model_directory_importer.dart#L16

Added line #L16 was not covered by tests

@override

Check warning on line 18 in lib/importers/model_directory_importer.dart

View check run for this annotation

Codecov / codecov/patch

lib/importers/model_directory_importer.dart#L18

Added line #L18 was not covered by tests
Future<Project> generateProject() async {
final projectJson = findProjectJsonFile();

Check warning on line 20 in lib/importers/model_directory_importer.dart

View check run for this annotation

Codecov / codecov/patch

lib/importers/model_directory_importer.dart#L20

Added line #L20 was not covered by tests
if (projectJson == null) {
throw Exception("no project json in file");

Check warning on line 22 in lib/importers/model_directory_importer.dart

View check run for this annotation

Codecov / codecov/patch

lib/importers/model_directory_importer.dart#L22

Added line #L22 was not covered by tests
} else {
final migrationManager = MigrationManager.withMigrations(

Check warning on line 24 in lib/importers/model_directory_importer.dart

View check run for this annotation

Codecov / codecov/patch

lib/importers/model_directory_importer.dart#L24

Added line #L24 was not covered by tests
destinationVersion: currentApplicationVersion,
manifest: [],

Check warning on line 26 in lib/importers/model_directory_importer.dart

View check run for this annotation

Codecov / codecov/patch

lib/importers/model_directory_importer.dart#L26

Added line #L26 was not covered by tests
);

final source = File(projectJson).readAsStringSync();
final migrated = migrationManager.migrate(jsonDecode(source));
project = Project.fromJson(migrated, directory);

Check warning on line 31 in lib/importers/model_directory_importer.dart

View check run for this annotation

Codecov / codecov/patch

lib/importers/model_directory_importer.dart#L29-L31

Added lines #L29 - L31 were not covered by tests
}
return project!;

Check warning on line 33 in lib/importers/model_directory_importer.dart

View check run for this annotation

Codecov / codecov/patch

lib/importers/model_directory_importer.dart#L33

Added line #L33 was not covered by tests
}

bool get containsProjectJson => findProjectJsonFile() != null;

Check warning on line 36 in lib/importers/model_directory_importer.dart

View check run for this annotation

Codecov / codecov/patch

lib/importers/model_directory_importer.dart#L36

Added line #L36 was not covered by tests

String? findProjectJsonFile() {
final files = Directory(directory).listSync();
return files.firstWhereOrNull((file) => basename(file.path) == "project.json")?.path;

Check warning on line 40 in lib/importers/model_directory_importer.dart

View check run for this annotation

Codecov / codecov/patch

lib/importers/model_directory_importer.dart#L38-L40

Added lines #L38 - L40 were not covered by tests
}

@override

Check warning on line 43 in lib/importers/model_directory_importer.dart

View check run for this annotation

Codecov / codecov/patch

lib/importers/model_directory_importer.dart#L43

Added line #L43 was not covered by tests
bool match() {
return findProjectJsonFile() != null;

Check warning on line 45 in lib/importers/model_directory_importer.dart

View check run for this annotation

Codecov / codecov/patch

lib/importers/model_directory_importer.dart#L45

Added line #L45 was not covered by tests
}

@override

Check warning on line 48 in lib/importers/model_directory_importer.dart

View check run for this annotation

Codecov / codecov/patch

lib/importers/model_directory_importer.dart#L48

Added line #L48 was not covered by tests
Future<void> setupFiles() async {

await Config().addExternalModel(project!.storagePath);
project!.loaded.complete();

Check warning on line 52 in lib/importers/model_directory_importer.dart

View check run for this annotation

Codecov / codecov/patch

lib/importers/model_directory_importer.dart#L51-L52

Added lines #L51 - L52 were not covered by tests
return;
}

@override

Check warning on line 56 in lib/importers/model_directory_importer.dart

View check run for this annotation

Codecov / codecov/patch

lib/importers/model_directory_importer.dart#L56

Added line #L56 was not covered by tests
Future<bool> askUser(BuildContext context) async {
return true;
}
}
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
WidgetsFlutterBinding.ensureInitialized();
await windowManager.ensureInitialized();
await ObjectBox.create();
await Config.loadFromFile();
await Config().loadFromFile();

Check warning on line 24 in lib/main.dart

View check run for this annotation

Codecov / codecov/patch

lib/main.dart#L24

Added line #L24 was not covered by tests
WindowOptions windowOptions = WindowOptions(
size: const Size(1400, 1024),
center: true,
Expand Down
14 changes: 14 additions & 0 deletions lib/migration/migration_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:collection/collection.dart';
import 'package:inference/importers/model_manifest.dart';
import 'package:inference/migration/migration.dart';
import 'package:inference/migration/migrations/migration_1.0.0_to_25.0.1.dart';

class MigrationManager {
final String destinationVersion;
Expand All @@ -17,6 +18,19 @@ class MigrationManager {
required this.migrations,
});

factory MigrationManager.withMigrations({
required String destinationVersion,
required List<ModelManifest> manifest,
}) {
return MigrationManager(
destinationVersion: destinationVersion,
manifest: manifest,
migrations: [
MigrationV1ToV2501(),
]
);
}

bool eligible(Map<String, dynamic> json) {
return migrations.firstWhereOrNull((migration) {
return migration.from == json["application_version"];
Expand Down
4 changes: 2 additions & 2 deletions lib/pages/import/import.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import 'package:go_router/go_router.dart';
import 'package:inference/pages/import/huggingface.dart';
import 'package:inference/pages/import/providers/import_provider.dart';
import 'package:inference/pages/import/widgets/import_geti_model_dialog.dart';
import 'package:inference/pages/import/widgets/import_model_dialog.dart';
import 'package:inference/project.dart';
import 'package:inference/providers/project_filter_provider.dart';
import 'package:inference/theme_fluent.dart';
Expand Down Expand Up @@ -67,7 +67,7 @@
PaneItemAction(
icon: const Icon(FluentIcons.project_collection),
title: const Text("Local disk"),
onTap: () => showImportGetiModelDialog(context,
onTap: () => showImportModelDialog(context,

Check warning on line 70 in lib/pages/import/import.dart

View check run for this annotation

Codecov / codecov/patch

lib/pages/import/import.dart#L70

Added line #L70 was not covered by tests
callback: (projects) {
if (projects != null && projects.isNotEmpty) {
if (projects.length == 1) {
Expand Down
Loading
Loading