This project demonstrates predicting a disease label from prescription records (synthetic dataset provided). It includes data generation, preprocessing, training, inference, and a small Streamlit demo.
Quickstart
- Create a virtual environment and install dependencies:
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt- Generate synthetic data and run a baseline training:
python data/generate_synthetic.py
python src/train.py --data data/synthetic_prescriptions.csv- Run the demo UI:
streamlit run app/streamlit_app.pyFiles
data/generate_synthetic.py: createsdata/synthetic_prescriptions.csv.src/preprocess.py: preprocessing helper functions.src/train.py: trains baseline model and savesmodels/model.joblib.src/infer.py: inference helper to load model and predict.src/prescription_ocr.py: OCR extraction and prescription text parsing.app/streamlit_app.py: simple Streamlit demo.requirements.txt: Python package requirements.
Next steps
- Replace synthetic data with a real prescription dataset if available.
- Improve feature engineering (drug embeddings, sequence models).
- Hyperparameter tuning and cross-validation.
OCR mode
The app also supports a prescription-image flow:
- Upload a prescription image.
- OCR extracts the text.
- The extracted text is parsed into structured fields such as
age,gender, and medicines. - The existing disease prediction model runs on those structured fields.
For OCR mode, you also need the Tesseract OCR system binary installed on your machine, in addition to the Python packages listed in requirements.txt.
On Ubuntu/Debian:
sudo apt-get update
sudo apt-get install tesseract-ocrFull Process
This project follows an end-to-end machine-learning workflow. The steps below map to the code in this repository and describe what to run and why.
- Data collection:: Gather prescription records with fields like
age,gender,drug_1,drug_2,drug_3, and a target labeldisease. Example generator: data/generate_synthetic.py. - Exploratory Data Analysis (EDA):: Inspect class balance, missing values, feature distributions, and drug frequency to guide preprocessing and modelling. Run the EDA script:
python notebooks/eda.py --data data/synthetic_prescriptions.csv. Output:notebooks/figs/*andnotebooks/eda_summary.md. - Preprocessing:: Clean data and build features. The pipeline is in src/preprocess.py. It performs basic cleaning, encodes
gender, scalesage, and converts drugs into a bag-of-drugs representation. - Feature engineering & selection:: Create additional features (e.g., drug combinations, counts, temporal features if available). Update
src/preprocess.pyto add domain-specific features. - Train baseline models:: Use
python src/train.py --data data/synthetic_prescriptions.csvto train a Logistic Regression baseline and an XGBoost model. Trained artifacts are saved inmodels/model.joblib. - Model tuning & validation:: Use cross-validation and hyperparameter search (e.g.,
GridSearchCV,RandomizedSearchCV, or Optuna) to improve performance. Evaluate with precision, recall, F1, and confusion matrices. - Evaluation & fairness checks:: Check class-wise metrics and ensure no sensitive-group leakage. Analyze per-class recall and precision; consider class weighting or resampling for imbalanced labels.
- Build inference pipeline::
src/infer.pyloads the saved artifacts and provides aPredictorclass for batch inference. - Demo / API:: A simple demo is in
app/streamlit_app.py. It supports both CSV input and OCR on prescription images. For production, wrapPredictorin a REST API using Flask or FastAPI. - Reporting & presentation:: Fill
report/README.mdwith methodology, results, and discussion. Prepare slides summarizing the problem, dataset, model, and performance.
Commands recap
# generate data
python data/generate_synthetic.py
# run EDA
python notebooks/eda.py --data data/synthetic_prescriptions.csv
# train
python src/train.py --data data/synthetic_prescriptions.csv
# evaluate
python src/evaluate.py --data data/synthetic_prescriptions.csv --model models/model.joblib
# run demo
streamlit run app/streamlit_app.py