mrm8488 commited on
Commit
f6318f6
1 Parent(s): dd1094a

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +113 -0
README.md ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 (13B)](https://huggingface.co/meta-llama/Llama-2-13b) 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, GenerationConfig
25
+
26
+ model_id = "clibrain/Llama-2-13b-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
+ ```
91
+ ## Example of Usage with `pipelines`
92
+
93
+ ```py
94
+ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
95
+
96
+ model_id = "clibrain/Llama-2-13b-ft-instruct-es"
97
+
98
+ model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True).to("cuda")
99
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
100
+
101
+ pipe = pipeline(task="text-generation", model=model, tokenizer=tokenizer, max_length=200, device=0)
102
+
103
+ prompt = """
104
+ A continuación hay una instrucción que describe una tarea. Escriba una respuesta que complete adecuadamente la solicitud.
105
+ ### Instrucción:
106
+ Dame una lista de 5 lugares a visitar en España.
107
+
108
+ ### Respuesta:
109
+ """
110
+
111
+ result = pipe(prompt)
112
+ print(result[0]['generated_text'])
113
+ ```