I intend to use the "vosk" library in my Android project written in Java. Initially, I was able to perform speech-to-text tasks using a small and lightweight model. However, I'm unsure how to handle larger and more complex models. Whenever I try to include the model files within my application, I encounter a "java.lang.OutOfMemoryError" error. Additionally, I lack information on how to load the model files from the device's memory. Any guidance on how to efficiently handle large models in my Android project would be greatly appreciated. this is my codes :
public class SpeakToText implements RecognitionListener {
private final String VOSK_TAG = "SpeakRecognizing";
private Context context;
private Model model;
private SpeechService speechService;
private SpeechStreamService speechStreamService;
public void initModel() {
StorageService.unpack(context, "vosk-model-small-fa-0.4", "model",
(model) -> {
this.model = model;
Toast.makeText(context, "Ready", Toast.LENGTH_SHORT).show();
recognizeMicrophone();
},
(exception) -> setErrorState("Failed to unpack the model" + exception.getMessage()));
}
@Override
public void onResult(String hypothesis) {
Log.i("TAG", hypothesis);
}
@Override
public void onFinalResult(String hypothesis) {
if (speechStreamService != null) {
speechStreamService = null;
}
Log.i(VOSK_TAG, "onFinalResult: " + hypothesis);
}
@Override
public void onPartialResult(String hypothesis) {
Log.i(VOSK_TAG, "onPartialResult: " + hypothesis);
}
@Override
public void onError(Exception e) {
setErrorState(e.getMessage());
Log.e(VOSK_TAG, "onError: ", e);
}
@Override
public void onTimeout() {
Log.i(VOSK_TAG, "onTimeout: ");
}
private void setErrorState(String message) {
}
public void recognizeMicrophone() {
if (speechService != null) {
speechService.stop();
speechService = null;
} else {
try {
Recognizer rec = new Recognizer(model, 16000.0f);
speechService = new SpeechService(rec, 16000.0f);
speechService.startListening(this);
} catch (IOException e) {
setErrorState(e.getMessage());
}
}
}
public void pause(boolean checked) {
if (speechService != null) {
speechService.setPause(checked);
}
}
}
I want either the model to be placed in the project files without errors and run, or the model file to be imported from the mobile memory.Thank you.
Update:
I manually copied the model file into the required folder (\Internal shared storage\Android\data\com.example.myapp\files\model). Now I don't know how to import this model into the app.
You can install the model in sdcard and then show the path