Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Imports
|
2 |
+
import os
|
3 |
+
import torch
|
4 |
+
import re
|
5 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
6 |
+
import transformers
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
|
10 |
+
WHITESPACE_HANDLER = lambda k: re.sub('\s+', ' ', re.sub('\n+', ' ', k.strip()))
|
11 |
+
|
12 |
+
model_name = "csebuetnlp/mT5_multilingual_XLSum"
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name,use_fast=False)
|
14 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
15 |
+
|
16 |
+
def generate_summary(text):
|
17 |
+
|
18 |
+
input_ids = tokenizer(
|
19 |
+
[WHITESPACE_HANDLER(text)],
|
20 |
+
return_tensors="pt",
|
21 |
+
padding="max_length",
|
22 |
+
truncation=True,
|
23 |
+
max_length=512)["input_ids"]
|
24 |
+
|
25 |
+
output_ids = model.generate(
|
26 |
+
input_ids=input_ids,
|
27 |
+
max_length=84,
|
28 |
+
no_repeat_ngram_size=2,
|
29 |
+
num_beams=4
|
30 |
+
)[0]
|
31 |
+
|
32 |
+
summary = tokenizer.decode(
|
33 |
+
output_ids,
|
34 |
+
skip_special_tokens=True,
|
35 |
+
clean_up_tokenization_spaces=False
|
36 |
+
)
|
37 |
+
|
38 |
+
return summary
|
39 |
+
|
40 |
+
demo = gr.Interface(fn=generate_summary,
|
41 |
+
inputs=gr.Textbox(lines=10, placeholder="Matinni kiriting!"),
|
42 |
+
outputs=gr.Textbox(lines=4)
|
43 |
+
)
|
44 |
+
|
45 |
+
demo.launch()
|