File size: 2,757 Bytes
0140c70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import requests
from urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter
from concurrent.futures import ThreadPoolExecutor, as_completed

class ChineseTranslator:
    def __init__(self):
        self.client = requests.Session()

    def translate(self, text):
        if not text:
            return None

        payload = {
            "appid": "105",
            "sgid": "en",
            "sbid": "en",
            "egid": "zh-CN",
            "ebid": "zh-CN",
            "content": text,
            "type": "2",
        }

        response = self.client.post("https://translate-api-fykz.xiangtatech.com/translation/webs/index", data=payload)
        if response.status_code == 200:
            json_data = response.json()
            by_value = json_data.get("by", "")
            if not by_value:
                return None
            return by_value

        return None

    def close_session(self):
        self.client.close()
        
class GPTTranslator:
    def __init__(self, api_key, api_url):
        self.headers = {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {api_key}"
        }

        self.session = requests.Session()
        retries = Retry(total=5, backoff_factor=0.1, status_forcelist=[429, 500, 502, 503, 504])
        self.session.mount('https://', HTTPAdapter(max_retries=retries))

        self.api_url = api_url

    def translate(self, text):
        data = {
            "model": "gpt-3.5-turbo",
            "messages": [
                {"role": "user", "content": f"你是一个英译中专家,请直接返回'{text}'最有可能的三种中文翻译结果,彼此间语义有所区分,结果以逗号间隔."}
            ]
        }
        response = self.session.post(self.api_url, headers=self.headers, json=data)
        response_data = response.json()

        if response.status_code == 200 and 'choices' in response_data and 'content' in response_data['choices'][0]['message']:
            return response_data['choices'][0]['message']['content']
        else:
            return f"Error or no translation for tag: {text}"

    def close_session(self):
        self.session.close()
        
def translate_tags(translator, tags):
    translations = [None] * len(tags)
    
    with ThreadPoolExecutor(max_workers=50) as executor:
        future_to_index = {executor.submit(translator.translate, tag): i for i, tag in enumerate(tags)}
        for future in as_completed(future_to_index):
            index = future_to_index[future]
            translations[index] = future.result()
            
    translator.close_session()
            
    return translations