Spaces:
Runtime error
Runtime error
| 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) | |