alessandro trinca tornidor
		
	commited on
		
		
					Commit 
							
							·
						
						8980653
	
1
								Parent(s):
							
							24292ed
								
ci: add HEALTHCHECK step calling the /health_models endpoint
Browse files- Dockerfile +3 -2
 - app.py +25 -0
 - scripts/healthcheck.py +23 -0
 
    	
        Dockerfile
    CHANGED
    
    | 
         @@ -177,6 +177,7 @@ ENV IS_AWS_LAMBDA="" 
     | 
|
| 177 | 
         | 
| 178 | 
         
             
            RUN mkdir ${FASTAPI_STATIC}
         
     | 
| 179 | 
         | 
| 
         | 
|
| 180 | 
         
             
            COPY --chown=python:python ./app.py ${WORKDIR_ROOT}/
         
     | 
| 181 | 
         
             
            COPY --chown=python:python ./sam-quantized/machine_learning_models ${WORKDIR_ROOT}/machine_learning_models
         
     | 
| 182 | 
         
             
            COPY --chown=python:python --from=node_prod_deps /appnode/node_modules* ${FASTAPI_STATIC}/node_modules
         
     | 
| 
         @@ -217,8 +218,6 @@ RUN echo "FASTAPI_STATIC:" 
     | 
|
| 217 | 
         
             
            RUN ls -l ${FASTAPI_STATIC}/ || true
         
     | 
| 218 | 
         
             
            RUN ls -l ${FASTAPI_STATIC}/dist || true
         
     | 
| 219 | 
         
             
            RUN ls -l ${FASTAPI_STATIC}/node_modules || true
         
     | 
| 220 | 
         
            -
            RUN chown python:python ${WORKDIR_ROOT}/docker_entrypoint.sh ${WORKDIR_ROOT}/entrypoint.sh
         
     | 
| 221 | 
         
            -
            RUN chmod 744 ${WORKDIR_ROOT}/docker_entrypoint.sh ${WORKDIR_ROOT}/entrypoint.sh
         
     | 
| 222 | 
         
             
            RUN ls -ld ${WORKDIR_ROOT}/
         
     | 
| 223 | 
         
             
            RUN ls -lA ${WORKDIR_ROOT}/
         
     | 
| 224 | 
         
             
            RUN ls -l ${WORKDIR_ROOT}/.venv
         
     | 
| 
         @@ -230,3 +229,5 @@ RUN ls -l ${WORKDIR_ROOT}/.venv/bin/activate 
     | 
|
| 230 | 
         
             
            #]
         
     | 
| 231 | 
         
             
            # CMD ["/usr/bin/bash", "-c", "source /var/task/.venv/bin/activate && uvicorn app:app --host 0.0.0.0 --port 7860"]
         
     | 
| 232 | 
         
             
            CMD ["/var/task/docker_entrypoint.sh"]
         
     | 
| 
         | 
|
| 
         | 
| 
         | 
|
| 177 | 
         | 
| 178 | 
         
             
            RUN mkdir ${FASTAPI_STATIC}
         
     | 
| 179 | 
         | 
| 180 | 
         
            +
            COPY --chown=python:python ./scripts/healthcheck.py ${WORKDIR_ROOT}/
         
     | 
| 181 | 
         
             
            COPY --chown=python:python ./app.py ${WORKDIR_ROOT}/
         
     | 
| 182 | 
         
             
            COPY --chown=python:python ./sam-quantized/machine_learning_models ${WORKDIR_ROOT}/machine_learning_models
         
     | 
| 183 | 
         
             
            COPY --chown=python:python --from=node_prod_deps /appnode/node_modules* ${FASTAPI_STATIC}/node_modules
         
     | 
| 
         | 
|
| 218 | 
         
             
            RUN ls -l ${FASTAPI_STATIC}/ || true
         
     | 
| 219 | 
         
             
            RUN ls -l ${FASTAPI_STATIC}/dist || true
         
     | 
