Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- Dockerfile +13 -13
- app.py +226 -0
- requirements.txt +3 -3
Dockerfile
CHANGED
|
@@ -1,20 +1,20 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
|
|
|
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
curl \
|
| 8 |
-
git \
|
| 9 |
-
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
ENTRYPOINT ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]
|
|
|
|
| 1 |
+
# Use Python 3.9 slim image
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
|
| 4 |
+
# Set working directory
|
| 5 |
WORKDIR /app
|
| 6 |
|
| 7 |
+
# Copy requirements first for better caching
|
| 8 |
+
COPY requirements.txt .
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Install dependencies
|
| 11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
|
| 13 |
+
# Copy application files
|
| 14 |
+
COPY . .
|
| 15 |
|
| 16 |
+
# Expose port 7860 (HuggingFace Spaces default)
|
| 17 |
+
EXPOSE 7860
|
| 18 |
|
| 19 |
+
# Run Streamlit app
|
| 20 |
+
CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0", "--server.enableXsrfProtection=false"]
|
|
|
app.py
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import requests
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
# Configure page
|
| 8 |
+
st.set_page_config(
|
| 9 |
+
page_title="SuperKart Sales Forecasting",
|
| 10 |
+
page_icon="π",
|
| 11 |
+
layout="wide"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Backend API URL - Replace with your deployed backend URL
|
| 15 |
+
BACKEND_URL = "https://huggingface.co/spaces/deepakdm411/ShoppingCartBackEnd" # Replace with your actual backend URL
|
| 16 |
+
|
| 17 |
+
st.title("π SuperKart Sales Forecasting System")
|
| 18 |
+
st.write("Predict sales revenue for SuperKart products using our advanced ML model")
|
| 19 |
+
|
| 20 |
+
# API connection status
|
| 21 |
+
def check_backend_connection():
|
| 22 |
+
try:
|
| 23 |
+
response = requests.get(f"{BACKEND_URL}/", timeout=10)
|
| 24 |
+
return response.status_code == 200
|
| 25 |
+
except:
|
| 26 |
+
return False
|
| 27 |
+
|
| 28 |
+
# Check backend status
|
| 29 |
+
with st.spinner("Checking API connection..."):
|
| 30 |
+
backend_online = check_backend_connection()
|
| 31 |
+
|
| 32 |
+
if backend_online:
|
| 33 |
+
st.success("β
Connected to backend API")
|
| 34 |
+
else:
|
| 35 |
+
st.error("β Backend API not available. Please check the backend URL.")
|
| 36 |
+
st.info(f"Current backend URL: {BACKEND_URL}")
|
| 37 |
+
|
| 38 |
+
st.subheader("Enter Product and Store Details:")
|
| 39 |
+
|
| 40 |
+
# Create input form
|
| 41 |
+
with st.form("prediction_form"):
|
| 42 |
+
col1, col2 = st.columns(2)
|
| 43 |
+
|
| 44 |
+
with col1:
|
| 45 |
+
st.markdown("**πͺ Store Information**")
|
| 46 |
+
store_type = st.selectbox(
|
| 47 |
+
"Store Type",
|
| 48 |
+
["Supermarket Type1", "Supermarket Type2", "Supermarket Type3", "Grocery Store"]
|
| 49 |
+
)
|
| 50 |
+
store_size = st.selectbox("Store Size", ["Small", "Medium", "High"])
|
| 51 |
+
store_location = st.selectbox("Store Location City Type", ["Tier 1", "Tier 2", "Tier 3"])
|
| 52 |
+
store_year = st.number_input("Store Establishment Year", min_value=1980, max_value=2024, value=2010)
|
| 53 |
+
|
| 54 |
+
with col2:
|
| 55 |
+
st.markdown("**π¦ Product Information**")
|
| 56 |
+
product_type = st.selectbox(
|
| 57 |
+
"Product Type",
|
| 58 |
+
["Dairy", "Soft Drinks", "Meat", "Fruits and Vegetables", "Household",
|
| 59 |
+
"Baking Goods", "Snack Foods", "Frozen Foods", "Breakfast",
|
| 60 |
+
"Health and Hygiene", "Hard Drinks", "Canned", "Bread",
|
| 61 |
+
"Starchy Foods", "Others", "Seafood"]
|
| 62 |
+
)
|
| 63 |
+
product_sugar = st.selectbox("Product Sugar Content", ["Low Fat", "Regular"])
|
| 64 |
+
product_weight = st.number_input("Product Weight", min_value=0.01, value=1.0)
|
| 65 |
+
product_mrp = st.number_input("Product MRP", min_value=1.0, value=100.0)
|
| 66 |
+
product_area = st.number_input("Product Allocated Area", min_value=0.001, value=0.1)
|
| 67 |
+
|
| 68 |
+
# Submit button
|
| 69 |
+
submitted = st.form_submit_button("π― Predict Sales Revenue", type="primary")
|
| 70 |
+
|
| 71 |
+
# Handle form submission
|
| 72 |
+
if submitted and backend_online:
|
| 73 |
+
# Prepare data for API
|
| 74 |
+
prediction_data = {
|
| 75 |
+
"Product_Weight": product_weight,
|
| 76 |
+
"Product_Sugar_Content": product_sugar,
|
| 77 |
+
"Product_Allocated_Area": product_area,
|
| 78 |
+
"Product_Type": product_type,
|
| 79 |
+
"Product_MRP": product_mrp,
|
| 80 |
+
"Store_Establishment_Year": store_year,
|
| 81 |
+
"Store_Size": store_size,
|
| 82 |
+
"Store_Location_City_Type": store_location,
|
| 83 |
+
"Store_Type": store_type
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
# Make API call
|
| 87 |
+
with st.spinner("Making prediction..."):
|
| 88 |
+
try:
|
| 89 |
+
response = requests.post(
|
| 90 |
+
f"{BACKEND_URL}/predict",
|
| 91 |
+
json=prediction_data,
|
| 92 |
+
headers={"Content-Type": "application/json"},
|
| 93 |
+
timeout=30
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
if response.status_code == 200:
|
| 97 |
+
result = response.json()
|
| 98 |
+
|
| 99 |
+
# Display results
|
| 100 |
+
st.success("β
Prediction Complete!")
|
| 101 |
+
|
| 102 |
+
# Main prediction display
|
| 103 |
+
col1, col2, col3 = st.columns([1, 2, 1])
|
| 104 |
+
with col2:
|
| 105 |
+
st.metric(
|
| 106 |
+
label="Predicted Sales Revenue",
|
| 107 |
+
value=result["formatted_prediction"]
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
# Additional details
|
| 111 |
+
st.markdown("---")
|
| 112 |
+
st.markdown("### π Prediction Details")
|
| 113 |
+
|
| 114 |
+
detail_col1, detail_col2 = st.columns(2)
|
| 115 |
+
|
| 116 |
+
with detail_col1:
|
| 117 |
+
st.info(f"""
|
| 118 |
+
**Store Profile:**
|
| 119 |
+
- Type: {store_type}
|
| 120 |
+
- Size: {store_size}
|
| 121 |
+
- Location: {store_location}
|
| 122 |
+
- Established: {store_year}
|
| 123 |
+
""")
|
| 124 |
+
|
| 125 |
+
with detail_col2:
|
| 126 |
+
st.info(f"""
|
| 127 |
+
**Product Profile:**
|
| 128 |
+
- Category: {product_type}
|
| 129 |
+
- Weight: {product_weight} kg
|
| 130 |
+
- MRP: βΉ{product_mrp}
|
| 131 |
+
- Sugar Content: {product_sugar}
|
| 132 |
+
""")
|
| 133 |
+
|
| 134 |
+
# Business insights
|
| 135 |
+
prediction_value = result["prediction"]
|
| 136 |
+
if prediction_value > product_mrp * 10:
|
| 137 |
+
st.success("π Excellent Revenue Potential!")
|
| 138 |
+
elif prediction_value > product_mrp * 5:
|
| 139 |
+
st.info("π Good Revenue Potential")
|
| 140 |
+
else:
|
| 141 |
+
st.warning("π Moderate Revenue Potential")
|
| 142 |
+
|
| 143 |
+
else:
|
| 144 |
+
error_data = response.json()
|
| 145 |
+
st.error(f"Prediction failed: {error_data.get('error', 'Unknown error')}")
|
| 146 |
+
|
| 147 |
+
except requests.exceptions.Timeout:
|
| 148 |
+
st.error("β° Request timed out. Please try again.")
|
| 149 |
+
except requests.exceptions.ConnectionError:
|
| 150 |
+
st.error("π Connection error. Please check if backend is running.")
|
| 151 |
+
except Exception as e:
|
| 152 |
+
st.error(f"An error occurred: {str(e)}")
|
| 153 |
+
|
| 154 |
+
elif submitted and not backend_online:
|
| 155 |
+
st.error("Cannot make prediction - backend API is not available.")
|
| 156 |
+
|
| 157 |
+
# Sidebar with API information
|
| 158 |
+
st.sidebar.markdown("### π§ API Information")
|
| 159 |
+
if backend_online:
|
| 160 |
+
try:
|
| 161 |
+
model_info = requests.get(f"{BACKEND_URL}/model-info", timeout=10).json()
|
| 162 |
+
st.sidebar.success("β
API Online")
|
| 163 |
+
st.sidebar.json(model_info)
|
| 164 |
+
except:
|
| 165 |
+
st.sidebar.warning("β οΈ Could not fetch model info")
|
| 166 |
+
else:
|
| 167 |
+
st.sidebar.error("β API Offline")
|
| 168 |
+
|
| 169 |
+
st.sidebar.markdown(f"**Backend URL:** {BACKEND_URL}")
|
| 170 |
+
|
| 171 |
+
# Batch prediction section
|
| 172 |
+
st.markdown("---")
|
| 173 |
+
st.markdown("### π Batch Predictions")
|
| 174 |
+
|
| 175 |
+
with st.expander("Upload CSV for batch predictions"):
|
| 176 |
+
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
|
| 177 |
+
|
| 178 |
+
if uploaded_file is not None and backend_online:
|
| 179 |
+
try:
|
| 180 |
+
# Read the uploaded file
|
| 181 |
+
df = pd.read_csv(uploaded_file)
|
| 182 |
+
st.write("Preview of uploaded data:")
|
| 183 |
+
st.dataframe(df.head())
|
| 184 |
+
|
| 185 |
+
if st.button("Process Batch Predictions"):
|
| 186 |
+
# Convert dataframe to list of dictionaries
|
| 187 |
+
batch_data = {"predictions": df.to_dict('records')}
|
| 188 |
+
|
| 189 |
+
with st.spinner("Processing batch predictions..."):
|
| 190 |
+
response = requests.post(
|
| 191 |
+
f"{BACKEND_URL}/batch-predict",
|
| 192 |
+
json=batch_data,
|
| 193 |
+
timeout=60
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
if response.status_code == 200:
|
| 197 |
+
batch_results = response.json()
|
| 198 |
+
|
| 199 |
+
st.success(f"β
Processed {batch_results['successful_predictions']} predictions successfully!")
|
| 200 |
+
|
| 201 |
+
# Create results dataframe
|
| 202 |
+
if batch_results['predictions']:
|
| 203 |
+
results_df = pd.DataFrame(batch_results['predictions'])
|
| 204 |
+
st.dataframe(results_df)
|
| 205 |
+
|
| 206 |
+
# Download results
|
| 207 |
+
csv = results_df.to_csv(index=False)
|
| 208 |
+
st.download_button(
|
| 209 |
+
"π₯ Download Results",
|
| 210 |
+
csv,
|
| 211 |
+
"batch_predictions.csv",
|
| 212 |
+
"text/csv"
|
| 213 |
+
)
|
| 214 |
+
else:
|
| 215 |
+
st.error("Batch prediction failed")
|
| 216 |
+
|
| 217 |
+
except Exception as e:
|
| 218 |
+
st.error(f"Error processing file: {str(e)}")
|
| 219 |
+
|
| 220 |
+
# Footer
|
| 221 |
+
st.markdown("---")
|
| 222 |
+
st.markdown("""
|
| 223 |
+
<div style='text-align: center; color: #666;'>
|
| 224 |
+
<p>SuperKart Sales Forecasting System | Built with Streamlit & Flask</p>
|
| 225 |
+
</div>
|
| 226 |
+
""", unsafe_allow_html=True)
|
requirements.txt
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
pandas
|
| 3 |
-
|
|
|
|
| 1 |
+
streamlit==1.43.2
|
| 2 |
+
pandas==2.2.2
|
| 3 |
+
requests==2.32.3
|