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 ''