A professional FastAPI-based REST API that predicts car prices using a Gradient Boosting machine learning model. Simply provide a car brand, and the API returns an intelligent price prediction along with the closest matching vehicle from a real user database.
- π― Accurate Price Predictions - Uses Gradient Boosting ML model trained on real car specifications
- π Smart Matching - Finds the closest matching car listing from user database
- π High Performance - Built with FastAPI for blazing-fast responses
- π Rich Data - Trained on 200+ car models with detailed specifications
- π³ Docker Ready - Containerized deployment with single command
- π Interactive Documentation - Auto-generated API docs with Swagger UI
- π§ CLI Tool - Command-line interface for quick predictions
- π CORS Enabled - Ready for web application integration
| Technology | Version | Purpose |
|---|---|---|
| Python | 3.9+ | Core programming language |
| FastAPI | 0.95.2 | High-performance web framework |
| scikit-learn | 1.2.2 | Machine learning model |
| Pandas | 2.0.0 | Data manipulation |
| Uvicorn | 0.22.0 | ASGI server |
| Docker | Latest | Containerization |
Car-Info/
βββ π README.md # Project documentation
βββ π .gitignore # Git ignore patterns
βββ π Dockerfile # Docker configuration
βββ π requirements.txt # Python dependencies
βββ π runtime.txt # Python runtime version
β
βββ π src/ # Source code
β βββ __init__.py # Package initializer
β βββ main.py # FastAPI application
β βββ predict_cli.py # CLI prediction tool
β
βββ π models/ # Machine learning models
β βββ gradient_boosting_model_v2.joblib
β
βββ π data/ # Datasets
βββ model.csv # Car specifications (205 models)
βββ user.csv # User car listings
- Python 3.9 or higher
- pip (Python package manager)
-
Clone the repository
git clone https://github.com/yourusername/Car-Info.git cd Car-Info -
Create a virtual environment (recommended)
python -m venv venv # On Windows venv\Scripts\activate # On macOS/Linux source venv/bin/activate
-
Install dependencies
pip install -r requirements.txt
Start the API server:
uvicorn src.main:app --reloadThe API will be available at: http://localhost:8000
Access Interactive Documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
Endpoint: POST /predict
Request Body:
{
"brand": "Maruti"
}Response:
{
"predicted_price": 750000,
"match": {
"fuel_type": 3,
"engine_displacement": 1197,
"no_cylinder": 4,
"seating_capacity": 5,
"transmission_type": 0,
"fuel_tank_capacity": 37,
"body_type": 2,
"max_torque_nm": 113,
"max_torque_rpm": 4400
}
}Status Codes:
200 OK- Successful prediction404 Not Found- Brand not found in dataset422 Unprocessable Entity- Invalid request format
curl -X POST "http://localhost:8000/predict" \
-H "Content-Type: application/json" \
-d "{\"brand\": \"Toyota\"}"import requests
response = requests.post(
"http://localhost:8000/predict",
json={"brand": "Hyundai"}
)
data = response.json()
print(f"Predicted Price: βΉ{data['predicted_price']:,}")Run the command-line interface for quick predictions:
python src/predict_cli.pyExample interaction:
Loading model and data...
π Enter car brand: Tata
π° Predicted Price: βΉ1,084,000 INR
β
Found matching car at the predicted price:
βββββββββββββ€ββββββββββββββββββββββ€βββββββββββββββ€βββββββββββββββββββββββ€βββββββββββββββββββββββ
β fuel_type β engine_displacement β no_cylinder β seating_capacity β transmission_type β
βββββββββββββͺββββββββββββββββββββββͺβββββββββββββββͺβββββββββββββββββββββββͺβββββββββββββββββββββββ‘
β 1 β 1497 β 4 β 5 β 0 β
βββββββββββββ§ββββββββββββββββββββββ§βββββββββββββββ§βββββββββββββββββββββββ§βββββββββββββββββββββββ
docker build -t car-price-api .docker run -d -p 8080:8080 --name car-api car-price-apiAccess the API at: http://localhost:8080
Create a docker-compose.yml:
version: '3.8'
services:
api:
build: .
ports:
- "8080:8080"
restart: unless-stoppedRun with:
docker-compose up -d- Algorithm: Gradient Boosting Regressor
- Training Data: 205 car models with 11 features
- Features Used:
- Fuel type
- Engine displacement
- Number of cylinders
- Seating capacity
- Transmission type
- Fuel tank capacity
- Body type
- Max torque (Nm & RPM)
- Max power (BHP & RPM)
The model uses a sophisticated matching algorithm:
- Predicts price based on brand specifications
- Calculates dynamic tolerance (50% of predicted price, minimum βΉ20,000)
- Finds closest matching car in user database
- Returns prediction with actual listing details
# Clone repository
git clone https://github.com/yourusername/Car-Info.git
cd Car-Info
# Create virtual environment
python -m venv venv
source venv/bin/activate # or venv\Scripts\activate on Windows
# Install dependencies
pip install -r requirements.txt
# Run in development mode
uvicorn src.main:app --reload --host 0.0.0.0 --port 8000This project follows:
- PEP 8 style guidelines
- Type hints for better code clarity
- Docstrings for all functions and classes
The API supports predictions for the following brands:
Click to expand brand list
- Maruti
- Mahindra
- Toyota
- Hyundai
- Tata
- Kia
- Honda
- Land Rover
- MG
- Citroen
- Nissan
- Renault
- Lamborghini
- Volkswagen
- Skoda
- Mercedes-Benz
- Volvo
- Jeep
- BMW
- Audi
- Force
- Ferrari
- Rolls-Royce
- Jaguar
- Porsche
- Lexus
- Bentley
- Maserati
- Aston Martin
- McLaren
- Mini
- BYD
- Datsun
- Isuzu
- Bajaj
- Strom Motors
- Compass
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create a feature branch:
git checkout -b feature/AmazingFeature - Commit your changes:
git commit -m 'Add some AmazingFeature' - Push to the branch:
git push origin feature/AmazingFeature - Open a Pull Request
- Follow PEP 8 coding standards
- Add tests for new features
- Update documentation as needed
- Ensure all tests pass before submitting PR
| Code | Description |
|---|---|
| 200 | Success - Price predicted successfully |
| 404 | Not Found - Brand not in dataset |
| 422 | Validation Error - Invalid input format |
| 500 | Server Error - Internal server error |
Issue: ModuleNotFoundError: No module named 'src'
- Solution: Ensure you're running commands from the project root directory
Issue: FileNotFoundError: model file not found
- Solution: Verify that
models/gradient_boosting_model_v2.joblibexists
Issue: Port already in use
- Solution: Change the port:
uvicorn src.main:app --port 8001
This project is licensed under the MIT License - see the LICENSE file for details.
Ahmed Mohamed
- GitHub: @3bsalam-1
- LinkedIn: Ahmed Abdulsalam
- Built with FastAPI
- ML powered by scikit-learn
- Data processing with Pandas
β Star this repository if you find it helpful!
Made with β€οΈ by Ahmed Abdulsalam