Spaces:
Runtime error
Runtime error
Update api/services/processing_service.py
Browse files
api/services/processing_service.py
CHANGED
|
@@ -1,74 +1,94 @@
|
|
| 1 |
-
"""
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
import
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
@router.post("/run", response_model=ProcessorTestResponse)
|
| 12 |
-
async def test_processor(request: ProcessorTestRequest):
|
| 13 |
-
"""Testa um processador específico."""
|
| 14 |
-
start_time = time.time()
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
processor_name=processor_name,
|
| 41 |
-
success=True,
|
| 42 |
-
result=result,
|
| 43 |
-
duration_ms=duration_ms
|
| 44 |
-
)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
)
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
"total": 9
|
| 73 |
-
}
|
| 74 |
-
|
|
|
|
| 1 |
+
"""Serviço de processamento de acórdãos"""
|
| 2 |
+
from typing import Dict, Any
|
| 3 |
+
import json
|
| 4 |
+
import tarfile
|
| 5 |
+
import hashlib
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from datetime import datetime
|
| 8 |
|
| 9 |
+
class ProcessingService:
|
| 10 |
+
"""Serviço para processar arquivos JSONL de acórdãos."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
async def process_jsonl_file(
|
| 13 |
+
self,
|
| 14 |
+
file_path: str,
|
| 15 |
+
task_id: str,
|
| 16 |
+
llm_provider: str = "groq",
|
| 17 |
+
model_type: str = "balanced",
|
| 18 |
+
enable_parallel: bool = True,
|
| 19 |
+
max_workers: int = 3
|
| 20 |
+
) -> Dict[str, Any]:
|
| 21 |
+
"""
|
| 22 |
+
Processa arquivo JSONL com acórdãos.
|
| 23 |
|
| 24 |
+
Returns:
|
| 25 |
+
Dict com resultados do processamento
|
| 26 |
+
"""
|
| 27 |
+
from api.config import get_settings
|
| 28 |
|
| 29 |
+
settings = get_settings()
|
| 30 |
+
processed = 0
|
| 31 |
+
failed = 0
|
| 32 |
+
results = []
|
| 33 |
|
| 34 |
+
# Ler arquivo JSONL
|
| 35 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 36 |
+
for line in f:
|
| 37 |
+
if not line.strip():
|
| 38 |
+
continue
|
| 39 |
+
|
| 40 |
+
try:
|
| 41 |
+
record = json.loads(line)
|
| 42 |
+
# Aqui seria o processamento real com os 9 especialistas
|
| 43 |
+
# Por enquanto, retornar mock
|
| 44 |
+
results.append({
|
| 45 |
+
"acordao_id": record.get("acordao_id"),
|
| 46 |
+
"status": "processed",
|
| 47 |
+
"timestamp": datetime.now().isoformat()
|
| 48 |
+
})
|
| 49 |
+
processed += 1
|
| 50 |
+
except Exception as e:
|
| 51 |
+
failed += 1
|
| 52 |
+
results.append({
|
| 53 |
+
"acordao_id": record.get("acordao_id", "unknown"),
|
| 54 |
+
"status": "error",
|
| 55 |
+
"error": str(e)
|
| 56 |
+
})
|
| 57 |
|
| 58 |
+
# Criar arquivo TAR.GZ com resultados
|
| 59 |
+
output_dir = Path(settings.OUTPUT_PATH) / "archives"
|
| 60 |
+
output_dir.mkdir(parents=True, exist_ok=True)
|
| 61 |
|
| 62 |
+
archive_path = output_dir / f"{task_id}.tar.gz"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
# Criar JSON com resultados
|
| 65 |
+
result_json = {
|
| 66 |
+
"task_id": task_id,
|
| 67 |
+
"processed": processed,
|
| 68 |
+
"failed": failed,
|
| 69 |
+
"results": results,
|
| 70 |
+
"completed_at": datetime.now().isoformat()
|
| 71 |
+
}
|
| 72 |
|
| 73 |
+
temp_json = output_dir / f"{task_id}_results.json"
|
| 74 |
+
with open(temp_json, 'w', encoding='utf-8') as f:
|
| 75 |
+
json.dump(result_json, f, ensure_ascii=False, indent=2)
|
| 76 |
+
|
| 77 |
+
# Criar TAR.GZ
|
| 78 |
+
with tarfile.open(archive_path, 'w:gz') as tar:
|
| 79 |
+
tar.add(temp_json, arcname=f"{task_id}_results.json")
|
| 80 |
+
|
| 81 |
+
# Remover JSON temporário
|
| 82 |
+
temp_json.unlink()
|
| 83 |
+
|
| 84 |
+
# Calcular hash
|
| 85 |
+
with open(archive_path, 'rb') as f:
|
| 86 |
+
file_hash = hashlib.sha256(f.read()).hexdigest()
|
| 87 |
+
|
| 88 |
+
return {
|
| 89 |
+
"processed": processed,
|
| 90 |
+
"failed": failed,
|
| 91 |
+
"archive_path": str(archive_path),
|
| 92 |
+
"hash": file_hash,
|
| 93 |
+
"elapsed_seconds": 1.5 # mock
|
| 94 |
+
}
|
|
|
|
|
|
|
|
|