Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,60 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#import gradio as gr
|
2 |
+
#gr.load("models/tclopess/bart_samsum").launch()
|
3 |
import gradio as gr
|
4 |
+
import nltk
|
5 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
6 |
|
7 |
+
# Carregue o modelo e o tokenizer
|
8 |
+
checkpoint = "tclopess/bart_samsum"
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
10 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint)
|
11 |
+
|
12 |
+
# Função para fragmentar o texto
|
13 |
+
def fragment_text(text, tokenizer):
|
14 |
+
sentences = nltk.tokenize.sent_tokenize(text)
|
15 |
+
max_len = tokenizer.max_len_single_sentence
|
16 |
+
|
17 |
+
chunks = []
|
18 |
+
chunk = ""
|
19 |
+
count = -1
|
20 |
+
|
21 |
+
for sentence in sentences:
|
22 |
+
count += 1
|
23 |
+
combined_length = len(tokenizer.tokenize(sentence)) + len(chunk)
|
24 |
+
|
25 |
+
if combined_length <= max_len:
|
26 |
+
chunk += sentence + " "
|
27 |
+
else:
|
28 |
+
chunks.append(chunk.strip())
|
29 |
+
chunk = sentence + " "
|
30 |
+
|
31 |
+
if chunk != "":
|
32 |
+
chunks.append(chunk.strip())
|
33 |
+
|
34 |
+
return chunks
|
35 |
+
|
36 |
+
def generate_summaries(text):
|
37 |
+
chunks = fragment_text(text, tokenizer)
|
38 |
+
summaries = []
|
39 |
+
for chunk in chunks:
|
40 |
+
input = tokenizer(chunk, return_tensors='pt')
|
41 |
+
output = model.generate(**input)
|
42 |
+
summary = tokenizer.decode(*output, skip_special_tokens=True)
|
43 |
+
summaries.append(summary)
|
44 |
+
return summaries
|
45 |
+
|
46 |
+
# Função para exibir o resumo final
|
47 |
+
def display_summary(summaries):
|
48 |
+
summary = " ".join(summaries)
|
49 |
+
gr.text("Resumo final:", summary)
|
50 |
+
|
51 |
+
# Função `input_fn()` para retornar o campo de input
|
52 |
+
def input_fn():
|
53 |
+
# Crie um campo de input do tipo `text`
|
54 |
+
input_text = gr.inputs.Textbox(label="Insira ou cole o texto aqui:")
|
55 |
+
|
56 |
+
# Retorne o campo de input
|
57 |
+
return input_text
|
58 |
+
|
59 |
+
# Altere a linha `gr.load()` para usar a função `input_fn()`
|
60 |
+
gr.load("models/tclopess/bart_samsum").launch(input_fn=input_fn)
|