-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.py
186 lines (145 loc) · 5.18 KB
/
translate.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
import openai
import time
import re
# OpenAI API 키 설정
openai.api_key = ''
def call_openai_api(model, msg):
model = "gpt-3.5-turbo"
try:
response = openai.ChatCompletion.create(
model=model,
messages=msg
)
return response
except openai.error.APIError as e:
print("API 호출 오류:", e)
print("다시 시도합니다...")
time.sleep(2)
return call_openai_api(model, msg)
except openai.error.RateLimitError as e:
print("API 호출 오류:", e)
print("다시 시도합니다...")
time.sleep(20) # 20초 대기
return call_openai_api(model, msg)
def check_korean_ratio(text):
korean_pattern = re.compile("[ㄱ-ㅎㅏ-ㅣ가-힣]")
special_pattern = re.compile("[\W]")
numeric_pattern = re.compile("[\d]")
korean_count = 0
special_count = 0
numeric_count = 0
total_count = len(text)
for char in text:
if korean_pattern.match(char):
korean_count += 1
elif special_pattern.match(char):
special_count += 1
elif numeric_pattern.match(char):
numeric_count += 1
korean_ratio = korean_count / total_count
special_ratio = special_count / total_count
numeric_ratio = numeric_count / total_count
total_ratio = korean_ratio + special_ratio + numeric_ratio
return total_ratio
def check_english_ratio(text):
english_pattern = re.compile("[A-Za-z]")
special_pattern = re.compile("[\W]")
numeric_pattern = re.compile("[\d]")
english_count = 0
special_count = 0
numeric_count = 0
total_count = len(text)
for char in text:
if english_pattern.match(char):
english_count += 1
elif special_pattern.match(char):
special_count += 1
elif numeric_pattern.match(char):
numeric_count += 1
english_ratio = english_count / total_count
special_ratio = special_count / total_count
numeric_ratio = numeric_count / total_count
total_ratio = english_ratio + special_ratio + numeric_ratio
print(total_ratio)
return total_ratio
def en2ko(input):
input_pmt = f"translate {input} into korean, Show only translated results. No English in the result"
model = "gpt-3.5-turbo"
print("1")
print(input_pmt)
while True:
try:
messages = [
{
"role": "system",
"content": "You are a helpful assistant who is good at translating."
},
{
"role": "user",
"content": input_pmt
}
]
response = call_openai_api(model, messages)
result = response['choices'][0]['message']['content']
korean_pattern = re.compile("[\uac00-\ud7a3]+")
print(result)
threshold = 0.75
# 결과가 한국어인지 확인
if result:
if check_korean_ratio(result) >= threshold:
return result
else:
print("Empty translation. Retrying...")
time.sleep(1)
continue
# 한도 에러가 발생하면 잠시 대기한 후 다시 시도
except openai.error.RateLimitError:
print("Rate limit exceeded. Waiting for 20 seconds...")
time.sleep(20)
continue
# 다른 오류가 발생하면 잠시 대기한 후 다시 시도
except Exception as e:
print(f"Error: {e}. Retrying...")
time.sleep(1)
continue
def ko2en(input):
input_pmt = f"translate {input} into english, Show only translated results. No korean in the result"
model = "gpt-3.5-turbo"
print("1")
print(input_pmt)
while True:
try:
messages = [
{
"role": "system",
"content": "You are a helpful assistant who is good at translating."
},
{
"role": "user",
"content": input_pmt
}
]
response = call_openai_api(model, messages)
result = response['choices'][0]['message']['content']
english_pattern = re.compile("[A-Za-z]+")
print(result)
threshold = 0.75
# 결과가 영어인지 확인
if result:
if check_english_ratio(result) >= threshold:
return result
else:
print(check_english_ratio(result) )
print("Empty translation. Retrying...")
time.sleep(1)
continue
# 한도 에러가 발생하면 잠시 대기한 후 다시 시도
except openai.error.RateLimitError:
print("Rate limit exceeded. Waiting for 20 seconds...")
time.sleep(20)
continue
# 다른 오류가 발생하면 잠시 대기한 후 다시 시도
except Exception as e:
print(f"Error: {e}. Retrying...")
time.sleep(1)
continue