Skip to content

3bsalam-1/Car-Info

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5,980 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš— Car Price Prediction API

Python FastAPI scikit-learn License

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.

✨ Features

  • 🎯 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 Stack

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

πŸ“ Project Structure

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

πŸš€ Quick Start

Prerequisites

  • Python 3.9 or higher
  • pip (Python package manager)

Installation

  1. Clone the repository

    git clone https://github.com/yourusername/Car-Info.git
    cd Car-Info
  2. Create a virtual environment (recommended)

    python -m venv venv
    
    # On Windows
    venv\Scripts\activate
    
    # On macOS/Linux
    source venv/bin/activate
  3. Install dependencies

    pip install -r requirements.txt

Running the API

Start the API server:

uvicorn src.main:app --reload

The API will be available at: http://localhost:8000

Access Interactive Documentation:

πŸ“– Usage

API Endpoint

Predict Car Price

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 prediction
  • 404 Not Found - Brand not found in dataset
  • 422 Unprocessable Entity - Invalid request format

Using cURL

curl -X POST "http://localhost:8000/predict" \
     -H "Content-Type: application/json" \
     -d "{\"brand\": \"Toyota\"}"

Using Python

import requests

response = requests.post(
    "http://localhost:8000/predict",
    json={"brand": "Hyundai"}
)

data = response.json()
print(f"Predicted Price: β‚Ή{data['predicted_price']:,}")

CLI Tool

Run the command-line interface for quick predictions:

python src/predict_cli.py

Example 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 Deployment

Build the Docker image

docker build -t car-price-api .

Run the container

docker run -d -p 8080:8080 --name car-api car-price-api

Access the API at: http://localhost:8080

Docker Compose (Optional)

Create a docker-compose.yml:

version: '3.8'
services:
  api:
    build: .
    ports:
      - "8080:8080"
    restart: unless-stopped

Run with:

docker-compose up -d

πŸ€– Model Information

Machine Learning Approach

  • 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)

Model Performance

The model uses a sophisticated matching algorithm:

  1. Predicts price based on brand specifications
  2. Calculates dynamic tolerance (50% of predicted price, minimum β‚Ή20,000)
  3. Finds closest matching car in user database
  4. Returns prediction with actual listing details

πŸ”§ Development

Project Setup for Development

# 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 8000

Code Style

This project follows:

  • PEP 8 style guidelines
  • Type hints for better code clarity
  • Docstrings for all functions and classes

πŸ“Š Supported Brands

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

🀝 Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/AmazingFeature
  3. Commit your changes: git commit -m 'Add some AmazingFeature'
  4. Push to the branch: git push origin feature/AmazingFeature
  5. Open a Pull Request

Contribution Guidelines

  • Follow PEP 8 coding standards
  • Add tests for new features
  • Update documentation as needed
  • Ensure all tests pass before submitting PR

πŸ“ API Response Codes

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

πŸ› Troubleshooting

Common Issues

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.joblib exists

Issue: Port already in use

  • Solution: Change the port: uvicorn src.main:app --port 8001

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘€ Author

Ahmed Mohamed

πŸ™ Acknowledgments


⭐ Star this repository if you find it helpful!

Made with ❀️ by Ahmed Abdulsalam

About

πŸš— 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.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors