Skip to content

Navigation Menu

Sign in
Sign up

Repository files navigation

πŸš€ Python GLM ML Pipeline

HowTo Data Science: Logistic Regression model churn probability

β†’ Watch it directly on YouTube

βœ… Logistic Regression churn prediction
βœ… FastAPI REST endpoint
βœ… OpenAI summarisation for human-readable explanations

πŸ”§ How to run

  1. Install requirements:
pip install -r requirements.txt
  1. Run locally:
uvicorn app:app --reload
  • app:app loads the FastAPI app from app.py.
  • --reload enables auto-reload for development (useful for code changes, not for production).
  1. Test with:
curl -X POST http://localhost:8000/predict -H "Content-Type: application/json" -d '{
 "age": 45.0,
 "tenure": 24.0,
 "monthly_charges": 79.85,
 "total_charges": 1800.0,
 "contract_type": "Month-to-month",
 "payment_method": "Electronic check"
}'

πŸ”‘ Environment Setup

Optionally enable external AI summaries:

export OPENAI_API_KEY="your_actual_openai_api_key"

πŸ“‚ Project Structure

Python_GML_ML_Pipeline/
β”œβ”€β”€ app.py # FastAPI application
β”œβ”€β”€ requirements.txt # Python dependencies
β”œβ”€β”€ logistic_model.pkl # Trained ML model (placeholder)
β”œβ”€β”€ scaler.pkl # Feature scaler (placeholder)
β”œβ”€β”€ Dockerfile # Container configuration
β”œβ”€β”€ .gitignore # Git ignore rules
└── README.md # This file

🐳 Run with Docker

  1. Build the Docker image:
docker build -t python-gml-ml-pipeline .
  1. Run the Docker container:
docker run -p 8000:8000 -e OPENAI_API_KEY="your_actual_openai_api_key" python-gml-ml-pipeline
  1. Access the FastAPI app:

πŸ§‘β€πŸŽ“ Learn More - How does this pipeline run?

Execution Flow:

  1. Load trained model and scaler (using joblib).
  2. API endpoint receives JSON data (new user or input data).
  3. Dataframe creation & scaling for consistency with training.
  4. Model predicts churn probability (or other target).
  5. Returns JSON response with prediction for integration into apps or dashboards.

βœ… Run locally:

uvicorn app:app --reload

βœ… Run in Docker:

docker build -t python-gml-ml-pipeline .
docker run -p 8000:8000 -e OPENAI_API_KEY="your_actual_openai_api_key" python-gml-ml-pipeline

Key reasons to use FastAPI:

  1. Modern async Python framework
  2. Automatic OpenAPI schema & Swagger docs
  3. Production-grade performance
  4. Can be integrated into microservices/SaaS

✨ Author

Pierre-Henry Soria

Made with ❀️ by Pierre-Henry Soria . A super passionate & enthusiastic Problem-Solver / Senior Software Engineer. Also a true cheese πŸ§€, ristretto β˜•οΈ, and dark chocolate lover! πŸ˜‹

@phenrysay pH-7 YouTube Video BlueSky

πŸ“Œ Notes

  • logistic_model.pkl and scaler.pkl are placeholders. Train and export your own models using joblib.dump.
  • This is an educational API prototype. The bundled three-feature artifacts were saved with scikit-learn 1.1.3 and do not match the six-field request or the pinned runtime. They are deliberately rejected; /health and /predict return HTTP 503 until compatible artifacts are supplied.
  • Export trusted artifacts with the pinned scikit-learn version. The scaler/preprocessor must record the six request fields in their declared order and handle the categorical values; the classifier must use classes [0, 1]. Training data, evaluation and a matching preprocessing pipeline are not included.
  • Generated summaries do not establish why a model made a prediction. With no OPENAI_API_KEY, no explanation request is sent; provider errors return a generic message.
  • Run python -m unittest discover -s tests for offline API tests. Success-path doubles verify routing, not predictive quality. Production deployment still needs dependency review, real model validation and access controls.

🧠 Final Wise Principle

"AI models become valuable when they’re deployable, explainable, and integrated into real products that create business value."

About

Developed an end-to-end customer churn prediction ML pipeline using Python, pandas, and scikit-learn. Implemented and trained a logistic regression model, then deployed it as a REST API service using FastAPI and containerized with Docker.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /