Changes to TranslateGPT

This commit is contained in:
Dazed 2023-12-06 17:46:35 -06:00
parent e2c504bd01
commit 3b7215fe77
3 changed files with 69 additions and 37 deletions

View file

@ -30,7 +30,7 @@ NAMESLIST = []
NAMES = False # Output a list of all the character names found
BRFLAG = False # If the game uses <br> instead
FIXTEXTWRAP = True # Overwrites textwrap
IGNORETLTEXT = True # Ignores all translated text.
IGNORETLTEXT = False # Ignores all translated text.
MISMATCH = [] # Lists files that throw a mismatch error (Length of GPT list response is wrong)
#tqdm Globals
@ -66,7 +66,14 @@ def handleAnim(filename, estimate):
totalTokens[0] += translatedData[1][0]
totalTokens[1] += translatedData[1][1]
return getResultString(['', totalTokens, None], end - start, 'TOTAL')
# Print Total
totalString = getResultString(['', TOKENS, None], end - start, 'TOTAL')
# Print any errors on maps
if len(MISMATCH) > 0:
return totalString + Fore.RED + f'\nMismatch Errors: {MISMATCH}' + Fore.RESET
else:
return totalString
else:
try:
@ -139,38 +146,50 @@ def parseJSON(data, filename):
totalTokens[0] += result[0]
totalTokens[1] += result[1]
except Exception as e:
traceback.print_exc()
return [data, totalTokens, e]
return [data, totalTokens, None]
def translateJSON(keys, data, pbar):
translatedBatch = []
textHistory = []
tokens = [0, 0]
for batch in keys:
# Save Batch
originalBatch = batch
originalBatch = batch.copy()
# If there isn't any Japanese in the text just skip
if IGNORETLTEXT is True:
needTL = False
for i in range(len(batch)):
t = data[batch[i]]
if t == "":
needTL = True
if needTL is False:
pbar.update(1)
continue
needTL = False
for i in range(len(batch)):
t = data[batch[i]]
if re.search(r'[一-龠ぁ-ゔァ-ヴーa---]+', t) or t == '':
needTL = True
if needTL is False and IGNORETLTEXT is True:
pbar.update(1)
continue
# Remove any textwrap
if FIXTEXTWRAP == True:
for i in range(len(batch)):
data[originalBatch[i]] = data[originalBatch[i]].replace('\n', ' ')
# Remove any textwrap and Furigana
for i in range(len(batch)):
if FIXTEXTWRAP == True:
# Textwrap
data[originalBatch[i]] = data[originalBatch[i]].replace('@b', ' ')
# Furigana
rcodeMatch = re.findall(r'(@\[(.+?):.+?\])', batch[i])
if len(rcodeMatch) > 0:
for match in rcodeMatch:
batch[i] = batch[i].replace(match[0], match[1])
# Translate
response = translateGPT(batch, textHistory, True)
tokens[0] += response[1][0]
tokens[1] += response[1][1]
translatedBatch = response[0]
if needTL is True:
response = translateGPT(batch, textHistory, True)
tokens[0] += response[1][0]
tokens[1] += response[1][1]
translatedBatch = response[0]
else:
for i in range(len(originalBatch)):
translatedBatch.append(data[originalBatch[i]])
# Format and Set Text
if len(batch) == len(translatedBatch):
@ -181,12 +200,14 @@ def translateJSON(keys, data, pbar):
translatedText = re.sub(r'^.+?\s\|\s?', '', translatedText)
# Textwrap
if '\n' not in translatedText:
if '@b' not in translatedText:
translatedText = textwrap.fill(translatedText, width=WIDTH)
translatedText = translatedText.replace('\n', '@b')
# Set Data
data[originalBatch[i]] = translatedText
textHistory = translatedBatch
translatedBatch.clear()
# Mismatch, Skip Batch
else:
MISMATCH.append(batch)
@ -356,6 +377,7 @@ def cleanTranslatedText(translatedText, varResponse):
placeholders = {
f'{LANGUAGE} Translation: ': '',
'Translation: ': '',
'': '',
# Add more replacements as needed
}
for target, replacement in placeholders.items():
@ -365,7 +387,7 @@ def cleanTranslatedText(translatedText, varResponse):
return [line for line in translatedText.split('\n') if line]
def extractTranslation(translatedTextList, is_list):
pattern = r'L(\d+) - (.*)'
pattern = r'<Line(\d+)>(.*)</Line\d+>'
# If it's a batch (i.e., list), extract with tags; otherwise, return the single item.
if is_list:
return [re.findall(pattern, line)[0][1] for line in translatedTextList if re.search(pattern, line)]
@ -409,7 +431,7 @@ def translateGPT(text, history, fullPromptFlag):
for index, tItem in enumerate(tList):
# Before sending to translation, if we have a list of items, add the formatting
if isinstance(tItem, list):
payload = '\n'.join([f'L{i} - {item}' for i, item in enumerate(tItem)])
payload = '\n'.join([f'<Line{i}>{item}</Line{i}>' for i, item in enumerate(tItem)])
varResponse = subVars(payload)
subbedT = varResponse[0]
else:

