From eee6ec282d79c34fc3637b3af40419c1ac1811d1 Mon Sep 17 00:00:00 2001 From: DazedAnon Date: Sat, 7 Sep 2024 19:53:24 -0500 Subject: [PATCH] Add backup model in case mismatch --- modules/rpgmakerace.py | 19 ++++++++++++------- modules/rpgmakermvmz.py | 14 +++++++++----- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/modules/rpgmakerace.py b/modules/rpgmakerace.py index 64b53fd..c9c96cd 100644 --- a/modules/rpgmakerace.py +++ b/modules/rpgmakerace.py @@ -39,6 +39,7 @@ IGNORETLTEXT = False # Ignores all translated text. MISMATCH = [] # Lists files that throw a mismatch error (Length of GPT list response is wrong) BRACKETNAMES = False PBAR = None +FILENAME = None # Pricing - Depends on the model https://openai.com/pricing # Batch Size - GPT 3.5 Struggles past 15 lines per request. GPT4 struggles past 50 lines per request @@ -84,8 +85,9 @@ CODE111 = False CODE108 = False def handleACE(filename, estimate): - global ESTIMATE, TOKENS + global ESTIMATE, TOKENS, FILENAME ESTIMATE = estimate + FILENAME = filename # Translate start = time.time() @@ -2173,7 +2175,7 @@ Output ONLY the {LANGUAGE} translation in the following format: `Translation: <{ user = subbedT return characters, system, user -def translateText(characters, system, user, history, penalty, format): +def translateText(characters, system, user, history, penalty, format, model=MODEL): # Prompt msg = [{"role": "system", "content": system}] @@ -2192,12 +2194,12 @@ def translateText(characters, system, user, history, penalty, format): else: responseFormat = { "type": "text" } - # Content to TL + # Content to TL Cleanup msg.append({"role": "user", "content": f'{user}'}) response = openai.chat.completions.create( temperature=0, frequency_penalty=penalty, - model=MODEL, + model=model, response_format=responseFormat, messages=msg, ) @@ -2282,7 +2284,7 @@ def combineList(tlist, text): @retry(exceptions=Exception, tries=5, delay=5) def translateGPT(text, history, fullPromptFlag): - global PBAR + global PBAR, MISMATCH, FILENAME mismatch = False totalTokens = [0, 0] @@ -2331,8 +2333,8 @@ def translateGPT(text, history, fullPromptFlag): if isinstance(tItem, list): extractedTranslations = extractTranslation(translatedText, True) if extractedTranslations == None or len(tItem) != len(extractedTranslations): - # Mismatch. Try Again - response = translateText(characters, system, user, history, 0.05, format) + # Mismatch. Try Again w/ Different Model + response = translateText(characters, system, user, history, 0.05, format, 'gpt-4o') translatedText = response.choices[0].message.content totalTokens[0] += response.usage.prompt_tokens totalTokens[1] += response.usage.completion_tokens @@ -2348,9 +2350,12 @@ def translateGPT(text, history, fullPromptFlag): if mismatch == False: tList[index] = extractedTranslations history = extractedTranslations[-10:] # Update history if we have a list + else: history = text[-10:] mismatch = False + if FILENAME not in MISMATCH: + MISMATCH.append(FILENAME) # Update Loading Bar with LOCK: diff --git a/modules/rpgmakermvmz.py b/modules/rpgmakermvmz.py index 42e67da..24a17c7 100644 --- a/modules/rpgmakermvmz.py +++ b/modules/rpgmakermvmz.py @@ -37,6 +37,7 @@ IGNORETLTEXT = False # Ignores all translated text. MISMATCH = [] # Lists files that throw a mismatch error (Length of GPT list response is wrong) BRACKETNAMES = False PBAR = None +FILENAME = None # Pricing - Depends on the model https://openai.com/pricing # Batch Size - GPT 3.5 Struggles past 15 lines per request. GPT4 struggles past 50 lines per request @@ -82,8 +83,9 @@ CODE111 = False CODE108 = False def handleMVMZ(filename, estimate): - global ESTIMATE, TOKENS + global ESTIMATE, TOKENS, FILENAME ESTIMATE = estimate + FILENAME = filename # Translate start = time.time() @@ -2168,7 +2170,7 @@ Output ONLY the {LANGUAGE} translation in the following format: `Translation: <{ user = subbedT return characters, system, user -def translateText(characters, system, user, history, penalty, format): +def translateText(characters, system, user, history, penalty, format, model=MODEL): # Prompt msg = [{"role": "system", "content": system}] @@ -2192,7 +2194,7 @@ def translateText(characters, system, user, history, penalty, format): response = openai.chat.completions.create( temperature=0, frequency_penalty=penalty, - model=MODEL, + model=model, response_format=responseFormat, messages=msg, ) @@ -2276,7 +2278,7 @@ def combineList(tlist, text): @retry(exceptions=Exception, tries=5, delay=5) def translateGPT(text, history, fullPromptFlag): - global PBAR + global PBAR, MISMATCH, FILENAME mismatch = False totalTokens = [0, 0] @@ -2326,7 +2328,7 @@ def translateGPT(text, history, fullPromptFlag): extractedTranslations = extractTranslation(translatedText, True) if extractedTranslations == None or len(tItem) != len(extractedTranslations): # Mismatch. Try Again - response = translateText(characters, system, user, history, 0.05, format) + response = translateText(characters, system, user, history, 0.05, format, 'gpt-4o') translatedText = response.choices[0].message.content totalTokens[0] += response.usage.prompt_tokens totalTokens[1] += response.usage.completion_tokens @@ -2345,6 +2347,8 @@ def translateGPT(text, history, fullPromptFlag): else: history = text[-10:] mismatch = False + if FILENAME not in MISMATCH: + MISMATCH.append(FILENAME) # Update Loading Bar with LOCK: