BEncoderRT commited on
Commit
f3e58b3
·
verified ·
1 Parent(s): d4220b1

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +0 -145
README.md CHANGED
@@ -100,151 +100,6 @@ base_model: TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T
100
 
101
  # Usage Example
102
 
103
- ```python
104
- from transformers import AutoTokenizer, AutoModelForCausalLM
105
- from peft import PeftModel
106
-
107
- # Define the base model and the LoRA model repositories
108
- base_model_name = "TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T"
109
- repo_id_sentiment = "BEncoderRT/IMDB-Sentiment-LoRA-TinyLlama-1.1B"
110
- repo_id_translation = "BEncoderRT/EN-FR-Translation-LoRA-TinyLlama-1.1B"
111
-
112
- # Load the tokenizer (assuming it's consistent across tasks and already defined earlier)
113
- tokenizer = AutoTokenizer.from_pretrained(base_model_name)
114
- tokenizer.pad_token = tokenizer.eos_token
115
-
116
- # Load the base model once
117
- base_model_for_inference = AutoModelForCausalLM.from_pretrained(
118
- base_model_name,
119
- device_map="auto"
120
- )
121
-
122
- # Load the first adapter (sentiment) on top of the base model
123
- # This creates the initial PeftModel instance with the 'sentiment' adapter active
124
- multi_task_model = PeftModel.from_pretrained(
125
- base_model_for_inference,
126
- repo_id_sentiment,
127
- adapter_name="sentiment" # Name this adapter
128
- )
129
-
130
- # Load the second adapter (translation) into the existing PeftModel instance
131
- multi_task_model.load_adapter(
132
- repo_id_translation,
133
- adapter_name="translation" # Name this adapter
134
- )
135
-
136
- multi_task_model.eval() # Set the model to evaluation mode
137
-
138
- print("Base model loaded and both sentiment and translation adapters attached.")
139
- print(f"Loaded adapters: {list(multi_task_model.peft_config.keys())}")
140
- ```
141
-
142
- ```python
143
- import torch
144
-
145
- def multi_task_inference(model, tokenizer, prompt_text, task_type, max_new_tokens=100):
146
- if task_type == "sentiment":
147
- model.set_adapter("sentiment")
148
- formatted_prompt = (
149
- "### Task: Sentiment Analysis\n"
150
- "### Review:\n"
151
- f"{prompt_text}\n"
152
- "### Answer:\n"
153
- )
154
- elif task_type == "translation":
155
- model.set_adapter("translation")
156
- formatted_prompt = (
157
- "### Task: Translation (English to French)\n"
158
- "### English:\n"
159
- f"{prompt_text}\n"
160
- "### French:\n"
161
- )
162
- else:
163
- raise ValueError("Invalid task_type. Must be 'sentiment' or 'translation'.")
164
-
165
- inputs = tokenizer(formatted_prompt, return_tensors="pt", truncation=True, max_length=512).to(model.device)
166
-
167
- with torch.no_grad():
168
- outputs = model.generate(
169
- **inputs,
170
- max_new_tokens=max_new_tokens,
171
- do_sample=True,
172
- temperature=0.7,
173
- top_k=50,
174
- top_p=0.95,
175
- eos_token_id=tokenizer.eos_token_id,
176
- pad_token_id=tokenizer.pad_token_id
177
- )
178
-
179
- generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
180
-
181
- # Extracting the answer based on task type
182
- if task_type == "sentiment":
183
- answer_start_index = generated_text.find("### Answer:\n")
184
- if answer_start_index != -1:
185
- extracted_answer = generated_text[answer_start_index + len("### Answer:\n"):].strip()
186
- if 'positive' in extracted_answer.lower():
187
- return 'positive'
188
- elif 'negative' in extracted_answer.lower():
189
- return 'negative'
190
- return extracted_answer.split('\n')[0].strip() # Fallback for sentiment
191
- return generated_text
192
- elif task_type == "translation":
193
- answer_start_index = generated_text.find("### French:\n")
194
- if answer_start_index != -1:
195
- extracted_answer = generated_text[answer_start_index + len("### French:\n"):].strip()
196
- end_of_french = extracted_answer.find("###")
197
- if end_of_french != -1:
198
- extracted_answer = extracted_answer[:end_of_french].strip()
199
- return extracted_answer
200
- return generated_text
201
-
202
- # --- Test Cases ---
203
- print("Testing Sentiment Analysis:")
204
- positive_review = "This movie was absolutely fantastic! The acting was superb and the story was captivating."
205
- print(f"Review: {positive_review}")
206
- print(f"Sentiment: {multi_task_inference(multi_task_model, tokenizer, positive_review, 'sentiment')}\n")
207
-
208
- negative_review = "I found this film to be incredibly boring and predictable. A complete waste of time."
209
- print(f"Review: {negative_review}")
210
- print(f"Sentiment: {multi_task_inference(multi_task_model, tokenizer, negative_review, 'sentiment')}\n")
211
-
212
- print("Testing English-French Translation:")
213
- english_sentence_1 = "The quick brown fox jumps over the lazy dog."
214
- print(f"English: {english_sentence_1}")
215
- print(f"French: {multi_task_inference(multi_task_model, tokenizer, english_sentence_1, 'translation')}\n")
216
-
217
- english_sentence_2 = "Life is beautiful."
218
- print(f"English: {english_sentence_2}")
219
- print(f"French: {multi_task_inference(multi_task_model, tokenizer, english_sentence_2, 'translation')}")
220
- ```
221
-
222
- ```
223
- Testing Sentiment Analysis:
224
- Review: This movie was absolutely fantastic! The acting was superb and the story was captivating.
225
- Sentiment: positive
226
-
227
- Review: I found this film to be incredibly boring and predictable. A complete waste of time.
228
- Sentiment: negative
229
-
230
- Testing English-French Translation:
231
- English: The quick brown fox jumps over the lazy dog.
232
- French: Le chien laissé derrière lui, il n'y a pas de fauve.
233
-
234
- English: Life is beautiful.
235
- French: La vie est beau.
236
- ```
237
-
238
- This project demonstrates:
239
-
240
- - ✅ True **multi-task inference** with LoRA
241
- - ✅ No catastrophic forgetting
242
- - ✅ No base model retraining
243
- - ✅ Clean task separation via adapters
244
- - ✅ Efficient GPU memory usage
245
-
246
- This pattern scales naturally to **more tasks** (e.g. summarization, classification, safety).
247
-
248
  ```python
249
  from transformers import AutoTokenizer, AutoModelForCausalLM
250
 
 
100
 
101
  # Usage Example
102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  ```python
104
  from transformers import AutoTokenizer, AutoModelForCausalLM
105