Skip to content

ORION2809/gail

Repository files navigation

Gail Model (BCRA) — Dockerized R + FastAPI Frontend

This project provides an end-to-end example: Dockerized R script that runs the NCI BCRA absolute.risk() (Gail Model), a FastAPI backend that calls the Docker container (or a local Rscript fallback), and a simple frontend served by FastAPI.

Files

  • gail_model.R — Rscript that calls BCRA::absolute.risk() and prints JSON.
  • Dockerfile — Builds an image with R and the BCRA package; entrypoint runs gail_model.R.
  • main.py — FastAPI application serving index.html and /predict endpoint which calls the R container or falls back to local Rscript.
  • index.html — Simple frontend form and JS to POST to /predict.

Prerequisites

  • Docker installed and running if you plan to use the Dockerized R image (recommended for isolation).
  • Conda (miniconda/anaconda) to create the provided conda environment which also includes R.
  • Python 3.8+ (the conda env provides Python)

Note: the conda environment now includes r-base so R is available inside the environment. The Python docker SDK is installed via requirements.txt.

Setup

  1. Create and activate the conda environment (this installs Python, pip and R). Use conda-forge to ensure r-base is available:

    conda env create -f environment.yml -c conda-forge
    conda activate gail-env
  2. Install Python dependencies in the conda env (the environment.yml already installs pip packages, but you can ensure by running):

    pip install -r requirements.txt
  3. Install required R packages inside the conda env (this installs BCRA and helper packages):

    Rscript -e "install.packages(c('BCRA','optparse','jsonlite'), repos='https://cloud.r-project.org')"

Run the FastAPI server

uvicorn main:app --reload --host 0.0.0.0 --port 8000

Open http://localhost:8000/ in your browser, fill the form and submit.

Notes

  • The FastAPI /predict endpoint runs the Docker image with docker run --rm gail_model_container and passes CLI args when Docker is available. If Docker is not installed or not on PATH, it falls back to running Rscript gail_model.R locally (ensure R and required packages are installed in that case).

  • If you rename the Docker image, set the GAIL_MODEL_IMAGE environment variable before starting FastAPI, e.g.:

    $env:GAIL_MODEL_IMAGE = 'my_gail_image'
    uvicorn main:app --reload

Troubleshooting

  • If the R container fails due to missing packages during image build, try rebuilding and inspect build logs.
  • If the FastAPI server cannot run docker, ensure the user has permission to run docker. On Windows using Docker Desktop, ensure Docker is running.

Security

  • This example runs docker from the FastAPI process using the host Docker engine. In production, consider running the R computation in a more secure way (e.g., via an internal queue, separate service, or using an API wrapper around the R runtime) and validate/sanitize inputs carefully.

License

  • Uses the BCRA package from CRAN (NCI). Follow the package license when using in production.

About

BCRA

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors