3

I’m using the Huawei ML Kit for on-device translation, but downloading the model always shows the Exception. I’m following the Huawei Doc On-device Translation. I have tried the following code, but yet I am getting the Exception continuously.

File build.gradle (app)

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'com.huawei.agconnect'
    id 'kotlin-kapt'
}

android {
    compileSdk 30

    defaultConfig {
        applicationId "com.example.huaweitestads"
        minSdk 21
        targetSdk 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    buildFeatures {
        dataBinding true
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    kapt "com.android.databinding:compiler:3.5.0"
    //Huawei Dependency
    implementation 'com.huawei.agconnect:agconnect-core:1.5.2.300'
    implementation 'com.huawei.hms:ads-lite:13.4.45.308'
    implementation 'com.huawei.hms:ads:3.4.45.308'

    implementation 'com.huawei.hms:ads-identifier:3.4.39.302'
    implementation 'com.huawei.hms:ads-installreferrer:3.4.39.302'
    implementation 'com.huawei.hms:ml-computer-voice-asr-plugin:1.0.4.300'
    implementation 'com.huawei.hms:ml-computer-translate:3.0.0.300'
    implementation 'com.huawei.hms:ml-computer-translate-model:3.0.0.300'
    //implementation 'com.huawei.hms:ml-computer-voice-asr:2.2.0.300'
}

MainActivity

class MainActivity : AppCompatActivity() {
    private val permissionsRequestCode = 123
    private lateinit var managePermissions: ManagePermissions
    private lateinit var binding: ActivityMainBinding
    private lateinit var buttonClick: Button
    private var interestitialAd: InterstitialAd? = null
    private lateinit var sharedPreferences: SharedPreferences
    val REQUEST_CODE_ASR : Int = 100
    var dialogBinding: LayCustomDialogBinding? = null
    var customDialog: AlertDialog? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        dialogBinding = DataBindingUtil.inflate(
            LayoutInflater.from(this),
            R.layout.lay_custom_dialog,
            null,
            false
        )

        customDialog = AlertDialog.Builder(this,0).create()
        customDialog?.apply {
            window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            setView(dialogBinding?.root)
            setCancelable(false)
        }
        //initialize the Huawei Ads
        HwAds.init(this)
        MLApplication.getInstance().apiKey = "App Key"
        val list = listOf(
           Manifest.permission.READ_EXTERNAL_STORAGE,
           Manifest.permission.RECORD_AUDIO,
           Manifest.permission.WRITE_EXTERNAL_STORAGE
        )

        sharedPreferences = getSharedPreferences(getString(R.string.pref_file_name), Context.MODE_PRIVATE)
        managePermissions = ManagePermissions(this, list, permissionsRequestCode, sharedPreferences)

        buttonClick = findViewById(R.id.btn_click)
        interestitialAd = InterstitialAd(this)
        interestitialAd!!.adId = "testb4znbuh3n2"
        createSharedPrefFile()

        binding.btnClick.setOnClickListener {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                val checkerDialog = managePermissions.checkPermissions()
                if (!checkerDialog) {
                    showDialogForListening()
                }

            }
        }
    }

    private fun showDialogForListening() {
        val intent = Intent(this, MLAsrCaptureActivity::class.java)
            .putExtra(MLAsrCaptureConstants.LANGUAGE, "en-US")
            .putExtra(MLAsrCaptureConstants.FEATURE, MLAsrCaptureConstants.FEATURE_WORDFLUX)
        startActivityForResult(intent, REQUEST_CODE_ASR)
    }

    override protected fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        var text = ""
        // REQUEST_CODE_ASR: request code between the current activity and speech pickup UI activity defined in step 3.
        if (requestCode == REQUEST_CODE_ASR) {
            when (resultCode) {
                MLAsrCaptureConstants.ASR_SUCCESS -> if (data != null) {
                    val bundle = data.extras
                    // Obtain the text information recognized from speech.
                    if (bundle!!.containsKey(MLAsrCaptureConstants.ASR_RESULT)) {
                        text = bundle.getString(MLAsrCaptureConstants.ASR_RESULT).toString()
                        // Process the recognized text information.
                        if(text.isNotEmpty()){
                            binding.tvInput.text = text
                            TranslateLocally(text)
                        }else{
                            Toast.makeText(this, "Text Couldn't Recognize. Please try again", Toast.LENGTH_SHORT).show()
                        }
                    }
                }

                // Processing logic for recognition failure.
                MLAsrCaptureConstants.ASR_FAILURE ->
                    if (data != null) {
                        val bundle = data.extras
                        // Check whether a result code is contained.
                        if (bundle!!.containsKey(MLAsrCaptureConstants.ASR_ERROR_CODE)) {
                            val errorCode = bundle.getInt(MLAsrCaptureConstants.ASR_ERROR_CODE)
                            // Perform troubleshooting based on the result code.
                        }
                        // Check whether error information is contained.
                        if (bundle.containsKey(MLAsrCaptureConstants.ASR_ERROR_MESSAGE)) {
                            val errorMsg = bundle.getString(MLAsrCaptureConstants.ASR_ERROR_MESSAGE)
                            // Perform troubleshooting based on the error information.
                        }
                        // Check whether a sub-result code is contained.
                        if (bundle.containsKey(MLAsrCaptureConstants.ASR_SUB_ERROR_CODE)) {
                            val subErrorCode = bundle.getInt(MLAsrCaptureConstants.ASR_SUB_ERROR_CODE)
                            // Process the sub-result code.
                        }
                    }
                else -> {
                }
            }
        }
    }

    private fun TranslateLocally(input: String){

        val setting = MLLocalTranslateSetting.Factory()
            .setSourceLangCode("zh")
            .setTargetLangCode("en")
            .create()
        val mlLocalTranslator = MLTranslatorFactory.getInstance().getLocalTranslator(setting)
        MLTranslateLanguage.getLocalAllLanguages().addOnSuccessListener{
            for (item in it)
                Log.d("SupportedLang", "TranslationBeginWithInternet: $item")
        }
        obtainModel()
    }

    private fun obtainModel() {
        val manager = MLLocalModelManager.getInstance()
        val sourceLangCode = "en"
        val model = MLLocalTranslatorModel.Factory(sourceLangCode).create()
        val downloadStrategy = MLModelDownloadStrategy.Factory()
            .needWifi()
            .create()

        customDialog?.show()
        val downloadProgressListener = MLModelDownloadListener{ alreadyDownLength, totalLength ->
            runOnUiThread {
                setProgressBar(alreadyDownLength,totalLength)
            }
        }
        manager.downloadModel(model,downloadStrategy,downloadProgressListener).addOnSuccessListener{
            Toast.makeText(this, "Downloading: $it", Toast.LENGTH_SHORT).show()
            //customDialog?.dismiss()
        }.addOnFailureListener{

    // This is the line where I'm getting the error. How can I fix it?
    // **//**Error is: The model doesn't Exist!****

            Toast.makeText(this, "this is Error: $it", Toast.LENGTH_SHORT).show()
            dialogBinding?.btnOk?.setOnClickListener{
                customDialog?.dismiss()
            }
        }
    }

    private fun setProgressBar(alreadyDownlength: Long, totalLength: Long) {
        dialogBinding?.customProgressBar?.setProgressWithAnimation(alreadyDownlength.toFloat())
        dialogBinding?.customProgressBar?.setMax(totalLength.toInt())
    }
}

