Name:
interface
Value:
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.

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 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 using the latest Amplify Release. As new Xcode and visionOS versions are released, the support will be updated with any necessary fixes on a best effort basis.

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

  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.

import Amplify
import AWSCognitoAuthPlugin
import AWSPredictionsPlugin

Configure Amplify at app launch

@main
struct MyAmplifyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
init() {
do {
try Amplify.add(plugin: AWSCognitoAuthPlugin())
try Amplify.add(plugin: AWSPredictionsPlugin())
try Amplify.configure()
print("Amplify configured with Auth and Predictions plugins")
} catch {
print("Failed to initialize Amplify with \(error)")
}
}
}
}

Add to your AppDelegate's application:didFinishLaunchingWithOptions method

func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
do {
try Amplify.add(plugin: AWSCognitoAuthPlugin())
try Amplify.add(plugin: AWSPredictionsPlugin())
try Amplify.configure()
print("Amplify configured with Auth and Predictions plugins")
} catch {
print("Failed to initialize Amplify with \(error)")
}
return true
}

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

Amplify 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.

func translateText() async {
do {
let translatedText = try await Amplify.Predictions.convert(
.translateText(
"I like to eat spaghetti",
from: .english,
to: .spanish
)
)
print("Translated text: \(translatedText)")
} catch let error as PredictionsError {
print("Error translating text: \(error)")
} catch {
print("Unexpected error: \(error)")
}
}
Amplify.Publisher.create {
try await Amplify.Predictions.convert(
.translateText(
"I like to eat spaghetti!",
from: .english,
to: .spanish
)
)
}
.sink(receiveCompletion: { completion in
if case let .failure(error) = completion {
print("Error translating text: \(error)")
}
}, receiveValue: { value in
print("Translated text: \(value.text)")
})

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: