RohanAi commited on
Commit
548fc5a
·
verified ·
1 Parent(s): 0a5ee29

Delete lang_translator.ipynb

Browse files
Files changed (1) hide show
  1. lang_translator.ipynb +0 -205
lang_translator.ipynb DELETED
@@ -1,205 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": 3,
6
- "id": "21a4341f",
7
- "metadata": {},
8
- "outputs": [],
9
- "source": [
10
- "import tqdm as notebook_tqdm"
11
- ]
12
- },
13
- {
14
- "cell_type": "code",
15
- "execution_count": 3,
16
- "id": "38dcf44c",
17
- "metadata": {},
18
- "outputs": [],
19
- "source": [
20
- "from transformers import AutoTokenizer, AutoModelForSeq2SeqLM\n",
21
- "import tqdm as notebook_tqdm\n",
22
- "import torch\n",
23
- "# Path where you want to store/cache the model\n",
24
- "cache_path = \"./\"\n",
25
- "\n",
26
- "# device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
27
- "model_name = \"facebook/nllb-200-distilled-600M\"\n",
28
- "# tokenizer = AutoTokenizer.from_pretrained(model_name, cache_dir=cache_path)\n",
29
- "# model = AutoModelForSeq2SeqLM.from_pretrained(model_name, cache_dir=cache_path).to(device)\n"
30
- ]
31
- },
32
- {
33
- "cell_type": "code",
34
- "execution_count": 4,
35
- "id": "b0860374",
36
- "metadata": {},
37
- "outputs": [],
38
- "source": [
39
- "from transformers import BitsAndBytesConfig\n",
40
- "# Quantization config (8-bit)\n",
41
- "bnb_config = BitsAndBytesConfig(\n",
42
- " load_in_8bit=True,\n",
43
- " llm_int8_threshold=6.0\n",
44
- ")\n",
45
- "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
46
- "# Load tokenizer\n",
47
- "tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
48
- "\n",
49
- "# Load quantized model\n",
50
- "model = AutoModelForSeq2SeqLM.from_pretrained(\n",
51
- " model_name,\n",
52
- " device_map=\"auto\",\n",
53
- " quantization_config=bnb_config\n",
54
- ")\n",
55
- "\n"
56
- ]
57
- },
58
- {
59
- "cell_type": "code",
60
- "execution_count": 5,
61
- "id": "3a38e931",
62
- "metadata": {},
63
- "outputs": [
64
- {
65
- "name": "stderr",
66
- "output_type": "stream",
67
- "text": [
68
- "/blue/parisa.rashidi/rohanbagulwar/hackathon/.venv/lib/python3.13/site-packages/transformers/modeling_utils.py:4037: UserWarning: Moving the following attributes in the config to the generation config: {'max_length': 200}. You are seeing this warning because you've set generation parameters in the model config, as opposed to in the generation config.\n",
69
- " warnings.warn(\n"
70
- ]
71
- }
72
- ],
73
- "source": [
74
- "SAVE_DIR = \"./nllb-600M-quantized\" \n",
75
- "tokenizer.save_pretrained(SAVE_DIR)\n",
76
- "model.save_pretrained(SAVE_DIR)"
77
- ]
78
- },
79
- {
80
- "cell_type": "code",
81
- "execution_count": null,
82
- "id": "6dac6669",
83
- "metadata": {},
84
- "outputs": [
85
- {
86
- "name": "stdout",
87
- "output_type": "stream",
88
- "text": [
89
- "नमस्कार, कसे आहात तुम्ही कुठे आहात तुम्ही कुठे जात आहात?\n"
90
- ]
91
- }
92
- ],
93
- "source": [
94
- "punct_normalizer = MosesPunctNormalizer(lang=\"en\")\n",
95
- "\n",
96
- "def translate(text: str, src_lang: str, tgt_lang: str):\n",
97
- " src_code = code_mapping[src_lang] # e.g. \"English\" -> \"eng_Latn\"\n",
98
- " tgt_code = code_mapping[tgt_lang] # e.g. \"Hindi\" -> \"hin_Deva\"\n",
99
- "\n",
100
- " tokenizer.src_lang = src_code\n",
101
- " tokenizer.tgt_lang = tgt_code\n",
102
- "\n",
103
- " # Normalize punctuation\n",
104
- " text = punct_normalizer.normalize(text)\n",
105
- "\n",
106
- " # Encode & generate\n",
107
- " inputs = tokenizer(text, return_tensors=\"pt\").to(device)\n",
108
- " outputs = model.generate(\n",
109
- " **inputs,\n",
110
- " forced_bos_token_id=tokenizer.convert_tokens_to_ids(tgt_code), # ✅ use FLORES code\n",
111
- " max_length=256,\n",
112
- " num_beams=5,\n",
113
- " no_repeat_ngram_size=4,\n",
114
- " )\n",
115
- " return tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
116
- "\n",
117
- "\n",
118
- "# Example usage\n",
119
- "print(translate(\"Hello, how are you where are you wher are you going?\", \"English\", \"Marathi\"))"
120
- ]
121
- },
122
- {
123
- "cell_type": "code",
124
- "execution_count": null,
125
- "id": "8dd925b3",
126
- "metadata": {},
127
- "outputs": [],
128
- "source": [
129
- "!uv pip install -U bitsandbytes"
130
- ]
131
- },
132
- {
133
- "cell_type": "code",
134
- "execution_count": 3,
135
- "id": "648597dd",
136
- "metadata": {},
137
- "outputs": [],
138
- "source": [
139
- "from flores import code_mapping\n",
140
- "from sacremoses import MosesPunctNormalizer\n",
141
- "\n"
142
- ]
143
- },
144
- {
145
- "cell_type": "code",
146
- "execution_count": 5,
147
- "id": "d1f14e91",
148
- "metadata": {},
149
- "outputs": [
150
- {
151
- "name": "stdout",
152
- "output_type": "stream",
153
- "text": [
154
- "नमस्कार, कसे आहात तुम्ही कुठे आहात तुम्ही कुठे जात आहात?\n"
155
- ]
156
- }
157
- ],
158
- "source": [
159
- "punct_normalizer = MosesPunctNormalizer(lang=\"en\")\n",
160
- "\n",
161
- "def translate(text: str, src_lang: str, tgt_lang: str):\n",
162
- " src_code = code_mapping[src_lang] # e.g. \"English\" -> \"eng_Latn\"\n",
163
- " tgt_code = code_mapping[tgt_lang] # e.g. \"Hindi\" -> \"hin_Deva\"\n",
164
- "\n",
165
- " tokenizer.src_lang = src_code\n",
166
- " tokenizer.tgt_lang = tgt_code\n",
167
- "\n",
168
- " # Normalize punctuation\n",
169
- " text = punct_normalizer.normalize(text)\n",
170
- "\n",
171
- " # Encode & generate\n",
172
- " inputs = tokenizer(text, return_tensors=\"pt\").to(device)\n",
173
- " outputs = model.generate(\n",
174
- " **inputs,\n",
175
- " forced_bos_token_id=tokenizer.convert_tokens_to_ids(tgt_code), # ✅ use FLORES code\n",
176
- " max_length=256,\n",
177
- " num_beams=5,\n",
178
- " no_repeat_ngram_size=4,\n",
179
- " )\n",
180
- " return tokenizer.decode(outputs[0], skip_special_tokens=True)\n",
181
- "\n",
182
- "\n",
183
- "# Example usage\n",
184
- "print(translate(\"Hello, how are you where are you wher are you going?\", \"English\", \"Marathi\"))"
185
- ]
186
- },
187
- {
188
- "cell_type": "code",
189
- "execution_count": null,
190
- "id": "544890af",
191
- "metadata": {},
192
- "outputs": [],
193
- "source": []
194
- }
195
- ],
196
- "metadata": {
197
- "kernelspec": {
198
- "display_name": "hackathon",
199
- "language": "python",
200
- "name": "hackathon"
201
- }
202
- },
203
- "nbformat": 4,
204
- "nbformat_minor": 5
205
- }