Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +3 -3
- superKartApi.py +100 -0
Dockerfile
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
FROM python:3.9-slim
|
| 2 |
|
| 3 |
# Set the working directory inside the container
|
| 4 |
-
WORKDIR /
|
| 5 |
|
| 6 |
# Copy all files from the current directory to the container's working directory
|
| 7 |
COPY . .
|
|
@@ -12,5 +12,5 @@ RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
|
| 12 |
# Define the command to start the application using Gunicorn with 4 worker processes
|
| 13 |
# - `-w 4`: Uses 4 worker processes for handling requests
|
| 14 |
# - `-b 0.0.0.0:7860`: Binds the server to port 7860 on all network interfaces
|
| 15 |
-
# - `app:
|
| 16 |
-
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:
|
|
|
|
| 1 |
FROM python:3.9-slim
|
| 2 |
|
| 3 |
# Set the working directory inside the container
|
| 4 |
+
WORKDIR /superKartApi
|
| 5 |
|
| 6 |
# Copy all files from the current directory to the container's working directory
|
| 7 |
COPY . .
|
|
|
|
| 12 |
# Define the command to start the application using Gunicorn with 4 worker processes
|
| 13 |
# - `-w 4`: Uses 4 worker processes for handling requests
|
| 14 |
# - `-b 0.0.0.0:7860`: Binds the server to port 7860 on all network interfaces
|
| 15 |
+
# - `app:superKartApi`: Runs the Flask app (assuming `superKartApi.py` contains the Flask instance named `superKartApi`)
|
| 16 |
+
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:superKartApi"]
|
superKartApi.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import xgboost as xgb
|
| 4 |
+
from xgboost import XGBRegressor
|
| 5 |
+
from flask import Flask, request, jsonify
|
| 6 |
+
|
| 7 |
+
# Initialize Flask app with a name
|
| 8 |
+
superKartApi = Flask("Super Kart Sales Predictor")
|
| 9 |
+
|
| 10 |
+
# Load the trained churn prediction model
|
| 11 |
+
model = joblib.load("superkart_prediction_model_v1_0.joblib")
|
| 12 |
+
|
| 13 |
+
# Define a route for the home page
|
| 14 |
+
@superKartApi.get('/')
|
| 15 |
+
def home():
|
| 16 |
+
return "Welcome to the Superkart Sales Forecast API"
|
| 17 |
+
|
| 18 |
+
# Define an endpoint to predict churn for a single customer
|
| 19 |
+
@superKartApi.post('/v1/product')
|
| 20 |
+
def predict_forecast():
|
| 21 |
+
"""
|
| 22 |
+
This function handles POST requests to the '/v1/product' endpoint.
|
| 23 |
+
It expects a JSON payload containing property details and returns
|
| 24 |
+
the predicted rental price as a JSON response.
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
# Get JSON data from the request
|
| 28 |
+
superKar_data = request.get_json()
|
| 29 |
+
|
| 30 |
+
# Extract relevant customer features from the input data
|
| 31 |
+
sample = {
|
| 32 |
+
'ProductWeight': superKar_data['ProductWeight'],
|
| 33 |
+
'ProductSugarContent': superKar_data['ProductSugarContent'],
|
| 34 |
+
'ProductAllocatedArea': superKar_data['ProductAllocatedArea'],
|
| 35 |
+
'ProductType': superKar_data['ProductType'],
|
| 36 |
+
'ProductMRP': superKar_data['ProductMRP'],
|
| 37 |
+
'StoreId': superKar_data['StoreId'],
|
| 38 |
+
'StoreEstablishmentYear': superKar_data['StoreEstablishmentYear'],
|
| 39 |
+
'StoreSize': superKar_data['StoreSize'],
|
| 40 |
+
'StoreLocationCityType': superKar_data['StoreLocationCityType'],
|
| 41 |
+
'StoreType': superKar_data['StoreType']
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
# Convert the extracted data into a DataFrame
|
| 45 |
+
input_data = pd.DataFrame([sample])
|
| 46 |
+
|
| 47 |
+
# Make a churn prediction using the trained model
|
| 48 |
+
predicted_sales_value = model.predict(input_data).tolist()[0]
|
| 49 |
+
|
| 50 |
+
# Calculate actual price
|
| 51 |
+
predicted_sales = np.exp(predicted_sales_value)
|
| 52 |
+
|
| 53 |
+
# Convert predicted_price to Python float
|
| 54 |
+
predicted_sales = round(float(predicted_sales), 2)
|
| 55 |
+
|
| 56 |
+
# Return the actual price
|
| 57 |
+
return jsonify({'Predicted Sales': predicted_sales})
|
| 58 |
+
|
| 59 |
+
# # Map prediction result to a human-readable label
|
| 60 |
+
# prediction_label = "churn" if prediction == 1 else "not churn"
|
| 61 |
+
|
| 62 |
+
# # Return the prediction as a JSON response
|
| 63 |
+
# return jsonify({'Prediction': prediction_label})
|
| 64 |
+
|
| 65 |
+
# Define an endpoint to predict churn for a batch of customers
|
| 66 |
+
@superKartApi.post('/v1/superKartbatch')
|
| 67 |
+
def predict_churn_batch():
|
| 68 |
+
|
| 69 |
+
"""
|
| 70 |
+
This function handles POST requests to the '/v1/superKartbatch' endpoint.
|
| 71 |
+
It expects a CSV file containing property details for multiple properties
|
| 72 |
+
and returns the predicted rental prices as a dictionary in the JSON response.
|
| 73 |
+
"""
|
| 74 |
+
|
| 75 |
+
# Get the uploaded CSV file from the request
|
| 76 |
+
file = request.files['file']
|
| 77 |
+
|
| 78 |
+
# Read the file into a DataFrame
|
| 79 |
+
input_data = pd.read_csv(file)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
# Make predictions for all properties in the DataFrame (get log_sales)
|
| 83 |
+
predicted_log_sales = model.predict(input_data).tolist()
|
| 84 |
+
|
| 85 |
+
# Calculate actual sales
|
| 86 |
+
predicted_sales = [round(float(np.exp(log_price)), 2) for log_price in predicted_log_sales]
|
| 87 |
+
|
| 88 |
+
# # Make predictions for the batch data and convert raw predictions into a readable format
|
| 89 |
+
# predictions = [
|
| 90 |
+
# 'Churn' if x == 1
|
| 91 |
+
# else "Not Churn"
|
| 92 |
+
# for x in model.predict(input_data.drop("ProductId",axis=1)).tolist()
|
| 93 |
+
# ]
|
| 94 |
+
|
| 95 |
+
# Create a dictionary of predictions with property IDs as keys
|
| 96 |
+
product_id_list = input_data.ProductId.values.tolist()
|
| 97 |
+
output_dict = dict(zip(product_id_list, predicted_sales))
|
| 98 |
+
|
| 99 |
+
# Return the predictions dictionary as a JSON response
|
| 100 |
+
return output_dict
|