BEncoderRT commited on
Commit
52dd443
·
verified ·
1 Parent(s): b1a8a4d

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +351 -3
README.md CHANGED
@@ -1,3 +1,351 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ language:
4
+ - en
5
+ - fr
6
+ pipeline_tag: translation
7
+ tags:
8
+ - translation
9
+ - lora
10
+ - peft
11
+ - finetuning
12
+ - tinyllama
13
+ - opus-100
14
+ base_model:
15
+ - TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T
16
+ datasets:
17
+ - Helsinki-NLP/opus-100
18
+ ---
19
+
20
+ # EN–FR Translation LoRA on TinyLlama-1.1B
21
+
22
+ A lightweight LoRA adapter fine-tuned on the **TinyLlama-1.1B** base model for ** **English → French translation**** (binary classification: Positive or Negative).
23
+
24
+ Trained with **supervised fine-tuning (SFT)** on only **8,000 examples** from the Hugging Face **Helsinki-NLP/opus-100** dataset, formatted as instruction prompts. Despite the limited data, it achieves solid performance with very low memory usage and fast inference.
25
+
26
+ ## Model Details
27
+
28
+ - **Base Model**: `TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T`
29
+ - **LoRA Configuration**:
30
+ - Rank (`r`): 8
31
+ - Scaling (`lora_alpha`): 16
32
+ - Target modules: `["q_proj", "v_proj"]`
33
+ - Dropout: 0.05
34
+ - Bias: "none"
35
+ - Task type: `CAUSAL_LM`
36
+ - **Training Data**: 8,000 labeled samples from **Helsinki-NLP/opus-100** (balanced)
37
+ - **Training Method**: Instruction-tuned SFT using PEFT + TRL
38
+
39
+ ## Usage Example
40
+
41
+
42
+
43
+ ```python
44
+ from transformers import AutoTokenizer, AutoModelForCausalLM
45
+ from peft import PeftModel
46
+ import torch
47
+
48
+ # 定义基础模型和 LoRA 模型仓库
49
+ base_model_name = "TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T"
50
+ repo_id_translation = "BEncoderRT/EN-FR-Translation-LoRA-TinyLlama-1.1B" # 请确认此 repo 是否存在,若不存在请替换为正确 ID 或本地路径
51
+
52
+ # 加载 tokenizer
53
+ tokenizer = AutoTokenizer.from_pretrained(base_model_name)
54
+ if tokenizer.pad_token is None:
55
+ tokenizer.pad_token = tokenizer.eos_token
56
+
57
+ # 加载基础模型(仅加载一次)
58
+ base_model = AutoModelForCausalLM.from_pretrained(
59
+ base_model_name,
60
+ device_map="auto", # 自动分配到 GPU/CPU
61
+ torch_dtype=torch.float16 # 推荐使用 half precision 节省显存
62
+ )
63
+
64
+ # 单独加载 translation LoRA adapter
65
+ translation_model = PeftModel.from_pretrained(
66
+ base_model,
67
+ repo_id_translation,
68
+ adapter_name="translation" # 可选,默认为 default
69
+ )
70
+
71
+ translation_model.eval() # 设置为评估模式
72
+ print("Translation LoRA 模型加载完成。")
73
+
74
+ # 推理函数(仅针对 translation 任务:English to French)
75
+ def translation_inference(model, tokenizer, english_text, max_new_tokens=100):
76
+ # 设置 adapter(如果有多个,这里确保使用 translation)
77
+ if hasattr(model, "set_adapter"):
78
+ model.set_adapter("translation")
79
+
80
+ # 构造 prompt(根据原多任务代码的格式)
81
+ formatted_prompt = (
82
+ "### Task: Translation (English to French)\n"
83
+ "### English:\n"
84
+ f"{english_text}\n"
85
+ "### French:\n"
86
+ )
87
+
88
+ inputs = tokenizer(formatted_prompt, return_tensors="pt", truncation=True, max_length=512).to(model.device)
89
+
90
+ with torch.no_grad():
91
+ outputs = model.generate(
92
+ **inputs,
93
+ max_new_tokens=max_new_tokens,
94
+ do_sample=True,
95
+ temperature=0.7,
96
+ top_k=50,
97
+ top_p=0.95,
98
+ eos_token_id=tokenizer.eos_token_id,
99
+ pad_token_id=tokenizer.pad_token_id
100
+ )
101
+
102
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
103
+
104
+ # 提取翻译结果
105
+ answer_start = generated_text.find("### French:\n")
106
+ if answer_start != -1:
107
+ extracted = generated_text[answer_start + len("### French:\n"):].strip()
108
+ # 去除可能的后续内容(如另一个 ###)
109
+ end_index = extracted.find("###")
110
+ if end_index != -1:
111
+ extracted = extracted[:end_index].strip()
112
+ return extracted
113
+
114
+ return generated_text # Fallback:返回完整生成文本
115
+
116
+ # --- 测试用例 ---
117
+ print("\n测试 English to French Translation:")
118
+
119
+ english_1 = "The quick brown fox jumps over the lazy dog."
120
+ print(f"English: {english_1}")
121
+ print(f"French: {translation_inference(translation_model, tokenizer, english_1)}\n")
122
+
123
+ english_2 = "Life is beautiful."
124
+ print(f"English: {english_2}")
125
+ print(f"French: {translation_inference(translation_model, tokenizer, english_2)}\n")
126
+
127
+ english_3 = "Hello, how are you today? I hope everything is going well."
128
+ print(f"English: {english_3}")
129
+ print(f"French: {translation_inference(translation_model, tokenizer, english_3)}\n")
130
+
131
+ english_4 = "Machine learning is a subset of artificial intelligence that focuses on the development of algorithms capable of learning from data."
132
+ print(f"English: {english_4}")
133
+ print(f"French: {translation_inference(translation_model, tokenizer, english_4)}")
134
+ ```
135
+
136
+
137
+
138
+ ```
139
+ Translation LoRA 模型加载完成。
140
+
141
+ 测试 English to French Translation:
142
+ English: The quick brown fox jumps over the lazy dog.
143
+ French: Le chien sauvage et le chat fastueux s'adressent à un autre chat, qui ne voit rien.
144
+
145
+ English: Life is beautiful.
146
+ French: La vie est belle.
147
+
148
+ English: Hello, how are you today? I hope everything is going well.
149
+ French: Bonjour, comment ça va aujourd'hui ?
150
+
151
+ English: Machine learning is a subset of artificial intelligence that focuses on the development of algorithms capable of learning from data.
152
+ French: Le machine learning est un sous-ensemble de l'intelligence artificielle qui s'intéresse au développement d'algorithmes capables de se former en apprenant les données.
153
+ ```
154
+
155
+
156
+
157
+
158
+ # Model Card for Model ID
159
+
160
+ <!-- Provide a quick summary of what the model is/does. -->
161
+
162
+ This modelcard aims to be a base template for new models. It has been generated using [this raw template](https://github.com/huggingface/huggingface_hub/blob/main/src/huggingface_hub/templates/modelcard_template.md?plain=1).
163
+
164
+ ## Model Details
165
+
166
+ ### Model Description
167
+
168
+ <!-- Provide a longer summary of what this model is. -->
169
+
170
+
171
+
172
+ - **Developed by:** [More Information Needed]
173
+ - **Funded by [optional]:** [More Information Needed]
174
+ - **Shared by [optional]:** [More Information Needed]
175
+ - **Model type:** [More Information Needed]
176
+ - **Language(s) (NLP):** [More Information Needed]
177
+ - **License:** [More Information Needed]
178
+ - **Finetuned from model [optional]:** [More Information Needed]
179
+
180
+ ### Model Sources [optional]
181
+
182
+ <!-- Provide the basic links for the model. -->
183
+
184
+ - **Repository:** [More Information Needed]
185
+ - **Paper [optional]:** [More Information Needed]
186
+ - **Demo [optional]:** [More Information Needed]
187
+
188
+ ## Uses
189
+
190
+ <!-- Address questions around how the model is intended to be used, including the foreseeable users of the model and those affected by the model. -->
191
+
192
+ ### Direct Use
193
+
194
+ <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
195
+
196
+ [More Information Needed]
197
+
198
+ ### Downstream Use [optional]
199
+
200
+ <!-- This section is for the model use when fine-tuned for a task, or when plugged into a larger ecosystem/app -->
201
+
202
+ [More Information Needed]
203
+
204
+ ### Out-of-Scope Use
205
+
206
+ <!-- This section addresses misuse, malicious use, and uses that the model will not work well for. -->
207
+
208
+ [More Information Needed]
209
+
210
+ ## Bias, Risks, and Limitations
211
+
212
+ <!-- This section is meant to convey both technical and sociotechnical limitations. -->
213
+
214
+ [More Information Needed]
215
+
216
+ ### Recommendations
217
+
218
+ <!-- This section is meant to convey recommendations with respect to the bias, risk, and technical limitations. -->
219
+
220
+ Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
221
+
222
+ ## How to Get Started with the Model
223
+
224
+ Use the code below to get started with the model.
225
+
226
+ [More Information Needed]
227
+
228
+ ## Training Details
229
+
230
+ ### Training Data
231
+
232
+ <!-- This should link to a Dataset Card, perhaps with a short stub of information on what the training data is all about as well as documentation related to data pre-processing or additional filtering. -->
233
+
234
+ [More Information Needed]
235
+
236
+ ### Training Procedure
237
+
238
+ <!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
239
+
240
+ #### Preprocessing [optional]
241
+
242
+ [More Information Needed]
243
+
244
+
245
+ #### Training Hyperparameters
246
+
247
+ - **Training regime:** [More Information Needed] <!--fp32, fp16 mixed precision, bf16 mixed precision, bf16 non-mixed precision, fp16 non-mixed precision, fp8 mixed precision -->
248
+
249
+ #### Speeds, Sizes, Times [optional]
250
+
251
+ <!-- This section provides information about throughput, start/end time, checkpoint size if relevant, etc. -->
252
+
253
+ [More Information Needed]
254
+
255
+ ## Evaluation
256
+
257
+ <!-- This section describes the evaluation protocols and provides the results. -->
258
+
259
+ ### Testing Data, Factors & Metrics
260
+
261
+ #### Testing Data
262
+
263
+ <!-- This should link to a Dataset Card if possible. -->
264
+
265
+ [More Information Needed]
266
+
267
+ #### Factors
268
+
269
+ <!-- These are the things the evaluation is disaggregating by, e.g., subpopulations or domains. -->
270
+
271
+ [More Information Needed]
272
+
273
+ #### Metrics
274
+
275
+ <!-- These are the evaluation metrics being used, ideally with a description of why. -->
276
+
277
+ [More Information Needed]
278
+
279
+ ### Results
280
+
281
+ [More Information Needed]
282
+
283
+ #### Summary
284
+
285
+
286
+
287
+ ## Model Examination [optional]
288
+
289
+ <!-- Relevant interpretability work for the model goes here -->
290
+
291
+ [More Information Needed]
292
+
293
+ ## Environmental Impact
294
+
295
+ <!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
296
+
297
+ Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
298
+
299
+ - **Hardware Type:** [More Information Needed]
300
+ - **Hours used:** [More Information Needed]
301
+ - **Cloud Provider:** [More Information Needed]
302
+ - **Compute Region:** [More Information Needed]
303
+ - **Carbon Emitted:** [More Information Needed]
304
+
305
+ ## Technical Specifications [optional]
306
+
307
+ ### Model Architecture and Objective
308
+
309
+ [More Information Needed]
310
+
311
+ ### Compute Infrastructure
312
+
313
+ [More Information Needed]
314
+
315
+ #### Hardware
316
+
317
+ [More Information Needed]
318
+
319
+ #### Software
320
+
321
+ [More Information Needed]
322
+
323
+ ## Citation [optional]
324
+
325
+ <!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
326
+
327
+ **BibTeX:**
328
+
329
+ [More Information Needed]
330
+
331
+ **APA:**
332
+
333
+ [More Information Needed]
334
+
335
+ ## Glossary [optional]
336
+
337
+ <!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
338
+
339
+ [More Information Needed]
340
+
341
+ ## More Information [optional]
342
+
343
+ [More Information Needed]
344
+
345
+ ## Model Card Authors [optional]
346
+
347
+ [More Information Needed]
348
+
349
+ ## Model Card Contact
350
+
351
+ [More Information Needed]