ManifestFile

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.huaweitestads">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:name=".AdSampleApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.HuaweiTestAds"
        android:usesCleartextTraffic="true">
        <activity
            android:name=".InterestitialActivity"
            android:exported="true" />
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data
            android:name="com.huawei.hms.ml.DEPENDENCY"
            android:value= "translate"/>
    </application>

</manifest>

I have added the agconnect file in the project. Remote Translation is working fine.

Screenshot: Exception

Enter image description here

Detailed LogFile:

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
waheed shah
  • 494
  • 7
  • 19
  • 1
    hi@waheed shah, Could you please share the detailed log obtained when the issues occurred ? – zhangxaochen Oct 05 '21 at 11:52
  • hi@shirley, Thanks for the response. I have added both the screen shot and detailed logcatFile for reference. Please have a look at and suggest me solution. – waheed shah Oct 06 '21 at 07:10
  • @waheed shah Can you provide your log for the exception you saw? That will be helpful to troubleshoot the problem. If you can see the error code, you can refer to On-device model download error codes to troubleshoot the error. Here are the definitions: https://developer.huawei.com/consumer/en/doc/development/hiai-References/mlsdkmodeldownload-errorcode-0000001058609238 – Zinna Oct 06 '21 at 15:49
  • @waheedshah I am looking into your issue, will update you soon – Zinna Oct 06 '21 at 15:50
  • @Zinna ok waiting for the solution. – waheed shah Oct 07 '21 at 04:53
  • hi@waheed shah, Is this problem solved? If not, could you re-provide the log?Looks like the logs have been [deleted](https://i.stack.imgur.com/JTwIc.jpg). – zhangxaochen Oct 08 '21 at 03:15
  • hi @shirley. Not yet solved. Here is the new link. Please have look at it https://file.io/GD2pPuDqjt9F – waheed shah Oct 08 '21 at 05:31
  • @waheedshah, To solve this issue, may i know what is the language code set when a model is downloaded? – zhangxaochen Oct 08 '21 at 07:49
  • @shirley i m using chinese "zh" as source code to english "en" as target code. – waheed shah Oct 08 '21 at 08:01
  • @waheedshah since HW internal team is helping you. Will not proceed with further investigation. – Zinna Oct 12 '21 at 15:11
  • The last link is (effectively) broken: *"Deleted. The transfer you requested has been deleted."* – Peter Mortensen Aug 31 '22 at 02:02

1 Answers1

1

The log shows that the downloaded translation language is en_en, and the corresponding interface cannot be found when the interface is invoked.

So you could try to change the following code:

val sourceLangCode = "en"

to

val sourceLangCode = "zh"

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • 1
    I have added "Log.d("SourceLang", "obtainModel: ${model.languageCode}")" to check what is being obtained. I m getting the en-US as languageCode but problem remains there. When I changed from "en-US" to "zh". The model started to download. Why isn't it working for "en-US"? – waheed shah Oct 08 '21 at 10:51
  • The download translation language en_en was not found in the interface. Therefore, an error is reported. You can search the log file for the keyword "translation" and view the downloaded translation language en_name. [This picture](https://i.stack.imgur.com/zck0J.png) is where go wrong. – zhangxaochen Oct 09 '21 at 02:47
  • Actually, Initially i have added the sourceCode with null values where I m getting the error. Then, I have changed to what you showed in the picture. And finally I have added languageCode as "en-US" but the problem remains there. Is model "en-US" doesn't available? – waheed shah Oct 10 '21 at 12:47
  • 1
    The model "en-US" is not in ISO 639-1 format. The format of the downloaded model is "en_name" and "en" in front of "en_name" cannot be changed. You only need to set "name" and "name" is "sourceLangCode". Use ISO 639-1. For example, if you want to convert German to Russian, you need to download the "en_de" and "en_ru" models. – zhangxaochen Oct 11 '21 at 01:35