Spaces:
Paused
Paused
Upload app_flask.py with huggingface_hub
Browse files- app_flask.py +77 -0
app_flask.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import flask
|
| 2 |
+
from flask import Flask, request, jsonify
|
| 3 |
+
import joblib
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# Initialize the Flask application
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
|
| 10 |
+
# Load the trained model pipeline
|
| 11 |
+
# Ensure the path to your joblib file is correct
|
| 12 |
+
try:
|
| 13 |
+
model_pipeline = joblib.load('best_random_forest_pipeline.joblib')
|
| 14 |
+
except FileNotFoundError:
|
| 15 |
+
print("Error: Model file not found. Make sure 'best_random_forest_pipeline.joblib' is in the same directory.")
|
| 16 |
+
exit() # Exit if the model file is not found
|
| 17 |
+
|
| 18 |
+
@app.route('/')
|
| 19 |
+
def home():
|
| 20 |
+
return "Flask app is running. Use the /predict endpoint to get predictions."
|
| 21 |
+
|
| 22 |
+
# Define an endpoint for a single prediction
|
| 23 |
+
@app.route('/predict', methods=['POST'])
|
| 24 |
+
def predict():
|
| 25 |
+
if request.method == 'POST':
|
| 26 |
+
try:
|
| 27 |
+
# Get the data from the POST request
|
| 28 |
+
# This endpoint expects a single JSON object representing one data point
|
| 29 |
+
data = request.get_json()
|
| 30 |
+
|
| 31 |
+
# Convert the incoming JSON data to a pandas DataFrame
|
| 32 |
+
# Ensure the column names and order match the training data
|
| 33 |
+
# It's crucial that the keys in the incoming JSON match the original feature names
|
| 34 |
+
# expected by your preprocessor and model.
|
| 35 |
+
|
| 36 |
+
# Example: Assuming the incoming JSON has keys matching the original column names
|
| 37 |
+
# before one-hot encoding and dropping the target/log_sales.
|
| 38 |
+
# You might need to add validation here to ensure all required keys are present.
|
| 39 |
+
|
| 40 |
+
input_df = pd.DataFrame([data])
|
| 41 |
+
|
| 42 |
+
# Ensure categorical columns in input_df are of 'category' dtype
|
| 43 |
+
# This is important because the preprocessor expects this dtype for categorical columns
|
| 44 |
+
categorical_cols = ['Product_Sugar_Content', 'Product_Type', 'Store_Id', 'Store_Size', 'Store_Location_City_Type', 'Store_Type'] # List your actual categorical columns
|
| 45 |
+
for col in categorical_cols:
|
| 46 |
+
if col in input_df.columns:
|
| 47 |
+
input_df[col] = input_df[col].astype('category')
|
| 48 |
+
# If your original data had specific categories, you might need to set them here
|
| 49 |
+
# input_df[col] = input_df[col].cat.set_categories(your_original_data[col].cat.categories)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# Make a prediction using the loaded model pipeline
|
| 53 |
+
# The pipeline handles preprocessing internally
|
| 54 |
+
prediction = model_pipeline.predict(input_df)
|
| 55 |
+
|
| 56 |
+
# Return the prediction as a JSON response
|
| 57 |
+
# Since this is a single prediction, return the first (and only) element
|
| 58 |
+
return jsonify({'prediction': prediction[0]})
|
| 59 |
+
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return jsonify({'error': str(e)})
|
| 62 |
+
|
| 63 |
+
if __name__ == '__main__':
|
| 64 |
+
# To run this locally for testing:
|
| 65 |
+
# 1. Save this code as app_flask.py
|
| 66 |
+
# 2. Make sure your 'best_random_forest_pipeline.joblib' is in the same directory
|
| 67 |
+
# 3. Run 'python app_flask.py' in your terminal
|
| 68 |
+
|
| 69 |
+
# For Colab, you might need a tool like ngrok to expose the local server to the internet
|
| 70 |
+
# Or you can adapt this to run directly within Colab if needed, but a separate file is standard for deployment.
|
| 71 |
+
# Running directly in Colab:
|
| 72 |
+
# from flask_ngrok2 import run_with_ngrok
|
| 73 |
+
# run_with_ngrok(app)
|
| 74 |
+
# app.run()
|
| 75 |
+
|
| 76 |
+
# Standard way to run Flask app
|
| 77 |
+
app.run(debug=True)
|