Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- backend_files/app.py +79 -0
- backend_files/requirements.txt +16 -0
- frontend_files/Dockerfile +14 -0
- frontend_files/app.py +52 -0
- frontend_files/requirements.txt +2 -0
backend_files/app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import joblib
|
| 5 |
+
from flask import Flask, request, jsonify
|
| 6 |
+
from flask_cors import CORS
|
| 7 |
+
|
| 8 |
+
# Initialize Flask app
|
| 9 |
+
Extraa_Learn_api = Flask("Extraa_Learn_api")
|
| 10 |
+
CORS(Extraa_Learn_api)
|
| 11 |
+
|
| 12 |
+
# Load the trained model pipeline (preprocessing + model)
|
| 13 |
+
model = joblib.load("/content/drive/My Drive/Colab Notebooks/deployment_files/Extraa_Learn_model_v1_0.joblib")
|
| 14 |
+
|
| 15 |
+
# Health check route
|
| 16 |
+
@Extraa_Learn_api.get('/')
|
| 17 |
+
def home():
|
| 18 |
+
return "✅ Welcome to the Extraa_Learn_API"
|
| 19 |
+
|
| 20 |
+
# Prediction route
|
| 21 |
+
@Extraa_Learn_api.post('/v1/predict')
|
| 22 |
+
def predict_sales():
|
| 23 |
+
try:
|
| 24 |
+
# Parse JSON payload
|
| 25 |
+
data = request.get_json()
|
| 26 |
+
print("Raw incoming data:", data)
|
| 27 |
+
|
| 28 |
+
# Validate expected fields
|
| 29 |
+
required_fields = [
|
| 30 |
+
'age',
|
| 31 |
+
'current_occupation',
|
| 32 |
+
'first_interaction',
|
| 33 |
+
'profile_completed',
|
| 34 |
+
'website_visits',
|
| 35 |
+
'time_spent_on_website',
|
| 36 |
+
'page_views_per_visit',
|
| 37 |
+
'last_activity',
|
| 38 |
+
'print_media_type1',
|
| 39 |
+
'print_media_type2',
|
| 40 |
+
'digital_media',
|
| 41 |
+
'educational_channels',
|
| 42 |
+
'referral'
|
| 43 |
+
]
|
| 44 |
+
missing_fields = [f for f in required_fields if f not in data]
|
| 45 |
+
if missing_fields:
|
| 46 |
+
return jsonify({'error': f"Missing fields: {missing_fields}"}), 400
|
| 47 |
+
|
| 48 |
+
# Convert and transform input
|
| 49 |
+
sample = {
|
| 50 |
+
'age': float(data['age']),
|
| 51 |
+
'current_occupation': data['current_occupation'],
|
| 52 |
+
'first_interaction': data['first_interaction'],
|
| 53 |
+
'profile_completed': data['profile_completed'],
|
| 54 |
+
'website_visits': np.log1p(float(data['website_visits'])), # transform here
|
| 55 |
+
'time_spent_on_website': float(data['time_spent_on_website']),
|
| 56 |
+
'page_views_per_visit': data['page_views_per_visit'],
|
| 57 |
+
'last_activity': data['last_activity'],
|
| 58 |
+
'print_media_type1': data['print_media_type1'],
|
| 59 |
+
'print_media_type2': int(data['print_media_type2']),
|
| 60 |
+
'digital_media': data['digital_media'],
|
| 61 |
+
'educational_channels': data['educational_channels'],
|
| 62 |
+
'referral': data['referral']
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
input_df = pd.DataFrame([sample])
|
| 66 |
+
print("Transformed input for model:
|
| 67 |
+
", input_df)
|
| 68 |
+
|
| 69 |
+
# Make prediction
|
| 70 |
+
prediction = model.predict(input_df).tolist()[0]
|
| 71 |
+
return jsonify({'Status': prediction})
|
| 72 |
+
|
| 73 |
+
except Exception as e:
|
| 74 |
+
print("❌ Error during prediction:", str(e))
|
| 75 |
+
return jsonify({'error': f"Prediction failed: {str(e)}"}), 500
|
| 76 |
+
|
| 77 |
+
# Run the app (for local testing only)
|
| 78 |
+
if __name__ == '__main__':
|
| 79 |
+
Extraa_Learn_api.run(debug=True)
|
backend_files/requirements.txt
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Core libraries
|
| 2 |
+
pandas==2.2.2
|
| 3 |
+
numpy==2.0.2
|
| 4 |
+
scikit-learn==1.6.1
|
| 5 |
+
seaborn==0.13.2
|
| 6 |
+
joblib==1.4.2
|
| 7 |
+
xgboost==2.1.4
|
| 8 |
+
|
| 9 |
+
# Flask web server
|
| 10 |
+
flask==2.2.2
|
| 11 |
+
flask-cors==3.0.10
|
| 12 |
+
gunicorn==20.1.0
|
| 13 |
+
Werkzeug==2.2.2
|
| 14 |
+
|
| 15 |
+
# For API testing
|
| 16 |
+
requests==2.32.3
|
frontend_files/Dockerfile
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 7860 and make it accessible externally
|
| 14 |
+
CMD ["streamlit", "run", "app.py", "--server.port=7860", "--server.address=0.0.0.0", "--server.enableXsrfProtection=false"]
|
frontend_files/app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# Streamlit Web App for Extraa_Learn prediction
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import requests
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# App Title
|
| 8 |
+
st.title("Extraa_Learn_Prediction")
|
| 9 |
+
|
| 10 |
+
# Instructions
|
| 11 |
+
#st.markdown("🔍 Enter product and store attributes to forecast **monthly product sales revenue**.\n\n_All sales are reported in ($) USD._")
|
| 12 |
+
|
| 13 |
+
# User Inputs
|
| 14 |
+
Age = st.number_input("age)", min_value=0.0, value=100)
|
| 15 |
+
current_occupation = st.selectbox("current_occupation", ["Low Sugar", "Regular", "No Sugar"])
|
| 16 |
+
first_interaction = st.number_input("first_interaction (linear in.)", min_value=0.0, value=100.0)
|
| 17 |
+
profile_completed = st.number_input("profile_completed)", min_value=0.0, value=150.0)
|
| 18 |
+
website_visits = st.selectbox("website_visits", ["Small", "Medium", "High"])
|
| 19 |
+
time_spent_on_website = st.selectbox("time_spent_on_website", ["Tier 1", "Tier 2", "Tier 3"])
|
| 20 |
+
page_views_per_visit = st.selectbox("page_views_per_visit", ["Supermarket Type1", "Supermarket Type2", "Departmental Store", "Food Mart"])
|
| 21 |
+
#Store_Age_Years = st.slider("Store Age (years)", min_value=0, max_value=30, value=10)#
|
| 22 |
+
last_activity = st.number_input("last_activity)", min_value=0, value=10)
|
| 23 |
+
print_media_type1 = st.selectbox("print_media_type1", ["Perishables", "Non Perishables"])
|
| 24 |
+
print_media_type2 = st.selectbox("print_media_type2", ["Supermarket Type1", "Supermarket Type2", "Departmental Store", "Food Mart"])
|
| 25 |
+
#Store_Age_Years = st.slider("Store Age (years)", min_value=0, max_value=30, value=10)#
|
| 26 |
+
digital_media = st.number_input("digital_media)", min_value=0, value=10)
|
| 27 |
+
educational_channels = st.selectbox("educational_channels", ["Perishables", "Non Perishables"])
|
| 28 |
+
referral= st.selectbox("referral", ["Perishables", "Non Perishables"])
|
| 29 |
+
|
| 30 |
+
# Apply log1p transform (must match backend model training)
|
| 31 |
+
Product_Allocated_Area_Log = np.log1p(Product_Allocated_Area)
|
| 32 |
+
|
| 33 |
+
# Prepare JSON payload for the backend
|
| 34 |
+
product_data = {
|
| 35 |
+
"Age": str(Product_Weight),
|
| 36 |
+
"current_occupation ": Product_Sugar_Content,
|
| 37 |
+
|
| 38 |
+
# Trigger Prediction
|
| 39 |
+
if st.button("Predict", type='primary'):
|
| 40 |
+
try:
|
| 41 |
+
response = requests.post(
|
| 42 |
+
"https://HSSHETTY01-Extraa_Learns-backend2.hf.space/v1/predict",
|
| 43 |
+
json=product_data
|
| 44 |
+
)
|
| 45 |
+
if response.status_code == 200:
|
| 46 |
+
result = response.json()
|
| 47 |
+
predicted_sales = result["Status"]
|
| 48 |
+
st.success(f"📈 Ststus: **${status:,.2f} USD**")
|
| 49 |
+
else:
|
| 50 |
+
st.error("❌ API Error: Please verify input values or try again later.")
|
| 51 |
+
except Exception as e:
|
| 52 |
+
st.error(f"⚠️ Connection error: {e}")
|
frontend_files/requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
requests==2.32.3
|
| 2 |
+
streamlit==1.45.0
|