OpenNMT Forum

Machine Translation Web Interface

You might want to build a GUI for your Machine Translation model to show to a client or to be able to translate sentences online. I have created a simple web interface for OpenNMT which depends on Python Flask and Flask-PageDown libraries. To be able to use the web interface, you need first to implement an OpenNMT Rest API Server as explained by Paul Tardy
at: Simple OpenNMT-py REST server.

You can find the code of the machine translation web interface at:

If you have questions, please let me know.

Kind regards,
Yasmin

3 Likes

Thanks for sharing and will give it a try…

You are welcome, Liu!

aha, good job.
I am doing the same thing with PHP for GUI, and using CURL to connect with the Rest API Server.

But, the point is how to clear/normalize/tokenize the text from the Teaxtare submitted from the UI?

Dear Yaren,

You can choose a tokenizer in the JSON configuration file under the tokenizer part, either sentencepiece or pyonmttok

You can check this post for more details and examples: Simple OpenNMT-py REST server

If something is not covered by the tokenizer, you can apply your own pre-processing code on the text entered in the textarea, and even later your post-processing code on the text received from the translation server. As you know, you can put the text you receive in either cases in a string variable and apply any code on this variable.

I hope this helps.

Kind regards,
Yasmin

I have used and slightly adapted Yasmin’s interface which works very nicely. It’s a great contribution. It can also be seen in action at our free NMT service at www.nmtgateway.com.

Hello Telerence,
For your UI do you have a github account where you have shared the changes you made in building the UI?

Sorry, I didn’t share them personally but I think @ymoslem’s latest version now incorporates them as we have discussed this.

ok, thanks.

even later your post-processing code on the text received from the translation server. can yougive an exxample of what to put here instead of getting this error "raise JSONDecodeError(“Expecting value”, s, err.value) from None
"

Hi Sheila! I wonder why you receive None in the first place. Does your server work in the Terminal, outside of the code? I mean try to test it first in your Terminal/CMD using the command line mentioned at: Simple OpenNMT-py REST server

Hi, Thank you for sharing.

I’ve implemented the REST server mentioned above,
but I don’t have a clue how to run the web interface.
Do I just have to run the server and do “python mt.py” in another server?
Which website do I have to go to check the interface?

Thanks alot.

Hello! @SoYoungCho

Deploying a Python app requires some study per se. I use DigitalOcean, and I have an Apache HTTP Server on it; so I found this tutorial useful: Minimal Apache configuration for deploying a flask app (Ubuntu 18.04)

However, if you are just doing a quick assignment or a temporary test, you can use something like PythonAnywhere or Heroku. The main reason why I did not use PythonAnywhere is that, even with a paid plan, it has a time limitation for running scripts, which does not help for a REST server as you need it to be running 24/7. I do not know about Heroku with this regard.

It is worth mentioning as well that free plans of any server have space limitations. Recently, a colleague contacted me asking how reduce the size of their model, and the answer was to use Model Averaging, which can reduce the size of a model to 1/3 of its original size; for OpenNMT-py, there is a script called average_models.py under the “tools” folder.

Kind regards,
Yasmin

Hi! I’m John, I’ve already made an account for this forum. I saw your answer to @SoYoungCho, and I’ve the same problem as you know Yasmin… I don’t understand the apache server part, if I mount that, how am I supposed to connect that server with for example heroku?.
It would be so wonderfull if there were a tutorial to deploy our rest server to production…

Best regards!!

Hi John,

As I mentioned, I am using DigitalOcean, that is why I am having an Apache server. However, if you are using PythonAnywhere or Heroku, they are prepared to deploy Python apps directly.

You will run the command from the Terminal/command line:
python3 OpenNMT-py/server.py --ip "0.0.0.0" --port 5000 --url_root "/translator" --config available_models/conf.json

In the mt.py Python file, do not change the URL that I provided; it is:
url = "http://127.0.0.1:5000/translator/translate"

Now, you use the mt.py file (with the “templates” folders I provided) as your Heroku app. This official tutorial, explains how to deploy a Python app on Heroku.

All the best,
Yasmin

Thanks to @ymoslem’s code, I started from there.
Here is a link to a live demo of a web interface: https://nartlinux.pythonanywhere.com/
The code is located here: https://github.com/danielinux7/Multilingual-Parallel-Corpus/tree/master/gui

1 Like

Hello Nart, thank you!,
Only one question: can you explain how did you deploy it in pythonanywhere?

Hello John,
The code is not generic yet, so you will have to make changes to index.html, form.js and process.py to fit your needs.
I am using the free hosting plan so there are some limitations, but here are the steps as I recall:

pip install flask ctranslate2 sentencepiece mosestokenizer

  • Reload your website, and that should be it.
    Let me know if you come across some issues.

** Notes:
To build your ctranslate2 model from a checkpoint:

pip install ctranslate2
ct2-opennmt-tf-converter --model_path run/ --model_spec TransformerBaseRelative --output_dir ctranslate_model --src_vocab src-vocab.txt --tgt_vocab tgt-vocab.txt --quantization int8

1 Like

Thank you so much for your help Nart, you are so kind.

When I try it I will notice you the results.

1 Like