| 220 | 
         
             
            RUN ls -l ${FASTAPI_STATIC}/node_modules || true
         
     | 
| 
         | 
|
| 
         | 
|
| 221 | 
         
             
            RUN ls -ld ${WORKDIR_ROOT}/
         
     | 
| 222 | 
         
             
            RUN ls -lA ${WORKDIR_ROOT}/
         
     | 
| 223 | 
         
             
            RUN ls -l ${WORKDIR_ROOT}/.venv
         
     | 
| 
         | 
|
| 229 | 
         
             
            #]
         
     | 
| 230 | 
         
             
            # CMD ["/usr/bin/bash", "-c", "source /var/task/.venv/bin/activate && uvicorn app:app --host 0.0.0.0 --port 7860"]
         
     | 
| 231 | 
         
             
            CMD ["/var/task/docker_entrypoint.sh"]
         
     | 
| 232 | 
         
            +
            # HEALTHCHECK --interval=30s --timeout=900s --start-period=5s --retries=3 CMD "python -c 'import requests; r1 = requests.get(\"http://localhost:7860/health\"); print(r1.status_code); r2 = requests.get(\"http://localhost:7860/health_models\"); print(f\"status health:{r1.status_code}, health_models:{r2.status_code}!\"); exit(0) if r1.status_code == 200 and r2.status_code == 200 else exit(1)'"
         
     | 
| 233 | 
         
            +
            HEALTHCHECK --interval=10s --timeout=1080s --start-period=10s --start-interval=10s --retries=3 CMD [ "python", "healthcheck.py" ]
         
     | 
    	
        app.py
    CHANGED
    
    | 
         @@ -115,6 +115,31 @@ async def health() -> JSONResponse: 
     | 
|
| 115 | 
         
             
                return JSONResponse(status_code=200, content={"msg": "still alive..."})
         
     | 
| 116 | 
         | 
| 117 | 
         | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 118 | 
         
             
            def infer_lisa_gradio(request_input: StringPromptApiRequestBody) -> str:
         
     | 
| 119 | 
         
             
                from samgis_lisa.io_package.wrappers_helpers import get_parsed_bbox_points_with_string_prompt
         
     | 
| 120 | 
         
             
                from samgis_lisa.prediction_api import lisa
         
     | 
| 
         | 
|
| 115 | 
         
             
                return JSONResponse(status_code=200, content={"msg": "still alive..."})
         
     | 
| 116 | 
         | 
| 117 | 
         | 
| 118 | 
         
            +
            @app.get("/health_models")
         
     | 
| 119 | 
         
            +
            async def health_models() -> JSONResponse:
         
     | 
| 120 | 
         
            +
                from samgis_lisa.prediction_api import lisa
         
     | 
| 121 | 
         
            +
                from samgis_lisa.utilities.constants import LISA_INFERENCE_FN
         
     | 
| 122 | 
         
            +
                from samgis_web.__version__ import __version__ as version_web
         
     | 
| 123 | 
         
            +
                from samgis_core.__version__ import __version__ as version_core
         
     | 
| 124 | 
         
            +
                from lisa_on_cuda.__version__ import __version__ as version_lisa_on_cuda
         
     | 
| 125 | 
         
            +
                from samgis_lisa.__version__ import __version__ as version_samgis_lisa
         
     | 
| 126 | 
         
            +
                from samgis_lisa.prediction_api.global_models import models_dict
         
     | 
| 127 | 
         
            +
             
     | 
| 128 | 
         
            +
                app_logger.info(f"still alive, version_web:{version_web}, version_core:{version_core}.")
         
     | 
| 129 | 
         
            +
                app_logger.info(f"still alive, version_lisa_on_cuda:{version_lisa_on_cuda}, version_samgis_lisa:{version_samgis_lisa}.")
         
     | 
