Skip to content

Commit

Permalink
reorganise
Browse files Browse the repository at this point in the history
  • Loading branch information
danemadsen committed Jun 26, 2024
1 parent 5fb36ee commit d1f80ab
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 73 deletions.
76 changes: 69 additions & 7 deletions lib/classes/providers/huggingface_selection.dart
Original file line number Diff line number Diff line change
@@ -1,39 +1,101 @@
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:maid/classes/huggingface_model.dart';
import 'package:maid/classes/static/logger.dart';
import 'package:path_provider/path_provider.dart';
import 'package:provider/provider.dart';

class HuggingfaceSelection extends ChangeNotifier {
HuggingfaceModel? _model;
String? _tag;
String? _filePath;
double _progress = 0;

HuggingfaceModel? get model => _model;

String? get tag => _tag;

Future<String> get filePath async {
String? get filePath => _filePath;

double get progress => _progress;

Future<String> get filePathFuture async {
if (_model == null || _tag == null) {
Logger.log("Model or tag not selected");
return "";
}

final model = _model!;
final family = model.family;
final series = model.series;

final dir = await getApplicationDocumentsDirectory();
final path = "${dir.path}/$family/$series/$_tag.gguf";

return path;
}

Future<bool> get alreadyExists async {
return File(await filePathFuture).exists();
}

void download() async {
if (_model == null || _tag == null) {
return Future.error("Model or tag not selected");
Logger.log("Model or tag not selected");
return;
}

final family = _model!.family;
final series = _model!.series;
final filePath = await filePathFuture;

if (File(filePath).existsSync()) {
Logger.log("File already exists: $filePath");
_filePath = filePath;
notifyListeners();
return;
}

final model = _model!;
final repo = model.repo;
final branch = model.branch;
final tag = model.tags[_tag!]!;

try {
var dir = await getApplicationDocumentsDirectory();

return "${dir.path}/$family/$series/$_tag.gguf";
_progress = 0;

await Dio().download(
"https://huggingface.co/$repo/resolve/$branch/$tag?download=true",
filePath,
onReceiveProgress: (received, total) {
if (total != -1) {
_progress = received / total;
notifyListeners();
}
},
);

Logger.log("Huggingface file downloaded to: $filePath");

_filePath = filePath;
return;
} catch (e) {
return Future.error("Download failed: $e");
Logger.log("Download failed: $e");
return;
}
}

set model(HuggingfaceModel? model) {
_filePath = null;
_progress = 0;
_model = model;
notifyListeners();
}

set tag(String? tag) {
_filePath = null;
_progress = 0;
_tag = tag;
notifyListeners();
}
Expand Down
4 changes: 3 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:maid/classes/providers/app_data.dart';
import 'package:maid/classes/providers/app_preferences.dart';
import 'package:maid/classes/providers/huggingface_selection.dart';
import 'package:maid/classes/providers/user.dart';
import 'package:maid/ui/desktop/app.dart';
import 'package:maid/ui/mobile/app.dart';
Expand Down Expand Up @@ -40,7 +41,8 @@ class MaidApp extends StatelessWidget {
providers: [
ChangeNotifierProvider(create: (context) => appPreferences),
ChangeNotifierProvider(create: (context) => appData),
ChangeNotifierProvider(create: (context) => user)
ChangeNotifierProvider(create: (context) => user),
ChangeNotifierProvider(create: (context) => HuggingfaceSelection())
],
child: Consumer<AppPreferences>(
builder: (context, appPreferences, child) {
Expand Down
55 changes: 0 additions & 55 deletions lib/ui/desktop/buttons/huggingface_download_button.dart

This file was deleted.

16 changes: 6 additions & 10 deletions lib/ui/desktop/dialogs/huggingface_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
import 'package:flutter/material.dart';
import 'package:maid/classes/providers/huggingface_selection.dart';
import 'package:maid/ui/desktop/buttons/huggingface_download_button.dart';
import 'package:maid/ui/desktop/dropdowns/huggingface_model_dropdown.dart';
import 'package:provider/provider.dart';

class HuggingfaceDialog extends StatelessWidget {
const HuggingfaceDialog({super.key});

@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (context) => HuggingfaceSelection(),
child: buildDialog(context),
);
}

Widget buildDialog(BuildContext context) {
return AlertDialog(
title: const Text(
'Select HuggingFace Model',
textAlign: TextAlign.center
),
content: const HuggingfaceModelDropdown(),
actions: [
const HuggingfaceDownloadButton(),
FilledButton(
onPressed: HuggingfaceSelection.of(context).download,
child: const Text(
"Download"
),
),
FilledButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text(
Expand Down

0 comments on commit d1f80ab

Please sign in to comment.