rdlf commited on
Commit
65b75e6
·
verified ·
1 Parent(s): 1b4db44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -67
app.py CHANGED
@@ -1,67 +1,19 @@
1
- import torch
2
- from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
3
-
4
- model_id = "clibrain/Llama-2-7b-ft-instruct-es"
5
-
6
- model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True).to("cuda")
7
- tokenizer = AutoTokenizer.from_pretrained(model_id)
8
-
9
- def create_instruction(instruction, input_data=None, context=None):
10
- sections = {
11
- "Instrucción": instruction,
12
- "Entrada": input_data,
13
- "Contexto": context,
14
- }
15
-
16
- 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"
17
- prompt = system_prompt
18
-
19
- for title, content in sections.items():
20
- if content is not None:
21
- prompt += f"### {title}:\n{content}\n\n"
22
-
23
- prompt += "### Respuesta:\n"
24
-
25
- return prompt
26
-
27
-
28
- def generate(
29
- instruction,
30
- input=None,
31
- context=None,
32
- max_new_tokens=128,
33
- temperature=0.1,
34
- top_p=0.75,
35
- top_k=40,
36
- num_beams=4,
37
- **kwargs
38
- ):
39
-
40
- prompt = create_instruction(instruction, input, context)
41
- print(prompt.replace("### Respuesta:\n", ""))
42
- inputs = tokenizer(prompt, return_tensors="pt")
43
- input_ids = inputs["input_ids"].to("cuda")
44
- attention_mask = inputs["attention_mask"].to("cuda")
45
- generation_config = GenerationConfig(
46
- temperature=temperature,
47
- top_p=top_p,
48
- top_k=top_k,
49
- num_beams=num_beams,
50
- **kwargs,
51
- )
52
- with torch.no_grad():
53
- generation_output = model.generate(
54
- input_ids=input_ids,
55
- attention_mask=attention_mask,
56
- generation_config=generation_config,
57
- return_dict_in_generate=True,
58
- output_scores=True,
59
- max_new_tokens=max_new_tokens,
60
- early_stopping=True
61
- )
62
- s = generation_output.sequences[0]
63
- output = tokenizer.decode(s)
64
- return output.split("### Respuesta:")[1].lstrip("\n")
65
-
66
- instruction = "Dame una lista de lugares a visitar en España."
67
- print(generate(instruction))
 
1
+ from transformers import GPT2LMHeadModel, GPT2Tokenizer
2
+ tokenizer = GPT2Tokenizer.from_pretrained("sberbank-ai/mGPT")
3
+ model = GPT2LMHeadModel.from_pretrained("sberbank-ai/mGPT")
4
+
5
+ text = "Александр Сергеевич Пушкин родился в "
6
+ input_ids = tokenizer.encode(text, return_tensors="pt").cuda(device)
7
+ out = model.generate(
8
+ input_ids,
9
+ min_length=100,
10
+ max_length=100,
11
+ eos_token_id=5,
12
+ pad_token=1,
13
+ top_k=10,
14
+ top_p=0.0,
15
+ no_repeat_ngram_size=5
16
+ )
17
+ generated_text = list(map(tokenizer.decode, out))[0]
18
+ print(generated_text)
19
+ Александр Сергеевич Пушкин родился в г. Санкт-Петербурге.