Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
language:
|
4 |
+
- es
|
5 |
+
pipeline_tag: text-generation
|
6 |
+
library_name: transformers
|
7 |
+
inference: false
|
8 |
+
---
|
9 |
+
|
10 |
+
# Llama-2-7B-ft-instruct-es
|
11 |
+
|
12 |
+
[Llama 2 (7B)](https://huggingface.co/meta-llama/Llama-2-7b) fine-tuned on [Clibrain](https://huggingface.co/clibrain)'s Spanish instructions dataset.
|
13 |
+
|
14 |
+
|
15 |
+
## Model Details
|
16 |
+
|
17 |
+
Llama 2 is a collection of pre-trained and fine-tuned generative text models ranging in scale from 7 billion to 70 billion parameters. This is the repository for the 7B pre-trained model. Links to other models can be found in the index at the bottom.
|
18 |
+
|
19 |
+
|
20 |
+
## Example of Usage
|
21 |
+
|
22 |
+
```py
|
23 |
+
import torch
|
24 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoTokenizer, GenerationConfig
|
25 |
+
|
26 |
+
model_id = "clibrain/Llama-2-7b-ft-instruct-es"
|
27 |
+
|
28 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True).to("cuda")
|
29 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
30 |
+
|
31 |
+
def create_instruction(instruction, input_data=None, context=None):
|
32 |
+
sections = {
|
33 |
+
"Instrucci贸n": instruction,
|
34 |
+
"Entrada": input_data,
|
35 |
+
"Contexto": context,
|
36 |
+
}
|
37 |
+
|
38 |
+
system_prompt = "A continuaci贸n hay una instrucci贸n que describe una tarea, junto con una entrada que proporciona m谩s contexto. Escriba una respuesta que complete adecuadamente la solicitud.\n\n"
|
39 |
+
prompt = system_prompt
|
40 |
+
|
41 |
+
for title, content in sections.items():
|
42 |
+
if content is not None:
|
43 |
+
prompt += f"### {title}:\n{content}\n\n"
|
44 |
+
|
45 |
+
prompt += "### Respuesta:\n"
|
46 |
+
|
47 |
+
return prompt
|
48 |
+
|
49 |
+
|
50 |
+
def generate(
|
51 |
+
instruction,
|
52 |
+
input=None,
|
53 |
+
context=None,
|
54 |
+
max_new_tokens=128,
|
55 |
+
temperature=0.1,
|
56 |
+
top_p=0.75,
|
57 |
+
top_k=40,
|
58 |
+
num_beams=4,
|
59 |
+
**kwargs
|
60 |
+
):
|
61 |
+
|
62 |
+
prompt = create_instruction(instruction, input, context)
|
63 |
+
print(prompt.replace("### Respuesta:\n", ""))
|
64 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
65 |
+
input_ids = inputs["input_ids"].to("cuda")
|
66 |
+
attention_mask = inputs["attention_mask"].to("cuda")
|
67 |
+
generation_config = GenerationConfig(
|
68 |
+
temperature=temperature,
|
69 |
+
top_p=top_p,
|
70 |
+
top_k=top_k,
|
71 |
+
num_beams=num_beams,
|
72 |
+
**kwargs,
|
73 |
+
)
|
74 |
+
with torch.no_grad():
|
75 |
+
generation_output = model.generate(
|
76 |
+
input_ids=input_ids,
|
77 |
+
attention_mask=attention_mask,
|
78 |
+
generation_config=generation_config,
|
79 |
+
return_dict_in_generate=True,
|
80 |
+
output_scores=True,
|
81 |
+
max_new_tokens=max_new_tokens,
|
82 |
+
early_stopping=True
|
83 |
+
)
|
84 |
+
s = generation_output.sequences[0]
|
85 |
+
output = tokenizer.decode(s)
|
86 |
+
return output.split("### Respuesta:")[1].lstrip("\n")
|
87 |
+
|
88 |
+
instruction = "Dame una lista de lugares a visitar en Espa帽a."
|
89 |
+
print(generate(instruction))
|
90 |
+
```
|