Beispiel #1
0
 def train_model(background_tasks: BackgroundTasks, bot: Text, user: Text,
                 email: Text, process_type: Text):
     """
     train model common code when uploading files or training a model
     :param background_tasks: fast api background task
     :param bot: bot id
     :param user: user id
     :param email: user email for generating token for reload
     :param process_type: either upload or train
     """
     from ...shared.data.model_processor import ModelProcessor
     from ...shared.auth import Authentication
     from ...shared.data.constant import MODEL_TRAINING_STATUS
     from ...train import start_training
     exception = process_type != 'upload'
     ModelProcessor.is_training_inprogress(bot, raise_exception=exception)
     ModelProcessor.is_daily_training_limit_exceeded(
         bot, raise_exception=exception)
     ModelProcessor.set_training_status(
         bot=bot,
         user=user,
         status=MODEL_TRAINING_STATUS.INPROGRESS.value,
     )
     token = Authentication.generate_integration_token(
         bot,
         email,
         ACCESS_ROLES.TESTER.value,
         expiry=180,
         token_type=TOKEN_TYPE.DYNAMIC.value)
     background_tasks.add_task(start_training, bot, user, token)
Beispiel #2
0
def save_label(
        background_tasks: BackgroundTasks,
        image: str,
        params: str = Form("{}"),
        tag: str = DefaultLabelTag.FINAL.value,
        label: UploadFile = File(...),
):
    file_ext = "".join(pathlib.Path(
        label.filename).suffixes) if label.filename else ".nii.gz"
    label_file = tempfile.NamedTemporaryFile(suffix=file_ext).name
    tag = tag if tag else DefaultLabelTag.FINAL.value

    with open(label_file, "wb") as buffer:
        shutil.copyfileobj(label.file, buffer)
        background_tasks.add_task(remove_file, label_file)

    instance: MONAILabelApp = app_instance()
    save_params: Dict[str, Any] = json.loads(params) if params else {}
    logger.info(f"Save Label params: {params}")

    label_id = instance.datastore().save_label(image, label_file, tag,
                                               save_params)
    res = instance.on_save_label(image, label_id)
    res = res if res else {}
    res.update({
        "image": image,
        "label": label_id,
    })
    return res
Beispiel #3
0
async def register(user: UserInput, background: BackgroundTasks):
    if User.find_user_with_email(email=user.email):
        return Fortauto.response({"message": "Account already exist"},
                                 status_code=status.HTTP_400_BAD_REQUEST)
    if user.password.strip() == user.confirm_password.strip():
        try:
            password = UserMixin.hash_password(
                user.password) if UserMixin.hash_password(
                    user.password) else None
            newUser = User(email=user.email.lower(),
                           first_name=user.first_name.lower(),
                           last_name=user.last_name.lower(),
                           phone_number=user.phone_number,
                           city=user.city.lower(),
                           state=user.state.lower(),
                           address=user.address,
                           password=password)
            newUser.save(clean=True)
            if newUser:
                background.add_task(
                    Fortauto.mailUsr,
                    email_title=f"Account activation",
                    user_email=user.email,
                    email_message=f"Welcome to {WEBSITE_NAME}, "
                    f"kindly click on the link below to activate your "
                    f"account\n\n {user.website_url}/{newUser.id}/activate")
                return Fortauto.response(
                    {
                        "message":
                        "Account created successfully,"
                        " an verification link has been sent to your email"
                    },
                    status_code=status.HTTP_201_CREATED)
            return Fortauto.response({
                "message":
                "Error Creating account, please check your details or try"
            })
        except errors.NotUniqueError:
            return Fortauto.response(
                {"message": "User with email already exist"},
                status_code=status.HTTP_400_BAD_REQUEST)

        except errors.ValidationError:
            return Fortauto.response(
                {
                    "message":
                    "Error validating details, please check your details and try again"
                },
                status_code=status.HTTP_400_BAD_REQUEST)
        except Exception.__base__:
            return Fortauto.response(
                {
                    "message":
                    "Error Creating account, please check your details or try"
                },
                status_code=status.HTTP_400_BAD_REQUEST)
    return Fortauto.response({"message": "Password is does not match"},
                             status_code=status.HTTP_400_BAD_REQUEST)
