-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.dart
More file actions
276 lines (239 loc) · 9.35 KB
/
Copy pathmain.dart
File metadata and controls
276 lines (239 loc) · 9.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:image/image.dart' as img;
import 'package:logging/logging.dart';
import 'package:share_plus/share_plus.dart';
import 'package:simple_frame_app/simple_frame_app.dart';
import 'package:simple_frame_app/text_utils.dart';
import 'package:frame_msg/tx/image_sprite_block.dart';
import 'package:frame_msg/tx/sprite.dart';
import 'package:frame_msg/tx/plain_text.dart';
import 'package:speech_to_text/speech_recognition_error.dart';
import 'package:speech_to_text/speech_recognition_result.dart';
import 'package:speech_to_text/speech_to_text.dart';
import 'pollinations.dart';
void main() => runApp(const MainApp());
final _log = Logger("MainApp");
class MainApp extends StatefulWidget {
const MainApp({super.key});
@override
MainAppState createState() => MainAppState();
}
/// SimpleFrameAppState mixin helps to manage the lifecycle of the Frame connection outside of this file
class MainAppState extends State<MainApp> with SimpleFrameAppState {
MainAppState() {
Logger.root.level = Level.INFO;
Logger.root.onRecord.listen((record) {
debugPrint('${record.level.name}: [${record.loggerName}] ${record.time}: ${record.message}');
});
}
// Speech to text members
final SpeechToText _speechToText = SpeechToText();
bool _speechEnabled = false;
String _partialResult = "N/A";
String _finalResult = "N/A";
String? _prevText;
// Image gen/display/sharing members
Image? _image;
Uint8List? _imageBytes;
static const _textStyle = TextStyle(fontSize: 30);
@override
void initState() {
super.initState();
// asynchronously kick off Speech-to-text initialization and Frame connection/startup
currentState = ApplicationState.initializing;
_initSpeech()
.then((dynamic) {tryScanAndConnectAndStart(andRun: false);});
}
@override
void dispose() async {
_speechToText.cancel();
super.dispose();
}
/// This has to happen only once per app, but microphone permission must be provided
Future<void> _initSpeech() async {
_speechEnabled = await _speechToText.initialize(onError: _onSpeechError);
if (!_speechEnabled) {
_finalResult = 'The user has denied the use of speech recognition. Microphone permission must be added manually in device settings.';
_log.severe(_finalResult);
currentState = ApplicationState.disconnected;
}
else {
_log.fine('Speech-to-text initialized');
// this will initialise before Frame is connected, so proceed to disconnected state
currentState = ApplicationState.disconnected;
}
if (mounted) setState(() {});
}
/// Manually stop the active speech recognition session, but timeouts will also stop the listening
Future<void> _stopListening() async {
await _speechToText.stop();
}
/// Timeouts invoke this function, but also other permanent errors
void _onSpeechError(SpeechRecognitionError error) {
if (error.errorMsg != 'error_speech_timeout') {
_log.severe(error.errorMsg);
currentState = ApplicationState.ready;
}
else {
currentState = ApplicationState.running;
}
if (mounted) setState(() {});
}
/// This application uses platform speech-to-text to listen to audio from the host mic, convert to text,
/// and send the text to the Frame.
/// An image generation request is also sent, and the resulting content is shown in Frame.
/// So the lifetime of this run() is 10s of seconds or so, due to image generation time and image transfer time
/// It has a running main loop on the Frame (frame_app.lua)
@override
Future<void> run() async {
currentState = ApplicationState.running;
if (mounted) setState(() {});
// listen for STT
await _speechToText.listen(
listenOptions: SpeechListenOptions(
cancelOnError: true, onDevice: true, listenMode: ListenMode.search
),
onResult: (SpeechRecognitionResult result) async {
if (currentState == ApplicationState.ready) {
// user has cancelled already, don't process result
return;
}
if (result.finalResult) {
// on a final result we generate the image
_finalResult = result.recognizedWords;
_partialResult = '';
_log.fine('Final result: $_finalResult');
_stopListening();
// send final query text to Frame line 1 (before we confirm the title)
if (_finalResult != _prevText) {
var text = TxPlainText(text: TextUtils.wrapText(_finalResult, 300, 4).join('\n'));
await frame!.sendMessage(0x0a, text.pack());
_prevText = _finalResult;
}
// first, download the image based on the prompt
String? error;
Uint8List? bytes;
(bytes, error) = await fetchImage(_finalResult);
if (bytes != null) {
try {
_imageBytes = bytes;
// Update the UI based on the original image
setState(() {
_image = Image.memory(_imageBytes!, gaplessPlayback: true, fit: BoxFit.cover);
});
// yield here a moment in order to show the first image first
await Future.delayed(const Duration(milliseconds: 10));
// creating the sprite this way will quantize colors and possibly scale the image
var sprite = TxSprite.fromImageBytes(imageBytes: _imageBytes!);
// Update the UI with the modified image
setState(() {
_image = Image.memory(img.encodePng(sprite.toImage()), gaplessPlayback: true, fit: BoxFit.cover);
});
// create the image sprite block header and its sprite lines
// based on the sprite
TxImageSpriteBlock isb = TxImageSpriteBlock(
image: sprite,
spriteLineHeight: 20,
progressiveRender: true);
// and send the block header then the sprite lines to Frame
await frame!.sendMessage(0x0d, isb.pack());
for (var sprite in isb.spriteLines) {
await frame!.sendMessage(0x0d, sprite.pack());
}
// final result is done
currentState = ApplicationState.ready;
if (mounted) setState(() {});
}
catch (e) {
_log.severe('Error processing image: $e');
}
}
else {
_log.fine('Error fetching image for "$_finalResult": "$error"');
}
}
else {
// partial result - just display in-progress text
_partialResult = result.recognizedWords;
if (mounted) setState((){});
_log.fine('Partial result: $_partialResult, ${result.alternates}');
if (_partialResult != _prevText) {
// send partial result to Frame line 1
var text = TxPlainText(text: TextUtils.wrapText(_partialResult, 300, 4).join('\n'));
await frame!.sendMessage(0x0a, text.pack());
_prevText = _partialResult;
}
}
},
);
}
/// The run() function will run for 5-25 seconds or so, but if the user
/// interrupts it, we can cancel the speech to text/image generation and return to ApplicationState.ready state.
@override
Future<void> cancel() async {
await _stopListening();
currentState = ApplicationState.ready;
if (mounted) setState(() {});
}
void _shareImage(Uint8List jpegBytes, String prompt) async {
try {
// Share the image bytes as a JPEG file
await Share.shareXFiles(
[XFile.fromData(jpegBytes, mimeType: 'image/jpeg', name: 'image.jpg')],
text: prompt,
);
}
catch (e) {
_log.severe('Error preparing image for sharing: $e');
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Frame - Pollinations.ai',
theme: ThemeData.dark(),
home: Scaffold(
appBar: AppBar(
title: const Text('Frame - Pollinations.ai'),
actions: [getBatteryWidget()]
),
body: Center(
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Align(alignment: Alignment.centerLeft,
child: Text('Query: ${_partialResult == '' ? _finalResult : _partialResult}', style: _textStyle)
),
const Divider(),
SizedBox(
width: 640,
height: 400,
child: Row(
children: [
Expanded(
flex: 3,
child: Container(
alignment: Alignment.topCenter,
color: Colors.black,
child: (_image != null) ? GestureDetector(
onTap: () => _shareImage(_imageBytes!, _finalResult),
child: _image!) : null
)
),
],
),
),
],
),
),
),
floatingActionButton: getFloatingActionButtonWidget(const Icon(Icons.search), const Icon(Icons.cancel)),
persistentFooterButtons: getFooterButtonsWidget(),
),
);
}
}