mrm8488's picture
First commit
495c4a1
raw
history blame
1.35 kB
import gradio as gr
import torch
from transformers import RobertaTokenizerFast, BertTokenizerFast, EncoderDecoderModel
models_paths = dict()
models_paths["fr"] = "mrm8488/camembert2camembert_shared-finetuned-french-summarization"
models_paths["de"] = "mrm8488/bert2bert_shared-german-finetuned-summarization"
models_paths["tu"] = "mrm8488/bert2bert_shared-turkish-summarization"
models_paths["es"] = "Narrativa/bsc_roberta2roberta_shared-spanish-finetuned-mlsum-summarization"
device = 'cuda' if torch.cuda.is_available() else 'cpu'
def summarize(lang, text):
tokenizer = RobertaTokenizerFast.from_pretrained(models_paths[lang]) if lang in [
"fr", "es"] else BertTokenizerFast.from_pretrained(models_paths[lang])
model = EncoderDecoderModel.from_pretrained(models_paths[lang]).to(device)
inputs = tokenizer([text], padding="max_length",
truncation=True, max_length=512, return_tensors="pt")
input_ids = inputs.input_ids.to(device)
attention_mask = inputs.attention_mask.to(device)
output = model.generate(input_ids, attention_mask=attention_mask)
return tokenizer.decode(output[0], skip_special_tokens=True)
gr.Interface(fn=summarize, inputs=[gr.inputs.CheckboxGroup(["fr", "de", "tu", "es"]), gr.inputs.Textbox(
lines=7, label="Input Text")], outputs="text").launch(inline=False)