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

Amplify iOS v1 is deprecated as of June 1st, 2024. No new features or bug fixes will be added. Dependencies may become outdated and potentially introduce compatibility issues.

Please use the latest version (v2) of Amplify Library for Swift to get started. Refer to the upgrade guide for instructions on upgrading your application to the latest version.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for iOS, 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.

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 iOS application targeting at least iOS 13.0 with Amplify libraries integrated

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

To install the libraries required to translating text, add both AWSPredictionsPlugin and CoreMLPredictionsPlugin to your Podfile. Your Podfile should look similar to:

target 'MyAmplifyApp' do
use_frameworks!
pod 'Amplify'
pod 'AmplifyPlugins/AWSCognitoAuthPlugin'
pod 'AWSPredictionsPlugin'
pod 'CoreMLPredictionsPlugin'
end

To install, download and resolve these pods, execute the command:

pod install --repo-update

Now you can open your project by opening the .xcworkspace file using the following command:

xed .

Initialize Amplify Predictions

To initialize the Amplify Predictions and Authentication categories, you are required to use the Amplify.add() method for each category you want. When you are done calling add() on each category, you finish configuring Amplify by calling Amplify.configure().

Add the following imports to the top of your AppDelegate.swift file:

import Amplify
import AmplifyPlugins
import AWSPredictionsPlugin

Add the following code

Create a custom AppDelegate, and add to your application:didFinishLaunchingWithOptions method

class AppDelegate: NSObject, UIApplicationDelegate {
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
}
}

Then in the App scene, use UIApplicationDelegateAdaptor property wrapper to use your custom AppDelegate

@main
struct MyAmplifyApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}

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() {
Amplify.Predictions.convert(textToTranslate: "I like to eat spaghetti",
language: .english,
targetLanguage: .spanish,
listener: { (event) in
switch event {
case .success(let result):
print(result.text)
case .failure(let error):
print("Error: \(error)")
}
})
}
func translateText() -> AnyCancellable {
Amplify.Predictions.convert(
textToTranslate: "I like to eat spaghetti",
language: .english,
targetLanguage: .spanish
)
.resultPublisher
.sink {
if case let .failure(error) = $0 {
print("Error: \(error)")
}
}
receiveValue: { result in
print(result.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: