Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Oct 15, 2024

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.

Note: In the following example, we configure the policy to enable all supported ML capabilities. Ensure to include only the actions & resources relevant to your specific use cases. To learn more, check the docs of Amazon Translate, Amazon Polly, Amazon Transcribe, Amazon Rekognition, Amazon Textract, and Amazon Comprehend.

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 capabilities
backend.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

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 { Predictions } from "aws-amplify/predictions";
import outputs from "./amplify_outputs.json";
Amplify.configure(outputs);
Amplify.configure({
...Amplify.getConfig(),
Predictions: config.custom.Predictions,
});