Skip to content
This repository has been archived by the owner on Mar 15, 2022. It is now read-only.

Commit

Permalink
Update DS to 0.6.0a14
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Lissy authored and andrenatal committed Nov 14, 2019
1 parent 75ca994 commit d2f4514
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
2 changes: 1 addition & 1 deletion mozillaspeechlibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ android {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.loopj.android:android-async-http:1.4.9'
implementation 'org.mozilla.deepspeech:libdeepspeech:0.6.0-alpha.8@aar'
implementation 'org.mozilla.deepspeech:libdeepspeech:0.6.0-alpha.14@aar'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import java.util.HashMap;
import java.util.Map;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.File;
import java.io.FileReader;

import java.nio.ByteOrder;
import java.nio.ShortBuffer;
Expand All @@ -21,6 +23,8 @@

import android.util.Log;

import org.json.JSONObject;

import org.mozilla.deepspeech.libdeepspeech.DeepSpeechModel;
import org.mozilla.deepspeech.libdeepspeech.DeepSpeechStreamingState;

Expand All @@ -34,11 +38,6 @@ class LocalDSInference implements Runnable {

boolean stopStream;

final int BEAM_WIDTH = 250;

final float LM_WEIGHT = 0.75f;
final float VALID_WORD_COUNT_WEIGHT = 1.85f;

static final String _tag = "LocalDSInference";

static boolean keepClips = false;
Expand All @@ -48,21 +47,47 @@ class LocalDSInference implements Runnable {

String modelRoot;
String tfliteModel;
String alphabet;
String LM;
String trie;
String infoJson;

protected LocalDSInference(MozillaSpeechService aService, int aSampleRate) {
int beamWidth = -1;
float lmAlpha = -1;
float lmBeta = -1;

protected LocalDSInference(MozillaSpeechService aService) {
Log.e(this._tag, "new LocalDSInference()");

modelRoot = aService.getModelPath() + "/" + aService.getLanguageDir();

Log.e(this._tag, "Loading model from " + modelRoot);

this.tfliteModel = this.modelRoot + "/" + LocalSpeechRecognition.kTfLiteModel;
this.alphabet = this.modelRoot + "/" + LocalSpeechRecognition.kAlphabet;
this.LM = this.modelRoot + "/" + LocalSpeechRecognition.kLM;
this.trie = this.modelRoot + "/" + LocalSpeechRecognition.kTrie;
this.infoJson = this.modelRoot + "/" + LocalSpeechRecognition.kInfoJson;

try {
String infoJsonContent = new String();
BufferedReader br = new BufferedReader(new FileReader(this.infoJson));
String line;
while ((line = br.readLine()) != null) {
Log.e(this._tag, "line=" + line);
infoJsonContent += line;
}
br.close();
Log.e(this._tag, "infoJsonContent=" + infoJsonContent);

JSONObject modelParameters = (new JSONObject(infoJsonContent)).getJSONObject("parameters");
this.beamWidth = modelParameters.getInt("beamWidth");
this.lmAlpha = (float)modelParameters.getDouble("lmAlpha");
this.lmBeta = (float)modelParameters.getDouble("lmBeta");
} catch (Exception ex) {
}

Log.e(this._tag, "Read model parameters: beamWidth=" + this.beamWidth);
Log.e(this._tag, "Read model parameters: lmAlpha=" + this.lmAlpha);
Log.e(this._tag, "Read model parameters: lmBeta=" + this.lmBeta);

this.clipNumber += 1;

Expand All @@ -76,11 +101,11 @@ protected LocalDSInference(MozillaSpeechService aService, int aSampleRate) {

if (this.mModel == null) {
Log.e(this._tag, "new DeepSpeechModel(\"" + this.tfliteModel + "\")");
this.mModel = new DeepSpeechModel(this.tfliteModel, this.alphabet, BEAM_WIDTH);
this.mModel = new DeepSpeechModel(this.tfliteModel, this.beamWidth);
}

if (this.useDecoder) {
this.mModel.enableDecoderWihLM(this.LM, this.trie, LM_WEIGHT, VALID_WORD_COUNT_WEIGHT);
this.mModel.enableDecoderWihLM(this.LM, this.trie, this.lmAlpha, this.lmBeta);
}

if (this.keepClips) {
Expand All @@ -90,7 +115,7 @@ protected LocalDSInference(MozillaSpeechService aService, int aSampleRate) {
}
}

this.mStreamingState = this.mModel.createStream(aSampleRate);
this.mStreamingState = this.mModel.createStream();
this.stopStream = false;
}

Expand Down Expand Up @@ -189,17 +214,17 @@ class LocalSpeechRecognition implements Runnable {
Thread mInferenceThread;

public static String kTfLiteModel = "output_graph.tflite";
public static String kAlphabet = "alphabet.txt";
public static String kLM = "lm.binary";
public static String kTrie = "trie";
public static String kInfoJson = "info.json";

private static Map<String,String> mLanguages = new HashMap<String, String>();
static {
mLanguages.put("en-US", "en-us");
mLanguages.put("fr-FR", "fr-fr");
}

private static String kBaseModelURL = "https://github.com/lissyx/DeepSpeech/releases/download/v0.6.0-alpha.7/";
private static String kBaseModelURL = "https://github.com/lissyx/DeepSpeech/releases/download/v0.6.0-alpha.14/";

protected LocalSpeechRecognition(int aSampleRate, int aChannels, Vad aVad,
MozillaSpeechService aService) {
Expand All @@ -209,7 +234,7 @@ protected LocalSpeechRecognition(int aSampleRate, int aChannels, Vad aVad,
this.mChannels = aChannels;
this.mService = aService;

this.mInferer = new LocalDSInference(this.mService, this.mSampleRate);
this.mInferer = new LocalDSInference(this.mService);
this.mInferenceThread = new Thread(this.mInferer);
this.mInferenceThread.start();
}
Expand Down Expand Up @@ -324,11 +349,10 @@ public static String getLanguageDir(String aLanguage) {

return rv;
}

public static boolean ensureModelInstalled(String aModelPath) {
Log.e(_tag, "ensureModelInstalled(" + aModelPath + ")");
return (new File(aModelPath + "/" + kTfLiteModel)).exists()
&& (new File(aModelPath + "/" + kAlphabet)).exists()
&& (new File(aModelPath + "/" + kLM)).exists()
&& (new File(aModelPath + "/" + kTrie)).exists();
}
Expand Down

0 comments on commit d2f4514

Please sign in to comment.