integrated code from one-ai-mobile repo. working on GUI integration now.
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import 'package:app/util/speech_to_text.dart';
|
||||
|
||||
class Server {
|
||||
// TODO: One-time process to establish server connection is required for easy setup/maintenance.
|
||||
final url = "http://10.0.2.2:11434";
|
||||
final headers = {'Content-Type': 'application/json'};
|
||||
|
||||
final SpeechToText stt = SpeechToText();
|
||||
late bool _speechEnabled;
|
||||
|
||||
String prompt = "";
|
||||
String response = "";
|
||||
|
||||
Server() {
|
||||
// Speech-To-Text engine activation.
|
||||
_speechEnabled = stt.isSpeechEnabled();
|
||||
|
||||
// TODO: Initialize all different components of STT, TTS, LLM, etc.
|
||||
// TODO: Handshake w/ server to validate identity
|
||||
}
|
||||
|
||||
// Using native on-device Speech-To-Text capability, get the server response when ready.
|
||||
Future<String> sttGetResponseWhenReady() async {
|
||||
// TODO: In case on-device STT is unavailable, use server-side STT service.
|
||||
if (!_speechEnabled) "Unable to process Speech-To-Text on-device.";
|
||||
|
||||
setPrompt(stt.getTextWhenReady() as String);
|
||||
constructPrompt();
|
||||
httpSendRequest();
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
// Arbitrary prompt, used for standard text interaction rather than STT.
|
||||
void setPrompt(String p) => prompt = p;
|
||||
|
||||
// Get the last response or request a response from the server.
|
||||
String getResponse() {
|
||||
if(response.isEmpty) {
|
||||
constructPrompt();
|
||||
httpSendRequest();
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
// Get the latest prompt.
|
||||
String getPrompt() => prompt;
|
||||
|
||||
// Formats the prompt in JSON.
|
||||
String constructPrompt() {
|
||||
// Construct the JSON payload
|
||||
final Map<String, dynamic> data = {
|
||||
"model":
|
||||
"llama3", // TODO: Allow for different models to be dynamically selected.
|
||||
"prompt": prompt,
|
||||
"stream": false // TODO: Allow for toggleable states between stream
|
||||
// TODO: Allow for additional flags, ie. continuous conversation, images, etc.
|
||||
};
|
||||
|
||||
// Encode the JSON payload
|
||||
final String body = json.encode(data);
|
||||
|
||||
return body;
|
||||
}
|
||||
|
||||
// Send the HTTP request to the server.
|
||||
void httpSendRequest() async {
|
||||
String json = constructPrompt();
|
||||
|
||||
// Send the HTTP POST request
|
||||
final serverResponse = await http.post(
|
||||
Uri.parse('$url/api/generate'),
|
||||
headers: headers,
|
||||
body: json,
|
||||
);
|
||||
|
||||
if (serverResponse.statusCode == 200) {
|
||||
Map<String, dynamic> re = jsonDecode(serverResponse.body);
|
||||
response = re["response"]!;
|
||||
} else {
|
||||
response = ('Error: ${serverResponse.statusCode}');
|
||||
}
|
||||
}
|
||||
}
|
||||
//
|
||||
//
|
||||
// // GUI components
|
||||
// child: Column(
|
||||
// mainAxisAlignment: MainAxisAlignment.center,
|
||||
// children: <Widget>[
|
||||
// Container(
|
||||
// padding: const EdgeInsets.all(16),
|
||||
// child: const Text(
|
||||
// 'Recognized words:',
|
||||
// style: TextStyle(fontSize: 20.0),
|
||||
// ),
|
||||
// ),
|
||||
// Expanded(
|
||||
// child: Container(
|
||||
// padding: const EdgeInsets.all(16),
|
||||
// child: Text(
|
||||
// // If listening is active show the recognized words
|
||||
// _speechToText.isListening
|
||||
// ? prompt
|
||||
// // If listening isn't active but could be tell the user
|
||||
// // how to start it, otherwise indicate that speech
|
||||
// // recognition is not yet ready or not supported on
|
||||
// // the target device
|
||||
// : _speechEnabled
|
||||
// ? responseFromAI
|
||||
// : 'Speech disabled',
|
||||
// ),
|
||||
// ),
|
||||
// ),
|
||||
// ],
|
||||
// ),
|
||||
// ),
|
||||
// floatingActionButton: FloatingActionButton(
|
||||
// onPressed:
|
||||
// // If not yet listening for speech start, otherwise stop
|
||||
// //sendPrompt,
|
||||
// _speechToText.isNotListening ? _startListening : sendPrompt,
|
||||
// tooltip: 'Listen',
|
||||
// child: Icon(_speechToText.isNotListening ? Icons.mic_off : Icons.mic),
|
||||
// ),
|
||||
@@ -0,0 +1,43 @@
|
||||
import 'package:speech_to_text/speech_recognition_result.dart';
|
||||
import 'package:speech_to_text/speech_to_text.dart' as stt;
|
||||
|
||||
/// Handles standard Speech-To-Text using on-device processing.
|
||||
class SpeechToText {
|
||||
final stt.SpeechToText _speechToText = stt.SpeechToText();
|
||||
bool _speechEnabled = false;
|
||||
String text = "";
|
||||
|
||||
// Check if speech recognition is enabled.
|
||||
bool isSpeechEnabled() => _speechEnabled;
|
||||
|
||||
Future<String> getTextWhenReady() async {
|
||||
await _speechToText.stop();
|
||||
return text;
|
||||
}
|
||||
|
||||
// Retrieve the text.
|
||||
String getText() => text;
|
||||
|
||||
// Reset either after saving the text or prior to using STT again.
|
||||
void reset() => text = "";
|
||||
|
||||
/// Start a speech recognition session
|
||||
void _startListening() async => await _speechToText.listen(onResult: _onSpeechResult);
|
||||
|
||||
/// Manually stop the active speech recognition session
|
||||
/// Note that there are also timeouts that each platform enforces
|
||||
/// and the SpeechToText plugin supports setting timeouts on the
|
||||
/// listen method.
|
||||
void _stopListening() async => await _speechToText.stop();
|
||||
|
||||
SpeechToText() {
|
||||
_initSpeech();
|
||||
}
|
||||
|
||||
/// This has to happen only once per app
|
||||
void _initSpeech() async => _speechEnabled = await _speechToText.initialize();
|
||||
|
||||
/// This is the callback that the SpeechToText plugin calls when
|
||||
/// the platform returns recognized words.
|
||||
void _onSpeechResult(SpeechRecognitionResult result) => text = result.recognizedWords;
|
||||
}
|
||||
Reference in New Issue
Block a user