Set up Predictions
To enable Predictions we need to set up the appropriate IAM policy for Roles in your Cognito Identity Pool in order to use an appropriate feature. Additionally, we need to use the addOutput
method to patch the custom Predictions resource to the expected output configuration.
amplify/backend.ts
import { PolicyStatement } from "aws-cdk-lib/aws-iam";import { defineBackend } from "@aws-amplify/backend";import { auth } from "./auth/resource";
const backend = defineBackend({ auth,});
// Configure a policy for the required use case.// The actions included below cover all supported ML capabilitiesbackend.auth.resources.unauthenticatedUserIamRole.addToPrincipalPolicy( new PolicyStatement({ actions: [ "translate:TranslateText", "polly:SynthesizeSpeech", "transcribe:StartStreamTranscriptionWebSocket", "comprehend:DetectSentiment", "comprehend:DetectEntities", "comprehend:DetectDominantLanguage", "comprehend:DetectSyntax", "comprehend:DetectKeyPhrases", "rekognition:DetectFaces", "rekognition:RecognizeCelebrities", "rekognition:DetectLabels", "rekognition:DetectModerationLabels", "rekognition:DetectText", "rekognition:DetectLabel", "rekognition:SearchFacesByImage", "textract:AnalyzeDocument", "textract:DetectDocumentText", "textract:GetDocumentAnalysis", "textract:StartDocumentAnalysis", "textract:StartDocumentTextDetection", ], resources: ["*"], }));
backend.addOutput({ custom: { Predictions: { convert: { translateText: { defaults: { sourceLanguage: "en", targetLanguage: "es", }, proxy: false, region: backend.auth.stack.region, }, speechGenerator: { defaults: { voiceId: "Ivy", }, proxy: false, region: backend.auth.stack.region, }, transcription: { defaults: { language: "en-US", }, proxy: false, region: backend.auth.stack.region, }, }, identify: { identifyEntities: { defaults: { collectionId: "default", maxEntities: 10, }, celebrityDetectionEnabled: true, proxy: false, region: backend.auth.stack.region, }, identifyLabels: { defaults: { type: "ALL", }, proxy: false, region: backend.auth.stack.region, }, identifyText: { defaults: { format: "ALL", }, proxy: false, region: backend.auth.stack.region, }, }, interpret: { interpretText: { defaults: { type: "ALL", }, proxy: false, region: backend.auth.stack.region, }, }, }, },});
Install Amplify Libraries
To install the Amplify library to use predictions features, run the following commands in your project's root folder:
Terminal
npm add aws-amplify @aws-amplify/predictions
Configure the frontend
Import and load the configuration file in your app. It is recommended you add the Amplify configuration step to your app's root entry point. For example main.ts
in React and Angular.
src/main.ts
import { Amplify } from "aws-amplify";import outputs from "./amplify_outputs.json";
Amplify.configure(outputs);
Amplify.configure({ ...Amplify.getConfig(), Predictions: outputs.custom.Predictions,});