from pathlib import Path
import pandas as pd
import shutil
from utils.aioLogger import aioLogger


class deployModelMlflow:
    """this class read csv file downloaded from mlflow ui, then do the renaming and copy/paste for the model."""

    DEPLOY_MODEL_PATH = Path(r"./models/deploy")
    MLRUNS_FOLDER_ROOT = Path(r"./mlruns")

    def __init__(self, csv_file: Path, exp_id: int) -> None:
        self.logger = aioLogger(self.__class__.__name__).logger
        self.csv_file = csv_file
        self.csv_file_name = self.csv_file.stem
        self.mlruns_folder = self.MLRUNS_FOLDER_ROOT / str(exp_id)
        self._read_csv()

    def _read_csv(self):
        self.df = pd.read_csv(self.csv_file, header=0)

    def deploy_with_id(self, id: str, model_name_prefix: str):
        source = self.get_model_path_from_id(id)
        destination = self.DEPLOY_MODEL_PATH.joinpath(f"{model_name_prefix}-{id}.pkl")
        shutil.copy(str(source), str(destination))
        self.logger.info(f"copy and renaming to deploy model to {destination}")

    def get_model_path_from_id(self, runid: str):
        model_path = self.mlruns_folder / runid / "artifacts" / "model" / "model.pkl"
        return model_path

    def deploy_all_from_df(self):
        for idx in self.df.index:
            id = self.df.loc[idx, "Run ID"]
            model_name = self.df.loc[idx, "Name"].strip()
            self.deploy_with_id(
                id=id, model_name_prefix=f"{self.csv_file_name}-{model_name}"
            )


if __name__ == "__main__":
    csv_file_folder = Path(r"D:\Github\aio\mlflow_csv")
    csv_file_height = csv_file_folder / "height.csv"
    csv_file_weight = csv_file_folder / "weight.csv"
    deployModelMlflow(csv_file_height, exp_id=0).deploy_all_from_df()
    deployModelMlflow(csv_file_weight, exp_id=1).deploy_all_from_df()

from pathlib import Path
import pandas as pd
import shutil
from utils.aioLogger import aioLogger


class deployModelMlflow:
    """this class read csv file downloaded from mlflow ui, then do the renaming and copy/paste for the model."""

    DEPLOY_MODEL_PATH = Path(r"./models/deploy")
    MLRUNS_FOLDER_ROOT = Path(r"./mlruns")

    def __init__(self, csv_file: Path, exp_id: int) -> None:
        self.logger = aioLogger(self.__class__.__name__).logger
        self.csv_file = csv_file
        self.csv_file_name = self.csv_file.stem
        self.mlruns_folder = self.MLRUNS_FOLDER_ROOT / str(exp_id)
        self._read_csv()

    def _read_csv(self):
        self.df = pd.read_csv(self.csv_file, header=0)

    def deploy_with_id(self, id: str, model_name_prefix: str):
        source = self.get_model_path_from_id(id)
        destination = self.DEPLOY_MODEL_PATH.joinpath(f"{model_name_prefix}-{id}.pkl")
        shutil.copy(str(source), str(destination))
        self.logger.info(f"copy and renaming to deploy model to {destination}")

    def get_model_path_from_id(self, runid: str):
        model_path = self.mlruns_folder / runid / "artifacts" / "model" / "model.pkl"
        return model_path

    def deploy_all_from_df(self):
        for idx in self.df.index:
            id = self.df.loc[idx, "Run ID"]
            model_name = self.df.loc[idx, "Name"].strip()
            self.deploy_with_id(
                id=id, model_name_prefix=f"{self.csv_file_name}-{model_name}"
            )


if __name__ == "__main__":
    csv_file_folder = Path(r"D:\Github\aio\mlflow_csv")
    csv_file_height = csv_file_folder / "height.csv"
    csv_file_weight = csv_file_folder / "weight.csv"
    deployModelMlflow(csv_file_height, exp_id=0).deploy_all_from_df()
    deployModelMlflow(csv_file_weight, exp_id=1).deploy_all_from_df()

this class read csv file downloaded from mlflow ui, then do the renaming and copy/paste for the model.