Beispiel #4
0
def run_inference(
    background_tasks: BackgroundTasks,
    model: str,
    image: str = "",
    session_id: str = "",
    params: str = Form("{}"),
    file: UploadFile = File(None),
    label: UploadFile = File(None),
    output: Optional[ResultType] = None,
):
    request = {"model": model, "image": image}

    if not file and not image and not session_id:
        raise HTTPException(status_code=500, detail="Neither Image nor File not Session ID input is provided")

    instance: MONAILabelApp = app_instance()

    if file:
        file_ext = "".join(pathlib.Path(file.filename).suffixes) if file.filename else ".nii.gz"
        image_file = tempfile.NamedTemporaryFile(suffix=file_ext).name

        with open(image_file, "wb") as buffer:
            shutil.copyfileobj(file.file, buffer)
            request["image"] = image_file
            background_tasks.add_task(remove_file, image_file)

    if label:
        file_ext = "".join(pathlib.Path(label.filename).suffixes) if label.filename else ".nii.gz"
        label_file = tempfile.NamedTemporaryFile(suffix=file_ext).name

        with open(label_file, "wb") as buffer:
            shutil.copyfileobj(label.file, buffer)
            background_tasks.add_task(remove_file, label_file)

        # if binary file received, e.g. scribbles from OHIF - then convert using reference image
        if file_ext == ".bin":
            image_uri = instance.datastore().get_image_uri(image)
            label_file = binary_to_image(image_uri, label_file)

        request["label"] = label_file

    config = instance.info().get("config", {}).get("infer", {})
    request.update(config)

    p = json.loads(params) if params else {}
    request.update(p)

    if session_id:
        session = instance.sessions().get_session(session_id)
        if session:
            request["image"] = session.image
            request["session"] = session.to_json()

    logger.info(f"Infer Request: {request}")
    result = instance.infer(request)
    if result is None:
        raise HTTPException(status_code=500, detail="Failed to execute infer")
    return send_response(instance.datastore(), result, output, background_tasks)
Beispiel #5
0
def registerUserAccount(user:UserRegisterationInput, background:BackgroundTasks):
    if accountModel.UserAccount.get_singleUserByEmail(email=user.email):
        ErrorResponse = {"message": "Account does not exist"}
        return KhairoFullMixin.Response(userMessage=ErrorResponse, success=False,
                                    status_code=status.HTTP_400_BAD_REQUEST)
    if user.password.strip() == user.confirmPassword.strip():
        try:
            password = AccountManager.hash_password(password=user.password)
            newUserDetails = {
                "firstname": user.firstname,
                "lastname":user.lastname,
                "email": user.email,
                "password": password
            }
            newUser = accountModel.UserAccount(**newUserDetails).save(clean=True)
            if newUser:
                mailData = {
                    "title":"Khairo diet Account verification",
                    "message":f" Welcome to {WEBSITE_NAME}, {newUser.firstname} { newUser.lastname}\n "
                              f"Your account was created successfully please "
                              f"verify your email to continue\n {WEBSITE_URL}{API_BASE_URI}/user/{newUser.id}"
                }
                
                background.add_task(KhairoFullMixin.mailUser, userEmail=newUser.email,
                                             emailTitle=mailData.get("title"),
                                             emailMessage=mailData.get("message"))

                SuccessResponseData = {
                    "user": {"firstname":newUser.firstname, "lastname":newUser.lastname},
                    "message": "Account was created successfully",
                    "confirm email": "A mail was sent to your to confirm your email address"
                    }

                return KhairoFullMixin.Response(userMessage=SuccessResponseData, success=True,
                                                   status_code=status.HTTP_201_CREATED)
            ErrorResponse = {"message": "Error creating account, check your detail and try again"}
            return KhairoFullMixin.Response(userMessage=ErrorResponse, success=False,
                                               status_code=status.HTTP_400_BAD_REQUEST)
        except errors.ValidationError:
            ErrorResponse = {"message": "Error creating account, check your detail and try again"}
            return KhairoFullMixin.Response(userMessage=ErrorResponse, success=False,
                                               status_code=status.HTTP_400_BAD_REQUEST)

        except errors.NotUniqueError:
            ErrorResponse = {"message": "Account with this email already exist, try again"}
            return KhairoFullMixin.Response(userMessage=ErrorResponse, success=False,
                                            status_code=status.HTTP_400_BAD_REQUEST)

    ErrorResponse = {"message": "Password do not match, try again"}
    return KhairoFullMixin.Response(userMessage=ErrorResponse,
                                    success=False,
                                    status_code=status.HTTP_400_BAD_REQUEST)
Beispiel #6
0
def getPasswordLink(userOldData: GetPasswordResetLink, background: BackgroundTasks):
    user = accountModel.UserAccount.get_singleUserByEmail(email=userOldData.email)
    if user:
        mailData = {
            "title": "Password reset",
            "message": f"password reset pass code\n Password reset link: {userOldData.passwordReset_url}/{user.id}"
        }
        background.add_task(KhairoFullMixin.mailUser, userEmail=user.email,
                            emailTitle=mailData.get("title"),
                            emailMessage=mailData.get("message"))
        ResponseData = {"message": "Check your email for password reset link"}
        return KhairoFullMixin.Response(userMessage=ResponseData,
                                        status_code=status.HTTP_200_OK)
    ErrorResponseData = {"message": "Account does not exist"}
    return KhairoFullMixin.Response(userMessage=ErrorResponseData,
                                    status_code=status.HTTP_401_UNAUTHORIZED)
