Views
No views yet
meta-llama/Llama-3.1-8B-Instruct (trained against
the ungated mirror NousResearch/Meta-Llama-3.1-8B-Instruct) that converts raw, informal
Arabic workplace maintenance tickets into clean, structured English JSON.meta-llama/Llama-3.1-8B-Instruct /
NousResearch/Meta-Llama-3.1-8B-Instructtitle + description), the model outputs a JSON
object with a cleaned English title, description, and a reasoning field explaining
the edits made. Intended to run as an automated preprocessing step immediately upstream of
an English-language ticket classifier — not intended as a general-purpose Arabic-English
translator or general-purpose chat assistant.1import json
2import torch
3from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
4from peft import PeftModel
5
6BASE_MODEL_ID = "NousResearch/Meta-Llama-3.1-8B-Instruct"
7ADAPTER_DIR = "./llama31-8b-ar-ticket-cleaner-final"
8
9bnb_config = BitsAndBytesConfig(
10 load_in_4bit=True,
11 bnb_4bit_quant_type="nf4",
12 bnb_4bit_compute_dtype=torch.bfloat16,
13 bnb_4bit_use_double_quant=True,
14)
15
16base_model = AutoModelForCausalLM.from_pretrained(
17 BASE_MODEL_ID, quantization_config=bnb_config, device_map="auto",
18)
19model = PeftModel.from_pretrained(base_model, ADAPTER_DIR)
20model.eval()
21
22tokenizer = AutoTokenizer.from_pretrained(ADAPTER_DIR) # loads the training chat template
23tokenizer.pad_token = tokenizer.pad_token or tokenizer.eos_token
24
25SYSTEM_PROMPT = """You are a workplace maintenance ticket cleaner and translator.
26You receive a raw Arabic maintenance ticket (title and description) and must output ONLY a JSON object with three fields: "title", "description", "reasoning".
27
28Rules:
29- Fix spelling/typos without changing the meaning.
30- If the title and description repeat the same issue, merge them.
31- If a ticket has multiple distinct issues, split the description into bullet points (one per issue).
32- Translate place names to their standard English form.
33- Normalize all variant spellings of store signage (جارمة، الجارمة، ارمات، الارمات، قارمة، القارمه، قارمه، قارمت، القارمة، للقارمه، بقارما) to the same English term.
34- Translate everything to clear, professional English.
35- "reasoning" is a short note explaining what was fixed and how it was translated.
36Output valid JSON only, no extra text."""
37
38def clean_ticket(source_title, source_description):
39 messages = [
40 {"role": "system", "content": SYSTEM_PROMPT},
41 {"role": "user", "content": json.dumps(
42 {"title": source_title, "description": source_description}, ensure_ascii=False)},
43 ]
44 prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
45 inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
46 out = model.generate(**inputs, max_new_tokens=300, do_sample=False)
47 return tokenizer.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
48
49print(clean_ticket("ماكينة الباريستا", "مشكلة بجروبين الاول ما بشبك و الثاني بنزل ماء من الاطراف"))NousResearch/Meta-Llama-3.1-8B-Instruct or, once access is approved,
meta-llama/Llama-3.1-8B-Instruct) and attach this adapter with
peft.PeftModel.from_pretrained, as shown above. Load the tokenizer from the adapter
directory, not the base model, since the saved chat_template.jinja contains
generation-span markers required for the model to have been trained correctly.case_id, source_title, source_description (Arabic
input), target_title, target_description, reasoning (English target, combined into a
single JSON object as the training label).system / user / assistant message list (not a
pre-flattened string), so that trl's assistant_only_loss masking correctly restricts the
training loss to the assistant's JSON output only. The base model's default chat template
was patched with explicit {% generation %} / {% endgeneration %} markers to support this
masking.r=16, alpha=32, dropout=0.05, targeting q_proj, k_proj, v_proj,
o_proj, gate_proj, up_proj, down_projassistant_only_loss=True)validation split of the same dataset, including a manually curated subset of
case IDs chosen to stress-test known tricky patterns: signage-word spelling variants
(قائمة/القارمة/القارمه/بقارما), multi-issue tickets (numbered lists, "+"-joined issues),
and single vs. merged-issue tickets.title, description, reasoning).sentence-transformers,
all-mpnet-base-v2 embeddings) between model output and ground truth, computed
separately for title, description, and combined title+description, averaged across the
validation set.| Metric | Value |
|---|---|
| JSON validity rate | TBD — fill in from your Cell 12 output |
| Mean title similarity | TBD |
| Mean description similarity | TBD |
| Mean combined similarity | TBD |
q_proj, k_proj, v_proj, o_proj) and MLP
(gate_proj, up_proj, down_proj) projection layers, with the base weights frozen.
Training objective: standard next-token cross-entropy loss, masked to the assistant's JSON
output span only.transformerspefttrl 1.8.0bitsandbytes 0.49.2