How to convert REST API of custom model from AutoML Translation into Autohotkey script
Thread poster: Michael79
Michael79
Michael79  Identity Verified
China
Local time: 23:53
English to Chinese
+ ...
Jul 30, 2018

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 Translat
... See more
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!
Collapse


 
Samuel Murray
Samuel Murray  Identity Verified
Netherlands
Local time: 16:53
Member (2006)
English to Afrikaans
+ ...
@Michael Jul 30, 2018

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
Michael79  Identity Verified
China
Local time: 23:53
English to Chinese
+ ...
TOPIC STARTER
thanks my mentor Jul 30, 2018

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 prov
... See more
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]
Collapse


 
Samuel Murray
Samuel Murray  Identity Verified
Netherlands
Local time: 16:53
Member (2006)
English to Afrikaans
+ ...
@Michael Jul 31, 2018

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
Michael79  Identity Verified
China
Local time: 23:53
English to Chinese
+ ...
TOPIC STARTER
thanks again Jul 31, 2018

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






Trados Business Manager Lite
Create customer quotes and invoices from within Trados Studio

Trados Business Manager Lite helps to simplify and speed up some of the daily tasks, such as invoicing and reporting, associated with running your freelance translation business.

More info »
Protemos translation business management system
Create your account in minutes, and start working! 3-month trial for agencies, and free for freelancers!

The system lets you keep client/vendor database, with contacts and rates, manage projects and assign jobs to vendors, issue invoices, track payments, store and manage project files, generate business reports on turnover profit per client/manager etc.

More info »