ALDashboard.translation
gpt_is_available
def gpt_is_available() -> bool
Return True if the GPT API is available.
may_have_mako
def may_have_mako(text: str) -> bool
Return True if the text appears to contain any Mako code, such as ${...} or % at the beginning of a line.
may_have_html
def may_have_html(text: str) -> bool
Return True if the text appears to contain any HTML code, such as <p> or <div>.
translate_fragments_gpt
def translate_fragments_gpt(
        fragments: Union[str, List[Tuple[int, str]]],
        source_language: str,
        tr_lang: str,
        interview_context: Optional[str] = None,
        special_words: Optional[Dict[int, str]] = None,
        model="gpt-4.1-nano",
        openai_base_url: Optional[str] = None,
        max_output_tokens: Optional[int] = None,
        max_input_tokens: Optional[int] = None,
        openai_api: Optional[str] = None) -> Dict[int, str]
Use an AI model to translate a list of fragments (strings) from one language to another and provide a dictionary with the original text and the translated text.
You can optionally provide an alternative model, but it must support JSON mode.
Arguments
- fragments- A list of strings to be translated.
- source_language- The language of the original text.
- tr_lang- The language to translate the text into.
- special_words- A dictionary of special words that should be translated in a specific way.
- model- The GPT model to use. The default is "gpt-4.1-nano"
- openai_base_url- The base URL for the OpenAI API. If not provided, the default OpenAI URL will be used.
- max_output_tokens- The maximum number of tokens to generate in the output.
- max_input_tokens- The maximum number of tokens in the input. If not provided, it will be set to 4000.
- openai_api- The OpenAI API key. If not provided, it will use the key from the configuration.
Returns
A dictionary where the keys are the indices of the fragments and the values are the translated text.
Translation Objects
class Translation(NamedTuple)
file: DAFile
an XLSX or XLIFF file
untranslated_segments: int
Number of rows in the output that have untranslated text - one for each question, subquestion, field, etc.
translation_file
def translation_file(yaml_filename: str,
                     tr_lang: str,
                     use_gpt=False,
                     use_google_translate=False,
                     openai_api: Optional[str] = None,
                     max_tokens=4000,
                     interview_context: Optional[str] = None,
                     special_words: Optional[Dict[int, str]] = None,
                     model: Optional[str] = None,
                     openai_base_url: Optional[str] = None,
                     max_input_tokens: Optional[int] = None,
                     max_output_tokens: Optional[int] = None) -> Translation
Return a tuple of the translation file in XLSX format, plus a count of the number of words and segments that need to be translated.
The word and segment count only apply when filetype="XLSX".
This code was adjusted from the Flask endpoint-only version in server.py. XLIFF support was removed for now but can be added later.