Update routers/textclas.py
Browse files- routers/textclas.py +26 -18
routers/textclas.py
CHANGED
@@ -1,36 +1,44 @@
|
|
1 |
from fastapi import APIRouter, Query, HTTPException
|
2 |
-
from
|
3 |
-
from langdetect.lang_detect_exception import LangDetectException
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
|
8 |
router = APIRouter()
|
9 |
|
10 |
-
@router.get("/
|
11 |
-
def
|
12 |
-
text: str = Query(..., description="Texto para
|
|
|
13 |
):
|
14 |
"""
|
15 |
-
|
16 |
-
Retorna true se for pt-BR, false caso contrário.
|
17 |
"""
|
18 |
try:
|
19 |
-
#
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
|
25 |
-
return {
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
raise HTTPException(
|
29 |
status_code=400,
|
30 |
-
detail="
|
31 |
)
|
32 |
except Exception as e:
|
33 |
raise HTTPException(
|
34 |
status_code=500,
|
35 |
-
detail=f"An error occurred during
|
36 |
)
|
|
|
1 |
from fastapi import APIRouter, Query, HTTPException
|
2 |
+
from keybert import KeyBERT
|
|
|
3 |
|
4 |
+
# Inicializa o modelo KeyBERT
|
5 |
+
kw_model = KeyBERT()
|
6 |
|
7 |
router = APIRouter()
|
8 |
|
9 |
+
@router.get("/keywords/extract/")
|
10 |
+
def extract_keywords(
|
11 |
+
text: str = Query(..., description="Texto para extração de palavras-chave"),
|
12 |
+
num_keywords: int = Query(5, description="Número de palavras-chave a serem retornadas", ge=1, le=20)
|
13 |
):
|
14 |
"""
|
15 |
+
Extrai palavras-chave relevantes de um texto.
|
|
|
16 |
"""
|
17 |
try:
|
18 |
+
# Extrai palavras-chave
|
19 |
+
keywords = kw_model.extract_keywords(
|
20 |
+
text,
|
21 |
+
keyphrase_ngram_range=(1, 2),
|
22 |
+
stop_words='english',
|
23 |
+
top_n=num_keywords
|
24 |
+
)
|
25 |
|
26 |
+
# Formata o retorno
|
27 |
+
keyword_list = [kw[0] for kw in keywords]
|
28 |
|
29 |
+
return {
|
30 |
+
"text": text,
|
31 |
+
"num_keywords": num_keywords,
|
32 |
+
"keywords": keyword_list
|
33 |
+
}
|
34 |
+
|
35 |
+
except ValueError as ve:
|
36 |
raise HTTPException(
|
37 |
status_code=400,
|
38 |
+
detail=f"Invalid input: {str(ve)}"
|
39 |
)
|
40 |
except Exception as e:
|
41 |
raise HTTPException(
|
42 |
status_code=500,
|
43 |
+
detail=f"An error occurred during keyword extraction: {str(e)}"
|
44 |
)
|