File size: 10,001 Bytes
5c4fb2b
ffd0d01
c00a8e8
 
 
 
ffd0d01
 
 
 
 
 
 
 
5c4fb2b
c00a8e8
be49435
 
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
 
 
c00a8e8
ffd0d01
 
 
 
be49435
 
 
ffd0d01
be49435
 
 
 
 
 
 
 
 
 
c00a8e8
be49435
 
 
 
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
 
 
 
 
 
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
 
 
 
 
 
 
 
 
 
 
 
 
 
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
 
 
 
 
 
 
 
 
ffd0d01
c00a8e8
be49435
c00a8e8
be49435
 
 
c00a8e8
ffd0d01
 
be49435
 
 
 
 
 
ffd0d01
c00a8e8
ffd0d01
be49435
 
 
 
 
 
 
c00a8e8
be49435
c00a8e8
be49435
 
 
 
 
 
 
ffd0d01
be49435
ffd0d01
 
c00a8e8
be49435
ffd0d01
be49435
 
ffd0d01
c00a8e8
ffd0d01
c00a8e8
be49435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c00a8e8
ffd0d01
 
 
 
c00a8e8
be49435
 
 
c00a8e8
be49435
ffd0d01
 
be49435
 
 
 
 
ffd0d01
c00a8e8
be49435
 
 
ffd0d01
 
c00a8e8
be49435
ffd0d01
 
 
c00a8e8
be49435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffd0d01
be49435
 
 
 
 
 
 
 
 
ffd0d01
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
 
 
 
 
 
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
 
 
 
 
 
 
 
 
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
c00a8e8
be49435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c00a8e8
ffd0d01
 
c00a8e8
be49435
c00a8e8
be49435
 
 
c00a8e8
ffd0d01
c00a8e8
be49435
 
 
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
---
license: other
base_model: google/gemma-3-1b-it
library_name: peft
pipeline_tag: text-generation
tags:
  - balochi
  - english-to-balochi
  - translation
  - machine-translation
  - gemma
  - lora
  - peft
  - low-resource-language
---

# BalochiBridge πŸŒ‰
### English-to-Balochi Machine Translation Using Gemma 3 1B and LoRA

BalochiBridge is an AI-powered English-to-Balochi translation model developed using Google's Gemma 3 1B Instruct model and Low-Rank Adaptation (LoRA).

The project adapts a pretrained language model for English-to-Balochi translation using a Balochi language dataset. It aims to support Natural Language Processing (NLP) research and contribute to the development of AI tools for the Balochi language.

**This repository contains the fine-tuned LoRA adapter. It does not contain the complete Gemma base model.**

## 1. Model Details

| Property | Description |
|---|---|
| Model Name | BalochiBridge |
| Task | English-to-Balochi Translation |
| Base Model | Google Gemma 3 1B Instruct |
| Base Model ID | `google/gemma-3-1b-it` |
| Fine-Tuning Method | LoRA |
| Framework | Hugging Face Transformers |
| Fine-Tuning Library | PEFT |
| Quantization During Training | 4-bit NF4 |
| Dataset | `Aynkader/Balochi-dataset` |
| Model Type | LoRA Adapter |

## 2. Important: Gemma Access Requirement

The base model used by BalochiBridge is a gated Hugging Face model.

Before running BalochiBridge, you must:

