idk
This commit is contained in:
parent
a084c7a685
commit
d3423c02de
2 changed files with 18 additions and 16 deletions
|
|
@ -4267,7 +4267,7 @@ def getSpeaker(speaker: str):
|
||||||
THREAD_CTX.in_speaker = False
|
THREAD_CTX.in_speaker = False
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
translated = response[0].title().replace("'S", "'s").replace("Speaker: ", "")
|
translated = response[0].strip().title().replace("'S", "'s").replace("Speaker: ", "")
|
||||||
translated = re.sub(r'(\d)(St|Nd|Rd|Th)\b', lambda m: m.group(1) + m.group(2).lower(), translated)
|
translated = re.sub(r'(\d)(St|Nd|Rd|Th)\b', lambda m: m.group(1) + m.group(2).lower(), translated)
|
||||||
|
|
||||||
if re.search(r"([a-zA-Z??])", translated) is None:
|
if re.search(r"([a-zA-Z??])", translated) is None:
|
||||||
|
|
@ -4284,7 +4284,7 @@ def getSpeaker(speaker: str):
|
||||||
THREAD_CTX.in_speaker = False
|
THREAD_CTX.in_speaker = False
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
translated = response[0].title().replace("'S", "'s")
|
translated = response[0].strip().title().replace("'S", "'s")
|
||||||
translated = re.sub(r'(\d)(St|Nd|Rd|Th)\b', lambda m: m.group(1) + m.group(2).lower(), translated)
|
translated = re.sub(r'(\d)(St|Nd|Rd|Th)\b', lambda m: m.group(1) + m.group(2).lower(), translated)
|
||||||
|
|
||||||
with _speakerCacheLock:
|
with _speakerCacheLock:
|
||||||
|
|
|
||||||
|
|
@ -754,18 +754,20 @@ def createContext(config, subbedText, formatType, history=None):
|
||||||
return static_system, matchedVocabText, user
|
return static_system, matchedVocabText, user
|
||||||
|
|
||||||
|
|
||||||
# Static array-based schema for structured JSON output.
|
def createTranslationSchema(numLines):
|
||||||
_TRANSLATION_SCHEMA = {
|
"""Create a JSON schema for translation response based on number of lines."""
|
||||||
"type": "object",
|
properties = {}
|
||||||
"properties": {
|
required = []
|
||||||
"translations": {
|
for i in range(1, numLines + 1):
|
||||||
"type": "array",
|
line_key = f"Line{i}"
|
||||||
"items": {"type": "string"},
|
properties[line_key] = {"type": "string"}
|
||||||
}
|
required.append(line_key)
|
||||||
},
|
return {
|
||||||
"required": ["translations"],
|
"type": "object",
|
||||||
"additionalProperties": False,
|
"properties": properties,
|
||||||
}
|
"required": required,
|
||||||
|
"additionalProperties": False,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def translateText(system, user, history, penalty, formatType, model, numLines=None, vocab_text=""):
|
def translateText(system, user, history, penalty, formatType, model, numLines=None, vocab_text=""):
|
||||||
|
|
@ -824,7 +826,7 @@ def translateText(system, user, history, penalty, formatType, model, numLines=No
|
||||||
# OpenAI, Claude, Gemini: use json_schema with strict enforcement
|
# OpenAI, Claude, Gemini: use json_schema with strict enforcement
|
||||||
responseFormat = {
|
responseFormat = {
|
||||||
"type": "json_schema",
|
"type": "json_schema",
|
||||||
"json_schema": {"name": "translation_response", "strict": True, "schema": _TRANSLATION_SCHEMA}
|
"json_schema": {"name": "translation_response", "strict": True, "schema": createTranslationSchema(numLines)}
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
responseFormat = {"type": "text"}
|
responseFormat = {"type": "text"}
|
||||||
|
|
@ -928,7 +930,7 @@ def translateText(system, user, history, penalty, formatType, model, numLines=No
|
||||||
ant_kwargs["output_config"] = {
|
ant_kwargs["output_config"] = {
|
||||||
"format": {
|
"format": {
|
||||||
"type": "json_schema",
|
"type": "json_schema",
|
||||||
"schema": _TRANSLATION_SCHEMA,
|
"schema": createTranslationSchema(numLines),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue