Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Apr 29, 2024

Set up Predictions

The Predictions category enables you to integrate machine learning into your application without any prior machine learning experience. It supports translating text from one language to another, converting text to speech, text recognition from an image, entities recognition, labeling real world objects, interpretation of text, and uploading images for automatic training. This functionality is powered by AWS services including: Amazon Translate, Amazon Polly, Amazon Transcribe, Amazon Rekognition, Amazon Textract, and Amazon Comprehend.

Goal

To setup and configure your application with Amplify Predictions and go through a simple example of translating text.

Prerequisites

  • An Android application targeting Android API level 24 (Android 7.0) or above

Provision Backend Services

To use the predictions category you will need to setup the auth backend resources. To provision auth resources in the backend, go to your project directory and execute the command:

amplify add predictions

Enter the following when prompted:

? Please select from one of the categories below
`Convert`
? You need to add auth (Amazon Cognito) to your project in order to add storage for user files. Do you want to add auth now? (Y/n)
`Y`
? Do you want to use the default authentication and security configuration?
`Default configuration`
? How do you want users to be able to sign in?
`Email`
? Do you want to configure advanced settings?
`No, I am done.`
? What would you like to convert?
`Translate text into a different language`
? Provide a friendly name for your resource
`transTextSample`
? What is the source language?
`English`
? What is the target language?
`Italian`
? Who should have access?
` Auth and Guest users`

Note that the languages selected during this stage will be the default language your app will translate to/from. These source and target languages can be overridden when you write the code in your application.

To push your change to the cloud, execute the command:

amplify push

Upon completion, amplifyconfiguration.json will be updated to reference the newly provisioned backend resources.

Install Amplify Libraries

Expand Gradle Scripts, open build.gradle (Module :app). You will already have configured Amplify by following the steps in the Project Setup walkthrough.

Add Predictions by adding these libraries into the dependencies block:

dependencies {
// Add these lines in `dependencies`
implementation 'com.amplifyframework:aws-predictions:ANDROID_VERSION'
implementation 'com.amplifyframework:aws-auth-cognito:ANDROID_VERSION'
}

aws-auth-cognito provides authentication for the backend services used by aws-predictions.

Click Sync Now.

Initialize Amplify Predictions

To initialize the Amplify Predictions and Authentication categories you call Amplify.addPlugin() method for each category. To complete initialization call Amplify.configure().

Add the following code to your onCreate() method in your application class:

import android.util.Log;
import com.amplifyframework.AmplifyException;
import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin;
import com.amplifyframework.core.Amplify;
import com.amplifyframework.predictions.aws.AWSPredictionsPlugin;
Amplify.addPlugin(new AWSCognitoAuthPlugin());
Amplify.addPlugin(new AWSPredictionsPlugin());

Your class will look like this:

public class MyAmplifyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
try {
// Add these lines to add the AWSCognitoAuthPlugin and AWSPredictionsPlugin plugins
Amplify.addPlugin(new AWSCognitoAuthPlugin());
Amplify.addPlugin(new AWSPredictionsPlugin());
Amplify.configure(getApplicationContext());
Log.i("MyAmplifyApp", "Initialized Amplify");
} catch (AmplifyException error) {
Log.e("MyAmplifyApp", "Could not initialize Amplify", error);
}
}
}
import android.util.Log
import com.amplifyframework.AmplifyException
import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin
import com.amplifyframework.core.Amplify
import com.amplifyframework.predictions.aws.AWSPredictionsPlugin
Amplify.addPlugin(AWSCognitoAuthPlugin())
Amplify.addPlugin(AWSPredictionsPlugin())

Your class will look like this:

class MyAmplifyApp : Application() {
override fun onCreate() {
super.onCreate()
try {
// Add these lines to add the AWSCognitoAuthPlugin and AWSPredictionsPlugin plugins
Amplify.addPlugin(AWSCognitoAuthPlugin())
Amplify.addPlugin(AWSPredictionsPlugin())
Amplify.configure(applicationContext)
Log.i("MyAmplifyApp", "Initialized Amplify")
} catch (error: AmplifyException) {
Log.e("MyAmplifyApp", "Could not initialize Amplify", error)
}
}
}
import android.util.Log;
import com.amplifyframework.AmplifyException;
import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin;
import com.amplifyframework.predictions.aws.AWSPredictionsPlugin;
import com.amplifyframework.rx.RxAmplify;
RxAmplify.addPlugin(new AWSCognitoAuthPlugin());
RxAmplify.addPlugin(new AWSPredictionsPlugin());

Your class will look like this:

public class MyAmplifyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
try {
// Add these lines to add the AWSCognitoAuthPlugin and AWSPredictionsPlugin plugins
RxAmplify.addPlugin(new AWSCognitoAuthPlugin());
RxAmplify.addPlugin(new AWSPredictionsPlugin());
RxAmplify.configure(getApplicationContext());
Log.i("MyAmplifyApp", "Initialized Amplify");
} catch (AmplifyException error) {
Log.e("MyAmplifyApp", "Could not initialize Amplify", error);
}
}
}

Note that because the predictions category requires auth, you will need to either configure guest access or sign in a user before using features in the predictions category.

Translating text

To translate text from one language to another, specify the text you want translated, a source language, and a target language. The source and target language parameters will override any choice you made while adding this resource using the Amplify CLI.

Open MainActivity.java and add the following to the bottom of onCreate():

import android.util.Log;
import com.amplifyframework.core.Amplify;
import com.amplifyframework.predictions.models.LanguageType;
Amplify.Predictions.translateText(
"I like to eat spaghetti",
LanguageType.ENGLISH,
LanguageType.SPANISH,
result -> Log.i("MyAmplifyApp", result.getTranslatedText()),
error -> Log.e("MyAmplifyApp", "Translation failed", error)
);

Open MainActivity.kt and add the following to the bottom of onCreate():

import android.util.Log
import com.amplifyframework.core.Amplify
import com.amplifyframework.predictions.models.LanguageType
Amplify.Predictions.translateText(
"I like to eat spaghetti", LanguageType.ENGLISH, LanguageType.SPANISH,
{ Log.i("MyAmplifyApp", it.translatedText) },
{ Log.e("MyAmplifyApp", "Translation failed", it) }
)

Open MainActivity.kt and add the following to the bottom of onCreate():

import android.util.Log
import com.amplifyframework.kotlin.core.Amplify
import com.amplifyframework.predictions.models.LanguageType
val text = "I like to eat spaghetti"
try {
val result = Amplify.Predictions.translateText(text, ENGLISH, SPANISH)
Log.i("MyAmplifyApp", result.translatedText)
} catch (error: PredictionsException) {
Log.e("MyAmplifyApp", "Translation failed", error)
}

Open MainActivity.java and add the following to the bottom of onCreate():

import android.util.Log;
import com.amplifyframework.predictions.models.LanguageType;
import com.amplifyframework.rx.RxAmplify;
RxAmplify.Predictions.translateText(
"I like to eat spaghetti",
LanguageType.ENGLISH,
LanguageType.SPANISH)
.subscribe(
result -> Log.i("MyAmplifyApp", result.getTranslatedText()),
error -> Log.e("MyAmplifyApp", "Translation failed", error)
);

Next, build and run the application.

As a result of executing this code, you will see the following printed to your console:

Me gusta comer espaguetis

Next steps

Congratulations! You've translated text from one language to another. Check out the following links to explore other Amplify Predictions use cases: