Commit
·
ef9f10e
1
Parent(s):
c14f62b
Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
datasets:
|
| 3 |
+
- tbboukhari/Alpaca_french_instruct
|
| 4 |
+
language:
|
| 5 |
+
- fr
|
| 6 |
+
library_name: transformers
|
| 7 |
+
tags:
|
| 8 |
+
- Alpaca
|
| 9 |
+
- Instruction-fine-tuning
|
| 10 |
+
- NLP
|
| 11 |
+
- Instruct Alpaca
|
| 12 |
+
- PEFT
|
| 13 |
+
- LoRA
|
| 14 |
+
---
|
| 15 |
+
|
| 16 |
+
## How to use🦙:
|
| 17 |
+
```py
|
| 18 |
+
import torch
|
| 19 |
+
import bitsandbytes as bnb
|
| 20 |
+
from peft import PeftModel, PeftConfig, prepare_model_for_int8_training, LoraConfig, get_peft_model
|
| 21 |
+
from transformers import LlamaTokenizer, LlamaForCausalLM, GenerationConfig
|
| 22 |
+
|
| 23 |
+
peft_model_id = "tbboukhari/Alpaca_instruction_fine_tune_French"
|
| 24 |
+
config = PeftConfig.from_pretrained(peft_model_id)
|
| 25 |
+
|
| 26 |
+
tokenizer = LlamaTokenizer.from_pretrained(config.base_model_name_or_path)
|
| 27 |
+
model = LlamaForCausalLM.from_pretrained(config.base_model_name_or_path,
|
| 28 |
+
load_in_8bit=True,
|
| 29 |
+
device_map="auto",)
|
| 30 |
+
# Load the Lora model
|
| 31 |
+
model = PeftModel.from_pretrained(model, peft_model_id)
|
| 32 |
+
|
| 33 |
+
# Based on the inference code by `tloen/alpaca-lora`
|
| 34 |
+
def generate_prompt(instruction, entree=None):
|
| 35 |
+
if entree :
|
| 36 |
+
return f"""Vous trouverez ci-dessous des instructions décrivant une tâche, ainsi qu'une entrée qui fournit plus de contexte. Rédigez une réponse qui complète convenablement la demande.
|
| 37 |
+
### instructions:
|
| 38 |
+
{instruction}
|
| 39 |
+
### entrée:
|
| 40 |
+
{entree}
|
| 41 |
+
### sortie:"""
|
| 42 |
+
|
| 43 |
+
else:
|
| 44 |
+
return f"""Vous trouverez ci-dessous des instructions décrivant une tâche, ainsi qu'une entrée qui fournit plus de contexte. Rédigez une réponse qui complète convenablement la demande.
|
| 45 |
+
|
| 46 |
+
### instructions:
|
| 47 |
+
{instruction}
|
| 48 |
+
### sortie:"""
|
| 49 |
+
|
| 50 |
+
# Inputs to instantiate the model:
|
| 51 |
+
generation_config = GenerationConfig(
|
| 52 |
+
temperature=0.2,
|
| 53 |
+
top_p=0.75,
|
| 54 |
+
num_beams=4,
|
| 55 |
+
)
|
| 56 |
+
# Evaluate the model:
|
| 57 |
+
def evaluate(instruction, input=None):
|
| 58 |
+
prompt = generate_prompt(instruction, input)
|
| 59 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 60 |
+
input_ids = inputs["input_ids"].cuda()
|
| 61 |
+
generation_output = model.generate(
|
| 62 |
+
input_ids=input_ids,
|
| 63 |
+
generation_config=generation_config,
|
| 64 |
+
return_dict_in_generate=True,
|
| 65 |
+
output_scores=True,
|
| 66 |
+
max_new_tokens=256
|
| 67 |
+
)
|
| 68 |
+
for s in generation_output.sequences:
|
| 69 |
+
output = tokenizer.decode(s)
|
| 70 |
+
print("sortie:", output.split("### sortie:")[1].strip())
|
| 71 |
+
|
| 72 |
+
evaluate(input("instructions: "))
|
| 73 |
+
```
|