A comprehensive REST API built with FastAPI for managing hospital operations, including patient records, doctor information, admin authentication, and insurance premium prediction using machine learning.
- Patient Management: Complete CRUD operations for patient records
- Doctor Management: Manage doctor profiles and specialties
- Admin Authentication: Secure JWT-based authentication for admin users
- Insurance Premium Prediction: ML-powered insurance premium category prediction based on user demographics and lifestyle factors
- PostgreSQL Database: Robust database management with SQLAlchemy ORM
- RESTful API: Well-structured API endpoints following REST principles
- Streamlit Frontend: User-friendly web interface for all operations
The Streamlit frontend provides an intuitive interface for:
- Dashboard: Overview and quick access to all features
- Patient Management:
- Create, view, update, and delete patient records
- View list of all patients with pagination
- Doctor Management:
- Create, view, update, and delete doctor profiles
- Manage doctor specialties
- Insurance Premium Prediction:
- Interactive form for user input
- Real-time BMI and risk calculations
- Visual probability distribution
- Model health status
- Settings: Configure API URL and manage session
- Backend Framework: FastAPI 0.126.0
- Frontend Framework: Streamlit 1.39.0
- Database: PostgreSQL
- ORM: SQLAlchemy 2.0.45
- Authentication: JWT (python-jose), bcrypt
- Machine Learning: scikit-learn, pandas, numpy
- Server: Uvicorn
- HTTP Client: requests
- Python 3.11+
- PostgreSQL database
- pip (Python package manager)
-
Clone the repository (or navigate to the project directory)
-
Create a virtual environment (recommended):
python -m venv env
-
Activate the virtual environment:
- On Windows:
env\Scripts\activate
- On Linux/Mac:
source env/bin/activate
- On Windows:
-
Install dependencies:
pip install -r requirements.txt
-
Install PostgreSQL and ensure it's running on your system
-
Create a database:
CREATE DATABASE hospital_management;
-
Update database connection in
database.py:SQLALCHEMY_DATABASE_URL = "postgresql://username:password@localhost:5432/hospital_management"
Replace
usernameandpasswordwith your PostgreSQL credentials. -
Database tables will be created automatically when you run the application (via
schemas.Base.metadata.create_all(bind=engine)inmain.py)
-
Start the FastAPI server:
uvicorn main:app --reload
-
Access the API:
- API Base URL:
http://localhost:8000 - Interactive API Documentation (Swagger UI):
http://localhost:8000/docs - Alternative API Documentation (ReDoc):
http://localhost:8000/redoc
- API Base URL:
-
Make sure the FastAPI server is running (see above)
-
Start the Streamlit application:
streamlit run streamlit_app.py
-
Access the frontend:
- The Streamlit app will open automatically in your browser at
http://localhost:8501 - If it doesn't open automatically, navigate to the URL shown in the terminal
- The Streamlit app will open automatically in your browser at
-
Login:
- If you don't have an admin account, create one using the "Create New Admin Account" form on the login page
- Then login with your credentials to access the dashboard
GET /- Welcome messageGET /check-connection- Database connection status
POST /admin/add- Create a new admin userPOST /admin/token- Login and get JWT access token
POST /patients/patient/- Create a new patientGET /patients/patient/{patient_id}- Get patient by IDGET /patients/patients_list/{limit}- Get list of patients (with limit)PUT /patients/patient_id/{id}- Update patient informationDELETE /patients/patient_id/{id}- Delete a patient
POST /doctors/doctor/- Create a new doctorGET /doctors/doctor/{doctor_id}- Get doctor by IDPUT /doctors/doctor_id/{id}- Update doctor informationDELETE /doctors/doctor_id/{id}- Delete a doctor
GET /insurance_premium/- API informationGET /insurance_premium/health- Health check with model versionPOST /insurance_premium/predict- Predict insurance premium category
curl -X POST "http://localhost:8000/admin/add" \
-H "Content-Type: application/json" \
-d '{
"username": "admin",
"password": "securepassword123"
}'curl -X POST "http://localhost:8000/admin/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=admin&password=securepassword123"curl -X POST "http://localhost:8000/patients/patient/" \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"age": 35,
"weight": 75.5,
"height": 1.75
}'curl -X POST "http://localhost:8000/doctors/doctor/" \
-H "Content-Type: application/json" \
-d '{
"name": "Dr. Jane Smith",
"specialty": "Cardiology"
}'curl -X POST "http://localhost:8000/insurance_premium/predict" \
-H "Content-Type: application/json" \
-d '{
"age": 35,
"weight": 75.5,
"height": 1.75,
"income_lpa": 12.5,
"smoker": false,
"city": "Mumbai",
"occupation": "Engineer"
}'The insurance premium prediction endpoint uses a machine learning model to predict premium categories (Low, Medium, High) based on:
- Age: User's age (0-120)
- Weight: Weight in kg
- Height: Height in meters (0-2.5)
- Income: Annual income in LPA (Lakhs Per Annum)
- Smoker: Boolean indicating smoking status
- City: City name (automatically categorized into tier 1, 2, or 3)
- Occupation: One of: Engineer, Driver, Teacher, Banker, Sales Manager, Businessman, Factory Worker
The model automatically calculates:
- BMI: Body Mass Index
- Lifestyle Risk: Low, Medium, or High based on smoking and BMI
- Age Group: young, adult, middle_aged, or senior
- City Tier: 1, 2, or 3 based on city classification
FastAPI/
├── main.py # FastAPI application entry point
├── streamlit_app.py # Streamlit frontend application
├── database.py # Database connection and configuration
├── database_models.py # Database models (legacy)
├── schemas.py # Pydantic models and SQLAlchemy models
├── requirements.txt # Python dependencies
├── router/
│ ├── auth.py # Admin authentication routes
│ ├── patients.py # Patient management routes
│ ├── doctors.py # Doctor management routes
│ └── insurance.py # Insurance premium prediction routes
└── model/
├── model1.pkl # Trained ML model
└── predict.py # Prediction logic
- JWT Secret Key: The current secret key in
router/auth.pyshould be changed in production. Use a secure, randomly generated key. - Password Hashing: Passwords are hashed using bcrypt before storage.
- Token Expiration: Access tokens expire after 20 minutes (configurable).
- The API uses automatic database table creation on startup
- CORS middleware is commented out but available for frontend integration
- The application runs in development mode with auto-reload enabled
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
This project is open source and available for use.
For issues and questions, please open an issue in the repository.