| 130 | 
         
            +
                app_logger.info(f"try to load inference function for '{LISA_INFERENCE_FN}' model...")
         
     | 
| 131 | 
         
            +
                if models_dict[LISA_INFERENCE_FN]["inference"] is None:
         
     | 
| 132 | 
         
            +
                    app_logger.info(f"model not found, loading inference function for '{LISA_INFERENCE_FN}' model. This could take some minutes...")
         
     | 
| 133 | 
         
            +
                    lisa.load_model_and_inference_fn(LISA_INFERENCE_FN, inference_decorator=None, device_map="auto", device="cuda")
         
     | 
| 134 | 
         
            +
                try:
         
     | 
| 135 | 
         
            +
                    model_name = models_dict[LISA_INFERENCE_FN]["inference"]
         
     | 
| 136 | 
         
            +
                    app_logger.info(f"inference function for '{LISA_INFERENCE_FN}' model => '{model_name.__name__}' found and loaded...")
         
     | 
| 137 | 
         
            +
                except KeyError as ke:
         
     | 
| 138 | 
         
            +
                    app_logger.error(f"model not found, error:{ke}.")
         
     | 
| 139 | 
         
            +
                    raise HTTPException(status_code=500, detail="Internal Server Error")
         
     | 
| 140 | 
         
            +
                return JSONResponse(status_code=200, content={"msg": f"still alive, inference function for '{LISA_INFERENCE_FN}' model loaded..."})
         
     | 
| 141 | 
         
            +
             
     | 
| 142 | 
         
            +
             
     | 
| 143 | 
         
             
            def infer_lisa_gradio(request_input: StringPromptApiRequestBody) -> str:
         
     | 
| 144 | 
         
             
                from samgis_lisa.io_package.wrappers_helpers import get_parsed_bbox_points_with_string_prompt
         
     | 
| 145 | 
         
             
                from samgis_lisa.prediction_api import lisa
         
     | 
    	
        scripts/healthcheck.py
    ADDED
    
    | 
         @@ -0,0 +1,23 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
             
     | 
| 2 | 
         
            +
            def run_healthcheck():
         
     | 
| 3 | 
         
            +
                import os
         
     | 
| 4 | 
         
            +
                import requests
         
     | 
| 5 | 
         
            +
                import sys
         
     | 
| 6 | 
         
            +
                import structlog
         
     | 
| 7 | 
         
            +
                from samgis_core.utilities.session_logger import setup_logging
         
     | 
| 8 | 
         
            +
             
     | 
| 9 | 
         
            +
                log_level = os.getenv("LOG_LEVEL", "INFO")
         
     | 
| 10 | 
         
            +
                url1 = os.getenv("HEALTHCHECK_URL1", "http://localhost:7860/health")
         
     | 
| 11 | 
         
            +
                url2 = os.getenv("HEALTHCHECK_URL2", "http://localhost:7860/health_models")
         
     | 
| 12 | 
         
            +
                setup_logging(log_level=log_level)
         
     | 
| 13 | 
         
            +
                app_logger = structlog.stdlib.get_logger()
         
     | 
| 14 | 
         
            +
             
     | 
| 15 | 
         
            +
                r1 = requests.get(url1)
         
     | 
| 16 | 
         
            +
                app_logger.info(r1.status_code)
         
     | 
| 17 | 
         
            +
                r2 = requests.get(url2)
         
     | 
| 18 | 
         
            +
                app_logger.info(f"status health:{r1.status_code}, health_models:{r2.status_code}!")
         
     | 
| 19 | 
         
            +
                sys.exit(0) if r1.status_code == 200 and r2.status_code == 200 else sys.exit(1)
         
     | 
| 20 | 
         
            +
             
     | 
| 21 | 
         
            +
             
     | 
| 22 | 
         
            +
            if __name__ == "__main__":
         
     | 
| 23 | 
         
            +
                run_healthcheck()
         
     |