Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from peft import PeftModel
|
| 2 |
+
from transformers import LlamaForCausalLM, LlamaTokenizer, GenerationConfig
|
| 3 |
+
|
| 4 |
+
tokenizer = LlamaTokenizer.from_pretrained("decapoda-research/llama-13b-hf")
|
| 5 |
+
model = LlamaForCausalLM.from_pretrained(
|
| 6 |
+
"decapoda-research/llama-13b-hf"
|
| 7 |
+
)
|
| 8 |
+
model = PeftModel.from_pretrained(model, "plncmm/guanaco-lora-13b")
|
| 9 |
+
|
| 10 |
+
def generate_prompt(instruction, input=None):
|
| 11 |
+
if input:
|
| 12 |
+
return f"""Abajo est谩 una instrucci贸n que describe una tarea, en conjunto con una entrada que entrega mas contexto. Escribe una respuesta que complete adecuadamente lo pedido.
|
| 13 |
+
|
| 14 |
+
### Instrucci贸n:
|
| 15 |
+
{instruction}
|
| 16 |
+
|
| 17 |
+
### Entrada:
|
| 18 |
+
{input}
|
| 19 |
+
|
| 20 |
+
### Respuesta:"""
|
| 21 |
+
else:
|
| 22 |
+
return f"""Abajo est谩 una instrucci贸n que describe una tarea. Escribe una respuesta que complete adecuadamente lo pedido.
|
| 23 |
+
|
| 24 |
+
### Instrucci贸n:
|
| 25 |
+
{instruction}
|
| 26 |
+
|
| 27 |
+
### Respuesta:"""
|
| 28 |
+
|
| 29 |
+
generation_config = GenerationConfig(
|
| 30 |
+
temperature=0.1,
|
| 31 |
+
top_p=0.75,
|
| 32 |
+
num_beams=4,
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
def evaluate(instruction, input=None):
|
| 36 |
+
prompt = generate_prompt(instruction, input)
|
| 37 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 38 |
+
input_ids = inputs["input_ids"].cuda()
|
| 39 |
+
generation_output = model.generate(
|
| 40 |
+
input_ids=input_ids,
|
| 41 |
+
generation_config=generation_config,
|
| 42 |
+
return_dict_in_generate=True,
|
| 43 |
+
output_scores=True,
|
| 44 |
+
max_new_tokens=256
|
| 45 |
+
)
|
| 46 |
+
for s in generation_output.sequences:
|
| 47 |
+
output = tokenizer.decode(s)
|
| 48 |
+
print("Respuesta:", output.split("### Respuesta:")[1].strip())
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
|
| 53 |
+
evaluate("nombre las capitales regionales de Chile")
|
| 54 |
+
evaluate("dime sobre guanacos")
|
| 55 |
+
evaluate("dime sobre el presidente de mexico en 2019")
|
| 56 |
+
evaluate("dime sobre el rey de francia en 2019")
|
| 57 |
+
evaluate("traduce la frase 'hola mundo' a ingl茅s")
|
| 58 |
+
evaluate("escribe un programa de Python que imprima los primeros 10 n煤meros de Fibonacci")
|