1. Create or sign in to your Hugging Face account.
2. Visit [Google Gemma 3 1B Instruct](https://huggingface.co/google/gemma-3-1b-it).
3. Accept Google's model access conditions.
4. Wait until your account has been granted access.

**Important:** You need access to the Gemma base model even though the BalochiBridge adapter repository is public.

You also need a Hugging Face access token with read permission. Your token is private and must never be shared.

## 3. Run BalochiBridge Directly in Google Colab

The easiest way to use BalochiBridge is through Google Colab.

**Recommended environment:**
- Google Colab
- Python 3
- GPU runtime (T4 or better recommended)
- Internet connection
- Hugging Face account with Gemma access

### Step 1: Enable GPU

Open [Google Colab](https://colab.research.google.com/).

Create a new notebook.

Select:

`Runtime β†’ Change runtime type β†’ T4 GPU`

Save the settings.

### Step 2: Install Dependencies

Run this cell:

```python
!pip uninstall -y torchao -q

!pip install -q -U \
    transformers \
    peft \
    accelerate \
    sentencepiece \
    huggingface_hub
```

**Important:** After installation, restart the Colab runtime using:

`Runtime β†’ Restart session`

This prevents old packages from remaining loaded in memory.

### Step 3: Authenticate with Hugging Face

Run:

```python
from huggingface_hub import notebook_login, whoami

notebook_login()

account = whoami()

print("Logged in successfully!")
print("Hugging Face account:", account["name"])
```

When prompted, enter your own Hugging Face access token.

### Step 4: Load the Base Model and BalochiBridge Adapter

Run:

```python
import torch

from transformers import (
    AutoTokenizer,
    AutoModelForCausalLM
)

from peft import PeftModel

BASE_MODEL = "google/gemma-3-1b-it"
ADAPTER_MODEL = "ullahhafeez321/BalochiBridge"

# Check GPU availability
if not torch.cuda.is_available():
    raise RuntimeError(
        "GPU not detected. Enable a GPU runtime in Google Colab."
    )

print("GPU:", torch.cuda.get_device_name(0))

# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(
    BASE_MODEL
)

# Load pretrained Gemma model
base_model = AutoModelForCausalLM.from_pretrained(
    BASE_MODEL,
    torch_dtype=torch.float16,
    device_map="auto"
)

# Attach BalochiBridge LoRA adapter
model = PeftModel.from_pretrained(
    base_model,
    ADAPTER_MODEL
)

model.eval()

print("BalochiBridge loaded successfully!")
```

This loads the pretrained Gemma model and attaches the BalochiBridge adapter. No training or fine-tuning is required.

### Step 5: Create the Translation Function

Run:

```python
import torch

def translate_to_balochi(
    english_text,
    max_new_tokens=128
):
    if not english_text.strip():
        return "Please enter an English sentence."

    prompt = f"""Translate the following English text into Balochi.
Only provide the Balochi translation. Do not explain.

English: {english_text}
Balochi:"""

    inputs = tokenizer(
        prompt,
        return_tensors="pt"
    )

    # Use the device where the model's input embeddings are located
    input_device = model.get_input_embeddings().weight.device
    inputs = inputs.to(input_device)

    with torch.inference_mode():
        outputs = model.generate(
            **inputs,
            max_new_tokens=max_new_tokens,
            do_sample=False,
            repetition_penalty=1.1,
            pad_token_id=tokenizer.eos_token_id,
            eos_token_id=tokenizer.eos_token_id
        )

    # Extract only the generated translation
    generated_tokens = outputs[
        0,
        inputs["input_ids"].shape[1]:
    ]

    translation = tokenizer.decode(
        generated_tokens,
        skip_special_tokens=True
    ).strip()

    return translation
```

### Step 6: Translate Your First Sentence

Run:

```python
english = "Hello, how are you?"

translation = translate_to_balochi(english)

print("English:", english)
print("Balochi:", translation)
```

### Step 7: Translate Multiple Sentences

```python
test_sentences = [
    "Hello, how are you?",
    "My name is Ahmed.",
    "I live in Gwadar.",
    "I love my country.",
    "What is your name?",
    "Thank you very much.",
    "Today the weather is beautiful.",
    "I am learning artificial intelligence."
]

for sentence in test_sentences:

    translation = translate_to_balochi(sentence)

    print("\nEnglish:", sentence)
    print("Balochi:", translation)
    print("-" * 60)
```

### Step 8: Interactive Translation

You can also enter your own English sentences:

```python
while True:

    english = input(
        "\nEnter an English sentence (or type 'exit'): "
    )

    if english.lower().strip() == "exit":
        print("Translation session ended.")
        break

    translation = translate_to_balochi(english)

    print("\nBalochi Translation:")
    print(translation)
```

## 4. How BalochiBridge Works

The model follows this workflow:

1. **Input:** An English sentence is provided.
2. **Tokenization:** The tokenizer converts the input into tokens.
3. **Base Model:** Gemma 3 1B processes the input.
4. **LoRA Adapter:** The trained BalochiBridge adapter adapts the model for Balochi translation.
5. **Generation:** The model generates the Balochi translation.
6. **Output:** The generated tokens are decoded into readable text.

## 5. Training and Fine-Tuning

BalochiBridge was developed using Parameter-Efficient Fine-Tuning (PEFT), specifically Low-Rank Adaptation (LoRA).

LoRA makes it possible to adapt a pretrained model without updating all its original parameters. Instead, it introduces smaller trainable matrices that learn task-specific information.

The base model was loaded using 4-bit NF4 quantization during the training workflow to reduce GPU memory requirements.

**The published repository contains the resulting adapter weights, allowing users to use the trained model without repeating the training process.**

## 6. Evaluation Metrics

The following metrics can be used to evaluate BalochiBridge:

| Metric | Description |
|---|---|
| BLEU | Measures n-gram overlap between generated and reference translations. |
| chrF | Measures character-level similarity. |
| ROUGE-L | Measures the longest common subsequence. |
| CER | Measures character-level errors. |
| Exact Match | Measures the percentage of predictions that exactly match the reference. |

Higher BLEU, chrF, and ROUGE-L scores generally indicate greater similarity to reference translations. Lower CER indicates fewer character-level errors.

Actual evaluation scores should be reported separately using the results obtained from the project's test dataset.

## 7. Repository Contents

The Hugging Face repository contains the LoRA adapter and associated configuration and tokenizer files.

| File | Purpose |
|---|---|
| `adapter_model.safetensors` | Trained LoRA adapter weights |
| `adapter_config.json` | LoRA adapter configuration |
| `tokenizer.json` | Tokenizer vocabulary and configuration data |
| `tokenizer_config.json` | Tokenizer settings |
| `chat_template.jinja` | Chat formatting template |
| `training_args.bin` | Saved training arguments |
| `README.md` | Model documentation |

## 8. Limitations

- Translation quality depends on the quality and diversity of the training dataset.
- Balochi contains dialect and spelling variations.
- Low-resource language translation remains challenging.
- Some generated translations may contain grammatical or semantic errors.
- Human evaluation by fluent Balochi speakers is recommended.
- The model is intended for English-to-Balochi translation, not general-purpose translation.

## 9. Future Improvements

- Expand the English-Balochi parallel dataset.
- Improve translation quality across Balochi dialects.
- Perform additional evaluation using a larger independent test dataset.
- Improve grammatical accuracy and sentence fluency.
- Develop a web-based translation application.
- Explore additional fine-tuning and optimization methods.

## 10. Contributors

- Hafsa
- Abdullah

## 11. Acknowledgements

- [Google Gemma](https://huggingface.co/google/gemma-3-1b-it) β€” pretrained language model.
- [Hugging Face](https://huggingface.co/) β€” model hosting and machine learning libraries.
- [Aynkader/Balochi-dataset](https://huggingface.co/datasets/Aynkader/Balochi-dataset) β€” Balochi dataset.

---

**BalochiBridge β€” Building a bridge between English and Balochi through Artificial Intelligence.**

Model repository: [ullahhafeez321/BalochiBridge](https://huggingface.co/ullahhafeez321/BalochiBridge)