Download app.py from data2aihub/SuperKartPredictionBackend: direct link, hf CLI and curl.
- Browser
- Download file 3.81 kB
-
https://huggingface.co/spaces/data2aihub/SuperKartPredictionBackend/resolve/main/app.py
- Command line
-
hf download hf://spaces/data2aihub/SuperKartPredictionBackend/app.py
-
curl -L -o app.py https://huggingface.co/spaces/data2aihub/SuperKartPredictionBackend/resolve/main/app.py
3.81 kB
| # Import necessary libraries | |
| import numpy as np | |
| import joblib # For loading the serialized model | |
| import pandas as pd # For data manipulation | |
| from flask import Flask, request, jsonify # For creating the Flask API | |
| # Initialize the Flask application | |
| rental_price_predictor_api = Flask("SuperKart Revenue Predictor") | |
| # Load the trained machine learning model | |
| model = joblib.load("superKart_price_prediction_model_v1_0.joblib") | |
| # Define a route for the home page (GET request) | |
| def home(): | |
| """ | |
| This function handles GET requests to the root URL ('/') of the API. | |
| It returns a simple welcome message. | |
| """ | |
| return "Welcome to the SuperKart Revenue Prediction API!" | |
| # Define an endpoint for single Product prediction (POST request) | |
| #@rental_price_predictor_api.post('/v1/rental') | |
| def predict_rental_price(): | |
| """ | |
| This function handles POST requests to the '/v1/revenue' endpoint. | |
| It expects a JSON payload containing input details and returns | |
| the predicted revenue as a JSON response. | |
| """ | |
| # Get the JSON data from the request body | |
| property_data = request.get_json() | |
| # Extract relevant features from the JSON data | |
| sample = { | |
| 'Product_Weight': property_data['product_weight'], | |
| 'Product_Sugar_Content': property_data['product_sugar_content'], | |
| 'Product_Allocated_Area': property_data['product_allocated_area'], | |
| 'Product_Type': property_data['product_type'], | |
| 'Product_MRP': property_data['product_mrp'], | |
| 'Store_Id': property_data['store_id'], | |
| 'Store_Age': property_data['store_age'], | |
| 'Store_Size': property_data['store_size'], | |
| 'Store_Location_City_Type': property_data['store_location_city_type'], | |
| 'Store_Type': property_data['store_type'] | |
| } | |
| # Convert the extracted data into a Pandas DataFrame | |
| input_data = pd.DataFrame([sample]) | |
| # Make prediction (get log_price) | |
| predicted_price = model.predict(input_data)[0] # The model predicts the final price, not log price | |
| # Return the actual price | |
| predicted_price = round(float(predicted_price), 2) | |
| return jsonify({'Predicted Revenue (in dollars)': predicted_price}) | |
| # Define an endpoint for batch prediction (POST request) | |
| def predict_rental_price_batch(): | |
| """ | |
| This function handles POST requests to the '/v1/rentalbatch' endpoint. | |
| It expects a CSV file containing property details for multiple properties | |
| and returns the predicted rental prices as a dictionary in the JSON response. | |
| """ | |
| # Get the uploaded CSV file from the request | |
| file = request.files['file'] | |
| # Read the CSV file into a Pandas DataFrame | |
| input_data = pd.read_csv(file) | |
| # Make predictions for all properties in the DataFrame (get log_prices) | |
| predicted_prices = model.predict(input_data).tolist() # The model predicts the final price, not log price | |
| # Calculate actual prices | |
| predicted_prices = [round(float(price), 2) for price in predicted_prices] # Use predicted prices directly | |
| # Create a dictionary of predictions with property IDs as keys | |
| # Assuming the batch input CSV has an 'id' column | |
| if 'Product_Id' in input_data.columns: | |
| product_ids = input_data['Product_Id'].tolist() | |
| output_dict = dict(zip(product_ids, predicted_prices)) | |
| else: | |
| # If no 'Product_Id' column, return predictions in a list | |
| output_dict = {'predictions': predicted_prices} | |
| # Return the predictions dictionary as a JSON response | |
| return jsonify(output_dict) | |
| # Run the Flask application in debug mode if this script is executed directly | |
| if __name__ == '__main__': | |
| rental_price_predictor_api.run(debug=True) | |