fvelasco
commited on
Commit
•
243307b
1
Parent(s):
d787566
modified the app
Browse files
app.py
CHANGED
@@ -1,7 +1,60 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
def greet(name):
|
4 |
return "Hello " + name + "!!"
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
iface.launch()
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
2 |
+
import os
|
3 |
+
os.environ["CUDA_VISIBLE_DEVICES"]="0"
|
4 |
import gradio as gr
|
5 |
|
6 |
def greet(name):
|
7 |
return "Hello " + name + "!!"
|
8 |
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
import torch
|
13 |
+
first_generation = True
|
14 |
+
prefix = ''
|
15 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
16 |
+
|
17 |
+
|
18 |
+
model_checkpoint = "fermaat/es_nlp_text_neutralizer"
|
19 |
+
|
20 |
+
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
|
21 |
+
|
22 |
+
|
23 |
+
|
24 |
+
# model = T5ForConditionalGeneration.from_pretrained(model_checkpoint)
|
25 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint)
|
26 |
+
# TODO: jarl!! check this for avoiding short segments
|
27 |
+
model.config.max_length = 512
|
28 |
+
model.to(device)
|
29 |
+
|
30 |
+
|
31 |
+
# sentences = ["El libro relata las aventuras y desventuras de un hidalgo de 50 años llamado Alonso Quijano, quien decide ser un caballero andante como aquellos que aparecen en sus libros de caballerías favoritos.Las hazañas de don Quijote están contenidas en dos tomos que narran tres salidas. Por un lado, la “Primera parte” denominada como El ingenioso Hidalgo Don Quijote de la Mancha está formada por 52 capítulos y en ella se encuentran la primera salida y la segunda salida."]
|
32 |
+
# sentences = ['De acuerdo con las informaciones anteriores , las alumnas se han quejado de la actitud de los profesores en los exámenes finales. Los representantes estudiantiles son los alumnos Juanju y Javi.']
|
33 |
+
def get_output(sentences, first_generation=True):
|
34 |
+
inputs = tokenizer([prefix + sentence for sentence in sentences], return_tensors="pt", padding=True)
|
35 |
+
with torch.no_grad():
|
36 |
+
if first_generation:
|
37 |
+
output_sequences = model.generate(
|
38 |
+
input_ids=inputs["input_ids"].to(device),
|
39 |
+
attention_mask=inputs["attention_mask"].to(device),
|
40 |
+
do_sample=False, # disable sampling to test if batching affects output
|
41 |
+
)
|
42 |
+
else:
|
43 |
+
|
44 |
+
output_sequences = model.generate(
|
45 |
+
input_ids=inputs["input_ids"].to(device),
|
46 |
+
attention_mask=inputs["attention_mask"].to(device),
|
47 |
+
do_sample=False,
|
48 |
+
num_beams=2,
|
49 |
+
repetition_penalty=2.5,
|
50 |
+
# length_penalty=1.0,
|
51 |
+
early_stopping=True# disable sampling to test if batching affects output
|
52 |
+
)
|
53 |
+
|
54 |
+
preds = [tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=True) for g in output_sequences]
|
55 |
+
return preds
|
56 |
+
# get_output(sentences)
|
57 |
+
|
58 |
+
|
59 |
+
iface = gr.Interface(fn=get_output, inputs="text", outputs="text")
|
60 |
iface.launch()
|