How to get response from the running Server.py at port 7790

Hi,

I have a server.py running at the port number 7790 :
administrator@:~/OpenNMT/OpenNMT-py$ python3 server.py --ip “0.0.0.0” --port “7790” --url_root “/translator” --config "./available_models/conf_pyonmttok.json"
Pre-loading model 1
[2019-07-30 15:58:13,899 INFO] Loading model 1
[2019-07-30 15:58:23,680 INFO] Loading tokenizer
Pre-loading model 0

  • Serving Flask app “server” (lazy loading)
  • Environment: production
    WARNING: Do not use the development server in a production environment.
    Use a production WSGI server instead.
  • Debug mode: off
    [2019-07-30 15:58:23,806 INFO] * Running on http://0.0.0.0:7790/ (Press CTRL+C to quit)

The client for getting response for GER-ENG translation is :
import requests

def translate_de_en():
** url = “http://0.0.0.0:7790/translator/translate”**
** ans = ‘y’**
** while ans is ‘y’ :**
** source = input("Enter the sentence ==> ")**

** if not source:**
** #print("")**
** return “”**

** headers = {‘content-type’:‘application/json’}**

** params = [{“src”:source, “id”:1}]**

** response = requests.post(url, json=params, headers=headers)**

** target = response.json()**

** print(target[0][0][“tgt”])**

** ans = input(“Do you want to continue: y or n ?”)**

if name == “main”:
** translate_de_en()**

When I run the client for getting the response, I get the following error message:

administrator@~/OpenNMT$ python3 translate_de_en.py
Enter the sentence ==> Hallo Welt
Traceback (most recent call last):
File “translate_de_en.py”, line 30, in
translate_de_en()
File “translate_de_en.py”, line 23, in translate_de_en
print(target[0][0][“tgt”])
KeyError: 0
administrator@~/OpenNMT$

Please guide me what part of response should be taken for printing the output text.

Thank You,
Kishor.

Dear Kishor,

What do you get when you print(target) only? Printing “target” (without keys) should show you the error message.

Kind regards,
Yasmin

Dear Yasmin,

I get the following error message when I print only target:
administrator@~/OpenNMT$ python3 translate_de_en.py
Enter the sentence ==> Hallo Welt
{‘error’: “Error: ‘TextMultiField’ object has no attribute ‘is_target’”, ‘status’: ‘error’}
Do you want to continue: y or n ?

But when I print response I get the following message:
administrator@~/OpenNMT$ python3 translate_de_en.py
Enter the sentence ==> Hallo Welt
<Response [200]>
Do you want to continue: y or n ?

Regards,
Kishor.

Dear Kishor,

According to this issue, it seems it has to do with the version of PyTorch/TorchText.

Try to run this command: pip3 install --upgrade -r requirements.txt

Kind Regards,
Yasmin

Dear Yasmin,

  Thank you very much for the solution. It worked. Thanks a lot.

I am receiving < unk > as the response for translated sentences from GERMAN to ENGLISH.
Please find the below response :

**administrator@:~/OpenNMT$ python3 translate_de_en.py **
Enter the sentence ==> Hallo Welt
< unk > < unk > < unk > < unk >
Do you want to continue: y or n ?

How do I go ahead resolving this < unk > issue?

Please assist me in resolving this issue as early as possible .

Thank You,
Kishor.

Dear Kishor,

Is it the pre-trained model from the website, or your own model? The pre-trained model is limited, you need to train your own model. If it is your model, how many sentences did you use?

For now, to test it, try other sentences.

Kind regards,
Yasmin

Dear Yasmin,

Thank you very much for the reply.
It is pre-trained model from the website. Since this is for POC, I thought I could
use the existing model before I start training my own model.

I tested other sentences, the output is as follows:

**administrator@:~/OpenNMT$ python3 translate_de_en.py **
Enter the sentence ==> Hallo Welt
< unk > < unk > < unk > < unk >
Do you want to continue: y or n ?y
Enter the sentence ==> Wie geht es dir
< unk > is going to go to you .
Do you want to continue: y or n ?y
Enter the sentence ==> Ich Kann dich heute nicht treffen.
< unk > won 't meet you today .
Do you want to continue: y or n ?
It is not possible to translate even a single sentence properly.

Is there a workaround for this so that sentences are atleast translated decently ?

Thank You,
Kishor.

Dear Kishor,

First of all, you have to lower-case the source sentence before passing to the model. I guess this should solve most of the cases you mentioned.

An extra solution will be to use the -replace_unk option with the -phrase_table option as explained here and here.

Kind regards,
Yasmin

Dear Yasmin,

  The resolved issue has reappeared. As per the issue, you had posted the resolution as to do with the 

version of Pytorch/TorchText.
The issue was that I was getting the following error message when I print target:
I get the following error message when I print only target:
administrator@~/OpenNMT$ python3 translate_de_en.py
Enter the sentence ==> Hallo Welt
{‘error’: “Error: ‘TextMultiField’ object has no attribute ‘is_target’”, ‘status’: ‘error’}
Do you want to continue: y or n ?

But when I print response I get the following message:
administrator@~/OpenNMT$ python3 translate_de_en.py
Enter the sentence ==> Hallo Welt
<Response [200]>
Do you want to continue: y or n ?

  After upgrading the version using the below command : 
       sudo python3 -m pip install --upgrade git+https://github.com/pytorch/text .  The above issue had disappeared. Now this has again reappeared. I am slightly confused.

I am getting the same error message again. How do I resolve it?

Could you please assist me in resolving this issue ?

Thank You,
Kishor.

Dear Kishor,

Try to upgrade the whole requirements:

It might help to restart your Flask server or reboot after the upgrade.

Kind regards,
Yasmin

Dear Yasmin,

 Thank you very much for this support. 

The requirement.txt is same as in the master github. Rebooting resolved the response issue. Thanks a lot for this.

I am still working on < unk > issue based on the resolution provided by you.

Kind Regards,
Kishor.

Dear Yasmin,

The Pytorch/Torchtext issue has again reappeared today after rebooting the system.
I checked the requirements.txt with respect to master document, they are both identical.

The below has reappeared :
The issue was that I was getting the following error message when I print target:
I get the following error message when I print only target:
administrator@~/OpenNMT$ python3 translate_de_en.py
Enter the sentence ==> Hallo Welt
{‘error’: “Error: ‘TextMultiField’ object has no attribute ‘is_target’”, ‘status’: ‘error’}
Do you want to continue: y or n ?

But when I print response I get the following message:
administrator@~/OpenNMT$ python3 translate_de_en.py
Enter the sentence ==> Hallo Welt
<Response [200]>
Do you want to continue: y or n ?

  I am getting the same error message again. How do I resolve it?

Could you please suggest a permanent solution to this ?

Regards,
Kishor.

Dear Kishor,
It has been a holiday here, and I have just noticed your reply.
Anyhow, I see you have already got a good answer in another post.
All the best!

Dear Yasmin,

 Thank You very much for the reply. 

Since it was really urgent to get the response on the pytorch issue and issue.,
I had a post a new one in the forum. I have taken note of the reply in the another post.
Working on this and trying to find a permanent solution.

Regards,
Kishor.