Spaces:
Runtime error
Runtime error
First commit
Browse files- app.py +29 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import RobertaTokenizerFast, BertTokenizerFast, EncoderDecoderModel
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
models_paths = dict()
|
| 7 |
+
models_paths["fr"] = "mrm8488/camembert2camembert_shared-finetuned-french-summarization"
|
| 8 |
+
models_paths["de"] = "mrm8488/bert2bert_shared-german-finetuned-summarization"
|
| 9 |
+
models_paths["tu"] = "mrm8488/bert2bert_shared-turkish-summarization"
|
| 10 |
+
models_paths["es"] = "Narrativa/bsc_roberta2roberta_shared-spanish-finetuned-mlsum-summarization"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def summarize(lang, text):
|
| 17 |
+
tokenizer = RobertaTokenizerFast.from_pretrained(models_paths[lang]) if lang in [
|
| 18 |
+
"fr", "es"] else BertTokenizerFast.from_pretrained(models_paths[lang])
|
| 19 |
+
model = EncoderDecoderModel.from_pretrained(models_paths[lang]).to(device)
|
| 20 |
+
inputs = tokenizer([text], padding="max_length",
|
| 21 |
+
truncation=True, max_length=512, return_tensors="pt")
|
| 22 |
+
input_ids = inputs.input_ids.to(device)
|
| 23 |
+
attention_mask = inputs.attention_mask.to(device)
|
| 24 |
+
output = model.generate(input_ids, attention_mask=attention_mask)
|
| 25 |
+
return tokenizer.decode(output[0], skip_special_tokens=True)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
gr.Interface(fn=summarize, inputs=[gr.inputs.CheckboxGroup(["fr", "de", "tu", "es"]), gr.inputs.Textbox(
|
| 29 |
+
lines=7, label="Input Text")], outputs="text").launch(inline=False)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|