A comprehensive FastAPI-based healthcare management system that provides intelligent patient scheduling using geographic clustering, disease outbreak forecasting, and Hausa language speech-to-text capabilities.
- Geographic Clustering: Automatically groups patients by residential location using KMeans clustering
- Smart Scheduling: Schedules patients from the same cluster on similar dates to optimize healthcare delivery
- Capacity Management: Enforces daily appointment limits to prevent facility overload
- Cluster-Based Recommendations: Suggests appointment dates based on cluster patterns
- Time Series Analysis: Uses SARIMAX models to forecast disease trends
- Multiple Diseases: Tracks and predicts malaria, cholera, meningitis, and diarrhea cases
- Performance Metrics: Calculates RMSE, MAE, and MAPE for forecast accuracy
- 12-Week Forecasts: Provides forward-looking predictions with confidence intervals
- Audio Processing: Converts audio files to text in Hausa language
- Multi-Format Support: Handles various audio formats (automatically converted to WAV)
- Google Speech Recognition: Leverages Google's speech recognition API
- Daily Clustering Jobs: Automatically re-clusters patients every day at 00:05 (Lagos time)
- Background Processing: Runs intensive tasks asynchronously to maintain API responsiveness
- Health Monitoring: Built-in health check endpoints for system status
- FastAPI: Modern Python web framework for building APIs
- SQLAlchemy: SQL toolkit and ORM for database operations
- PostgreSQL: Primary database (via psycopg2-binary)
- APScheduler: Background job scheduling
- scikit-learn: Machine learning for KMeans clustering
- statsmodels: SARIMAX time series forecasting
- pandas: Data manipulation and analysis
- SpeechRecognition: Speech-to-text conversion
- pydub: Audio file processing
- Python 3.8+
- PostgreSQL database
- FFmpeg (required for audio processing with pydub)
-
Clone the repository
git clone https://github.com/Rajput2000/AI4PHC.git cd AI4PHC
-
Create virtual environment
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Configure environment variables Create a
.env
file in the root directory:DATABASE_URL=postgresql://username:password@localhost:5432/your_database
-
Initialize the database Ensure your PostgreSQL database is running and tables are created according to the schema in
src/DBSchema.py
. -
Run the application
uvicorn app:app --reload --host 0.0.0.0 --port 8000
- GET
/
or/health
- Returns database and scheduler status
- Response includes timestamp in Lagos timezone
- POST
/upload-audio/
- Upload audio file for Hausa speech-to-text conversion
- Accepts: Audio files (WAV, MP3, etc.)
- Returns: Transcribed text
- POST
/batch-cluster-patients
- Manually trigger patient clustering job
- Runs in background
- Groups patients by geographic proximity
- POST
/schedule
- Schedule a patient appointment based on cluster assignment
- Request body:
{ "patient_number": "KA-2025-00029", "appointment_type_name": "General Consultation", "max_per_day": 50 }
- Response:
{ "status": "scheduled", "patient_number": "KA-2025-00029", "cluster_id": 3, "appointment_type_id": 1, "appointment_date": "2025-10-15" }
.
βββ app.py # Main FastAPI application
βββ requirements.txt # Python dependencies
βββ notebook.ipynb
βββ .env # Environment variables (create this)
βββ src/
β βββ audio_translate.py # Speech-to-text functionality
β βββ database.py # Database connection and session management
β βββ DBSchema.py # SQLAlchemy ORM models
β βββ disease_forecast.py # SARIMAX forecasting models
β βββ models.py # Pydantic request/response schemas
β βββ scheduler.py # Patient clustering and scheduling logic
β βββ utils.py # Utility functions (health checks)
βββ recordings/ # Directory for uploaded audio files
patient_id
: Primary keypatient_number
: Unique patient identifierresidential_latitude
: Geographic coordinateresidential_longitude
: Geographic coordinatecluster_id
: Assigned cluster group
appointment_id
: Primary keypatient_id
: Foreign key to patientsappointment_date
: Scheduled dateappointment_type_id
: Type of appointment
- Daily Clustering: Runs at 00:05 (Lagos time) to re-cluster all patients based on updated geographic data
- Default clusters: 5
- Batch size: 500 patients per update
- Algorithm: KMeans with k-means++ initialization
- Model: SARIMAX(1,1,1)(1,1,1,52)
- Forecast horizon: 12 weeks
- Seasonal period: 52 weeks
- Default max appointments per day: 50
- Default appointment type: "General Consultation"
- Connection Pooling: Configured with 10 persistent connections, 20 overflow connections
- Batch Processing: Clustering updates run in batches to prevent database timeouts
- Background Tasks: Intensive operations run asynchronously
- Pool Pre-ping: Ensures connection health before use
- Comprehensive logging throughout the application
- HTTP exceptions with appropriate status codes
- Database transaction rollback on errors
- Graceful scheduler shutdown
uvicorn app:app --reload
Once running, visit:
- Swagger UI:
http://localhost:8000/docs
- ReDoc:
http://localhost:8000/redoc
All timestamps use Africa/Lagos timezone (WAT, UTC+1).