Multiple local HMS ML Kit translator models in Flutter?

298 views Asked by At

I've defined a class that wraps the HMS ML Kit in-device translator. This class has two translator instances, with two different settings:

MLLocalTranslator translatorSend = new MLLocalTranslator();
MLLocalTranslator translatorReceive = new MLLocalTranslator();
MLTranslateSetting settingSend = new MLTranslateSetting();
MLTranslateSetting settingReceive = new MLTranslateSetting();

translatorSend translates request from a language (for example it) to English (en). translatorReceive translates the response of the request from en to it.
However, the prepare method only downloads the model for en_it translation and not the it_en model (if exists).

HMSTranslator(String languageCode) {
  settingSend.sourceLangCode = languageCode;
  settingSend.targetLangCode = "en";
  settingReceive.sourceLangCode = "en";
  settingReceive.targetLangCode = languageCode;
}

Future<bool> prepare() async {
  if(settingSend.sourceLangCode != settingSend.targetLangCode) {
    bool isSendPrepared = await translatorSend.prepareModel(setting: settingSend)
    bool isReceivePrepared = await translatorReceive.prepareModel(setting: settingReceive);
    isPrepared = isSendPrepared && isReceivePrepared;
  }
  else {
    isPrepared = false;
  }
  return isPrepared;
}

The problem comes when I translate a string with translatorSend.

Future<String> translateString(String stringToTranslate) async {
  if(settingSend.sourceLangCode != settingSend.targetLangCode) {
    String result;
    if (isPrepared) {
      result = await translatorSend.asyncTranslate(sourceText: stringToTranslate);
    }
    else {
      settingSend.sourceTextOnRemote = stringToTranslate;
      result = await sendRemoteTranslator.asyncTranslate(setting: settingSend);
    }
    return result;
  }
  else {
    return stringToTranslate;
  }
}

This method should translate an it String to an en String. However, it seems to call the en_it model and fails the translation:

I/flutter (28228): TRANSLATOR: it to en
I/flutter (28228): TRANSLATOR: PREPARED
I/MLLocalTranslator(28228): translate sourceLanguage: en targetLanguage: it
WHAT: vestiti usati -> vestiti usati  - WHERE applicazione -> applicazione 

The translation of the response, from en to it works.
I've tested other languages and that happens also with fr.
Further testing showed that the process worked with es:

WHAT: ropa usada -> Used clothing  - WHERE aplicación -> application 
2

There are 2 answers

1
zhangxaochen On BEST ANSWER

Pls check whether you are using the new version of the Flutter plug-in.

Language packs can be used in two-way. For example, en-it can be used for en to it or it to en.

The following are for your reference:

Modify based on the plugin Demo in the official website

The same instance is used for bidirectional translation by invoking multiple times.

//Entry function
_translationMake() async {
  try {
    await _prepareModel_run("it","en","vestiti usati");
    await _prepareModel_run("en","it","application");
  } on Exception catch (e) {
    print(e.toString());
  }
}

_prepareModel_run(String srcLang, String dstLang, String content) async {
  setting.sourceLangCode = srcLang;
  setting.targetLangCode = dstLang;
  try {
    final bool res = await translator.prepareModel(setting: setting); 
    if (res) {
      final String s = await _localTranslate_run(content);
      if (s != null) {
        print("_prepareModel_run " + content + " translate to "+s);
      }
    }else {
      print("_prepareModel_run res false");
    }
  } on Exception catch (e) {
    print(e.toString());
  }
}


Future<String> _localTranslate_run(String Content) async {
  try {
    final String s =
    await translator.syncTranslate(sourceText: Content);
    if (s != null) {
      _stopLocalTranslate();
      
      setState(() => _translateResult = s);
      return s;
    } else {
      print("no Translation");
      setState(() => _translateResult = "no translation");
      return "no translation";
    }
  } on Exception catch (e) {
    print(e.toString());
  }
}

And the log print results are as follows:

_prepareModel_run vestiti usati translate to Used clothes
_prepareModel_run application translate to applicazione
3
Zinna On

We can use HMS ML kit to translate text into different languages. The following is the info. you can take reference for.

ML services can currently translate texts between 12 languages: Simplified Chinese, English, French, Arabic, Thai, Spanish, Turkish, Portuguese, Japanese, German, Italian, and Russian.

enter image description here

Step 1: Text is fetched from UI and provided to ML model

Step 2: Parameters are set before making API call to server

    · Source language code

    · Desired Language code

    · String which needs to be translated.

Step 3: Once API data reaches the server ML Model translates the text into desired output

Step 4: Server returns the translated output to application.

Step 5: Application shows output to UI.

Precautions: The machine learning model is stored on cloud. An Internet call is made so its permission is required.

Below are the changes you have to do in order to run build and run the project

Open App.gradle file and add this line on top.

apply plugin: 'com.huawei.agconnect'

To use Text Translation service add this dependency to pro

mplementation 'com.huawei.hms:ml-computer-translate:1.0.3.300'

MLRemoteTranslateSetting object is being created which takes Source Language as setSourceLangCode() and Output Language as setTargetLangCode()

MLRemoteTranslator object is created by passing previously created MLRemoteTranslateSetting object to it.

You can create a Task where mlRemoteTranslator will have an async call by asyncTranslate() and we will provide user string as the input to this method.

This task will yield to 2 callbacks

addOnSuccessListener

addOnFailureListener

As the name suggests you can add your code in success listener and can add notification/Log in failure listener.

For Flutter:

First: create MlTranslatorSettings object and instance in initState enter image description here

Second: set the settings to the translator, for example, initial language and final language of the translation, see below example. enter image description here

In the properties, you can customize the type of map, controls, camera position, initial position, etc.

Here are also some detailed info. regarding how to use HMS ML kit with Flutter: Link Hope it will be helpful to you.