diff --git a/services/admin/admin_ai_service.py b/services/admin/admin_ai_service.py index f484b264..6ddc8373 100644 --- a/services/admin/admin_ai_service.py +++ b/services/admin/admin_ai_service.py @@ -3,6 +3,7 @@ import tempfile import numpy import re +import gc from typing import List from config import Config @@ -48,6 +49,37 @@ def get_substance(drugs: List[Drug]): return drugs_array +def get_substance_by_drug_name(drug_names: list[str]): + drugs_dict = {} + drug_names_words = [] + + if not drug_names: + return drugs_dict + + for n in drug_names: + drug_names_words.append(" ".join(re.findall("\w{4,}", n))) + + model_subst = _get_model("models/noharm-ml-subst.gz") + token_subst = _get_model("models/noharm-tk-subst.gz") + + vector = token_subst.transform(drug_names_words) + prediction = model_subst.predict(vector) + probabilities = model_subst.predict_proba(vector).round(2) + MIN_PROB = 0.5 + + for idx, p in enumerate(prediction): + subst_idx = numpy.where(model_subst.classes_ == p)[0][0] + subst_prob = probabilities[idx][subst_idx] + + if subst_prob >= MIN_PROB: + drugs_dict[drug_names[idx]] = p + + del model_subst, token_subst, vector, prediction, probabilities + gc.collect() + + return drugs_dict + + def get_factors(conversions): if len(conversions) == 0: return conversions diff --git a/services/prescription_view_service.py b/services/prescription_view_service.py index f2fe5d34..8222749a 100644 --- a/services/prescription_view_service.py +++ b/services/prescription_view_service.py @@ -102,6 +102,7 @@ def _internal_get_prescription( alerts_data=alerts_data, exams_data=exam_data, config_data=config_data, + is_complete=is_complete, ) review_data = _get_review_data(prescription=prescription, is_complete=is_complete) @@ -522,6 +523,7 @@ def _get_drug_data( alerts_data: dict, exams_data: dict, config_data: dict, + is_complete: bool, ): drug_list = DrugList( drugList=drugs, @@ -578,6 +580,9 @@ def _get_drug_data( ) concilia_list = drug_list.conciliaList(concilia_drugs, []) + if is_complete and concilia_list: + p_drugs = drug_list.infer_substance(pDrugs=p_drugs) + return { "drug_list": drug_list, "headers": headers, diff --git a/utils/drug_list.py b/utils/drug_list.py index 141407bd..aa81fb9a 100644 --- a/utils/drug_list.py +++ b/utils/drug_list.py @@ -3,6 +3,7 @@ from utils.dateutils import to_iso from services import drug_service +from services.admin import admin_ai_service from utils import stringutils, numberutils, prescriptionutils @@ -398,6 +399,27 @@ def changeDrugName(pDrugs): return result + @staticmethod + def infer_substance(pDrugs): + names = [] + for p in pDrugs: + if p["idDrug"] == 0: + names.append(p["drug"]) + + substances = admin_ai_service.get_substance_by_drug_name(drug_names=names) + + result = [] + for p in pDrugs: + if p["idDrug"] == 0: + if p["drug"] in substances: + p["sctid_infer"] = substances[p["drug"]] + + result.append(p) + else: + result.append(p) + + return result + @staticmethod def conciliaList(pDrugs, result=[]): for pd in pDrugs: @@ -434,6 +456,7 @@ def conciliaList(pDrugs, result=[]): ), "time": prescriptionutils.timeValue(pd[0].interval), "recommendation": pd[0].notes, + "sctid": str(pd.Substance.id) if pd.Substance else None, } )