51 lines
2.2 KiB
Python
51 lines
2.2 KiB
Python
from app.schemas.document_standard import DocumentEvaluationRubric, StandardDocumentAnalysis, DocumentEvaluationCriteria
|
|
|
|
def encode_document_from_bytes(document_bytes: bytes) -> str:
|
|
"""
|
|
Codifica un documento a base64 a partir de sus bytes.
|
|
"""
|
|
import base64
|
|
return base64.b64encode(document_bytes).decode('utf-8')
|
|
|
|
def json_to_rubric(json_data: dict) -> DocumentEvaluationRubric:
|
|
"""Convierte un diccionario JSON en un objeto DocumentEvaluationRubric mapeando los campos correctos."""
|
|
|
|
def parse_criteria(criteria_list: list) -> list:
|
|
parsed_criteria = []
|
|
for item in criteria_list:
|
|
# Convertir el valor a float si existe
|
|
score_value = float(item["value"]) if item.get("value") is not None else None
|
|
description = str(item["description"]) if item.get("description") is not None else None
|
|
|
|
# Procesar subcriterios recursivamente si existen
|
|
sub_raw = item.get("sub_criteria")
|
|
sub_parsed = parse_criteria(sub_raw) if sub_raw else None
|
|
|
|
parsed_criteria.append(DocumentEvaluationCriteria(
|
|
name=item["name"],
|
|
description=description,
|
|
score=score_value,
|
|
subcriteria=sub_parsed
|
|
))
|
|
return parsed_criteria
|
|
|
|
# Jerarquía de la categoría
|
|
category_info = json_data.get("category", {})
|
|
hierarchy_list = category_info.get("hierarchy", [])
|
|
|
|
# Ordenamos por nivel para asegurar la lógica: Nivel 0 -> Nivel 1 -> Nivel 2
|
|
sorted_hierarchy = sorted(hierarchy_list, key=lambda x: x.get("level", 0))
|
|
|
|
# Unimos los nombres con ' > '
|
|
specialization_path = " > ".join([item["name"] for item in sorted_hierarchy]) if sorted_hierarchy else None
|
|
|
|
# Extraer la información principal de la rúbrica
|
|
rubric_info = json_data.get("rubric", {})
|
|
|
|
return DocumentEvaluationRubric(
|
|
name=rubric_info.get("name", "Sin nombre"),
|
|
description=str(rubric_info["description"]) if rubric_info.get("description") is not None else None,
|
|
category=specialization_path,
|
|
criteria=parse_criteria(json_data.get("criteria", []))
|
|
)
|
|
|