Skip to content

Instantly share code, notes, and snippets.

@realBjornRoden
Created October 13, 2019 12:59
Show Gist options
  • Select an option

  • Save realBjornRoden/0afcfe61247efed998e937af4beb2537 to your computer and use it in GitHub Desktop.

Select an option

Save realBjornRoden/0afcfe61247efed998e937af4beb2537 to your computer and use it in GitHub Desktop.

Cognitive Artificial Intelligence

  • Cloud Vendor Based NoOps

Use Cases

  1. Translation

AWS (Amazon Web Services)

  1. Prepare to configure AWS CLI
    NB. Do not use the AWS account root user access key. The access key for the AWS account root user gives full access to all resources for all AWS services, including billing information. The permissions cannot be reduce for the AWS account root user access key.
    1. Create a GROUP in the Console, such as cognitive, and assign TranslateFullAccess as Policy create-admin-group
      Select one or more policies to attach. Each group can have up to 10 policies attached.
    2. Create a USER in the Console, such as aiuser, assign it to the GROUP, and save the credentials.csv file (store and keep it secret) create-admin-user
    3. Set a PASSWORD for the user aws-password
  2. Run the aws configure command to configure the AWS CLI using the keys for the USER (aiuser)
    NB. The command prompts for: access key, secret access key, AWS Region, and output format; stores this in a profile ("default"), this is used when running an AWS CLI command without explicitly specify another profile.
    $ aws configure list
          Name                    Value             Type    Location
          ----                    -----             ----    --------
       profile                <not set>             None    None
    access_key     ****************MYVZ shared-credentials-file    
    secret_key     ****************nEac shared-credentials-file    
        region                <not set>             None    None
    
  • Create S3 Bucket
    • In this case the bucket is named blobbucket and set to private, with LocationConstraint set to the specified region
    $ aws s3api create-bucket --bucket blobbucket --acl private --region us-east-2 --create-bucket-configuration LocationConstraint=us-east-2
    http://blobbucket.s3.amazonaws.com/
    
    • Upload files to the S3 Bucket (s3 and s3api commands)
    $ aws s3 cp --recursive ../data/ s3://blobbucket/
    
    $ aws s3api put-object --bucket blobbucket --key texttyped1.png --body ../data/texttyped1.png --acl private
    
    • List objects (files) in the S3 Bucket (s3 and s3api commands)
    $ aws s3 ls s3://blobbucket
    
    $ aws s3api list-objects --bucket blobbucket --query 'Contents[].{Key: Key}' | jq -r '.[].Key'
    
    • Trying to access this bucket over HTTP without authenticating is denied
    <Error>
          <Code>AccessDenied</Code>
          <Message>Access Denied</Message>
          <RequestId>090832BE4B92F4DC</RequestId>
       <HostId>
          27Ec+Sx6rPwGJFpWIQ4ktZrdlG5m710m+yUKjXJ9IfWE3GWXde6e2OdaY0OdKnV6Y3NEUSOI4iw=
       </HostId>
    </Error>
    

Translation

  • Shorter source text - Run aws translate translate-text directly
$ aws translate translate-text --region us-east-2 --source-language-code "en" --target-language-code "ar" --text "Hello World" > result-1.json

$ jq '.SourceLanguageCode,.TargetLanguageCode,.TranslatedText' result-1.json

$ aws translate translate-text --region us-east-2 --source-language-code "ar" --target-language-code "en" --text "$(jq -r '.TranslatedText' result-1.json)" | tee result-2.json
{
    "TranslatedText": "Hello World",
    "SourceLanguageCode": "ar",
    "TargetLanguageCode": "en"
}

$ jq -r '.TranslatedText' result-1.json | base64
2YXYsdit2KjYpyDZiNmI2LHZhNivCg==

$ jq -r '.TranslatedText' result-1.json | base64 --decode
مرحبا وورلد
  • Longer source text - Create JSON formatted request file (request.json)

$ cat <<-EOD > request.json
	{ "SourceLanguageCode": "en", "TargetLanguageCode": "ar", "Text": "Hello World" }
EOD

$ cat request.json
{ "SourceLanguageCode": "en", "TargetLanguageCode": "ar", "Text": "Hello World" }
  • Submit the job (input: JSON file "request.json"; output: JSON file "result$RANDOM.json)
$ aws translate translate-text --region us-east-2 --cli-input-json file://request.json | tee result$RANDOM.json | jq -r '.TranslatedText' |base64
2YXYsdit2KjYpyDZiNmI2LHZhNivCg==
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment