-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslator.py
335 lines (295 loc) · 12.7 KB
/
translator.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import hashlib
import time
import uuid
import requests
import json
from datetime import datetime
import os
from tencentcloud.common.common_client import CommonClient
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from volcengine.ApiInfo import ApiInfo
from volcengine.Credentials import Credentials
from volcengine.ServiceInfo import ServiceInfo
from volcengine.base.Service import Service
class Translator():
def __init__(self,
volcengine_access_key,
volcengine_secret_key,
volcengine_url,
tencent_secret_id,
tencent_secret_key,
tencent_endpoint,
youdao_APP_KEY,
youdao_APP_SECRET,
youdao_url,
openai_base_url,
openai_api_key,
claude_base_url,
claude_api_key,
api_usage='./api_character_count.json'
):
self.volcengine_access_key = volcengine_access_key # https://console.volcengine.com/iam/keymanage/
self.volcengine_secret_key = volcengine_secret_key
self.volcengine_url = volcengine_url
self.tencent_secret_id = tencent_secret_id
self.tencent_secret_key = tencent_secret_key
self.tencent_endpoint = tencent_endpoint
self.youdao_APP_KEY = youdao_APP_KEY
self.youdao_APP_SECRET = youdao_APP_SECRET
self.youdao_url = youdao_url
self.openai_base_url = openai_base_url
self.openai_api_key = openai_api_key
self.claude_base_url = claude_base_url
self.claude_api_key = claude_api_key
self.data_file = api_usage
def translate(self, api_name, sentence, target_language='zh'):
if api_name in ['volcengine', 'tencent', 'youdao']:
self.add_character_count(api_name, sentence)
if api_name == 'volcengine':
return self.volcengine_translation(sentence, target_language)
elif api_name == 'tencent':
return self.tencent_translation(sentence, target_language)
elif api_name == 'youdao':
return self.youdao_translation(sentence, target_language)
elif api_name == 'opus':
message = self.claude_translation(sentence, target_language, model='claude-3-opus-20240229')
self.add_character_count(api_name, message.Usage.input_tokens+message.Usage.output_tokens)
return message.content[0].text
elif api_name == 'sonnet':
return self.claude_translation(sentence, target_language, model='claude-3-sonnet-20240229')
elif api_name == 'gpt3':
message = self.openai_translation(sentence, target_language, model='gpt-3.5-turbo-0613')
self.add_character_count(api_name, message[1])
return message[0]
elif api_name == 'gpt4':
message = self.openai_translation(sentence, target_language, model='gpt-4-turbo-2024-04-09')
self.add_character_count(api_name, message[1])
return message[0]
elif api_name == "gpt4o":
message = self.openai_translation(sentence, target_language, model='gpt-4o-2024-05-13')
self.add_character_count(api_name, message[1])
return message[0]
else:
raise ValueError("Unsupported API")
def openai_translation(self, sentence, target_language='zh', model="gpt-3.5-turbo-0613"):
from openai import OpenAI
import tiktoken
def num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613"):
"""Returns the number of tokens used by a list of messages."""
try:
encoding = tiktoken.encoding_for_model(model)
except KeyError:
encoding = tiktoken.get_encoding("cl100k_base")
if model == "gpt-3.5-turbo-0613": # note: future models may deviate from this
num_tokens = 0
for message in messages:
num_tokens += 4 # every message follows <im_start>{role/name}\n{content}<im_end>\n
for key, value in message.items():
num_tokens += len(encoding.encode(value))
if key == "name": # if there's a name, the role is omitted
num_tokens += -1 # role is always required and always 1 token
num_tokens += 2 # every reply is primed with <im_start>assistant
return num_tokens
else:
raise NotImplementedError(
f"""num_tokens_from_messages() is not presently implemented for model {model}.""")
question = f"请翻译这句话为{target_language},回答不需要带有前缀: {sentence}"
token_num = num_tokens_from_messages([{"role": "user", "content": question}])
client = OpenAI(
base_url=self.openai_base_url,
api_key=self.openai_api_key,
)
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": question,
}
],
model=model,
)
return chat_completion.choices[0].message.content, token_num
def claude_translation(self, sentence, target_language='zh', model="claude-3-sonnet-20240229"):
import anthropic
question = f"请翻译这句话为{target_language},回答不需要带有前缀: {sentence}"
client = anthropic.Anthropic(
# defaults to os.environ.get("ANTHROPIC_API_KEY")
base_url=self.claude_base_url,
api_key=self.claude_api_key,
)
message = client.messages.create(
model=model,
max_tokens=4096,
temperature=0.0,
messages=[
{"role": "user", "content": question}
]
)
return message
def volcengine_translation(self, sentence, target_language='zh'):
# Assuming volcengine_translation implementation
try:
k_service_info = \
ServiceInfo(self.volcengine_url,
{'Content-Type': 'application/json'},
Credentials(self.volcengine_access_key, self.volcengine_secret_key, 'translate', 'cn-north-1'),
5,
5)
k_query = {
'Action': 'TranslateText',
'Version': '2020-06-01'
}
k_api_info = {
'translate': ApiInfo('POST', '/', k_query, {}, {})
}
service = Service(k_service_info, k_api_info)
body = {
'TargetLanguage': target_language,
'TextList': [sentence],
}
res = service.json('translate', {}, json.dumps(body))
result = json.loads(res)
return result['TranslationList'][0]['Translation']
except Exception as err:
return False
def tencent_translation(self, sentence, target_language='zh'):
# Assuming tencent_translation implementation
try:
# cred = credential.ProfileCredential().get_credential()
cred = credential.Credential(self.tencent_secret_id,self.tencent_secret_key)
httpProfile = HttpProfile()
httpProfile.endpoint = self.tencent_endpoint
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
params = {
"SourceText": sentence,
"Source": "en",
"Target": target_language,
"ProjectId": 0
}
common_client = CommonClient("tmt", "2018-03-21", cred, "ap-beijing", profile=clientProfile)
result = common_client.call_json("TextTranslate", params)
return result["Response"]["TargetText"]
except TencentCloudSDKException as err:
return False
def youdao_translation(self, sentence, target_language='zh'):
# Assuming youdao_translation implementation
'''
note: 将下列变量替换为需要请求的参数
'''
def doCall(url, header, params, method):
if 'get' == method:
return requests.get(url, params)
elif 'post' == method:
return requests.post(url, params, header)
q = sentence
lang_from = 'en'
if target_language == 'zh':
lang_to = 'zh-CHS'
else:
lang_to = target_language
# vocab_id = '您的用户词表ID'
data = {
'q': q, 'from': lang_from, 'to': lang_to,
# 'vocabId': vocab_id
}
addAuthParams(self.youdao_APP_KEY, self.youdao_APP_SECRET, data)
header = {'Content-Type': 'application/x-www-form-urlencoded'}
res = doCall(self.youdao_url, header, data, 'post')
if res.status_code == 200:
result = res.json()
if "translation" not in result:
return "Error: " + result['errorCode'] + " " + result['error_msg']
return result['translation'][0]
else:
return False
def add_character_count(self, api_name, new_chars):
"""Add character count of the new request to the correct API count and reset if it's a new month."""
if api_name in ['volcengine', 'tencent', 'youdao']:
len_chars = len(new_chars)
else:
len_chars = new_chars
def load_or_initialize_counts():
"""Load current character counts or initialize them if not existing."""
if os.path.exists(self.data_file):
with open(self.data_file, 'r') as file:
return json.load(file)
else:
data = {
"last_reset": str(datetime.now().date()),
"counts": {"tencent": 0, "volcengine": 0, "youdao": 0}
}
return data
def save_counts(counts):
"""Save the current character counts to a JSON file."""
with open(self.data_file, 'w') as file:
json.dump(counts, file)
def reset_counts_if_new_month(data):
"""Reset counts on the first of the month."""
current_time = datetime.now().date()
last_reset = datetime.strptime(data['last_reset'], '%Y-%m-%d').date()
if current_time != last_reset and current_time.day == 1:
for key in data['counts'].keys():
data['counts'][key] = 0
data['last_reset'] = str(current_time)
return data
counts = load_or_initialize_counts()
counts = reset_counts_if_new_month(counts)
if api_name in counts['counts']:
counts['counts'][api_name] += len_chars
else:
counts['counts'][api_name] = len_chars
save_counts(counts)
return counts['counts']
'''
添加鉴权相关参数 -
appKey : 应用ID
salt : 随机值
curtime : 当前时间戳(秒)
signType : 签名版本
sign : 请求签名
@param appKey 您的应用ID
@param appSecret 您的应用密钥
@param paramsMap 请求参数表
'''
def addAuthParams(appKey, appSecret, params):
q = params.get('q')
if q is None:
q = params.get('img')
q = "".join(q)
salt = str(uuid.uuid1())
curtime = str(int(time.time()))
sign = calculateSign(appKey, appSecret, q, salt, curtime)
params['appKey'] = appKey
params['salt'] = salt
params['curtime'] = curtime
params['signType'] = 'v3'
params['sign'] = sign
'''
计算鉴权签名 -
计算方式 : sign = sha256(appKey + input(q) + salt + curtime + appSecret)
@param appKey 您的应用ID
@param appSecret 您的应用密钥
@param q 请求内容
@param salt 随机值
@param curtime 当前时间戳(秒)
@return 鉴权签名sign
'''
def calculateSign(appKey, appSecret, q, salt, curtime):
def getInput(input):
if input is None:
return input
inputLen = len(input)
return input if inputLen <= 20 else input[0:10] + str(inputLen) + input[inputLen - 10:inputLen]
def encrypt(strSrc):
hash_algorithm = hashlib.sha256()
hash_algorithm.update(strSrc.encode('utf-8'))
return hash_algorithm.hexdigest()
strSrc = appKey + getInput(q) + salt + curtime + appSecret
return encrypt(strSrc)
if __name__ == "__main__":
translator = Translator()
print(translator.translate('youdao', 'this is a test string from Tencent API request'))