Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- app.py +15 -11
- requirements.txt +0 -1
app.py
CHANGED
|
@@ -7,8 +7,13 @@ from flask import Flask, request, jsonify # For creating the Flask API
|
|
| 7 |
# Initialize the Flask application
|
| 8 |
super_kart_api = Flask("Super Kart Price Predictor")
|
| 9 |
|
| 10 |
-
# Load the trained machine learning model (updated to match
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Define a route for the home page (GET request)
|
| 14 |
@super_kart_api.get('/')
|
|
@@ -29,7 +34,7 @@ def predict_sales():
|
|
| 29 |
"""
|
| 30 |
# Get the JSON data from the request body
|
| 31 |
input_data = request.get_json()
|
| 32 |
-
|
| 33 |
# Extract relevant features from the JSON data
|
| 34 |
# Note: Exclude Product_Id and Store_Id if they are not used in prediction
|
| 35 |
sample = {
|
|
@@ -43,32 +48,31 @@ def predict_sales():
|
|
| 43 |
'Store_Location_City_Type': input_data['Store_Location_City_Type'],
|
| 44 |
'Store_Type': input_data['Store_Type']
|
| 45 |
}
|
| 46 |
-
|
| 47 |
# Convert the extracted data into a Pandas DataFrame
|
| 48 |
features_df = pd.DataFrame([sample])
|
| 49 |
-
|
| 50 |
# Apply one-hot encoding for nominal columns (matching training)
|
| 51 |
features_df = pd.get_dummies(features_df, columns=['Product_Type', 'Store_Type'], drop_first=True)
|
| 52 |
-
|
| 53 |
# Apply ordinal encoding (based on provided orders)
|
| 54 |
sugar_mapping = {'No Sugar': 0, 'Low Sugar': 1, 'Regular': 2}
|
| 55 |
size_mapping = {'Small': 0, 'Medium': 1, 'High': 2}
|
| 56 |
city_mapping = {'Tier 3': 0, 'Tier 2': 1, 'Tier 1': 2}
|
| 57 |
-
|
| 58 |
features_df['Product_Sugar_Content'] = features_df['Product_Sugar_Content'].map(sugar_mapping)
|
| 59 |
features_df['Store_Size'] = features_df['Store_Size'].map(size_mapping)
|
| 60 |
features_df['Store_Location_City_Type'] = features_df['Store_Location_City_Type'].map(city_mapping)
|
| 61 |
-
|
| 62 |
# Make prediction (assuming direct sales prediction; adjust if log-transformed)
|
| 63 |
predicted_sales = model.predict(features_df)[0]
|
| 64 |
-
|
| 65 |
# If your model predicts log(sales), uncomment and use this instead:
|
| 66 |
# predicted_log_sales = model.predict(features_df)[0]
|
| 67 |
# predicted_sales = np.exp(predicted_log_sales)
|
| 68 |
-
|
| 69 |
# Convert to Python float and round to 2 decimals
|
| 70 |
predicted_sales = round(float(predicted_sales), 2)
|
| 71 |
-
|
| 72 |
# Return the predicted sales total
|
| 73 |
return jsonify({'Predicted Sales Total (in dollars)': predicted_sales})
|
| 74 |
|
|
|
|
| 7 |
# Initialize the Flask application
|
| 8 |
super_kart_api = Flask("Super Kart Price Predictor")
|
| 9 |
|
| 10 |
+
# Load the trained machine learning model (updated path to match deployment structure)
|
| 11 |
+
model_path = "super_kart_model_v1_0.joblib"
|
| 12 |
+
try:
|
| 13 |
+
model = joblib.load(model_path)
|
| 14 |
+
print(f"Model loaded successfully from {model_path}")
|
| 15 |
+
except FileNotFoundError:
|
| 16 |
+
raise FileNotFoundError(f"Model file not found at {model_path}. Ensure it's included in the deployment.")
|
| 17 |
|
| 18 |
# Define a route for the home page (GET request)
|
| 19 |
@super_kart_api.get('/')
|
|
|
|
| 34 |
"""
|
| 35 |
# Get the JSON data from the request body
|
| 36 |
input_data = request.get_json()
|
| 37 |
+
|
| 38 |
# Extract relevant features from the JSON data
|
| 39 |
# Note: Exclude Product_Id and Store_Id if they are not used in prediction
|
| 40 |
sample = {
|
|
|
|
| 48 |
'Store_Location_City_Type': input_data['Store_Location_City_Type'],
|
| 49 |
'Store_Type': input_data['Store_Type']
|
| 50 |
}
|
|
|
|
| 51 |
# Convert the extracted data into a Pandas DataFrame
|
| 52 |
features_df = pd.DataFrame([sample])
|
| 53 |
+
|
| 54 |
# Apply one-hot encoding for nominal columns (matching training)
|
| 55 |
features_df = pd.get_dummies(features_df, columns=['Product_Type', 'Store_Type'], drop_first=True)
|
| 56 |
+
|
| 57 |
# Apply ordinal encoding (based on provided orders)
|
| 58 |
sugar_mapping = {'No Sugar': 0, 'Low Sugar': 1, 'Regular': 2}
|
| 59 |
size_mapping = {'Small': 0, 'Medium': 1, 'High': 2}
|
| 60 |
city_mapping = {'Tier 3': 0, 'Tier 2': 1, 'Tier 1': 2}
|
| 61 |
+
|
| 62 |
features_df['Product_Sugar_Content'] = features_df['Product_Sugar_Content'].map(sugar_mapping)
|
| 63 |
features_df['Store_Size'] = features_df['Store_Size'].map(size_mapping)
|
| 64 |
features_df['Store_Location_City_Type'] = features_df['Store_Location_City_Type'].map(city_mapping)
|
| 65 |
+
|
| 66 |
# Make prediction (assuming direct sales prediction; adjust if log-transformed)
|
| 67 |
predicted_sales = model.predict(features_df)[0]
|
| 68 |
+
|
| 69 |
# If your model predicts log(sales), uncomment and use this instead:
|
| 70 |
# predicted_log_sales = model.predict(features_df)[0]
|
| 71 |
# predicted_sales = np.exp(predicted_log_sales)
|
| 72 |
+
|
| 73 |
# Convert to Python float and round to 2 decimals
|
| 74 |
predicted_sales = round(float(predicted_sales), 2)
|
| 75 |
+
|
| 76 |
# Return the predicted sales total
|
| 77 |
return jsonify({'Predicted Sales Total (in dollars)': predicted_sales})
|
| 78 |
|
requirements.txt
CHANGED
|
@@ -7,4 +7,3 @@ Werkzeug==3.1.3
|
|
| 7 |
flask==3.1.1
|
| 8 |
gunicorn==20.1.0
|
| 9 |
requests==2.28.1
|
| 10 |
-
uvicorn[standard]
|
|
|
|
| 7 |
flask==3.1.1
|
| 8 |
gunicorn==20.1.0
|
| 9 |
requests==2.28.1
|
|
|