File size: 1,713 Bytes
495c4a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c9a793
495c4a1
 
 
 
 
 
 
 
 
230ed27
 
 
abdd693
230ed27
 
 
 
 
 
9c9a793
d4eb8b3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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 == "fr" or lang == "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)



theme = "darkgrass"

title = "Multilingual Summarization model (MLSUM)"

description = "Gradio Demo for Summarization models trained on MLSUM dataset by Manuel Romero"

article = "<p style='text-align: center'><a href='https://hf.com/mrm8488' target='_blank'>More models</a></p>"


gr.Interface(fn=summarize, inputs=[gr.inputs.Radio(["fr", "de", "tu", "es"]), gr.inputs.Textbox(
    lines=7, label="Input Text")], outputs="text", theme=theme, title=title, description=description, article=article, enable_queue=True).launch(inline=False)