|
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM |
|
import os |
|
os.environ["CUDA_VISIBLE_DEVICES"]="0" |
|
import gradio as gr |
|
|
|
def greet(name): |
|
return "Hello " + name + "!!" |
|
|
|
|
|
|
|
|
|
import torch |
|
first_generation = True |
|
prefix = '' |
|
device = 'cuda' if torch.cuda.is_available() else 'cpu' |
|
|
|
|
|
model_checkpoint = "fermaat/es_nlp_text_neutralizer" |
|
|
|
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint) |
|
|
|
|
|
|
|
|
|
model = AutoModelForSeq2SeqLM.from_pretrained(model_checkpoint) |
|
|
|
model.config.max_length = 512 |
|
model.to(device) |
|
|
|
|
|
|
|
|
|
def get_output(sentences, first_generation=True): |
|
inputs = tokenizer([prefix + sentence for sentence in sentences], return_tensors="pt", padding=True) |
|
with torch.no_grad(): |
|
if first_generation: |
|
output_sequences = model.generate( |
|
input_ids=inputs["input_ids"].to(device), |
|
attention_mask=inputs["attention_mask"].to(device), |
|
do_sample=False, |
|
) |
|
else: |
|
|
|
output_sequences = model.generate( |
|
input_ids=inputs["input_ids"].to(device), |
|
attention_mask=inputs["attention_mask"].to(device), |
|
do_sample=False, |
|
num_beams=2, |
|
repetition_penalty=2.5, |
|
|
|
early_stopping=True |
|
) |
|
|
|
preds = [tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=True) for g in output_sequences] |
|
return preds |
|
|
|
|
|
|
|
iface = gr.Interface(fn=get_output, inputs="text", outputs="text") |
|
iface.launch() |