Spaces:
Sleeping
Sleeping
Update ingest.py
Browse files
ingest.py
CHANGED
|
@@ -1,40 +1,65 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import faiss
|
| 4 |
from sentence_transformers import SentenceTransformer
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
"
|
| 12 |
-
|
| 13 |
-
"Documento 3: Conteúdo do documento 3."
|
| 14 |
-
# Adicione mais documentos conforme necessário
|
| 15 |
]
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
index.add(doc_embeddings)
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
with open("doc_map.pkl", "wb") as f:
|
| 38 |
-
pickle.dump(doc_map, f)
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from bs4 import BeautifulSoup
|
|
|
|
| 3 |
from sentence_transformers import SentenceTransformer
|
| 4 |
+
import faiss
|
| 5 |
+
import pickle
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
# Modelo de embeddings
|
| 9 |
+
EMBED_MODEL = "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
|
| 10 |
|
| 11 |
+
# URLs a serem indexadas
|
| 12 |
+
URLS = [
|
| 13 |
+
"https://labnoticias.jor.br/",
|
| 14 |
+
# você pode adicionar aqui manualmente outras URLs de artigos específicos
|
|
|
|
|
|
|
| 15 |
]
|
| 16 |
|
| 17 |
+
# Header para burlar o bloqueio 406
|
| 18 |
+
HEADERS = {
|
| 19 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
def fetch_text(url):
|
| 23 |
+
resp = requests.get(url, headers=HEADERS)
|
| 24 |
+
resp.raise_for_status()
|
| 25 |
+
soup = BeautifulSoup(resp.text, "html.parser")
|
| 26 |
+
container = soup.select_one("div.post-content") or soup.body
|
| 27 |
+
return container.get_text(separator=" ", strip=True)
|
| 28 |
+
|
| 29 |
+
def main():
|
| 30 |
+
os.makedirs("data", exist_ok=True)
|
| 31 |
+
texts = []
|
| 32 |
+
for url in URLS:
|
| 33 |
+
try:
|
| 34 |
+
txt = fetch_text(url)
|
| 35 |
+
texts.append(txt)
|
| 36 |
+
print(f"✔️ Coletado: {url}")
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"❌ Erro em {url}: {e}")
|
| 39 |
+
|
| 40 |
+
if not texts:
|
| 41 |
+
raise RuntimeError("Nenhum texto foi coletado. Verifique as URLs ou o seletor CSS.")
|
| 42 |
|
| 43 |
+
# Gera embeddings
|
| 44 |
+
model = SentenceTransformer(EMBED_MODEL)
|
| 45 |
+
embeddings = model.encode(texts, show_progress_bar=True)
|
| 46 |
|
| 47 |
+
# Verifica formato
|
| 48 |
+
if embeddings.ndim != 2:
|
| 49 |
+
raise RuntimeError(f"Formato inesperado de embeddings: {embeddings.shape}")
|
|
|
|
| 50 |
|
| 51 |
+
# Constrói índice FAISS
|
| 52 |
+
dim = embeddings.shape[1]
|
| 53 |
+
index = faiss.IndexFlatL2(dim)
|
| 54 |
+
index.add(embeddings)
|
| 55 |
|
| 56 |
+
# Salva arquivos
|
| 57 |
+
with open("data/index.faiss", "wb") as f:
|
| 58 |
+
pickle.dump(index, f)
|
| 59 |
+
with open("data/texts.pkl", "wb") as f:
|
| 60 |
+
pickle.dump(texts, f)
|
| 61 |
|
| 62 |
+
print("✅ Ingestão e indexação concluídas.")
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
main()
|