Sales Prediction API – FastAPI Deployment
Project Overview
This project demonstrates how to deploy a trained sales prediction model as a RESTful API using FastAPI. The API allows users to send business-related inputs and receive predicted sales values in real time. The solution bridges the gap between data analysis/modeling and real-world application by making predictions accessible to other systems such as dashboards, web apps, or backend services.
The focus of this project is model deployment and API design, not model training.
Problem Statement
Retail businesses need reliable sales forecasts to support decision-making in areas such as marketing, inventory planning, and operational scaling. This API predicts total sales based on key drivers:
- Marketing spend
- Store size
- Seasonal timing (month number)
The goal is to expose the prediction logic through a clean, validated, and easy-to-use API.
Model and Data Description
The model was trained offline using historical sales data. Since sales is a continuous numeric variable, the task was formulated as a regression problem.
Input Features
The model uses the following features:
- Marketing_Spend (float): Amount spent on marketing activities
- Store_Size (float): Size or capacity indicator of the store
- Month_Number (int): Month of the year (used to capture seasonality)
Saved Artifacts
After training and evaluation, the following files were saved using joblib:
-
sales.joblib– the trained regression model -
sales_features.joblib– ordered list of feature names used during training
Saving the feature list ensures consistency between training and prediction.
Why FastAPI?
FastAPI is a modern Python framework optimized for building APIs. It was chosen for this project because it offers:
- High performance and fast response times
- Automatic request validation using Pydantic
- Clear and interactive API documentation
- Simple and clean syntax
These features make FastAPI especially suitable for deploying machine learning and statistical models.
API Architecture
The FastAPI application performs the following steps:
- Loads the trained model and feature list at startup
- Validates incoming requests using a Pydantic schema
- Formats input data to match the model’s expected structure
- Generates predictions using the trained model
- Returns results as JSON responses
FastAPI Implementation
Application Initialization
The model and feature names are loaded once when the API starts:
model = joblib.load('sales.joblib')
feature_names = joblib.load('sales_features.joblib')
The FastAPI application is initialized with a descriptive title:
sale = FastAPI(title="Sales Prediction API")
Request Schema
Incoming requests are validated using a Pydantic model:
class SalesFeatures(BaseModel):
Marketing_Spend: float
Store_Size: float
Month_Number: int
This ensures that all required fields are present and correctly typed before prediction is performed.
API Endpoints
Root Endpoint
GET /
Used to verify that the API is running.
Response:
{
"message": "Welcome to the Sales Prediction API!!"
}
Prediction Endpoint
POST /predict
Accepts sales-related inputs and returns a predicted sales value.
Example Request
{
"Marketing_Spend": 50000,
"Store_Size": 1200,
"Month_Number": 6
}
Example Response
{
"predicted_sales": 245000.75
}
The API internally:
- Orders input features using the saved feature list
- Converts inputs into a NumPy array
- Generates a prediction using the trained model
Testing the API
FastAPI automatically provides interactive documentation at:
http://127.0.0.1:8000/docs
This interface allows users to test endpoints, submit sample inputs, and view responses without additional tools.
The API can also be tested using Postman or cURL for integration testing.
How to Run the API
- Install dependencies:
pip install fastapi uvicorn joblib numpy
- Start the server:
uvicorn main:sale --reload
- Open the browser and navigate to:
-
http://127.0.0.1:8000/– API root -
http://127.0.0.1:8000/docs– interactive documentation
Conclusion
This project demonstrates how a trained sales prediction model can be deployed as a production-ready API using FastAPI. By combining strict input validation, efficient model loading, and clear endpoint design, the API enables reliable and scalable access to predictive insights. This approach allows organizations to integrate analytics directly into operational systems, turning data models into actionable business tools.
Top comments (0)