Skip to content

Instantly share code, notes, and snippets.

@realBjornRoden
Last active October 13, 2019 13:26
Show Gist options
  • Save realBjornRoden/8a4339299ff2812fd5769eab66fcea8e to your computer and use it in GitHub Desktop.
Save realBjornRoden/8a4339299ff2812fd5769eab66fcea8e to your computer and use it in GitHub Desktop.

Cognitive Artificial Intelligence

  • Cloud Vendor Based NoOps

Use Cases

  1. Translation

GCP (Google Cloud Platform)

  • translate-docs
  • Prerequisites are to have a valid and activated GCP account and permissions to use API translate.googleapis.com or automl.googleapis.com cognitive services

  1. Create project; service account; download service account key file and enable API before-you-begin

  2. Authenticate CLI session with gcloud auth login

  3. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to point to the location of the service account key file

    export GOOGLE_APPLICATION_CREDENTIALS=$PWD/cognitive-aab254879251.json
    
  4. Check the currently active project

    $ gcloud config get-value project 
    bungabunga-123456
    
  5. Set the current project

    $ gcloud projects list
    PROJECT_ID          NAME                PROJECT_NUMBER
    cognitive-254305    cognitive           711533833686
    bungabunga-123456   bungabunga          400688388535
    
    $ gcloud config set project cognitive-254305
    Updated property [core/project].
    
    $ gcloud config get-value project 
    cognitive-254305
    
  6. List supported languages

    $ curl -sH "Authorization: Bearer "$(gcloud auth application-default print-access-token) "https://translation.googleapis.com/language/translate/v2/languages" | jq -r '.data.languages[].language' | pr -4 -t
    af		  gd		    lb		      sl
    am		  gl		    lo		      sm
    ar		  gu		    lt		      sn
    az		  ha		    lv		      so
    be		  haw		    mg		      sq
    bg		  hi		    mi		      sr
    bn		  hmn		    mk		      st
    bs		  hr		    ml		      su
    ca		  ht		    mn		      sv
    ceb		  hu		    mr		      sw
    co		  hy		    ms		      ta
    cs		  id		    mt		      te
    cy		  ig		    my		      tg
    da		  is		    ne		      th
    de		  it		    nl		      tl
    el		  iw		    no		      tr
    en		  ja		    ny		      uk
    eo		  jw		    pa		      ur
    es		  ka		    pl		      uz
    et		  kk		    ps		      vi
    eu		  km		    pt		      xh
    fa		  kn		    ro		      yi
    fi		  ko		    ru		      yo
    fr		  ku		    sd		      zh
    fy		  ky		    si		      zh-TW
    ga		  la		    sk		      zu
    

APIs

  • Enable API
  • Check if available, enable APIs and check if enabled, required: texttospeech.googleapis.com and speech.googleapis.com

$ gcloud services list --available --filter translate.googleapis.com
NAME                      TITLE
automl.googleapis.com     Cloud AutoML API
translate.googleapis.com  Cloud Translation API

$ gcloud services enable translate.googleapis.com
Operation "operations/acf.6920dcef-ef0d-4c40-b22c-e559bef6c4f4" finished successfully.

$ gcloud services list --enabled --filter translate.googleapis.com
NAME                      TITLE
translate.googleapis.com  Cloud Translation API

Translation

  • Create JSON formatted request file (request.json)
$ cat request.json 
{ "q": "�Ÿ�Ö�ÿ�±�ÿ�≠�ÿ�®�ÿ�ß �ÿ�®�ÿ�ß�Ÿ�Ñ�ÿ�π�ÿ�ß�Ÿ�Ñ�Ÿ�Ö", "source": "ar", "target": "en", "format": "text" }
  • Run curl to access the API
$ curl -s -X POST -H "Authorization: Bearer ya29.c.Kl6bB_jYUEsWxlO6Dw-XpAfZi5XGHia39RvgKYaVXWcbAClsGihy9OpLAa62bE-5SO4kTKu5a1-QZ3R3KM8yPVJa4frdsnk2MuZhRR6KtMQPjDEENgOvbCemGBOic7Mb" -H "Content-Type: application/json" -d @request.json https://translation.googleapis.com/language/translate/v2
  • Review the results from the JSON output file
$ jq '.data.translations[].translatedText' result26288.json
"Hello World"

$ jq '.' result26288.json                                  
{
  "data": {
    "translations": [
      {
        "translatedText": "Hello World"
      }
    ]
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment