Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- Dockerfile +9 -9
- app.py +78 -63
- requirements.txt +0 -8
Dockerfile
CHANGED
@@ -1,16 +1,16 @@
|
|
|
|
1 |
FROM python:3.9-slim
|
2 |
|
3 |
-
# Set the working directory inside the container
|
4 |
WORKDIR /app
|
5 |
|
6 |
-
# Copy all files from the current directory to the container's
|
7 |
COPY . .
|
8 |
|
9 |
-
# Install dependencies
|
10 |
-
RUN
|
11 |
|
12 |
-
# Define the command to
|
13 |
-
|
14 |
-
|
15 |
-
#
|
16 |
-
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:forecast_predictor_api"]
|
|
|
1 |
+
# Use a minimal base image with Python 3.9 installed
|
2 |
FROM python:3.9-slim
|
3 |
|
4 |
+
# Set the working directory inside the container to /app
|
5 |
WORKDIR /app
|
6 |
|
7 |
+
# Copy all files from the current directory on the host to the container's /app directory
|
8 |
COPY . .
|
9 |
|
10 |
+
# Install Python dependencies listed in requirements.txt
|
11 |
+
RUN pip3 install -r requirements.txt
|
12 |
|
13 |
+
# Define the command to run the Streamlit app on port 8501 and make it accessible externally
|
14 |
+
CMD ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0", "--server.enableXsrfProtection=false"]
|
15 |
+
|
16 |
+
# NOTE: Disable XSRF protection for easier external access in order to make batch predictions
|
|
app.py
CHANGED
@@ -1,70 +1,85 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import pandas as pd # For data manipulation
|
5 |
-
from flask import Flask, request, jsonify # For creating the Flask API
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
'Store_Location_City_Type_Tier 3': product_data['Store_Location_City_Type_Tier 3'],
|
50 |
-
'Store_Type_Food Mart': product_data['Store_Type_Food Mart'],
|
51 |
-
'Store_Type_Supermarket Type1': product_data['Store_Type_Supermarket Type1'],
|
52 |
-
'Store_Type_Supermarket Type2': product_data['Store_Type_Supermarket Type2']
|
53 |
-
}
|
54 |
-
# Convert the extracted data into a Pandas DataFrame
|
55 |
-
input_data = pd.DataFrame([sample])
|
56 |
|
57 |
-
# Make prediction (get log_price)
|
58 |
-
predicted_price = model.predict(input_data)[0]
|
59 |
|
60 |
-
|
61 |
-
predicted_price = round(float(predicted_price), 2)
|
62 |
-
# The conversion above is needed as we convert the model prediction (log price) to actual price 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 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import requests
|
|
|
|
|
4 |
|
5 |
+
# Set the title of the Streamlit app
|
6 |
+
st.title("Sales Prediction")
|
7 |
|
8 |
+
# Section for online prediction
|
9 |
+
st.subheader("Online Prediction")
|
10 |
|
11 |
+
# Collect user input for product and store features
|
12 |
+
Product_Weight = st.number_input("Product Weight", min_value=0.0, value=15.0)
|
13 |
+
Product_Allocated_Area = st.number_input("Product Allocated Area", min_value=0.0, value=200.0)
|
14 |
+
Product_MRP = st.number_input("Product MRP", min_value=0.0, value=100.0)
|
15 |
+
Store_Establishment_Year = st.number_input("Store Establishment Year", min_value=1900, max_value=2024, value=2000)
|
16 |
+
Product_Sugar_Content_No_Sugar = st.selectbox("Product Sugar Content No Sugar", [0, 1])
|
17 |
+
Product_Sugar_Content_Regular = st.selectbox("Product Sugar Content Regular", [0, 1])
|
18 |
+
Product_Sugar_Content_reg = st.selectbox("Product Sugar Content reg", [0, 1])
|
19 |
+
Product_Type_Breads = st.selectbox("Product Type Breads", [0, 1])
|
20 |
+
Product_Type_Breakfast = st.selectbox("Product Type Breakfast", [0, 1])
|
21 |
+
Product_Type_Canned = st.selectbox("Product Type Canned", [0, 1])
|
22 |
+
Product_Type_Dairy = st.selectbox("Product Type Dairy", [0, 1])
|
23 |
+
Product_Type_Frozen_Foods = st.selectbox("Product Type Frozen Foods", [0, 1])
|
24 |
+
Product_Type_Fruits_and_Vegetables = st.selectbox("Product Type Fruits and Vegetables", [0, 1])
|
25 |
+
Product_Type_Hard_Drinks = st.selectbox("Product Type Hard Drinks", [0, 1])
|
26 |
+
Product_Type_Health_and_Hygiene = st.selectbox("Product Type Health and Hygiene", [0, 1])
|
27 |
+
Product_Type_Household = st.selectbox("Product Type Household", [0, 1])
|
28 |
+
Product_Type_Meat = st.selectbox("Product Type Meat", [0, 1])
|
29 |
+
Product_Type_Others = st.selectbox("Product Type Others", [0, 1])
|
30 |
+
Product_Type_Seafood = st.selectbox("Product Type Seafood", [0, 1])
|
31 |
+
Product_Type_Snack_Foods = st.selectbox("Product Type Snack Foods", [0, 1])
|
32 |
+
Product_Type_Soft_Drinks = st.selectbox("Product Type Soft Drinks", [0, 1])
|
33 |
+
Product_Type_Starchy_Foods = st.selectbox("Product Type Starchy Foods", [0, 1])
|
34 |
+
Store_Size_Medium = st.selectbox("Store Size Medium", [0, 1])
|
35 |
+
Store_Size_Small = st.selectbox("Store Size Small", [0, 1])
|
36 |
+
Store_Location_City_Type_Tier_2 = st.selectbox("Store Location City Type Tier 2", [0, 1])
|
37 |
+
Store_Location_City_Type_Tier_3 = st.selectbox("Store Location City Type Tier 3", [0, 1])
|
38 |
+
Store_Type_Food_Mart = st.selectbox("Store Type Food Mart", [0, 1])
|
39 |
+
Store_Type_Supermarket_Type1 = st.selectbox("Store Type Supermarket Type1", [0, 1])
|
40 |
+
Store_Type_Supermarket_Type2 = st.selectbox("Store Type Supermarket Type2", [0, 1])
|
41 |
|
42 |
+
# Convert user input into a DataFrame
|
43 |
+
input_data = pd.DataFrame([{
|
44 |
+
'Product_Weight': Product_Weight,
|
45 |
+
'Product_Allocated_Area': Product_Allocated_Area,
|
46 |
+
'Product_MRP': Product_MRP,
|
47 |
+
'Store_Establishment_Year': Store_Establishment_Year,
|
48 |
+
'Product_Sugar_Content_No Sugar': Product_Sugar_Content_No_Sugar,
|
49 |
+
'Product_Sugar_Content_Regular': Product_Sugar_Content_Regular,
|
50 |
+
'Product_Sugar_Content_reg': Product_Sugar_Content_reg,
|
51 |
+
'Product_Type_Breads': Product_Type_Breads,
|
52 |
+
'Product_Type_Breakfast': Product_Type_Breakfast,
|
53 |
+
'Product_Type_Canned': Product_Type_Canned,
|
54 |
+
'Product_Type_Dairy': Product_Type_Dairy,
|
55 |
+
'Product_Type_Frozen Foods': Product_Type_Frozen_Foods,
|
56 |
+
'Product_Type_Fruits and Vegetables': Product_Type_Fruits_and_Vegetables,
|
57 |
+
'Product_Type_Hard Drinks': Product_Type_Hard_Drinks,
|
58 |
+
'Product_Type_Health and Hygiene': Product_Type_Health_and_Hygiene,
|
59 |
+
'Product_Type_Household': Product_Type_Household,
|
60 |
+
'Product_Type_Meat': Product_Type_Meat,
|
61 |
+
'Product_Type_Others': Product_Type_Others,
|
62 |
+
'Product_Type_Seafood': Product_Type_Seafood,
|
63 |
+
'Product_Type_Snack Foods': Product_Type_Snack_Foods,
|
64 |
+
'Product_Type_Soft Drinks': Product_Type_Soft_Drinks,
|
65 |
+
'Product_Type_Starchy Foods': Product_Type_Starchy_Foods,
|
66 |
+
'Store_Size_Medium': Store_Size_Medium,
|
67 |
+
'Store_Size_Small': Store_Size_Small,
|
68 |
+
'Store_Location_City_Type_Tier 2': Store_Location_City_Type_Tier_2,
|
69 |
+
'Store_Location_City_Type_Tier 3': Store_Location_City_Type_Tier_3,
|
70 |
+
'Store_Type_Food Mart': Store_Type_Food_Mart,
|
71 |
+
'Store_Type_Supermarket Type1': Store_Type_Supermarket_Type1,
|
72 |
+
'Store_Type_Supermarket Type2': Store_Type_Supermarket_Type2
|
73 |
+
}])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
|
|
|
|
75 |
|
76 |
+
https://huggingface.co/spaces/nlauchande/ForecastBackend/v1/predict
|
|
|
|
|
|
|
77 |
|
78 |
+
# Make prediction when the "Predict" button is clicked
|
79 |
+
if st.button("Predict"):
|
80 |
+
response = requests.post("https://nlauchande-nlauchande/ForecastBackend.hf.space/v1/predict", json=input_data.to_dict(orient='records')[0]) # Send data to Flask API
|
81 |
+
if response.status_code == 200:
|
82 |
+
prediction = response.json()['Predicted Sales']
|
83 |
+
st.success(f"Predicted Sales: {prediction}")
|
84 |
+
else:
|
85 |
+
st.error("Error making prediction.")
|
requirements.txt
CHANGED
@@ -1,11 +1,3 @@
|
|
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
|
10 |
-
uvicorn[standard]
|
11 |
streamlit==1.43.2
|
|
|
1 |
pandas==2.2.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
requests==2.28.1
|
|
|
3 |
streamlit==1.43.2
|