in src/fmeval/eval_algorithms/classification_accuracy.py [0:0]
def convert_model_output_to_label(model_output: str, valid_labels: List[str]) -> str:
"""Convert model output to string class label. The model is expected to return a label directly (if it has a
classification head), or a string containing a label (if it has a language modelling head). In the latter case we
strip any additional text (e.g. "The answer is 2." --> "2"). If no valid labels is contained in the
`model_output` an "unknown" label is returned. Users can define other `converter_fn`s, e.g. to translate a text
label to string ("NEGATIVE" --> "0").
:param model_output: Value returned by the model.
:param valid_labels: Valid labels.
:return: `model_output` transformed into a label
"""
# normalise to lowercase & strip
valid_labels = [label.lower().strip() for label in valid_labels]
response_words = model_output.split(" ")
predicted_labels = [word.lower().strip() for word in response_words if word.lower().strip() in valid_labels]
# if there is more than one label in the model output we pick the first
string_label = predicted_labels[0] if predicted_labels else UNKNOWN_LABEL
return string_label