Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -8,7 +8,12 @@ from flask import Flask, request, jsonify # For creating the Flask API
|
|
| 8 |
store_sales_predictor_api = Flask("SuperKart Store Sales Predictor")
|
| 9 |
|
| 10 |
# Load the trained machine learning model SuperKart_Project_model_v1_0.joblib
|
| 11 |
-
model = joblib.load("SuperKart_Project_model_v1_0.joblib")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Define a route for the home page (GET request)
|
| 14 |
@store_sales_predictor_api.get('/')
|
|
@@ -49,11 +54,11 @@ def predict_store_sales():
|
|
| 49 |
# Make prediction (get store_sales)
|
| 50 |
predicted_store_sales = model.predict(input_data)[0]
|
| 51 |
|
| 52 |
-
# Calculate actual sales
|
| 53 |
-
predicted_sales = np.exp(predicted_store_sales)
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
# The conversion above is needed as we convert the model prediction (store sales) to actual sales using np.exp, which returns predictions as NumPy float32 values.
|
| 58 |
# When we send this value directly within a JSON response, Flask's jsonify function encounters a datatype error
|
| 59 |
|
|
@@ -71,6 +76,8 @@ def predict_store_sales_batch():
|
|
| 71 |
"""
|
| 72 |
# Get the uploaded CSV file from the request
|
| 73 |
file = request.files['file']
|
|
|
|
|
|
|
| 74 |
|
| 75 |
# Read the CSV file into a Pandas DataFrame
|
| 76 |
input_data = pd.read_csv(file)
|
|
@@ -79,10 +86,10 @@ def predict_store_sales_batch():
|
|
| 79 |
predicted_store_sales = model.predict(input_data).tolist()
|
| 80 |
|
| 81 |
# Calculate actual sales
|
| 82 |
-
predicted_sales = [round(float(np.exp(store_sales)), 2) for
|
| 83 |
|
| 84 |
# Create a dictionary of predictions with store IDs as keys
|
| 85 |
-
store_ids = input_data['
|
| 86 |
output_dict = dict(zip(store_ids, predicted_sales)) # Use actual sales
|
| 87 |
|
| 88 |
# Return the predictions dictionary as a JSON response
|
|
|
|
| 8 |
store_sales_predictor_api = Flask("SuperKart Store Sales Predictor")
|
| 9 |
|
| 10 |
# Load the trained machine learning model SuperKart_Project_model_v1_0.joblib
|
| 11 |
+
#model = joblib.load("SuperKart_Project_model_v1_0.joblib")
|
| 12 |
+
try:
|
| 13 |
+
model = joblib.load("SuperKart_Project_model_v1_0.joblib")
|
| 14 |
+
except Exception as e:
|
| 15 |
+
model = None
|
| 16 |
+
print("⚠️ Failed to load model:", e)
|
| 17 |
|
| 18 |
# Define a route for the home page (GET request)
|
| 19 |
@store_sales_predictor_api.get('/')
|
|
|
|
| 54 |
# Make prediction (get store_sales)
|
| 55 |
predicted_store_sales = model.predict(input_data)[0]
|
| 56 |
|
| 57 |
+
# Calculate actual sales (convert to plain Python float and round)
|
| 58 |
+
predicted_sales = round(float(np.exp(predicted_store_sales)), 2)
|
| 59 |
|
| 60 |
+
# Return the actual sales
|
| 61 |
+
return jsonify({'Predicted Sales (in dollars)': predicted_sales})
|
| 62 |
# The conversion above is needed as we convert the model prediction (store sales) to actual sales using np.exp, which returns predictions as NumPy float32 values.
|
| 63 |
# When we send this value directly within a JSON response, Flask's jsonify function encounters a datatype error
|
| 64 |
|
|
|
|
| 76 |
"""
|
| 77 |
# Get the uploaded CSV file from the request
|
| 78 |
file = request.files['file']
|
| 79 |
+
if file is None:
|
| 80 |
+
return jsonify({"error": "No file uploaded. Please upload a CSV file with key 'file'."}), 400
|
| 81 |
|
| 82 |
# Read the CSV file into a Pandas DataFrame
|
| 83 |
input_data = pd.read_csv(file)
|
|
|
|
| 86 |
predicted_store_sales = model.predict(input_data).tolist()
|
| 87 |
|
| 88 |
# Calculate actual sales
|
| 89 |
+
predicted_sales = [round(float(np.exp(store_sales)), 2) for store_sales in predicted_store_sales]
|
| 90 |
|
| 91 |
# Create a dictionary of predictions with store IDs as keys
|
| 92 |
+
store_ids = input_data['Store_Id'].tolist() # Assuming 'id' is the store ID column
|
| 93 |
output_dict = dict(zip(store_ids, predicted_sales)) # Use actual sales
|
| 94 |
|
| 95 |
# Return the predictions dictionary as a JSON response
|