Page updated Jan 16, 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.

On iOS, we leverage Apple’s Core ML Vision Framework and Natural Language Framework to improve accuracy as well as support cases where your device is unable to reach AWS Services. For more information, see each individual use case.

Goal

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

Prerequisites

An application with Amplify libraries integrated and a minimum target of any of the following:

  • iOS 13.0, using Xcode 14.1 or later.
  • macOS 10.15, using Xcode 14.1 or later.
  • tvOS 13.0, using Xcode 14.3 or later.
  • watchOS 9.0, using Xcode 14.3 or later.
  • visionOS 1.0, using Xcode 15 beta 2 or later. (Preview support - see below for more details.)

For a full example, please follow the project setup walkthrough.

visionOS support is currently in preview and can be used by targeting the visionos-preview branch. As new Xcode 15 beta versions are released, the branch will be updated with any necessary fixes on a best effort basis.

For more information on how to use the visionos-preview branch, see Platform Support.

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

  1. To install Amplify Libraries in your application, open your project in Xcode and select File > Add Packages....

  2. Enter the Amplify Library for Swift GitHub repo URL (https://github.com/aws-amplify/amplify-swift) into the search bar and click Add Package.

Note: Up to Next Major Version should be selected from the Dependency Rule dropdown.

  1. Lastly, choose Amplify and AWSPredictionsPlugin. Then click Add Package.

Initialize Amplify Predictions

To initialize the Amplify Predictions and Auth categories, use the Amplify.add(plugin:) method to add AWSCognitoAuthPlugin and AWSPredictionsPlugin. Next finish configuring Amplify by calling Amplify.configure().

Open the main file of your application - AppDelegate.swift or <YOUR_APP_NAME>App.swift - and add the following import statements to the top of the file.

1import Amplify
2import AWSCognitoAuthPlugin
3import AWSPredictionsPlugin

Configure Amplify at app launch

1@main
2struct MyAmplifyApp: App {
3 var body: some Scene {
4 WindowGroup {
5 ContentView()
6 }
7 }
8
9 init() {
10 do {
11 try Amplify.add(plugin: AWSCognitoAuthPlugin())
12 try Amplify.add(plugin: AWSPredictionsPlugin())
13 try Amplify.configure()
14 print("Amplify configured with Auth and Predictions plugins")
15 } catch {
16 print("Failed to initialize Amplify with \(error)")
17 }
18 }
19 }
20}

Add to your AppDelegate's application:didFinishLaunchingWithOptions method

1func application(
2 _ application: UIApplication,
3 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
4) -> Bool {
5
6 do {
7 try Amplify.add(plugin: AWSCognitoAuthPlugin())
8 try Amplify.add(plugin: AWSPredictionsPlugin())
9 try Amplify.configure()
10 print("Amplify configured with Auth and Predictions plugins")
11 } catch {
12 print("Failed to initialize Amplify with \(error)")
13 }
14
15 return true
16}

Upon building and running this application you should see the following in your console window:

1Amplify configured with Auth and Predictions plugins

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.

1func translateText() async {
2 do {
3 let translatedText = try await Amplify.Predictions.convert(
4 .translateText(
5 "I like to eat spaghetti",
6 from: .english,
7 to: .spanish
8 )
9 )
10 print("Translated text: \(translatedText)")
11 } catch let error as PredictionsError {
12 print("Error translating text: \(error)")
13 } catch {
14 print("Unexpected error: \(error)")
15 }
16}
1Amplify.Publisher.create {
2 try await Amplify.Predictions.convert(
3 .translateText(
4 "I like to eat spaghetti!",
5 from: .english,
6 to: .spanish
7 )
8 )
9}
10.sink(receiveCompletion: { completion in
11 if case let .failure(error) = completion {
12 print("Error translating text: \(error)")
13 }
14}, receiveValue: { value in
15 print("Translated text: \(value.text)")
16})

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: