From a29b170b1323229836e1eb06f1ab2e9676311b7f Mon Sep 17 00:00:00 2001 From: Annie Date: Mon, 12 Aug 2024 15:46:56 +0800 Subject: [PATCH] chore: remove stability ai (#5927) * chore: remove stability ai * chore: remove stabilityAI widgets --------- Co-authored-by: Lucas.Xu --- .../image/image_placeholder.dart | 14 +- .../multi_image_menu.dart | 7 +- .../multi_image_placeholder.dart | 31 ++--- .../upload_image_menu/upload_image_menu.dart | 50 +------- .../widgets/open_ai_image_widget.dart | 105 --------------- .../widgets/stability_ai_image_widget.dart | 120 ------------------ .../stability_ai/stability_ai_client.dart | 95 -------------- .../stability_ai/stability_ai_error.dart | 10 -- .../lib/startup/deps_resolver.dart | 19 --- frontend/resources/translations/en.json | 4 +- 10 files changed, 29 insertions(+), 426 deletions(-) delete mode 100644 frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/upload_image_menu/widgets/open_ai_image_widget.dart delete mode 100644 frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/upload_image_menu/widgets/stability_ai_image_widget.dart delete mode 100644 frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/stability_ai/stability_ai_client.dart delete mode 100644 frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/stability_ai/stability_ai_error.dart diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/image_placeholder.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/image_placeholder.dart index 6e3941a584..08ad0a9ac9 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/image_placeholder.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/image_placeholder.dart @@ -102,7 +102,6 @@ class ImagePlaceholderState extends State { UploadImageType.local, UploadImageType.url, UploadImageType.unsplash, - UploadImageType.stabilityAI, ], onSelectedLocalImages: (paths) { controller.close(); @@ -188,12 +187,13 @@ class ImagePlaceholderState extends State { return [ Flexible( child: FlowyText( - PlatformExtension.isDesktop - ? isDraggingFiles - ? LocaleKeys.document_plugins_image_dropImageToInsert.tr() - : LocaleKeys.document_plugins_image_addAnImageDesktop.tr() - : LocaleKeys.document_plugins_image_addAnImageMobile.tr(), - color: Theme.of(context).hintColor,), + PlatformExtension.isDesktop + ? isDraggingFiles + ? LocaleKeys.document_plugins_image_dropImageToInsert.tr() + : LocaleKeys.document_plugins_image_addAnImageDesktop.tr() + : LocaleKeys.document_plugins_image_addAnImageMobile.tr(), + color: Theme.of(context).hintColor, + ), ), ]; } diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/multi_image_block_component/multi_image_menu.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/multi_image_block_component/multi_image_menu.dart index d419446328..a8534b6fd3 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/multi_image_block_component/multi_image_menu.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/multi_image_block_component/multi_image_menu.dart @@ -1,8 +1,5 @@ import 'dart:io'; -import 'package:flutter/material.dart'; -import 'package:flutter/services.dart'; - import 'package:appflowy/generated/flowy_svgs.g.dart'; import 'package:appflowy/generated/locale_keys.g.dart'; import 'package:appflowy/plugins/document/application/document_bloc.dart'; @@ -25,6 +22,8 @@ import 'package:flowy_infra/size.dart'; import 'package:flowy_infra/theme_extension.dart'; import 'package:flowy_infra/uuid.dart'; import 'package:flowy_infra_ui/flowy_infra_ui.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:http/http.dart'; import 'package:path/path.dart' as p; import 'package:provider/provider.dart'; @@ -129,7 +128,7 @@ class _MultiImageMenuState extends State { UploadImageType.local, UploadImageType.url, UploadImageType.unsplash, - UploadImageType.stabilityAI, + ], onSelectedLocalImages: insertLocalImages, onSelectedAIImage: insertAIImage, diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/multi_image_block_component/multi_image_placeholder.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/multi_image_block_component/multi_image_placeholder.dart index ba4c779928..4e1cfd1fcb 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/multi_image_block_component/multi_image_placeholder.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/multi_image_block_component/multi_image_placeholder.dart @@ -1,9 +1,7 @@ import 'dart:io'; -import 'package:flutter/material.dart'; - -import 'package:appflowy/generated/locale_keys.g.dart'; import 'package:appflowy/generated/flowy_svgs.g.dart'; +import 'package:appflowy/generated/locale_keys.g.dart'; import 'package:appflowy/mobile/presentation/bottom_sheet/show_mobile_bottom_sheet.dart'; import 'package:appflowy/plugins/document/application/document_bloc.dart'; import 'package:appflowy/plugins/document/application/document_service.dart'; @@ -24,6 +22,7 @@ import 'package:easy_localization/easy_localization.dart'; import 'package:flowy_infra/uuid.dart'; import 'package:flowy_infra_ui/flowy_infra_ui.dart'; import 'package:flowy_infra_ui/style_widget/hover.dart'; +import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:http/http.dart'; import 'package:path/path.dart' as p; @@ -67,19 +66,22 @@ class MultiImagePlaceholderState extends State { padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 12), child: Row( children: [ - FlowySvg(FlowySvgs.slash_menu_icon_photo_gallery_s, - color: Theme.of(context).hintColor, - size: const Size.square(24),), + FlowySvg( + FlowySvgs.slash_menu_icon_photo_gallery_s, + color: Theme.of(context).hintColor, + size: const Size.square(24), + ), const HSpace(10), FlowyText( - PlatformExtension.isDesktop - ? isDraggingFiles - ? LocaleKeys.document_plugins_image_dropImageToInsert - .tr() - : LocaleKeys.document_plugins_image_addAnImageDesktop - .tr() - : LocaleKeys.document_plugins_image_addAnImageMobile.tr(), - color: Theme.of(context).hintColor,), + PlatformExtension.isDesktop + ? isDraggingFiles + ? LocaleKeys.document_plugins_image_dropImageToInsert + .tr() + : LocaleKeys.document_plugins_image_addAnImageDesktop + .tr() + : LocaleKeys.document_plugins_image_addAnImageMobile.tr(), + color: Theme.of(context).hintColor, + ), ], ), ), @@ -104,7 +106,6 @@ class MultiImagePlaceholderState extends State { UploadImageType.local, UploadImageType.url, UploadImageType.unsplash, - UploadImageType.stabilityAI, ], onSelectedLocalImages: (paths) { controller.close(); diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/upload_image_menu/upload_image_menu.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/upload_image_menu/upload_image_menu.dart index cdda330115..8091452288 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/upload_image_menu/upload_image_menu.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/upload_image_menu/upload_image_menu.dart @@ -1,17 +1,15 @@ -import 'package:flutter/material.dart'; - import 'package:appflowy/generated/locale_keys.g.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/header/cover_editor.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/image/unsplash_image_widget.dart'; -import 'package:appflowy/plugins/document/presentation/editor_plugins/image/upload_image_menu/widgets/stability_ai_image_widget.dart'; +//import 'package:appflowy/plugins/document/presentation/editor_plugins/image/upload_image_menu/widgets/stability_ai_image_widget.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/image/upload_image_menu/widgets/upload_image_file_widget.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart'; -import 'package:appflowy/user/application/user_service.dart'; import 'package:appflowy_editor/appflowy_editor.dart' hide ColorOption; import 'package:easy_localization/easy_localization.dart'; import 'package:flowy_infra/theme_extension.dart'; import 'package:flowy_infra_ui/flowy_infra_ui.dart'; import 'package:flowy_infra_ui/style_widget/hover.dart'; +import 'package:flutter/material.dart'; import 'widgets/embed_image_url_widget.dart'; @@ -19,8 +17,6 @@ enum UploadImageType { local, url, unsplash, - stabilityAI, - // openAI, color; String get description { @@ -31,10 +27,6 @@ enum UploadImageType { return LocaleKeys.document_imageBlock_embedLink_label.tr(); case UploadImageType.unsplash: return LocaleKeys.document_imageBlock_unsplash_label.tr(); - // case UploadImageType.openAI: - // return LocaleKeys.document_imageBlock_ai_label.tr(); - case UploadImageType.stabilityAI: - return LocaleKeys.document_imageBlock_stability_ai_label.tr(); case UploadImageType.color: return LocaleKeys.document_plugins_cover_colors.tr(); } @@ -68,33 +60,12 @@ class UploadImageMenu extends StatefulWidget { class _UploadImageMenuState extends State { late final List values; int currentTabIndex = 0; - bool supportOpenAI = false; - bool supportStabilityAI = false; @override void initState() { super.initState(); values = widget.supportTypes; - UserBackendService.getCurrentUserProfile().then( - (value) { - final supportOpenAI = value.fold( - (s) => s.openaiKey.isNotEmpty, - (e) => false, - ); - final supportStabilityAI = value.fold( - (s) => s.stabilityAiKey.isNotEmpty, - (e) => false, - ); - if (supportOpenAI != this.supportOpenAI || - supportStabilityAI != this.supportStabilityAI) { - setState(() { - this.supportOpenAI = supportOpenAI; - this.supportStabilityAI = supportStabilityAI; - }); - } - }, - ); } @override @@ -196,23 +167,6 @@ class _UploadImageMenuState extends State { ), ), ); - case UploadImageType.stabilityAI: - return supportStabilityAI - ? Expanded( - child: Container( - padding: const EdgeInsets.all(8.0), - child: StabilityAIImageWidget( - onSelectImage: (url) => widget.onSelectedLocalImages([url]), - ), - ), - ) - : Padding( - padding: const EdgeInsets.all(8.0), - child: FlowyText( - LocaleKeys.document_imageBlock_pleaseInputYourStabilityAIKey - .tr(), - ), - ); case UploadImageType.color: final theme = Theme.of(context); final padding = PlatformExtension.isMobile diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/upload_image_menu/widgets/open_ai_image_widget.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/upload_image_menu/widgets/open_ai_image_widget.dart deleted file mode 100644 index 9c48cf6a1b..0000000000 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/upload_image_menu/widgets/open_ai_image_widget.dart +++ /dev/null @@ -1,105 +0,0 @@ -import 'dart:async'; - -import 'package:flutter/material.dart'; - -import 'package:appflowy/generated/locale_keys.g.dart'; -import 'package:appflowy/plugins/document/presentation/editor_plugins/openai/service/ai_client.dart'; -import 'package:appflowy/plugins/document/presentation/editor_plugins/openai/service/error.dart'; -import 'package:appflowy/startup/startup.dart'; -import 'package:appflowy_result/appflowy_result.dart'; -import 'package:easy_localization/easy_localization.dart'; -import 'package:flowy_infra_ui/flowy_infra_ui.dart'; - -class OpenAIImageWidget extends StatefulWidget { - const OpenAIImageWidget({ - super.key, - required this.onSelectNetworkImage, - }); - - final void Function(String url) onSelectNetworkImage; - - @override - State createState() => _OpenAIImageWidgetState(); -} - -class _OpenAIImageWidgetState extends State { - Future, AIError>>? future; - String query = ''; - - @override - Widget build(BuildContext context) { - return Column( - mainAxisSize: MainAxisSize.min, - children: [ - Row( - mainAxisSize: MainAxisSize.min, - children: [ - Expanded( - child: FlowyTextField( - hintText: LocaleKeys.document_imageBlock_ai_placeholder.tr(), - onChanged: (value) => query = value, - onEditingComplete: _search, - ), - ), - const HSpace(4.0), - FlowyButton( - useIntrinsicWidth: true, - text: FlowyText( - LocaleKeys.search_label.tr(), - ), - onTap: _search, - ), - ], - ), - const VSpace(12.0), - if (future != null) - Expanded( - child: FutureBuilder( - future: future, - builder: (context, value) { - final data = value.data; - if (!value.hasData || - value.connectionState != ConnectionState.done || - data == null) { - return const CircularProgressIndicator.adaptive(); - } - return data.fold( - (s) => GridView.count( - crossAxisCount: 3, - mainAxisSpacing: 16.0, - crossAxisSpacing: 10.0, - childAspectRatio: 4 / 3, - children: s - .map( - (e) => GestureDetector( - onTap: () => widget.onSelectNetworkImage(e), - child: Image.network(e), - ), - ) - .toList(), - ), - (e) => Center( - child: FlowyText( - e.message, - maxLines: 3, - textAlign: TextAlign.center, - ), - ), - ); - }, - ), - ), - ], - ); - } - - void _search() async { - final openAI = await getIt.getAsync(); - setState(() { - future = openAI.generateImage( - prompt: query, - n: 6, - ); - }); - } -} diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/upload_image_menu/widgets/stability_ai_image_widget.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/upload_image_menu/widgets/stability_ai_image_widget.dart deleted file mode 100644 index 0d5d986d10..0000000000 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/upload_image_menu/widgets/stability_ai_image_widget.dart +++ /dev/null @@ -1,120 +0,0 @@ -import 'dart:async'; -import 'dart:convert'; -import 'dart:io'; - -import 'package:appflowy/generated/locale_keys.g.dart'; -import 'package:appflowy/plugins/document/presentation/editor_plugins/stability_ai/stability_ai_client.dart'; -import 'package:appflowy/plugins/document/presentation/editor_plugins/stability_ai/stability_ai_error.dart'; -import 'package:appflowy/startup/startup.dart'; -import 'package:appflowy_result/appflowy_result.dart'; -import 'package:easy_localization/easy_localization.dart'; -import 'package:flowy_infra/uuid.dart'; -import 'package:flowy_infra_ui/flowy_infra_ui.dart'; -import 'package:flutter/material.dart'; -import 'package:path/path.dart' as p; -import 'package:path_provider/path_provider.dart'; - -class StabilityAIImageWidget extends StatefulWidget { - const StabilityAIImageWidget({ - super.key, - required this.onSelectImage, - }); - - final void Function(String url) onSelectImage; - - @override - State createState() => _StabilityAIImageWidgetState(); -} - -class _StabilityAIImageWidgetState extends State { - Future, StabilityAIRequestError>>? future; - String query = ''; - - @override - Widget build(BuildContext context) { - return Column( - mainAxisSize: MainAxisSize.min, - children: [ - Row( - mainAxisSize: MainAxisSize.min, - children: [ - Expanded( - child: FlowyTextField( - hintText: LocaleKeys - .document_imageBlock_stability_ai_placeholder - .tr(), - onChanged: (value) => query = value, - onEditingComplete: _search, - ), - ), - const HSpace(4.0), - FlowyButton( - useIntrinsicWidth: true, - text: FlowyText( - LocaleKeys.search_label.tr(), - ), - onTap: _search, - ), - ], - ), - const VSpace(12.0), - if (future != null) - Expanded( - child: FutureBuilder( - future: future, - builder: (context, value) { - final data = value.data; - if (!value.hasData || - value.connectionState != ConnectionState.done || - data == null) { - return const CircularProgressIndicator.adaptive(); - } - return data.fold( - (s) => GridView.count( - crossAxisCount: 3, - mainAxisSpacing: 16.0, - crossAxisSpacing: 10.0, - childAspectRatio: 4 / 3, - children: s.map( - (e) { - final base64Image = base64Decode(e); - return GestureDetector( - onTap: () async { - final tempDirectory = await getTemporaryDirectory(); - final path = p.join( - tempDirectory.path, - '${uuid()}.png', - ); - File(path).writeAsBytesSync(base64Image); - widget.onSelectImage(path); - }, - child: Image.memory(base64Image), - ); - }, - ).toList(), - ), - (e) => Center( - child: FlowyText( - e.message, - maxLines: 3, - textAlign: TextAlign.center, - ), - ), - ); - }, - ), - ), - ], - ); - } - - void _search() async { - final stabilityAI = await getIt.getAsync(); - setState(() { - future = stabilityAI.generateImage( - prompt: query, - n: 6, - ); - }); - } -} diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/stability_ai/stability_ai_client.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/stability_ai/stability_ai_client.dart deleted file mode 100644 index c3cbd11c97..0000000000 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/stability_ai/stability_ai_client.dart +++ /dev/null @@ -1,95 +0,0 @@ -import 'dart:async'; -import 'dart:convert'; - -import 'package:appflowy/plugins/document/presentation/editor_plugins/stability_ai/stability_ai_error.dart'; -import 'package:appflowy_result/appflowy_result.dart'; -import 'package:http/http.dart' as http; - -enum StabilityAIRequestType { - imageGenerations; - - Uri get uri { - switch (this) { - case StabilityAIRequestType.imageGenerations: - return Uri.parse( - 'https://api.stability.ai/v1/generation/stable-diffusion-xl-1024-v1-0/text-to-image', - ); - } - } -} - -abstract class StabilityAIRepository { - /// Generate image from Stability AI - /// - /// [prompt] is the prompt text - /// [n] is the number of images to generate - /// - /// the return value is a list of base64 encoded images - Future, StabilityAIRequestError>> generateImage({ - required String prompt, - int n = 1, - }); -} - -class HttpStabilityAIRepository implements StabilityAIRepository { - const HttpStabilityAIRepository({ - required this.client, - required this.apiKey, - }); - - final http.Client client; - final String apiKey; - - Map get headers => { - 'Authorization': 'Bearer $apiKey', - 'Content-Type': 'application/json', - }; - - @override - Future, StabilityAIRequestError>> generateImage({ - required String prompt, - int n = 1, - }) async { - final parameters = { - 'text_prompts': [ - { - 'text': prompt, - } - ], - 'samples': n, - }; - - try { - final response = await client.post( - StabilityAIRequestType.imageGenerations.uri, - headers: headers, - body: json.encode(parameters), - ); - - final data = json.decode( - utf8.decode(response.bodyBytes), - ); - if (response.statusCode == 200) { - final artifacts = data['artifacts'] as List; - final base64Images = artifacts - .map( - (e) => e['base64'].toString(), - ) - .toList(); - return FlowyResult.success(base64Images); - } else { - return FlowyResult.failure( - StabilityAIRequestError( - data['message'].toString(), - ), - ); - } - } catch (error) { - return FlowyResult.failure( - StabilityAIRequestError( - error.toString(), - ), - ); - } - } -} diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/stability_ai/stability_ai_error.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/stability_ai/stability_ai_error.dart deleted file mode 100644 index c699237762..0000000000 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/stability_ai/stability_ai_error.dart +++ /dev/null @@ -1,10 +0,0 @@ -class StabilityAIRequestError { - StabilityAIRequestError(this.message); - - final String message; - - @override - String toString() { - return 'StabilityAIRequestError{message: $message}'; - } -} diff --git a/frontend/appflowy_flutter/lib/startup/deps_resolver.dart b/frontend/appflowy_flutter/lib/startup/deps_resolver.dart index 3532bc8420..d19e0b3f7a 100644 --- a/frontend/appflowy_flutter/lib/startup/deps_resolver.dart +++ b/frontend/appflowy_flutter/lib/startup/deps_resolver.dart @@ -4,7 +4,6 @@ import 'package:appflowy/env/cloud_env.dart'; import 'package:appflowy/plugins/document/application/prelude.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_paste/clipboard_service.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/openai/service/ai_client.dart'; -import 'package:appflowy/plugins/document/presentation/editor_plugins/stability_ai/stability_ai_client.dart'; import 'package:appflowy/plugins/trash/application/prelude.dart'; import 'package:appflowy/shared/appflowy_cache_manager.dart'; import 'package:appflowy/shared/custom_image_cache_manager.dart'; @@ -43,7 +42,6 @@ import 'package:flowy_infra/file_picker/file_picker_impl.dart'; import 'package:flowy_infra/file_picker/file_picker_service.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:get_it/get_it.dart'; -import 'package:http/http.dart' as http; class DependencyResolver { static Future resolve( @@ -100,23 +98,6 @@ void _resolveCommonService( }, ); - getIt.registerFactoryAsync( - () async { - final result = await UserBackendService.getCurrentUserProfile(); - return result.fold( - (s) { - return HttpStabilityAIRepository( - client: http.Client(), - apiKey: s.stabilityAiKey, - ); - }, - (e) { - throw Exception('Failed to get user profile: ${e.msg}'); - }, - ); - }, - ); - getIt.registerFactory( () => ClipboardService(), ); diff --git a/frontend/resources/translations/en.json b/frontend/resources/translations/en.json index 5994543413..605ee9bc61 100644 --- a/frontend/resources/translations/en.json +++ b/frontend/resources/translations/en.json @@ -1159,7 +1159,6 @@ "tooltipSelectIcon": "Select icon", "selectAnIcon": "Select an icon", "pleaseInputYourOpenAIKey": "please input your AI key", - "pleaseInputYourStabilityAIKey": "please input your Stability AI key", "clickToLogout": "Click to logout the current user" }, "mobile": { @@ -1713,7 +1712,6 @@ }, "searchForAnImage": "Search for an image", "pleaseInputYourOpenAIKey": "please input your AI key in Settings page", - "pleaseInputYourStabilityAIKey": "please input your Stability AI key in Settings page", "saveImageToGallery": "Save image", "failedToAddImageToGallery": "Failed to add image to gallery", "successToAddImageToGallery": "Image added to gallery successfully", @@ -2411,4 +2409,4 @@ "commentAddedSuccessfully": "Comment added successfully.", "commentAddedSuccessTip": "You've just added or replied to a comment. Would you like to jump to the top to see the latest comments?" } -} \ No newline at end of file +}