File size: 1,878 Bytes
495c4a1
 
 
 
 
242e710
 
 
 
495c4a1
242e710
495c4a1
 
 
 
 
 
 
242e710
 
 
 
495c4a1
 
242e710
 
495c4a1
 
 
 
 
 
 
 
230ed27
 
 
abdd693
230ed27
 
 
242e710
230ed27
 
242e710
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
40
41
42
43
44
45
46
47
import gradio as gr
import torch
from transformers import RobertaTokenizerFast, BertTokenizerFast, EncoderDecoderModel


LANGUAGES = ["fr", "de", "tu", "es"]

models = dict()
tokenizers = dict()
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'

for lang in LANGUAGES:
    tokenizers[lang] = RobertaTokenizerFast.from_pretrained(models_paths[lang]) if lang in ["fr", "es"] else BertTokenizerFast.from_pretrained(models_paths[lang])
    models[lang] = EncoderDecoderModel.from_pretrained(models_paths[lang]).to(device)
                   

def summarize(lang, text):
    tokenizer = tokenizers[lang]
    model = models[lang]
    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.co/mrm8488' target='_blank'>More models</a></p>"


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