Update app.py
Browse files
app.py
CHANGED
@@ -1,74 +1,63 @@
|
|
1 |
-
import
|
2 |
-
os.environ["NUMBA_CACHE_DIR"] = "/tmp/numba_cache"
|
3 |
-
os.environ["U2NET_HOME"] = "/tmp/u2net"
|
4 |
-
|
5 |
from fastapi import FastAPI, HTTPException
|
6 |
from pydantic import BaseModel
|
7 |
-
|
8 |
-
from
|
9 |
-
import
|
10 |
-
|
11 |
-
|
12 |
-
import asyncio
|
13 |
-
import gc
|
14 |
-
import base64
|
15 |
|
16 |
-
|
|
|
|
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
|
23 |
-
|
24 |
-
|
25 |
|
26 |
-
def
|
27 |
-
"""
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
new_size = (int(width * ratio), int(height * ratio))
|
32 |
-
image = image.resize(new_size, Image.Resampling.LANCZOS)
|
33 |
-
return image
|
34 |
|
35 |
-
def
|
36 |
-
|
37 |
-
image = Image.open(BytesIO(base64.b64decode(image_data)))
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
|
42 |
-
#
|
43 |
-
|
44 |
|
45 |
-
#
|
46 |
-
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
#
|
49 |
-
|
50 |
-
|
51 |
-
img_byte_arr.seek(0)
|
52 |
|
53 |
-
#
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
|
59 |
-
@app.
|
60 |
-
async def
|
61 |
try:
|
62 |
-
#
|
63 |
-
|
64 |
-
processed_image_base64 = await loop.run_in_executor(executor, process_image, image_data.image)
|
65 |
|
66 |
-
|
67 |
-
return {"processed_image": processed_image_base64}
|
68 |
|
69 |
except Exception as e:
|
70 |
raise HTTPException(status_code=400, detail=str(e))
|
71 |
-
|
72 |
-
finally:
|
73 |
-
# Força a coleta de lixo após cada requisição
|
74 |
-
gc.collect()
|
|
|
1 |
+
import spacy
|
|
|
|
|
|
|
2 |
from fastapi import FastAPI, HTTPException
|
3 |
from pydantic import BaseModel
|
4 |
+
import nltk
|
5 |
+
from nltk.tokenize import word_tokenize
|
6 |
+
from nltk.corpus import stopwords
|
7 |
+
from collections import Counter
|
8 |
+
import re
|
|
|
|
|
|
|
9 |
|
10 |
+
# Baixar as stopwords do NLTK (se ainda não tiver feito)
|
11 |
+
nltk.download('punkt')
|
12 |
+
nltk.download('stopwords')
|
13 |
|
14 |
+
# Carregar o modelo spaCy para reconhecimento de entidades nomeadas
|
15 |
+
nlp = spacy.load("en_core_web_sm")
|
16 |
+
|
17 |
+
app = FastAPI()
|
18 |
|
19 |
+
class PostText(BaseModel):
|
20 |
+
post: str # O post (texto) que será enviado para a API
|
21 |
|
22 |
+
def clean_text(text):
|
23 |
+
"""Remove caracteres especiais e faz a limpeza do texto."""
|
24 |
+
text = re.sub(r'[^\w\s]', '', text) # Remove pontuação
|
25 |
+
text = text.lower() # Converte para minúsculas
|
26 |
+
return text
|
|
|
|
|
|
|
27 |
|
28 |
+
def extract_keywords(text):
|
29 |
+
"""Extrai palavras-chave usando spaCy e nltk."""
|
|
|
30 |
|
31 |
+
# Limpeza inicial do texto
|
32 |
+
cleaned_text = clean_text(text)
|
33 |
|
34 |
+
# Tokenização do texto
|
35 |
+
words = word_tokenize(cleaned_text)
|
36 |
|
37 |
+
# Remover stopwords
|
38 |
+
stop_words = set(stopwords.words("english"))
|
39 |
+
filtered_words = [word for word in words if word not in stop_words]
|
40 |
+
|
41 |
+
# Contar a frequência das palavras filtradas
|
42 |
+
word_counts = Counter(filtered_words)
|
43 |
|
44 |
+
# Processar entidades nomeadas com spaCy (ex.: pessoas, locais, eventos)
|
45 |
+
doc = nlp(text)
|
46 |
+
entities = [ent.text for ent in doc.ents]
|
|
|
47 |
|
48 |
+
# Juntar as palavras mais frequentes e as entidades encontradas
|
49 |
+
keywords = set(filtered_words + entities)
|
50 |
+
|
51 |
+
# Ordenar e retornar as palavras-chave mais relevantes (top 10)
|
52 |
+
return [keyword for keyword, _ in word_counts.most_common(10)] + entities[:10]
|
53 |
|
54 |
+
@app.get("/generate-keywords")
|
55 |
+
async def generate_keywords(post_text: PostText):
|
56 |
try:
|
57 |
+
# Gerar as palavras-chave a partir do texto do post
|
58 |
+
keywords = extract_keywords(post_text.post)
|
|
|
59 |
|
60 |
+
return {"keywords": keywords}
|
|
|
61 |
|
62 |
except Exception as e:
|
63 |
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
|
|
|
|