Spaces:
Build error
Build error
update train.py
Browse files
train.py
CHANGED
|
@@ -1,2 +1,51 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# =================== CÓDIGO COMPLETO E ATUALIZADO ===================
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import zipfile
|
| 5 |
+
import glob
|
| 6 |
+
import json
|
| 7 |
+
import csv
|
| 8 |
+
import numpy as np
|
| 9 |
+
from tqdm.auto import tqdm
|
| 10 |
+
from sentence_transformers import SentenceTransformer
|
| 11 |
+
|
| 12 |
+
# --- CONFIGURAÇÕES ---
|
| 13 |
+
# Lembre-se de colocar o nome correto do seu arquivo .zip!
|
| 14 |
+
ZIP_FILENAME = "seus-dados.zip"
|
| 15 |
+
EXTRACT_DIR = "/app/dados_extraidos"
|
| 16 |
+
# ---------------------
|
| 17 |
+
|
| 18 |
+
def setup_data():
|
| 19 |
+
"""Descompacta os dados se o diretório não existir."""
|
| 20 |
+
if not os.path.exists(EXTRACT_DIR) and os.path.exists(ZIP_FILENAME):
|
| 21 |
+
print(f"Descompactando '{ZIP_FILENAME}'...")
|
| 22 |
+
os.makedirs(EXTRACT_DIR, exist_ok=True)
|
| 23 |
+
with zipfile.ZipFile(ZIP_FILENAME, 'r') as zip_ref:
|
| 24 |
+
zip_ref.extractall(EXTRACT_DIR)
|
| 25 |
+
print("✅ Dados descompactados.")
|
| 26 |
+
elif not os.path.exists(ZIP_FILENAME):
|
| 27 |
+
print(f"⚠️ Arquivo '{ZIP_FILENAME}' não encontrado. Pulando descompactação.")
|
| 28 |
+
else:
|
| 29 |
+
print("✅ Dados já parecem estar descompactados.")
|
| 30 |
+
|
| 31 |
+
def serialize_item_to_text(item_dict):
|
| 32 |
+
"""Converte um dicionário em uma string de texto."""
|
| 33 |
+
parts = []
|
| 34 |
+
for key, value in item_dict.items():
|
| 35 |
+
if isinstance(value, dict):
|
| 36 |
+
nested_text = serialize_item_to_text(value)
|
| 37 |
+
parts.append(f"{key} ({nested_text})")
|
| 38 |
+
elif isinstance(value, list):
|
| 39 |
+
list_str = ', '.join([str(i) for i in value])
|
| 40 |
+
parts.append(f"{key}: [{list_str}]")
|
| 41 |
+
else:
|
| 42 |
+
parts.append(f"{key}: {value}")
|
| 43 |
+
return ", ".join(parts)
|
| 44 |
+
|
| 45 |
+
def main():
|
| 46 |
+
"""Função principal para carregar dados e gerar embeddings."""
|
| 47 |
+
setup_data()
|
| 48 |
+
csv.field_size_limit(10_000_000)
|
| 49 |
+
|
| 50 |
+
documents = []
|
| 51 |
+
all_files = glob.glob(EXTRACT_DIR + "/**/*.
|