Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- app.py +30 -60
- best_random_forest_model.joblib +1 -1
- requirements.txt +7 -1
app.py
CHANGED
|
@@ -1,68 +1,38 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
quarter = st.selectbox("Quarter", ["Q1", "Q2", "Q3", "Q4"])
|
| 13 |
-
year = st.number_input("Year", min_value=2000, max_value=2100, value=2025)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
st.error("Please enter Product ID and Store ID.")
|
| 18 |
-
else:
|
| 19 |
-
# Prepare JSON payload according to backend expected format
|
| 20 |
-
payload = {
|
| 21 |
-
"Product_ID": product_id,
|
| 22 |
-
"Store_ID": store_id,
|
| 23 |
-
"Quarter": quarter,
|
| 24 |
-
"Year": year
|
| 25 |
-
}
|
| 26 |
-
|
| 27 |
-
# Your backend API endpoint URL - adjust if different
|
| 28 |
-
API_URL = "https://huggingface.co/spaces/Fitjv/superkart_v0/api/v1/sales"
|
| 29 |
-
|
| 30 |
-
try:
|
| 31 |
-
response = requests.post(API_URL, json=payload)
|
| 32 |
-
response.raise_for_status()
|
| 33 |
-
data = response.json()
|
| 34 |
-
if 'Predicted Quarterly Sales (in INR)' in data:
|
| 35 |
-
st.success(f"Predicted Quarterly Sales: ₹{data['Predicted Quarterly Sales (in INR)']}")
|
| 36 |
-
else:
|
| 37 |
-
st.error("Unexpected response format from API.")
|
| 38 |
-
except Exception as e:
|
| 39 |
-
st.error(f"API request failed: {e}")
|
| 40 |
-
|
| 41 |
-
st.markdown("---")
|
| 42 |
-
st.markdown("### Batch Prediction (Upload CSV)")
|
| 43 |
-
|
| 44 |
-
uploaded_file = st.file_uploader("Upload CSV file with input data", type=["csv"])
|
| 45 |
-
|
| 46 |
-
if uploaded_file is not None:
|
| 47 |
try:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
files = {"file": uploaded_file.getvalue()}
|
| 55 |
-
# Note: For requests with file upload, send as 'files' parameter
|
| 56 |
-
files = {"file": (uploaded_file.name, uploaded_file, "text/csv")}
|
| 57 |
-
response = requests.post(API_BATCH_URL, files=files)
|
| 58 |
-
response.raise_for_status()
|
| 59 |
-
result = response.json()
|
| 60 |
-
if isinstance(result, list):
|
| 61 |
-
result_df = pd.DataFrame(result)
|
| 62 |
-
st.success("Batch predictions received:")
|
| 63 |
-
st.dataframe(result_df.head())
|
| 64 |
-
else:
|
| 65 |
-
st.error("Unexpected response format from batch API.")
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
except Exception as e:
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import joblib
|
| 3 |
import pandas as pd
|
| 4 |
+
from flask import Flask, request, jsonify
|
| 5 |
|
| 6 |
+
# Initialize the Flask app
|
| 7 |
+
superkart_api = Flask("SuperKart Sales Prediction API")
|
| 8 |
|
| 9 |
+
# Load the trained model
|
| 10 |
+
model = joblib.load("best_random_forest_model.joblib")
|
| 11 |
|
| 12 |
+
@superkart_api.route('/', methods=['GET'])
|
| 13 |
+
def home():
|
| 14 |
+
return "Welcome to the SuperKart Sales Prediction API!"
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
@superkart_api.route('/v1/sales', methods=['POST'])
|
| 17 |
+
def predict_sales():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
try:
|
| 19 |
+
input_data = request.get_json()
|
| 20 |
+
df = pd.DataFrame([input_data])
|
| 21 |
+
prediction = model.predict(df)[0]
|
| 22 |
+
return jsonify({'Predicted Quarterly Sales (in INR)': round(float(prediction), 2)})
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return jsonify({'error': str(e)}), 400
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
@superkart_api.route('/v1/salesbatch', methods=['POST'])
|
| 27 |
+
def predict_sales_batch():
|
| 28 |
+
try:
|
| 29 |
+
file = request.files['file']
|
| 30 |
+
input_df = pd.read_csv(file)
|
| 31 |
+
predictions = model.predict(input_df)
|
| 32 |
+
input_df['Predicted_Quarterly_Sales'] = [round(float(val), 2) for val in predictions]
|
| 33 |
+
return input_df.to_dict(orient='records')
|
| 34 |
except Exception as e:
|
| 35 |
+
return jsonify({'error': str(e)}), 400
|
| 36 |
+
|
| 37 |
+
if __name__ == '__main__':
|
| 38 |
+
superkart_api.run(debug=True)
|
best_random_forest_model.joblib
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
size 45516023
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cd81c274fa9e445e81160011b41edf0b732fe4f8b7147c12f3447610fdbd3ff3
|
| 3 |
size 45516023
|
requirements.txt
CHANGED
|
@@ -1,3 +1,9 @@
|
|
| 1 |
pandas==2.2.2
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
requests==2.28.1
|
|
|
|
| 1 |
pandas==2.2.2
|
| 2 |
+
numpy==2.0.2
|
| 3 |
+
scikit-learn==1.6.1
|
| 4 |
+
xgboost==2.1.4
|
| 5 |
+
joblib==1.4.2
|
| 6 |
+
Werkzeug==2.2.2
|
| 7 |
+
flask==2.2.2
|
| 8 |
+
gunicorn==20.1.0
|
| 9 |
requests==2.28.1
|