startup_locpred / app.py
SurajJha21's picture
Update app.py
e1a7020 verified
import streamlit as st
import pandas as pd
from fastai.tabular.all import load_learner
import random
import torch
import joblib
import numpy as np
# Set random seed for reproducibility
random.seed(42)
torch.manual_seed(42)
# Load the saved model and LabelEncoder
learn = load_learner('tabular_model1.pkl') # Replace with your model path
label_encoder = joblib.load('label_encoder.pkl') # Replace with your encoder path
# Function to make predictions
def predict_location(input_data):
input_df = pd.DataFrame([input_data])
pred_class, pred_idx, outputs = learn.predict(input_df.iloc[0])
# Apply softmax to get probabilities
probabilities = torch.nn.functional.softmax(torch.tensor(outputs), dim=0)
# Get the index of the maximum probability
pred_idx = np.argmax(probabilities.numpy())
# Convert the index to the corresponding location
location = label_encoder.inverse_transform([pred_idx])[0]
# Debugging: Output probabilities and index
print(f"Probabilities: {probabilities.numpy()}")
print(f"Predicted index: {pred_idx}")
return location
# Streamlit app
def main():
st.title('Location Prediction App')
# Example input fields (replace with your actual input fields)
product_name = st.text_input('Product Name')
day_of_purchase = st.selectbox('Day of Purchase', ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'])
product_price = st.number_input('Product Price (INR)')
avg_purchase_value = st.number_input('Average Purchase Value (INR)')
# Prepare input data for prediction
input_data = {
'Product Name': product_name,
'Day of Purchase': day_of_purchase,
'Product Price (INR)': product_price,
'Average Purchase Value (INR)': avg_purchase_value
}
# Predict button
if st.button('Predict Location'):
prediction = predict_location(input_data)
st.success(f'Predicted Location: {prediction}')
if __name__ == '__main__':
main()