alphacep / vosk-flutter

Apache License 2.0
57 stars 43 forks source link

How to add multiple models to the app and use it at once so that it improves accuracy and helpful for wide range of users #26

Open kuberanb opened 9 months ago

kuberanb commented 9 months ago

import 'dart:async'; import 'dart:convert'; import 'dart:developer'; import 'dart:io'; import 'dart:typed_data'; import 'package:edolms/modules/screens/student/ai/conversation/chat/functions/model/vosk_result_model.dart'; import 'package:flutter/services.dart'; import 'package:vosk_flutter/vosk_flutter.dart';

class VoskTranscriber { late VoskFlutterPlugin _vosk; Recognizer? _recognizer; SpeechService? _speechService; Model? _model; String? _error; late StreamSubscription? _resultSubscription;

VoskTranscriber() { initializeVosk(); }

Future initializeVosk() async { const _sampleRate = 16000;

print("_initializeVosk  called");
_vosk = VoskFlutterPlugin.instance();
final modelPath = await ModelLoader()
    .loadFromAssets('assets/models/vosk-model-small-en-in-0.4.zip')
    .then(
        (modelPath) => _vosk.createModel(modelPath)) // create model object
    .then((model) => _model = model)
    .then((_) => _vosk.createRecognizer(
        model: _model!, sampleRate: _sampleRate)) // create recognizer
    .then((value) => _recognizer = value)
    .then((recognizer) {
  if (Platform.isAndroid) {
    _vosk
        .initSpeechService(_recognizer!) // init speech service
        .then((speechService) => _speechService = speechService)
        .catchError((e) => _error = e.toString());
    print("sampleRate : ${_sampleRate}");
  }
}).catchError((e) async {
  _error = e.toString();
  print("catchError : $e");
  return null;
});

// print("modelPath :$modelPath");
// _recognizer = await _vosk.createRecognizer(
//     model: Model(modelPath, const MethodChannel("")), sampleRate: 16000);

}

Future transcribeAudio(Uint8List audioBytes) async { print("transcribeAudio function called"); print("audioBytes length : ${audioBytes.length}"); List results = []; int chunkSize = 348135; int pos = 0;

while (pos + chunkSize < audioBytes.length) {
  final resultReady = await _recognizer!.acceptWaveformBytes(
    Uint8List.fromList(audioBytes.sublist(pos, pos + chunkSize)),
  );
  // print("resultReady : ${resultReady}");
  pos += chunkSize;

  if (resultReady) {
    String result = await _recognizer!.getResult();
    dynamic decodedData = jsonDecode(result);

    if (decodedData["text"].isEmpty ||
        decodedData["text"] == "" ||
        decodedData["text"] == null) {
      print(" transcribed text empty ");
    } else {
      log("complete result in vosk : ${result}");
      results.add(result);
    }
  } else {
    String result = await _recognizer!.getPartialResult();
    dynamic decodedData = jsonDecode(result);
    if (decodedData["partial"].isEmpty ||
        decodedData["partial"] == "" ||
        decodedData["partial"] == null) {
      print("empty partial result");
    } else {
      log("partial result in vosk : ${result}");
      results.add(result);
    }
  }
}

// await _recognizer!
//     .acceptWaveformBytes(Uint8List.fromList(audioBytes.sublist(pos)));
results.add(await _recognizer!.getFinalResult());

return results.join(' ');

}

startSpeechToText(Function(String) onResult) async { String result = "";

// final speechService = await _vosk.initSpeechService(_recognizer!);
_speechService?.onPartial().forEach((partial) => print(partial));
_speechService?.onResult().forEach((result) => print(result));
await _speechService?.start();

_resultSubscription = _speechService?.onResult().listen((event) {
  result = _onSpeechResult(event);
  onResult(result);
});

}

String _onSpeechResult(dynamic result) { String _sttAllWords = ""; Map<String, dynamic> decoded = json.decode(result); VoskResultModel voskResult = VoskResultModel.fromJson(decoded); if (voskResult.text != null && voskResult.text != "") { _sttAllWords += voskResult.text?.trim() ?? ""; print("final text: ${_sttAllWords.trim()}"); }

return _sttAllWords;

}

Future stopSpeechToText() async { await _resultSubscription?.cancel(); await _speechService?.stop();

print("stopped speechToText wosk");

} }

kuberanb commented 9 months ago

@alejandrogiubel @sergsavchuk @nshmyrev @EvgeniyVabishchevich