How to convert REST API of custom model from AutoML Translation into Autohotkey script
Thread poster: Michael79

Michael79  Identity Verified
China
Local time: 01:34
Member (2009)
English to Chinese
+ ...
Jul 30

I have an AHK script that helps me to use Google Cloud Translation API more easily. To be more specific, I have converted this AHK script into exe file so that whenever I want to leverage Google Translate API all I need to do is opening the exe file,select source texts and then press ctrl+j and the source text will be translated into target language automatically without bothering to copy everything into the webpage of Google Translate.

Given Google has launched its AutoML Translation and I managed to train my own machine translation model recently but the problem is:

Google has posted REST API of my MT model as follows:

Execute the request
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://automl.googleapis.com/v1beta1/projects/"my project name"/locations/us-central1/models/"a string of project digits":predict \
-d @request.json


My question is that how can I convert this REST API into another AHK script so that I can use my own machine translation engine in any text editor?

Thanks much in advance!


 

Samuel Murray  Identity Verified
Netherlands
Local time: 19:34
Member (2006)
English to Afrikaans
+ ...
@Michael Jul 30

Michael79 wrote:
I have an AHK script that helps me to use Google Cloud Translation API more easily.


1. Do you also use curl in that script (e.g. curl.exe)?
2. Does Google provide a non-curl method of querying the trained model?

Execute the request
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://automl.googleapis.com/v1beta1/projects/"my project name"/locations/us-central1/models/"a string of project digits":predict \
-d @request.json


It seems that you'd have to save the source text to a file named "request.json" and then send it using curl.exe. Do you know what the required format of the JSON file is?

Obviously the exact syntax of that curl command will also depend on which version of curl.exe you are using (and possibly also whether you are using it on Windows or Linux), but hopefully your preferred version of curl.exe has a user guide.

I wonder if it would be possible to send the request by replacing @request.json with the actual source text (either formatted in JSON format, or just raw), obviously enclosed in quotes.

The request syntax that you mention mentions a bearer and a token. Do you have to use curl.exe before this query to get/generate a token and then use that token in this request, or can you simply use your API key as the "token"?


 

Michael79  Identity Verified
China
Local time: 01:34
Member (2009)
English to Chinese
+ ...
TOPIC STARTER
thanks my mentor Jul 30

Hi Samuel, glad to see your reply. I remember it is you who sent me an AHK script to facilitate process of using Google Translate API while the one I am using now is some kind of advanced version.

I can send the codes to you if you like.

To respond your questions+

1. Do you also use curl in that script (e.g. curl.exe)?
No for this case. I mean the script itself is an ahk file and I converted into some exe file in Win7.

2. Does Google provide a non-curl method of querying the trained model?

Python.

The detailed information as follow

predict.py

import sys

from google.cloud import automl_v1beta1
from google.cloud.automl_v1beta1.proto import service_pb2


def get_prediction(content, project_id, model_id):
prediction_client = automl_v1beta1.PredictionServiceClient()

name = 'projects/{}/locations/us-central1/models/{}'.format(project_id, model_id)
payload = {'text_snippet': {'content': content}}
params = {}
request = prediction_client.predict(name, payload, params)
return request # waits till request is returned

if __name__ == '__main__':
file_path = sys.argv[1]
project_id = sys.argv[2]
model_id = sys.argv[3]

with open(file_path, 'rb') as ff:
content = ff.read()

print get_prediction(content, project_id, model_id)

Execute the request
python predict.py 'YOUR_SOURCE_TEXT_FILE' project.id model.id

[Edited at 2018-07-30 12:20 GMT]


 

Samuel Murray  Identity Verified
Netherlands
Local time: 19:34
Member (2006)
English to Afrikaans
+ ...
@Michael Jul 31

Michael79 wrote:
I remember it is you who sent me an AHK script to facilitate process of using Google Translate API while the one I am using now is some kind of advanced version.


My Google Translate scripts are written in AutoIt, not AutoHotKey.

I was very lucky to be able to figure out how it works, though. I also tried to write a script for Bing's translator, but Google's system was infinitely simpler to figure out. The fact that AutoML requires a token and a bearer is disconcerting -- it may be the thing that stops you from getting it to work.

2. Does Google provide a non-curl method of querying the trained model?
Python.


I know nothing of Python, unfortunately.

I suggest you (a) find a version of curl that works on your computer and learn how it works (e.g. should you use single or double quotes, i.e. Windows vs Linux dialects) and (b) figure out what the format of the JSON file should be. Then you could add the command in your previous mail to a BAT file and run it to see what it does. You may have to add "> file.txt" or something like that so that it writes the returned data to a file for you.



[Edited at 2018-07-31 07:34 GMT]


 

Michael79  Identity Verified
China
Local time: 01:34
Member (2009)
English to Chinese
+ ...
TOPIC STARTER
thanks again Jul 31

Your advice is much appreciated.

I will give it a try.

Have a nice day and sorry for my mistake. Your script is written in AutoIt not AHK.


 


To report site rules violations or get help, contact a site moderator:


You can also contact site staff by submitting a support request »

How to convert REST API of custom model from AutoML Translation into Autohotkey script

Advanced search






SDL MultiTerm 2019
Guarantee a unified, consistent and high-quality translation with terminology software by the industry leaders.

SDL MultiTerm 2019 allows translators to create one central location to store and manage multilingual terminology, and with SDL MultiTerm Extract 2019 you can automatically create term lists from your existing documentation to save time.

More info »
Anycount & Translation Office 3000
Translation Office 3000

Translation Office 3000 is an advanced accounting tool for freelance translators and small agencies. TO3000 easily and seamlessly integrates with the business life of professional freelance translators.

More info »



Forums
  • All of ProZ.com
  • Term search
  • Jobs
  • Forums
  • Multiple search