/// <summary> /// Download lang model /// </summary> /// <param name="sourceLangCode"></param> private async void DownloadModel(String sourceLangCode) { MLLocalTranslatorModel model = new MLLocalTranslatorModel.Factory(sourceLangCode).Create(); MLModelDownloadStrategy downloadStrategy = new MLModelDownloadStrategy.Factory() .NeedWifi() // It is recommended that you download the package in a Wi-Fi environment. .Create(); Task downloadModelTask = manager.DownloadModelAsync(model, downloadStrategy); try { await downloadModelTask; if (downloadModelTask.IsCompleted) { // Delete success. this.DisplaySuccess("Download success.", true); } else { // Delete failure. Log.Debug(Tag, " Download failure."); } } catch (Exception e) { // Operation failure. DisplayFailure(e); } }
private async void DownloadModel(string person) { MLTtsLocalModel model = new MLTtsLocalModel.Factory(person).Create(); MLModelDownloadStrategy request = new MLModelDownloadStrategy.Factory() .NeedWifi() .SetRegion(MLModelDownloadStrategy.RegionDrEurope) .Create(); Task downloadTask = manager.DownloadModelAsync(model, request, this); try { await downloadTask; if (downloadTask.IsCompleted) { mlTtsEngine.UpdateConfig(mlTtsConfigs); Log.Info(Tag, "downloadModel: " + model.ModelName + " success"); ShowToast("downloadModel Success"); Speak(mEditText.Text.ToString().Trim()); } else { Log.Info(Tag, "failed "); } } catch (Exception e) { Log.Error(Tag, "downloadModel failed: " + e.Message); ShowToast(e.Message); } }
/// <summary> /// Download Model Method /// </summary> public async void DownloadModels(MLCustomRemoteModel customRemoteModel) { MLModelDownloadStrategy strategy = new MLModelDownloadStrategy.Factory() .NeedWifi() .SetRegion(MLModelDownloadStrategy.RegionDrEurope) .Create(); MLModelDownloadListener modelDownloadListener = new MLModelDownloadListener(this, DownloadCase); System.Threading.Tasks.Task downloadTask = MLLocalModelManager.Instance.DownloadModelAsync(customRemoteModel, strategy, modelDownloadListener); try { await downloadTask; if (downloadTask.IsCompleted) { //Detection success Toast.MakeText(this, "Model download successful", ToastLength.Short).Show(); } else { //Detection success Toast.MakeText(this, "Model download failed", ToastLength.Short).Show(); } } catch (System.Exception e) { //Operation failed Log.Debug(Tag, "Download operation failed: " + e.Message); } }
/// <summary> /// Text translation on the device. /// </summary> private async void LocalTranslator() { // Create an offline translator. MLLocalTranslateSetting setting = new MLLocalTranslateSetting .Factory() // Set the source language code. The ISO 639-1 standard is used. This parameter is mandatory. If this parameter is not set, an error may occur. .SetSourceLangCode("en") // Set the target language code. The ISO 639-1 standard is used. This parameter is mandatory. If this parameter is not set, an error may occur. .SetTargetLangCode("zh") .Create(); this.localTranslator = MLTranslatorFactory.Instance.GetLocalTranslator(setting); // Download the offline model required for local offline translation. // Obtain the model manager. MLModelDownloadStrategy downloadStrategy = new MLModelDownloadStrategy.Factory() // It is recommended that you download the package in a Wi-Fi environment. .NeedWifi() .SetRegion(MLModelDownloadStrategy.RegionDrEurope) .Create(); Task downloadTask = this.localTranslator.PreparedModelAsync(downloadStrategy); try { await downloadTask; if (downloadTask.IsCompleted) { // Download success. string input = mEditText.Text.ToString(); Task <string> translateTask = this.localTranslator.TranslateAsync(input); try { await translateTask; if (translateTask.IsCompleted && translateTask.Result != null) { // Translate success. var detectResult = translateTask.Result; this.DisplaySuccess(detectResult, true); } else { // Translate failure. Log.Info(Tag, " Translate failure "); } } catch (Exception e) { // Operation failure. Log.Info(Tag, " Local translate operation failure: " + e.Message); this.DisplayFailure(e); } } else { // Download failure. Log.Info(Tag, " Download failure "); } } catch (Exception e) { // Operation failure. Log.Info(Tag, " Download operation failure: " + e.Message); this.DisplayFailure(e); } }