Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- Dockerfile +18 -0
- app.py +87 -0
- requirements.txt +2 -0
Dockerfile
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use Python 3.9 as base image
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
+
|
| 4 |
+
# Set working directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy requirements and install
|
| 8 |
+
COPY requirements.txt .
|
| 9 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 10 |
+
|
| 11 |
+
# Copy the Streamlit app
|
| 12 |
+
COPY app.py .
|
| 13 |
+
|
| 14 |
+
# Expose port 8501 (Streamlit's default)
|
| 15 |
+
EXPOSE 8501
|
| 16 |
+
|
| 17 |
+
# Run Streamlit
|
| 18 |
+
CMD ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]
|
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
# Page configuration
|
| 6 |
+
st.set_page_config(
|
| 7 |
+
page_title="SuperKart Sales Predictor",
|
| 8 |
+
page_icon="🛒",
|
| 9 |
+
layout="centered"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
# Debug: Print to logs
|
| 13 |
+
print("Streamlit app starting...")
|
| 14 |
+
|
| 15 |
+
# Title and description
|
| 16 |
+
st.title("🛒 SuperKart Sales Predictor")
|
| 17 |
+
st.markdown("Predict product sales using your tuned Random Forest model. Enter details below!")
|
| 18 |
+
|
| 19 |
+
# Input fields matching SuperKart dataset
|
| 20 |
+
col1, col2 = st.columns(2)
|
| 21 |
+
|
| 22 |
+
with col1:
|
| 23 |
+
st.subheader("Product Information")
|
| 24 |
+
product_weight = st.number_input("Product Weight", min_value=0.0, max_value=50.0, value=12.0, step=0.1)
|
| 25 |
+
product_mrp = st.number_input("Product MRP ($)", min_value=0.0, max_value=10000.0, value=150.0, step=0.01)
|
| 26 |
+
product_sugar = st.selectbox("Product Sugar Content", ['Low Fat', 'Regular', 'Low Sugar', 'LF'])
|
| 27 |
+
product_type = st.selectbox("Product Type",
|
| 28 |
+
['Dairy', 'Soft Drinks', 'Meat', 'Fruits and Vegetables', 'Household',
|
| 29 |
+
'Baking Goods', 'Snack Foods', 'Frozen Foods', 'Breakfast',
|
| 30 |
+
'Health and Hygiene', 'Hard Drinks', 'Canned', 'Breads',
|
| 31 |
+
'Starchy Foods', 'Others'])
|
| 32 |
+
|
| 33 |
+
with col2:
|
| 34 |
+
st.subheader("Store Information")
|
| 35 |
+
store_size = st.selectbox("Store Size", ['Small', 'Medium', 'High'])
|
| 36 |
+
store_location = st.selectbox("Store Location Type", ['Tier 1', 'Tier 2', 'Tier 3'])
|
| 37 |
+
store_type = st.selectbox("Store Type",
|
| 38 |
+
['Grocery Store', 'Supermarket Type1', 'Supermarket Type2', 'Supermarket Type3'])
|
| 39 |
+
|
| 40 |
+
# Prediction button
|
| 41 |
+
if st.button("Predict Sales"):
|
| 42 |
+
# Prepare data for your backend API
|
| 43 |
+
data = {
|
| 44 |
+
"Product_Weight": product_weight,
|
| 45 |
+
"Product_MRP": product_mrp,
|
| 46 |
+
"Product_Sugar_Content": product_sugar,
|
| 47 |
+
"Product_Type": product_type,
|
| 48 |
+
"Store_Size": store_size,
|
| 49 |
+
"Store_Location_City_Type": store_location,
|
| 50 |
+
"Store_Type": store_type
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
# Debug: Print data being sent
|
| 54 |
+
print(f"Sending data: {data}")
|
| 55 |
+
|
| 56 |
+
# Call your deployed backend API
|
| 57 |
+
# REPLACE YOUR_USERNAME with your actual Hugging Face username
|
| 58 |
+
api_url = "https://toddmattingly-superkart-backend.hf.space/predict"
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
response = requests.post(api_url, json=data, timeout=10)
|
| 62 |
+
print(f"API response status: {response.status_code}")
|
| 63 |
+
|
| 64 |
+
if response.status_code == 200:
|
| 65 |
+
# API returns a list directly (based on your testing)
|
| 66 |
+
predictions = response.json()
|
| 67 |
+
prediction = predictions[0] if isinstance(predictions, list) and len(predictions) > 0 else 0
|
| 68 |
+
|
| 69 |
+
st.success(f"🎯 Predicted Sales Total: ${prediction:,.2f}")
|
| 70 |
+
st.info(f"📊 Based on: {product_type} at ${product_mrp:,.2f} MRP in a {store_type}")
|
| 71 |
+
else:
|
| 72 |
+
st.error(f"API Error: {response.status_code} - {response.text}")
|
| 73 |
+
print(f"API Error: {response.status_code} - {response.text}")
|
| 74 |
+
|
| 75 |
+
except requests.exceptions.RequestException as e:
|
| 76 |
+
st.error(f"Connection Error: {str(e)}")
|
| 77 |
+
print(f"Connection Error: {str(e)}")
|
| 78 |
+
except Exception as e:
|
| 79 |
+
st.error(f"Unexpected Error: {str(e)}")
|
| 80 |
+
print(f"Unexpected Error: {str(e)}")
|
| 81 |
+
|
| 82 |
+
# Footer
|
| 83 |
+
st.markdown("---")
|
| 84 |
+
st.markdown("*Powered by Streamlit & Hugging Face Spaces*")
|
| 85 |
+
st.markdown("*Using your tuned Random Forest model*")
|
| 86 |
+
|
| 87 |
+
print("Streamlit app loaded successfully.")
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
requests
|