|
| 1 | +from fastapi import FastAPI |
| 2 | +from pydantic import BaseModel |
| 3 | +import pandas as pd |
| 4 | +import numpy as np |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | +from chemprop_solvation.solvation_estimator import load_DirectML_Gsolv_estimator, load_DirectML_Hsolv_estimator, load_SoluteML_estimator |
| 8 | +from solvation_predictor.solubility.solubility_calculator import SolubilityCalculations |
| 9 | +from solvation_predictor.solubility.solubility_predictions import SolubilityPredictions |
| 10 | +from solvation_predictor.solubility.solubility_data import SolubilityData |
| 11 | +from solvation_predictor.solubility.solubility_models import SolubilityModels |
| 12 | + |
| 13 | + |
| 14 | +class SolubilityDataWrapper: |
| 15 | + """ |
| 16 | + Class for storing the input data for solubility prediction |
| 17 | + """ |
| 18 | + def __init__(self, solvent_smiles=None, solute_smiles=None, temp=None, ref_solub=None, ref_solv=None): |
| 19 | + self.smiles_pairs = [(solvent_smiles, solute_smiles)] |
| 20 | + self.temperatures = np.array([temp]) if temp is not None else None |
| 21 | + self.reference_solubility = np.array([ref_solub]) if ref_solub is not None else None |
| 22 | + self.reference_solvents = np.array([ref_solv]) if ref_solv is not None else None |
| 23 | + |
| 24 | + |
| 25 | +app = FastAPI() |
| 26 | + |
| 27 | +dGsolv_estimator = None |
| 28 | +dHsolv_estimator = None |
| 29 | +SoluteML_estimator = None |
| 30 | +solub_models = None |
| 31 | + |
| 32 | +@app.on_event("startup") |
| 33 | +def load_models(): |
| 34 | + global dGsolv_estimator, dHsolv_estimator, SoluteML_estimator, solub_models |
| 35 | + |
| 36 | + print("Loading models...") |
| 37 | + |
| 38 | + dGsolv_estimator = load_DirectML_Gsolv_estimator() |
| 39 | + dHsolv_estimator = load_DirectML_Hsolv_estimator() |
| 40 | + |
| 41 | + solub_models = SolubilityModels( |
| 42 | + load_g=True, load_h=True, |
| 43 | + reduced_number=False, load_saq=True, |
| 44 | + load_solute=True, logger=None, verbose=False |
| 45 | + ) |
| 46 | + |
| 47 | + SoluteML_estimator = load_SoluteML_estimator() |
| 48 | + |
| 49 | + print("Models loaded.") |
| 50 | + |
| 51 | +class SolubilityRequest(BaseModel): |
| 52 | + solvent_smiles: Optional[str] = None |
| 53 | + solute_smiles: Optional[str] = None |
| 54 | + temperature: Optional[float] = None |
| 55 | + reference_solvent: Optional[str] = None |
| 56 | + reference_solubility: Optional[float] = None |
| 57 | + hsub298: Optional[float] = None |
| 58 | + cp_gas_298: Optional[float] = None |
| 59 | + cp_solid_298: Optional[float] = None |
| 60 | + use_reference: bool = False |
| 61 | + |
| 62 | +@app.post("/dGsolv_estimator") |
| 63 | +def _dGsolv_estimator(req: SolubilityRequest): |
| 64 | + result = dGsolv_estimator([[req.solvent_smiles, req.solute_smiles]]) |
| 65 | + return { |
| 66 | + "avg_pred": result[0], |
| 67 | + "epi_unc": result[1], |
| 68 | + "valid_indices": result[2] |
| 69 | + } |
| 70 | + |
| 71 | +@app.post("/dHsolv_estimator") |
| 72 | +def _dHsolv_estimator(req: SolubilityRequest): |
| 73 | + result = dHsolv_estimator([[req.solvent_smiles, req.solute_smiles]]) |
| 74 | + return { |
| 75 | + "avg_pred": result[0], |
| 76 | + "epi_unc": result[1], |
| 77 | + "valid_indices": result[2] |
| 78 | + } |
| 79 | + |
| 80 | +@app.post("/SoluteML_estimator") |
| 81 | +def _SoluteML_estimator(req: SolubilityRequest): |
| 82 | + result = SoluteML_estimator([[req.solute_smiles]]) |
| 83 | + return { |
| 84 | + "avg_pred": result[0], |
| 85 | + "epi_unc": result[1], |
| 86 | + "valid_indices": result[2] |
| 87 | + } |
| 88 | + |
| 89 | +@app.post("/calc_solubility_no_ref") |
| 90 | +def _calc_solubility_no_ref(req: SolubilityRequest): |
| 91 | + """ |
| 92 | + Calculate solubility with no reference solvent and reference solubility |
| 93 | + """ |
| 94 | + hsubl_298 = np.array([req.hsub298]) if req.hsub298 is not None else None |
| 95 | + Cp_solid = np.array([req.cp_solid_298]) if req.cp_solid_298 is not None else None |
| 96 | + Cp_gas = np.array([req.cp_gas_298]) if req.cp_gas_298 is not None else None |
| 97 | + |
| 98 | + solub_data = SolubilityDataWrapper(solvent_smiles=req.solvent_smiles, solute_smiles=req.solute_smiles, temp=req.temperature) |
| 99 | + predictions = SolubilityPredictions(solub_data, solub_models, predict_aqueous=True, |
| 100 | + predict_reference_solvents=False, predict_t_dep=True, |
| 101 | + predict_solute_parameters=True, verbose=False) |
| 102 | + calculations = SolubilityCalculations(predictions, calculate_aqueous=True, |
| 103 | + calculate_reference_solvents=False, calculate_t_dep=True, |
| 104 | + calculate_t_dep_with_t_dep_hdiss=True, verbose=False, |
| 105 | + hsubl_298=hsubl_298, Cp_solid=Cp_solid, Cp_gas=Cp_gas) |
| 106 | + |
| 107 | + return { |
| 108 | + "logsT_method1": calculations.logs_T_with_const_hdiss_from_aq[0], |
| 109 | + "logsT_method2": calculations.logs_T_with_T_dep_hdiss_from_aq[0], |
| 110 | + "gsolv_T": calculations.gsolv_T[0], |
| 111 | + "hsolv_T": calculations.hsolv_T[0], |
| 112 | + "ssolv_T": calculations.ssolv_T[0], |
| 113 | + "hsubl_298": calculations.hsubl_298[0], |
| 114 | + "Cp_gas": calculations.Cp_gas[0], |
| 115 | + "Cp_solid": calculations.Cp_solid[0], |
| 116 | + "logs_T_with_T_dep_hdiss_error_message": None if calculations.logs_T_with_T_dep_hdiss_error_message is None else calculations.logs_T_with_T_dep_hdiss_error_message[0], |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | +@app.post("/calc_solubility_with_ref") |
| 121 | +def _calc_solubility_with_ref(req: SolubilityRequest): |
| 122 | + """ |
| 123 | + Calculate solubility with a reference solvent and reference solubility |
| 124 | + """ |
| 125 | + hsubl_298 = np.array([req.hsub298]) if req.hsub298 is not None else None |
| 126 | + Cp_solid = np.array([req.cp_solid_298]) if req.cp_solid_298 is not None else None |
| 127 | + Cp_gas = np.array([req.cp_gas_298]) if req.cp_gas_298 is not None else None |
| 128 | + |
| 129 | + solub_data = SolubilityDataWrapper(solvent_smiles=req.solvent_smiles, solute_smiles=req.solute_smiles, temp=req.temperature, |
| 130 | + ref_solub=req.reference_solubility, ref_solv=req.reference_solvent) |
| 131 | + predictions = SolubilityPredictions(solub_data, solub_models, predict_aqueous=False, |
| 132 | + predict_reference_solvents=True, predict_t_dep=True, |
| 133 | + predict_solute_parameters=True, verbose=False) |
| 134 | + calculations = SolubilityCalculations(predictions, calculate_aqueous=False, |
| 135 | + calculate_reference_solvents=True, calculate_t_dep=True, |
| 136 | + calculate_t_dep_with_t_dep_hdiss=True, verbose=False, |
| 137 | + hsubl_298=hsubl_298, Cp_solid=Cp_solid, Cp_gas=Cp_gas) |
| 138 | + |
| 139 | + return { |
| 140 | + "logsT_method1": calculations.logs_T_with_const_hdiss_from_ref[0], |
| 141 | + "logsT_method2": calculations.logs_T_with_T_dep_hdiss_from_ref[0], |
| 142 | + "gsolv_T": calculations.gsolv_T[0], |
| 143 | + "hsolv_T": calculations.hsolv_T[0], |
| 144 | + "ssolv_T": calculations.ssolv_T[0], |
| 145 | + "hsubl_298": calculations.hsubl_298[0], |
| 146 | + "Cp_gas": calculations.Cp_gas[0], |
| 147 | + "Cp_solid": calculations.Cp_solid[0], |
| 148 | + "logs_T_with_T_dep_hdiss_error_message": None if calculations.logs_T_with_T_dep_hdiss_error_message is None else calculations.logs_T_with_T_dep_hdiss_error_message[0], |
| 149 | + } |
0 commit comments