Beispiel #7
0
async def get_user_account_activation_link(background: BackgroundTasks,
                                           user: GetPasswordResetLink):
    current_user = User.find_user_with_email(email=user.email)
    if current_user:
        background.add_task(
            Fortauto.mailUsr,
            email_title=f"Account activation",
            user_email=user.email,
            email_message=f"Welcome to {WEBSITE_NAME}, "
            f"kindly click on the link below to activate your "
            f"account\n\n {user.website_url}/{current_user.id}/activate")
        return Fortauto.response(
            {
                "message":
                "Account created successfully,"
                "an verification link has been sent to your email"
            },
            status_code=status.HTTP_201_CREATED)
    return Fortauto.response({"message": "Account does not exist"},
                             status_code=status.HTTP_400_BAD_REQUEST)
Beispiel #8
0
async def get_password_reset_link(background: BackgroundTasks,
                                  user: GetPasswordResetLink):
    current_user = User.find_user_with_email(email=user.email)
    if current_user:
        background.add_task(
            Fortauto.mailUsr,
            user_email=user.email,
            email_message=
            f" Good day {current_user.first_name} {current_user.last_name}, You requested for password reset, "
            f"please click\n\n "
            f"on to link below to continue \n\n {user.website_url}/{current_user.id}/password/reset",
            email_title="Password reset link")
        return Fortauto.response(
            {
                "message":
                "password reset link has been sent your email address, click it to continue."
            },
            status_code=status.HTTP_200_OK)
    return Fortauto.response({"message": "Account does not exist"},
                             status_code=status.HTTP_400_BAD_REQUEST)
Beispiel #9
0
def create_session(
    background_tasks: BackgroundTasks,
    uncompress: bool = False,
    expiry: int = 0,
    files: List[UploadFile] = File(...),
):
    instance: MONAILabelApp = app_instance()
    sessions: Sessions = instance.sessions()
    if sessions is None:
        logger.error("Session Feature is Not Enabled")
        raise HTTPException(status_code=406, detail="Session Feature is Not Enabled")

    logger.info(f"Uncompress: {uncompress}; Expiry: {expiry}")
    logger.info(f"Request Files: {files}")

    received_dir = tempfile.NamedTemporaryFile().name
    os.makedirs(received_dir, exist_ok=True)

    input_image = ""
    total_files = 0
    for f in files:
        basename = get_basename(f.filename) if f.filename else tempfile.NamedTemporaryFile().name
        input_image = os.path.join(received_dir, basename)
        with open(input_image, "wb") as fb:
            shutil.copyfileobj(f.file, fb)

        total_files += 1
        logger.info(f"{total_files} => {f} => {input_image}")

    if total_files > 1:
        logger.info(f"Input has multiple files; Saving ALL into: {received_dir}")
        input_image = received_dir

    session_id, session_info = sessions.add_session(input_image, expiry, uncompress)
    background_tasks.add_task(remove_file, received_dir)

    if total_files == 0:
        raise HTTPException(status_code=404, detail="Image(s) Not Found")

    logger.info(f"Session ID: {session_id}; Info: {session_info.to_str()}")
    return {"session_id": session_id, "session_info": session_info.to_json()}
Beispiel #10
0
def add_image(
        background_tasks: BackgroundTasks,
        image: Optional[str] = None,
        params: str = Form("{}"),
        file: UploadFile = File(...),
):
    logger.info(f"Image: {image}; File: {file}; params: {params}")
    file_ext = "".join(pathlib.Path(
        file.filename).suffixes) if file.filename else ".nii.gz"

    image_id = image if image else os.path.basename(file.filename).replace(
        file_ext, "")
    image_file = tempfile.NamedTemporaryFile(suffix=file_ext).name

    with open(image_file, "wb") as buffer:
        shutil.copyfileobj(file.file, buffer)
        background_tasks.add_task(remove_file, image_file)

    instance: MONAILabelApp = app_instance()
    save_params: Dict[str, Any] = json.loads(params) if params else {}
    image_id = instance.datastore().add_image(image_id, image_file,
                                              save_params)
    return {"image": image_id}
Beispiel #11
0
 def train_model(background_tasks: BackgroundTasks, bot: Text, user: Text, email: Text, process_type: Text):
     """
     train model common code when uploading files or training a model
     :param background_tasks: fast api background task
     :param bot: bot id
     :param user: user id
     :param email: user email for generating token for reload
     :param process_type: either upload or train
     """
     from .data_processor.model_processor import ModelProcessor
     from .api.auth import Authentication
     from .data_processor.constant import MODEL_TRAINING_STATUS
     from .train import start_training
     exception = process_type != 'upload'
     ModelProcessor.is_training_inprogress(bot, raise_exception=exception)
     ModelProcessor.is_daily_training_limit_exceeded(bot, raise_exception=exception)
     ModelProcessor.set_training_status(
         bot=bot, user=user, status=MODEL_TRAINING_STATUS.INPROGRESS.value,
     )
     token = Authentication.create_access_token(data={"sub": email}, token_expire=180)
     background_tasks.add_task(
         start_training, bot, user, token.decode('utf8')
     )