Page updated Jan 16, 2024

Set up Predictions

Amplify Android v1 is now in Maintenance Mode until May 31st, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v1.

Please use the latest version (v2) of Amplify Library for Android to get started.

If you are currently using v1, follow these instructions to upgrade to v2.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for Android, you can access the documentation here.

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 16 (Android 4.1) 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:

1amplify add predictions

Enter the following when prompted:

1? Please select from one of the categories below
2 `Convert`
3? 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)
4 `Y`
5? Do you want to use the default authentication and security configuration?
6 `Default configuration`
7? How do you want users to be able to sign in?
8 `Email`
9? Do you want to configure advanced settings?
10 `No, I am done.`
11? What would you like to convert?
12 `Translate text into a different language`
13? Provide a friendly name for your resource
14 `transTextSample`
15? What is the source language?
16 `English`
17? What is the target language?
18 `Italian`
19? Who should have access?
20 ` 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:

1amplify 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:

1dependencies {
2 // Add these lines in `dependencies`
3 implementation 'com.amplifyframework:aws-predictions:ANDROID_V1_VERSION'
4 implementation 'com.amplifyframework:aws-auth-cognito:ANDROID_V1_VERSION'
5}

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:

1Amplify.addPlugin(new AWSCognitoAuthPlugin());
2Amplify.addPlugin(new AWSPredictionsPlugin());

Your class will look like this:

1public class MyAmplifyApp extends Application {
2 @Override
3 public void onCreate() {
4 super.onCreate();
5
6 try {
7 // Add these lines to add the AWSCognitoAuthPlugin and AWSPredictionsPlugin plugins
8 Amplify.addPlugin(new AWSCognitoAuthPlugin());
9 Amplify.addPlugin(new AWSPredictionsPlugin());
10 Amplify.configure(getApplicationContext());
11
12 Log.i("MyAmplifyApp", "Initialized Amplify");
13 } catch (AmplifyException error) {
14 Log.e("MyAmplifyApp", "Could not initialize Amplify", error);
15 }
16 }
17}
1Amplify.addPlugin(AWSCognitoAuthPlugin())
2Amplify.addPlugin(AWSPredictionsPlugin())

Your class will look like this:

1class MyAmplifyApp : Application() {
2 override fun onCreate() {
3 super.onCreate()
4
5 try {
6 // Add these lines to add the AWSCognitoAuthPlugin and AWSPredictionsPlugin plugins
7 Amplify.addPlugin(AWSCognitoAuthPlugin())
8 Amplify.addPlugin(AWSPredictionsPlugin())
9 Amplify.configure(applicationContext)
10
11 Log.i("MyAmplifyApp", "Initialized Amplify")
12 } catch (error: AmplifyException) {
13 Log.e("MyAmplifyApp", "Could not initialize Amplify", error)
14 }
15 }
16}
1RxAmplify.addPlugin(new AWSCognitoAuthPlugin());
2RxAmplify.addPlugin(new AWSPredictionsPlugin());

Your class will look like this:

1public class MyAmplifyApp extends Application {
2 @Override
3 public void onCreate() {
4 super.onCreate();
5
6 try {
7 // Add these lines to add the AWSCognitoAuthPlugin and AWSPredictionsPlugin plugins
8 RxAmplify.addPlugin(new AWSCognitoAuthPlugin());
9 RxAmplify.addPlugin(new AWSPredictionsPlugin());
10 RxAmplify.configure(getApplicationContext());
11
12 Log.i("MyAmplifyApp", "Initialized Amplify");
13 } catch (AmplifyException error) {
14 Log.e("MyAmplifyApp", "Could not initialize Amplify", error);
15 }
16 }
17}

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():

1Amplify.Predictions.translateText(
2 "I like to eat spaghetti",
3 LanguageType.ENGLISH,
4 LanguageType.SPANISH,
5 result -> Log.i("MyAmplifyApp", result.getTranslatedText()),
6 error -> Log.e("MyAmplifyApp", "Translation failed", error)
7);

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

1Amplify.Predictions.translateText(
2 "I like to eat spaghetti", LanguageType.ENGLISH, LanguageType.SPANISH,
3 { Log.i("MyAmplifyApp", it.translatedText) }
4 { Log.e("MyAmplifyApp", "Translation failed", it) }
5)

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

1val text = "I like to eat spaghetti"
2try {
3 val result = Amplify.Predictions.translateText(text, ENGLISH, SPANISH)
4 Log.i("MyAmplifyApp", result.translatedText)
5} catch (error: PredictionsException) {
6 Log.e("MyAmplifyApp", "Translation failed", error)
7}

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

1RxAmplify.Predictions.translateText(
2 "I like to eat spaghetti",
3 LanguageType.ENGLISH,
4 LanguageType.SPANISH)
5 .subscribe(
6 result -> Log.i("MyAmplifyApp", result.getTranslatedText()),
7 error -> Log.e("MyAmplifyApp", "Translation failed", error)
8 );

Next, build and run the application.

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

1Me 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: