Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.apk filter=lfs diff=lfs merge=lfs -text
12 changes: 5 additions & 7 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1,041 changes: 0 additions & 1,041 deletions .idea/caches/deviceStreaming.xml

This file was deleted.

19 changes: 0 additions & 19 deletions .idea/gradle.xml

This file was deleted.

14 changes: 14 additions & 0 deletions apks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
If you want to early test this version of the app, first of all consider that this is a development preview, so it could be pretty unstable, and it will require initial setup, so to try it you need to:

Download from [here](https://github.com/niedev/OnnxModelsEnhancer/releases/tag/v1.0.0-beta) the Mozilla.zip,
Madlad.zip, HY-MT.zip. After the download extract these folders (their name should remain "Mozilla", "Madlad" and "HY-MT", with the content of these .zip directly inside the corresponding extracted folders, if you change the structure of these folder the app will not work).

After that create a folder named "models", inside it create a folder named "Translation", and inside it paste all the extracted folders.

Now download one of the apk in this folder, install it, open the app, enable the requested file access and start the download.

After the download has finished, exit the app and enable all its permissions from the Android Settings (Settings -> Applications -> RTranslator).

Then re open the app and everything should work.

By default, the models used for translation are the Mozilla ones, to select the other supported model you can select one from RTranslator's settings, at the bottom (note that probably, to execute Madlad and HY-MT, you will need a phone with at least 12GB of RAM, if the RAM won't be enough the app will crash until you wipe its data).
3 changes: 3 additions & 0 deletions apks/RTranslator_3.0.0_alpha1.apk
Git LFS file not shown
7 changes: 6 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ android {
//bergamot flags
'-DANDROID_ABI=arm64-v8a',
'-DANDROID_PLATFORM=android-28',
'-DANDROID_STL=c++_static',
'-DANDROID_STL=c++_shared',
"-DANDROID_PIE=ON",
"-DANDROID_CPP_FEATURES=exceptions",
'-DCMAKE_BUILD_TYPE=Release',
Expand Down Expand Up @@ -121,6 +121,11 @@ dependencies {
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
//Download library
implementation 'com.github.amitshekhariitbhu:PRDownloader:1.0.2'
// DJL HuggingFace tokenizers wrapper
implementation "ai.djl.android:core:0.33.0"
implementation "ai.djl.huggingface:tokenizers:0.33.0"
implementation "ai.djl.android:tokenizer-native:0.33.0"

//implementation 'androidx.core:core-ktx:1.10.0'
implementation 'androidx.work:work-runtime:2.7.1'
implementation 'androidx.exifinterface:exifinterface:1.3.7'
Expand Down
15 changes: 13 additions & 2 deletions app/src/main/java/nie/translator/rtranslator/Global.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@


public class Global extends Application implements DefaultLifecycleObserver {
public static final boolean ONLY_TEXT_TRANSLATION_MODE = true;
public static final boolean ONLY_TEXT_TRANSLATION_MODE = false;
public enum RTranslatorMode {
TEXT_TRANSLATION_MODE,
WALKIE_TALKIE_MODE,
Expand Down Expand Up @@ -128,7 +128,18 @@ public void restartTranslator(Translator.GeneralListener listener){
getLanguages(false);
SharedPreferences sharedPreferences = getSharedPreferences("default", Context.MODE_PRIVATE);
int mode = sharedPreferences.getInt("selectedTranslationModel", Translator.MOZILLA);
translator.restart(mode, listener);
translator.restart(mode, new Translator.GeneralListener() {
@Override
public void onSuccess() {
getTranslatorLanguages(false); //refresh languages
listener.onSuccess();
}

@Override
public void onFailure(int[] reasons, long value) {
listener.onFailure(reasons, value);
}
});
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;

import androidx.appcompat.app.AlertDialog;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.view.View;

import androidx.fragment.app.Fragment;
Expand Down Expand Up @@ -78,6 +82,12 @@ protected void onStart() {
super.onStart(); //called here because otherwise the onStart of the DownloadFragment is called before this onStart, and this could cause problems.
}

@Override
protected void onResume() {
super.onResume();
checkAllFilesPermission(); //todo: remove before the final release
}

@Override
protected void onStop() {
super.onStop();
Expand Down Expand Up @@ -151,6 +161,27 @@ public void onBackPressed() {
}
super.onBackPressed();
}

//todo: remove before the final release
private void checkAllFilesPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (!Environment.isExternalStorageManager()) {
requestAllFilesPermission();
}
}
}

//todo: remove before the final release
private void requestAllFilesPermission() {
try {
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 100);
} catch (Exception e) {
Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivityForResult(intent, 100);
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public void onClick(View v) {
}

private void showList() {
progressBar.setVisibility(View.GONE);
reloadButton.setVisibility(View.GONE);

final ArrayList<CustomLocale> languages = global.getLanguages(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,9 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import nie.translator.rtranslator.Global;
import nie.translator.rtranslator.R;
import nie.translator.rtranslator.voice_translation.VoiceTranslationActivity;
import nie.translator.rtranslator.voice_translation.neural_networks.translation.Translator;

public class ModelManagerFragment extends Fragment {
Expand Down Expand Up @@ -71,8 +67,8 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
case Translator.MADLAD_CACHE:
radioGroup.check(R.id.madlad_radio);
break;
case Translator.GEMMA:
radioGroup.check(R.id.gemma_radio);
case Translator.HY_MT:
radioGroup.check(R.id.hy_radio);
break;
}

Expand All @@ -87,8 +83,8 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
case R.id.madlad_radio:
changeModel(Translator.MADLAD_CACHE);
break;
case R.id.gemma_radio:
changeModel(Translator.GEMMA);
case R.id.hy_radio:
changeModel(Translator.HY_MT);
break;
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,34 @@
import ai.onnxruntime.OrtEnvironment;
import ai.onnxruntime.OrtException;
import ai.onnxruntime.OrtSession;
import nie.translator.rtranslator.voice_translation.neural_networks.translation.Translator;

public class CacheContainerNative {
private int[] shape;
private OnnxTensor[] cacheTensors;
private long cacheContainerNativePointer;
private int mode = Translator.NLLB;

//Used to load CacheContainerNative.cpp on application startup
static {
System.loadLibrary("cache_container_native");
}

public CacheContainerNative(OrtEnvironment env, OrtSession.Result cache, int nLevels, int batchSize, int nHeads, int sequenceLength, int hiddenSize){
public CacheContainerNative(OrtEnvironment env, OrtSession.Result cache, int nLevels, int batchSize, int nHeads, int sequenceLength, int hiddenSize, int mode){
try {
cacheTensors = new OnnxTensor[nLevels*2];
cacheContainerNativePointer = initialize(nLevels*2, batchSize, nHeads, sequenceLength, hiddenSize);
int count=0;
for (int i = 0; i < nLevels; i++) {
cacheTensors[count] = (OnnxTensor) cache.get("present." + i + ".decoder.key").get();
cacheTensors[count] = (OnnxTensor) cache.get("present." + i + (mode!= Translator.HY_MT ? ".decoder" : "") + ".key").get();
//we use OnnxTensor's private getBuffer method, which returns the data reference without making a copy of it and we pass this reference to the native cache container
Method method = cacheTensors[count].getClass().getDeclaredMethod("getBuffer");
method.setAccessible(true);
ByteBuffer buffer = (ByteBuffer) method.invoke(cacheTensors[count]);
insertValues(cacheContainerNativePointer, count, buffer);
count++;

cacheTensors[count] = (OnnxTensor) cache.get("present." + i + ".decoder.value").get();
cacheTensors[count] = (OnnxTensor) cache.get("present." + i + (mode!= Translator.HY_MT ? ".decoder" : "") + ".value").get();
//we use OnnxTensor's private getBuffer method, which returns the data reference without making a copy of it and we pass this reference to the native cache container
method = cacheTensors[count].getClass().getDeclaredMethod("getBuffer");
method.setAccessible(true);
Expand All @@ -63,6 +65,7 @@ public CacheContainerNative(OrtEnvironment env, OrtSession.Result cache, int nLe
count++;
}
shape = new int[]{nLevels*2, batchSize, nHeads, sequenceLength, hiddenSize};
this.mode = mode;
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
Expand All @@ -86,7 +89,7 @@ public OrtSession.Result getCacheResult(OrtEnvironment env){
int count = 0;
for (int i = 0; i < shape[0]/2; i++) {
for (String suffix: suffixes) {
names[count] = "present." + i + ".decoder."+suffix;
names[count] = "present." + i + (mode!= Translator.HY_MT ? ".decoder." : ".") + suffix;
ByteBuffer buffer = getBuffer(cacheContainerNativePointer, count);
values[count] = OnnxTensor.createTensor(env, buffer, new long[]{shape[1], shape[2], shape[3], shape[4]}, OnnxJavaType.FLOAT);
count++;
Expand Down
Loading
Loading