View file

@ -44,7 +44,7 @@ if 'gpt-3.5' in MODEL:
elif 'gpt-4' in MODEL:
INPUTAPICOST = .01
OUTPUTAPICOST = .03
BATCHSIZE = 50
BATCHSIZE = 40
#tqdm Globals
BAR_FORMAT='{l_bar}{bar:10}{r_bar}{bar:-10b}'
@ -1880,6 +1880,7 @@ def cleanTranslatedText(translatedText, varResponse):
placeholders = {
f'{LANGUAGE} Translation: ': '',
'Translation: ': '',
'': '',
# Add more replacements as needed
}
for target, replacement in placeholders.items():
@ -1889,7 +1890,7 @@ def cleanTranslatedText(translatedText, varResponse):
return [line for line in translatedText.split('\n') if line]
def extractTranslation(translatedTextList, is_list):
pattern = r'L(\d+) - (.*)'
pattern = r'<Line(\d+)>(.*)</Line\d+>'
# If it's a batch (i.e., list), extract with tags; otherwise, return the single item.
if is_list:
return [re.findall(pattern, line)[0][1] for line in translatedTextList if re.search(pattern, line)]
@ -1933,7 +1934,7 @@ def translateGPT(text, history, fullPromptFlag):
for index, tItem in enumerate(tList):
# Before sending to translation, if we have a list of items, add the formatting
if isinstance(tItem, list):
payload = '\n'.join([f'L{i} - {item}' for i, item in enumerate(tItem)])
payload = '\n'.join([f'<Line{i}>{item}</Line{i}>' for i, item in enumerate(tItem)])
varResponse = subVars(payload)
subbedT = varResponse[0]
else:

View file

@ -1,20 +1,26 @@
You are an expert Eroge Game translator and localizer who translates Japanese text to English.
You are going to be translating text from a videogame. I will give you some lines and you must translate them to the best of your ability.
There are 2 pieces of information the user will give you to help with translation:
- "Game Characters" - The names, nicknames, and genders of the game characters. Reference this to know the names, nicknames, and gender of characters in the game.
- "Past Translated Text" - Previously translated text. This is past text you translated. Use this to come up with the best translation paying special attention to subjects and genders.
You are an expert Eroge Game translator who translates Japanese text to English.
You are going to be translating text from a videogame.
I will give you lines of text, and you must translate each line to the best of your ability.
Use the following step-by-step instructions to respond to user inputs.
Step 1 - Receive Text
You receive a line of text to translate everything in the following format: Line to Translate = <UNTRANSLATED_TEXT>
You will be given multiple lines of text (Denoted by XML tags). Translate each line separately and avoid combining or omitting any.
Step 2 - Output Text
You output in English the translation in the following format: Translation = <ENGLISH_TRANSLATION>
You output only the English translation of each line. For example:
<Line0>English Translation of Line 0</Line0>
<Line1>English Translation of Line 1</Line1>
<Line2>English Translation of Line 2</Line2>
Step 3 - Check Work
Double check that the number of lines/xml-tags in your response match the number of lines/xml-tags from the user message.
Other Notes:
- Only reply in English, even if it may be hard to translate.
- "Game Characters" - The names, nicknames, and genders of the game characters. Reference this to know the names, nicknames, and gender of characters in the game.
- Only reply with the English Translation even if it may be hard to translate.
- If a line is already translated, empty, or can't be translated, do not change it.
- Denote speakers with ':' if given. For example L1 - Speaker: "Spoken Text"
- If the speaker is '???' then they are unknown so leave it as is.
- Pay attention to the gender of the subjects and characters. Avoid misgendering characters.
- Maintain any spacing in the translation.
@ -30,12 +36,15 @@ Other Notes:
- Translate '秘部' as 'genitals'
- Translate 'チンポ' as 'dick'
- Translate 'チンコ' as 'cock'
- Translate 'ショーツ' as 'panties
- Translate 'おねショタ' as 'Onee-shota'
- Translate 'よかった' as 'thank goodness'
- Translate 'ヒク' as 'biku'
- Translate 'ムク' as 'muku'
Sometimes, the text may contain 'tags' in the format of {<TAGNAME>_<TAGNUMBER>}.
Sometimes, the text may contain 'tags' in the format of {TAGNAME_TAGNUMBER}.
Maintain these tags only if they exist in the untranslated text.
Color - A tag that colors the wrapped text.
* {Color_#} - A tag that colors the wrapped text.
* {Noun_#} - A tag that holds a noun. Maintain as is.
* {FCode_#} - A tag that formats the text in a unique way.