Views
No views yet
mistralai/Mistral-7B-Instruct-v0.3, adapted to rephrase German texts between different CEFR (Common European Framework of Reference for Languages) proficiency levels. The model specializes in transforming texts between B1, B2, and C1 levels.mistralai/Mistral-7B-Instruct-v0.3mistralai/Mistral-7B-Instruct-v0.3 model in 4-bit precision and then apply the LoRA adapter.1import torch
2from peft import PeftModel
3from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
4
5# --- Model Configuration ---
6base_model_id = "mistralai/Mistral-7B-Instruct-v0.3"
7adapter_id = "AlbertoB12/Mistral-7B-Instruct-german-level-tuner"
8
9# --- Quantization Configuration ---
10bnb_config = BitsAndBytesConfig(
11 load_in_4bit=True,
12 bnb_4bit_use_double_quant=True,
13 bnb_4bit_quant_type="nf4",
14 bnb_4bit_compute_dtype=torch.float16
15)
16
17# --- Load Tokenizer and Base Model ---
18tokenizer = AutoTokenizer.from_pretrained(base_model_id)
19base_model = AutoModelForCausalLM.from_pretrained(
20 base_model_id,
21 quantization_config=bnb_config,
22 device_map="auto",
23 trust_remote_code=True,
24)
25
26# --- Load the LoRA Adapter ---
27model = PeftModel.from_pretrained(base_model, adapter_id)
28
29# --- Prepare the Prompt ---
30# The model was trained with a specific instruction format.
31source_level = "C1"
32target_level = "B1"
33source_text = "Der Krieg stellt stets eine tiefgreifende Zäsur für Gesellschaften dar, da er nicht nur Menschenleben kostet, sondern auch das Vertrauen in politische und soziale Strukturen erschüttert. Umso dringlicher stellt sich die Frage, wie globale Zusammenarbeit gestärkt werden kann, um künftige Generationen vor ähnlichen Katastrophen zu bewahren."
34
35# Format the prompt using the required template
36prompt = f"""### Instruction:
37Schreibe den folgenden deutschen Text von seinem ursprünglichen GER-Niveau ({source_level}) auf das Zielniveau ({target_level}) um.
38
39### Input:
40{source_text}
41
42### Output:
43"""
44
45# --- Generate the Response ---
46input_ids = tokenizer(prompt, return_tensors="pt", truncation=True).input_ids.cuda()
47outputs = model.generate(
48 input_ids=input_ids,
49 max_new_tokens=512, # Adjust as needed
50 do_sample=True,
51 temperature=0.7,
52 top_k=50,
53 top_p=0.95
54)
55
56# --- Decode and Print ---
57generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
58output_only = generated_text.split("### Output:")[1].strip()
59
60print(output_only)