vamshf commited on
Commit
fece451
·
verified ·
1 Parent(s): 82dcee9

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +47 -46
app.py CHANGED
@@ -1,46 +1,47 @@
1
- import streamlit as st
2
-
3
- st.title("SuperKart Sales Forecast")
4
-
5
- st.write("Enter the product and store details to get a sales forecast.")
6
-
7
- # Add input fields for the features
8
- product_weight = st.number_input("Product Weight", min_value=0.0)
9
- product_sugar_content = st.selectbox("Product Sugar Content", ['Low Sugar', 'Regular', 'No Sugar'])
10
- Product_Allocated_Area = st.number_input("Product Weight", min_value=0.0016)
11
- Product_Type = st.selectbox("Product Sugar Content", ['Frozen Foods','Dairy','Canned','Baking Goods','Health and Hygiene','Snack Foods','Household','Breakfast'])
12
- Product_MRP = st.number_input("Product_MRP", min_value=5.0)
13
- Store_Id = st.selectbox("Product Sugar Content", ['OUT001', 'OUT002', 'OUT003','OUT004'])
14
- Store_Establishment_Year = st.selectbox["Store_Establishment_Year",'1987','1998','1999','2009']
15
- Store_Size = st.selectbox["Store_Size",'Small','Medium','High']
16
- Store_Location_City_Type = st.selectbox["Store_Location_City_Type",'Tier 1','Tier 2','Tier 3']
17
- Store_Type= st.selectbox["Store_Location_City_Type",'Supermarket Type1','Food Mart','Departmental Store','Supermarket Type2']
18
-
19
- # Button to trigger prediction
20
- if st.button("Get Forecast"):
21
- # # Prepare data for the API
22
- # # This should be a dictionary or list of dictionaries matching the expected input format of your Flask API
23
- input_data = {
24
- 'Product_Weight': product_weight,
25
- 'Product_Sugar_Content': product_sugar_content,
26
- 'Product_Allocated_Area' : Product_Allocated_Area,
27
- 'Product_Type' : Product_Type,
28
- 'Product_MRP' : Product_MRP,
29
- 'Store_Id' : Store_Id,
30
- 'Store_Establishment_Year' : Store_Establishment_Year,
31
- 'Store_Size' : Store_Size,
32
- 'Store_Location_City_Type' : Store_Location_City_Type,
33
- 'Store_Type' :Store_Type
34
- }
35
-
36
- # # Send data to the backend API
37
- # # Replace with the actual URL of your deployed Flask API
38
- backend_url = "https://vamshf/Docker-gl-project.hf.space/predict"
39
-
40
- try:
41
- response = requests.post(backend_url, json=[input_data]) # Send as a list if your API expects a list
42
- predictions = response.json()
43
-
44
- st.success(f"Predicted Sales: {predictions[0]:.2f}") # Assuming the API returns a list with one prediction
45
- except Exception as e:
46
- st.error(f"Error getting forecast: {e}")
 
 
1
+ from flask import Flask,jsonify,request
2
+
3
+ # Initialize Flask app
4
+ superKart_Sales_forecast = Flask("SuperKart Sales Forecast")
5
+
6
+ # Load the serialized model
7
+ try:
8
+ loaded_model = joblib.load('tuned_random_forest_model.pkl')
9
+ print("Model loaded successfully!")
10
+ except Exception as e:
11
+ print(f"Error loading model: {e}")
12
+ loaded_model = None # Set model to None if loading fails
13
+
14
+
15
+ @superKart_Sales_forecast.route('/predict', methods=['POST'])
16
+ def predict():
17
+ if loaded_model is None:
18
+ return jsonify({'error': 'Model not loaded'}), 500
19
+
20
+ try:
21
+ # Get data from the request
22
+ data = request.get_json(force=True)
23
+ # Convert the incoming data to a pandas DataFrame
24
+ # Assuming the incoming data is a list of dictionaries, where each dictionary is a row
25
+ # The columns should match the features used during training (excluding the target)
26
+ input_df = pd.DataFrame(data)
27
+
28
+ # Ensure the columns are in the same order as the training data
29
+ # You might need to store the order of columns from X_train during training
30
+ # For now, assuming input_df columns match X_train columns
31
+ # A more robust solution would involve saving and loading the column order
32
+ # For demonstration, let's assume the column order is consistent
33
+
34
+ # Make predictions
35
+ predictions = loaded_model.predict(input_df)
36
+
37
+ # Convert predictions to a list and return as JSON
38
+ return jsonify(predictions.tolist())
39
+
40
+ except Exception as e:
41
+ return jsonify({'error': str(e)}), 400
42
+
43
+ # To run the Flask superKart_Sales_forecast (for local testing)
44
+ #if __name__ == '__main__':
45
+ # # This will run the server locally on port 5000
46
+ # # In a production environment, you would use a production-ready server like Gunicorn or uWSGI
47
+ # superKart_Sales_forecast.run(debug=True, host='0.0.0.0', port=5000)