File size: 2,420 Bytes
d41247e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54b81c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d41247e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
---
license: mit
datasets:
- ahmedheakl/arzen-llm-dataset
language:
- ar
- en
metrics:
- bleu
- ecody726/bertscore
- meteor
library_name: transformers
pipeline_tag: translation
---

## How to use
Just install `peft`, `transformers` and `pytorch` first.

```bash
pip install peft transformers torch
```

Then login with your huggingface token to get access to base models
```bash
huggingface-cli login --token <YOUR_HF_TOKEN>
```

Then load the model.
```python
from peft import PeftConfig, PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer

peft_model_id = "ahmedheakl/arazn-gemma1.1-7B-arabic"
peft_config = PeftConfig.from_pretrained(peft_model_id)
base_model_name = peft_config.base_model_name_or_path
base_model = AutoModelForCausalLM.from_pretrained(base_model_name)
model = PeftModel.from_pretrained(base_model, peft_model_id)
model = model.to("cuda")
tokenizer = AutoTokenizer.from_pretrained(peft_model_id)
```

Then do inference
```python
import torch

raw_prompt = """<bos><start_of_turn>user
Translate the following code-switched Arabic-English-mixed text to Arabic only.
{source}<end_of_turn>
<start_of_turn>model
"""
def inference(prompt) -> str:
    prompt = raw_prompt.format(source=prompt)
    inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
    generated_ids = model.generate(
        **inputs,
        use_cache=True,
        num_return_sequences=1,
        max_new_tokens=100,
        do_sample=True,
        num_beams=1,
        temperature=0.7,
        eos_token_id=tokenizer.eos_token_id,
        pad_token_id=tokenizer.pad_token_id,
    )
    outputs = tokenizer.batch_decode(generated_ids)[0]
    torch.cuda.empty_cache()
    torch.cuda.synchronize()
    return outputs.split("<start_of_turn>model\n")[-1].split("<end_of_turn>")[0]

print(inference("أنا أحب الbanana")) # I like bananas.
```

**Please see paper & code for more information:**
- https://github.com/ahmedheakl/arazn-llm
- https://arxiv.org/abs/2406.18120


## Citation

**BibTeX:**
```
@article{heakl2024arzen,
  title={ArzEn-LLM: Code-Switched Egyptian Arabic-English Translation and Speech Recognition Using LLMs},
  author={Heakl, Ahmed and Zaghloul, Youssef and Ali, Mennatullah and Hossam, Rania and Gomaa, Walid},
  journal={arXiv preprint arXiv:2406.18120},
  year={2024}
}
```


## Model Card Authors

- Email: [email protected]
- Linkedin: https://linkedin.com/in/ahmed-heakl