File size: 1,059 Bytes
5120311
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from langdetect import detect
import requests
import json
import time

URL_TRANSLATOR = "http://10.9.3.241:8093/translator"
def detect_lang(text):
    try:
        lang = detect(text)
    except:
        lang = 'en'
    return lang

def translate_text_multi_layer(source, target, text, url = URL_TRANSLATOR):
    if source == "":
        source = detect_lang(text)
    print("PPPPPPPPPPPPP")
    if not text.strip() or source == target:
        return text
    
    json_body = {
        "doc": text,
        "src_lang": source,
        "tgt_lang": target
    }
    print("CCCCCCCCCCCC")
    res= requests.post(url, json=json_body)
    print("translate: ", res.status_code)
    path_log = f"log_tran/requests_tran_{time.time()}.txt"
    with open(path_log, "w", encoding="utf-8") as f:
        f.write(json.dumps(json_body) + "\n")
    if res.status_code == 200:
        res = res.json()
        with open(path_log, "a", encoding="utf-8") as f:
            f.write(json.dumps(res) + "\n")
        return res
    return ''