Source code for wimarka.tasks.explanation

from wimarka.utils.model import load_model

[docs] def generate_explanation(src, tgt, errors, fluency, adequacy, overall): model = load_model("explanation") prompt = ( f"""Evaluate translation with scores and tags. Language tags: [EN]-English, [CEB]-Cebuano, [ILO]-Ilocano, [TGL]-Tagalog. Error tags: [MI_ST]-Minor Syntax, [MI_SE]-Minor Semantic, [MA_ST]-Major Syntax, [MA_SE]-Major Semantic You will also be given the fluency score, adequacy score, and overall score. Generate an explanation for each error. Wrap them with the error tag each error is associated with. Respond with the explanation only. If there are no errors wrapped in the target sentence, reply with "The translation has conveyed the meaning with well-structured syntax.". Example: Source: [EN] I am your mother, Jerry! Target: [TGL] Lolo niya ako, Jerry. Errors: [TGL] [MA_SE]Lolo niya[/MA_SE] ako, Jerry [MI_ST] . [/MI_ST] Fluency Score: 0.85 Adequacy Score: 0.4 Overall Score: 0.65 Explanation: [MA_SE] “Lolo niya” refers to the grandfather of a third person, while the source refers to the mother of the person the speaker is talking to. [/MA_SE] [MI_ST] ! should be used instead of . to be inline with the expression [/MI_ST] Machine Translation: Source: {src} Target: {tgt} Errors: {errors} Fluency Score: {fluency} Adequacy Score: {adequacy} Overall Score: {overall} Explanation: """ ) messages = [ {"role": "user", "content": prompt} ] response = model.create_chat_completion( messages=messages, stop=["<eos>"], ) generated_text = response["choices"][0]["message"]["content"] start = generated_text.find("Explanation:") end = generated_text.find("Machine Translation:", start + 1) explanation = "" if start != -1: if end != -1: explanation = generated_text[start + len("Explanation:"):end].strip() else: explanation = generated_text[start + len("Explanation:"):].strip() else: explanation = generated_text[start + len("Explanation:"):].strip() return explanation