Create summarizer.py
Browse files- routers/summarizer.py +37 -0
routers/summarizer.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import APIRouter, Query, HTTPException
|
2 |
+
from transformers import BartForConditionalGeneration, BartTokenizer
|
3 |
+
|
4 |
+
# Cria um roteador para a API de resumo de texto
|
5 |
+
router = APIRouter()
|
6 |
+
|
7 |
+
# Carregar o modelo e o tokenizer do BART
|
8 |
+
model_name = "facebook/bart-large-cnn"
|
9 |
+
model = BartForConditionalGeneration.from_pretrained(model_name)
|
10 |
+
tokenizer = BartTokenizer.from_pretrained(model_name)
|
11 |
+
|
12 |
+
@router.get("/summarize/")
|
13 |
+
def summarize_text(text: str = Query(..., description="Texto a ser resumido")):
|
14 |
+
"""
|
15 |
+
Gera um resumo do texto fornecido utilizando o modelo BART.
|
16 |
+
"""
|
17 |
+
try:
|
18 |
+
# Tokenizar o texto e gerar um resumo
|
19 |
+
inputs = tokenizer([text], max_length=1024, return_tensors="pt", truncation=True)
|
20 |
+
summary_ids = model.generate(inputs["input_ids"], max_length=150, min_length=30, length_penalty=2.0, num_beams=4, early_stopping=True)
|
21 |
+
|
22 |
+
# Decodificar o resumo gerado
|
23 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
24 |
+
|
25 |
+
return {
|
26 |
+
"original_text": text,
|
27 |
+
"summary": summary,
|
28 |
+
"original_length": len(text.split()),
|
29 |
+
"summary_length": len(summary.split())
|
30 |
+
}
|
31 |
+
|
32 |
+
except Exception as e:
|
33 |
+
# Em caso de erro, retorna uma mensagem de erro
|
34 |
+
raise HTTPException(
|
35 |
+
status_code=500,
|
36 |
+
detail=f"An error occurred during text summarization: {str(e)}"
|
37 |
+
)
|