Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,19 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 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 |
+
Александр Сергеевич Пушкин родился в г. Санкт-Петербурге.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|