--- license: apache-2.0 datasets: - mtc/absinth_german_faithfulness_detection_dataset language: - de pipeline_tag: text-generation --- # Model Card for LeoLM-leo-mistral-Absinth This model is a finetuned version of the [LeoLM/leo-mistral-hessianai-7b ](https://huggingface.co/LeoLM/leo-mistral-hessianai-7b). The model was finetuned on the [Absinth dataset]((https://huggingface.co/datasets/mtc/absinth_german_faithfulness_detection_dataset) to predict for a given German news article and sentence a label indicating whether the sentence is faithful to the article or not. ## Instruction format This format must be strictly respected, otherwise the model will generate sub-optimal outputs. The template used to build a prompt for the Instruct model is defined as follows: ``` ### Anweisung: Analysiere ob der gegebene Satz dem Artikel treu ist. Wenn der Satz ausschließlich Informationen wiedergibt, die direkt aus dem Artikel stammen, ohne jegliche Ergänzungen oder Weglassungen, antworte mit 'Faithful'. Wenn der Satz Informationen enthält, die im direkten Widerspruch zum Artikel stehen, antworte mit 'Intrinsic Hallucination'. Wenn der Satz Informationen oder Details einführt, die im Artikel selbst nicht ausdrücklich erwähnt werden, antworte mit 'Extrinsic Hallucination'. Gib zuerst ein kurze Erklärung ob der Satz treu ist oder nicht und danach das entsprechende Label. Artikel: {article} Satz: {sentence} ### Erklärung und Label: ``` Paste into the template the desired article and the corresponding sentence. The model will output a short explanation followed by a label. For more information about the possible labels, see [here](https://huggingface.co/datasets/mtc/absinth_german_faithfulness_detection_dataset). ## Installation Install necessary packages. We also recommend to install the [vllm](https://github.com/vllm-project/vllm) library, which speeds up evaluation considerably: ``` pip3 install transformers pip3 install accelerate # Optional: pip3 install vllm ``` ## Usage ```python from typing import List, Dict import torch from transformers import pipeline from vllm import SamplingParams, LLM def generate_prompts_for_generation(prompt_template: str, article: str, summary_sentences: List[str]) -> List[str]: prompts = [] for sentence in summary_sentences: prompts.append(prompt_template.format(article=article, sentence=sentence)) return prompts def predict_with_vllm(prompts: List[str], model_name: str, max_context_length: int = 4096, max_gen_length: int = 256): sampling_params = SamplingParams(max_tokens=max_gen_length, skip_special_tokens=True, temperature=0.0) llm = LLM(model=model_name, max_model_len=max_context_length) completions = llm.generate(prompts, sampling_params) # Batch inference predictions = [output.text for completion in completions for output in completion.outputs] return predictions def predict_with_hf_generation_pipeline(prompts: List[str], model_name: str, max_context_length: int = 4096, batch_size: int = 2) -> List[str]: text_generation_pipeline = pipeline("text-generation", model=model_name, model_kwargs={"torch_dtype": torch.float16}, device_map="auto", batch_size=batch_size) batch_output = text_generation_pipeline(prompts, truncation=True, max_length=max_context_length, return_full_text=False) predictions = [result[0]['generated_text'] for result in batch_output] return predictions def main(): model_name = "mtc/LeoLM-leo-mistral-hessianai-7b-classification-with-mixtral-explanation-3-epochs-finetuned" # Prompts longer than 4096 tokens will be truncated max_context_length = 4096 article = "Ein neuer Zirkus ist gestern in Zürich angekommen. Viele Familien besuchten das große Zelt, um die Vorstellung zu sehen. Es gab Akrobaten, Clowns und Tiere, die das Publikum begeisterten. Der Zirkus bleibt noch eine Woche in der Stadt und bietet täglich Vorstellungen an." summary_sentences = [ "Ein Zirkus ist in Basel angekommen.", "Der Zirkus, der in 1950 gegründet wurde, wird von vielen Familien besucht."] prompt_template = """### Anweisung: Analysiere ob der gegebene Satz dem Artikel treu ist. Wenn der Satz ausschließlich Informationen wiedergibt, die direkt aus dem Artikel stammen, ohne jegliche Ergänzungen oder Weglassungen, antworte mit 'Faithful'. Wenn der Satz Informationen enthält, die im direkten Widerspruch zum Artikel stehen, antworte mit 'Intrinsic Hallucination'. Wenn der Satz Informationen oder Details einführt, die im Artikel selbst nicht ausdrücklich erwähnt werden, antworte mit 'Extrinsic Hallucination'. Gib zuerst ein kurze Erklärung ob der Satz treu ist oder nicht und danach das entsprechende Label. Artikel: {article} Satz: {sentence} ### Erklärung und Label:""" prompts = generate_prompts_for_generation(prompt_template=prompt_template, article=article, summary_sentences=summary_sentences) predictions = predict_with_hf_generation_pipeline(prompts=prompts, model_name=model_name, max_context_length=max_context_length, batch_size=batch_size) print(predictions) # Uncomment the following lines to use vllm for prediction # max_gen_length = 256 # predictions = predict_with_vllm(prompts=prompts, model_name=model_name, max_context_length=max_context_length, # max_gen_length=max_gen_length) # print(predictions) ``` The output should have the following format, when executing the code above: ``` ['\nErklärung:Der Satz ist falsch, da der Artikel über einen Zirkus in Zürich spricht, nicht in Basel.\nLabel:Intrinsic Hallucination', '\nErklärung:Der Satz ist nicht im Artikel enthalten. Es wird zwar erwähnt, dass der Zirkus viele Familien besucht, aber nicht, dass er 1950 gegründet wurde.\nLabel:Extrinsic Hallucination'] ``` ## Training procedure The model was finetuned using qlora with 4bit quantization on a A100-80Gb Gpu. The following `bitsandbytes` quantization config was used during training: - quant_method: QuantizationMethod.BITS_AND_BYTES - load_in_8bit: False - load_in_4bit: True - llm_int8_threshold: 6.0 - llm_int8_skip_modules: None - llm_int8_enable_fp32_cpu_offload: False - llm_int8_has_fp16_weight: False - bnb_4bit_quant_type: nf4 - bnb_4bit_use_double_quant: True - bnb_4bit_compute_dtype: bfloat16 ### Framework versions - PEFT 0.5.0