Skip to content

Commit

Permalink
opt.: model pick (#13)
Browse files Browse the repository at this point in the history
- fix: app update
- opt.: blur overlay
  • Loading branch information
lollipopkit committed May 29, 2024
1 parent 841749f commit 9e5f1bb
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 39 deletions.
13 changes: 13 additions & 0 deletions lib/data/res/openai.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:gpt_box/data/store/all.dart';

abstract final class OpenAICfg {
static final nameNotifier = ValueNotifier(_cfg.name);
static final models = <String>[].vn;

static ChatConfig _cfg = () {
final selectedKey = Stores.config.selectedKey.fetch();
Expand All @@ -20,6 +21,18 @@ abstract final class OpenAICfg {
config.save();
Stores.config.selectedKey.put(config.id);
nameNotifier.value = config.name;

updateModels();
}

static void updateModels() {
try {
OpenAI.instance.model.list().then((value) {
models.value = value.map((e) => e.id).toList();
});
} catch (e) {
Loggers.app.warning('Failed to update models', e);
}
}

static void apply() {
Expand Down
1 change: 1 addition & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Future<void> _initAppComponents() async {
OpenAI.showLogs = !BuildMode.isRelease;
OpenAI.showResponsesLogs = !BuildMode.isRelease;
OpenAICfg.apply();
OpenAICfg.updateModels();

SyncService.sync(force: true);
}
5 changes: 3 additions & 2 deletions lib/view/page/home/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class _ChatPageState extends State<_ChatPage>
final chatItem = chatItems[idx];
final node = _chatItemRNMap.putIfAbsent(chatItem.id, () => RNode());
return Padding(
padding: const EdgeInsets.all(7),
padding: const EdgeInsets.all(2),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expand Down Expand Up @@ -157,7 +157,7 @@ class _ChatPageState extends State<_ChatPage>
.joinWith(UIs.height13),
);
},
),
).paddingSymmetric(horizontal: 7, vertical: 3),
),
UIs.height13,
],
Expand Down Expand Up @@ -214,6 +214,7 @@ class _ChatPageState extends State<_ChatPage>
color: UIs.primaryColor,
),
padding: const EdgeInsets.symmetric(horizontal: 7, vertical: 1),
margin: const EdgeInsets.symmetric(horizontal: 7),
child: Text(
chatItem.role.localized,
style: TextStyle(
Expand Down
63 changes: 29 additions & 34 deletions lib/view/page/setting.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:dart_openai/dart_openai.dart';
import 'package:fl_lib/fl_lib.dart';
import 'package:flutter/material.dart';
import 'package:gpt_box/data/model/chat/config.dart';
Expand Down Expand Up @@ -392,19 +391,24 @@ class _SettingPageState extends State<SettingPage> {
}

Widget _buildOpenAIModels(ChatConfig cfg) {
return ExpandTile(
leading: const Icon(Icons.model_training),
title: Text(l10n.model),
children: [
_buildOpenAIChatModel(cfg),
_buildOpenAIImgModel(cfg),
_buildOpenAISpeechModel(cfg),
_buildOpenAITranscribeModel(cfg),
],
return ValBuilder(
listenable: OpenAICfg.models,
builder: (models) {
return ExpandTile(
leading: const Icon(Icons.model_training),
title: Text(l10n.model),
children: [
_buildOpenAIChatModel(cfg, models),
_buildOpenAIImgModel(cfg, models),
_buildOpenAISpeechModel(cfg, models),
_buildOpenAITranscribeModel(cfg, models),
],
);
},
);
}

Widget _buildOpenAIChatModel(ChatConfig cfg) {
Widget _buildOpenAIChatModel(ChatConfig cfg, List<String> models) {
final val = cfg.model;
return ListTile(
leading: const Icon(Icons.chat),
Expand All @@ -420,13 +424,10 @@ class _SettingPageState extends State<SettingPage> {
);
return;
}
final models = await context.showLoadingDialog(
fn: () async => await OpenAI.instance.model.list(),
);
final modelStrs = models.map((e) => e.id).toList();
final modelStrs = List<String>.from(models);
modelStrs.removeWhere((element) => !element.startsWith('gpt'));
if (modelStrs.isEmpty) {
modelStrs.addAll(models.map((e) => e.id));
modelStrs.addAll(models);
}
final model = await context.showPickSingleDialog(
items: modelStrs,
Expand Down Expand Up @@ -465,7 +466,7 @@ class _SettingPageState extends State<SettingPage> {
);
}

Widget _buildOpenAIImgModel(ChatConfig cfg) {
Widget _buildOpenAIImgModel(ChatConfig cfg, List<String> models) {
final val = cfg.imgModel;
return ListTile(
leading: const Icon(Icons.photo),
Expand All @@ -481,13 +482,11 @@ class _SettingPageState extends State<SettingPage> {
);
return;
}
final models = await context.showLoadingDialog(
fn: () async => await OpenAI.instance.model.list(),
);
final modelStrs = models.map((e) => e.id).toList();

final modelStrs = List<String>.from(models);
modelStrs.removeWhere((element) => !element.startsWith('dall-e'));
if (modelStrs.isEmpty) {
modelStrs.addAll(models.map((e) => e.id));
modelStrs.addAll(models);
}
final model = await context.showPickSingleDialog(
items: modelStrs,
Expand Down Expand Up @@ -526,7 +525,7 @@ class _SettingPageState extends State<SettingPage> {
);
}

Widget _buildOpenAISpeechModel(ChatConfig cfg) {
Widget _buildOpenAISpeechModel(ChatConfig cfg, List<String> models) {
final val = cfg.speechModel;
return ListTile(
leading: const Icon(Icons.speaker),
Expand All @@ -542,13 +541,11 @@ class _SettingPageState extends State<SettingPage> {
);
return;
}
final models = await context.showLoadingDialog(
fn: () async => await OpenAI.instance.model.list(),
);
final modelStrs = models.map((e) => e.id).toList();

final modelStrs = List<String>.from(models);
modelStrs.removeWhere((element) => !element.startsWith('tts'));
if (modelStrs.isEmpty) {
modelStrs.addAll(models.map((e) => e.id));
modelStrs.addAll(models);
}
final model = await context.showPickSingleDialog(
items: modelStrs,
Expand Down Expand Up @@ -587,7 +584,7 @@ class _SettingPageState extends State<SettingPage> {
);
}

Widget _buildOpenAITranscribeModel(ChatConfig cfg) {
Widget _buildOpenAITranscribeModel(ChatConfig cfg, List<String> models) {
final val = cfg.transcribeModel;
return ListTile(
leading: const Icon(Icons.transcribe),
Expand All @@ -603,13 +600,11 @@ class _SettingPageState extends State<SettingPage> {
);
return;
}
final models = await context.showLoadingDialog(
fn: () async => await OpenAI.instance.model.list(),
);
final modelStrs = models.map((e) => e.id).toList();

final modelStrs = List<String>.from(models);
modelStrs.removeWhere((element) => !element.startsWith('whisper'));
if (modelStrs.isEmpty) {
modelStrs.addAll(models.map((e) => e.id));
modelStrs.addAll(models);
}
final model = await context.showPickSingleDialog(
items: modelStrs,
Expand Down
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,8 @@ packages:
dependency: "direct main"
description:
path: "."
ref: main
resolved-ref: "8b6e86918deba6fd3fdac971429edfaeb7076212"
ref: "72a52694b7c023c309475043627f599dd916f5ef"
resolved-ref: "72a52694b7c023c309475043627f599dd916f5ef"
url: "https://github.com/lollipopkit/fl_lib"
source: git
version: "0.0.1"
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies:
fl_lib:
git:
url: https://github.com/lollipopkit/fl_lib
ref: main
ref: 72a52694b7c023c309475043627f599dd916f5ef
# path: ../fl_lib

dev_dependencies:
Expand Down

0 comments on commit 9e5f1bb

Please sign in to comment.