File size: 2,005 Bytes
9cb85b5
 
 
 
 
 
 
 
 
 
 
 
 
78c048e
 
9cb85b5
 
 
 
 
 
 
 
 
 
 
 
 
 
e1a7020
 
 
 
 
9cb85b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
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
50
51
52
53
54
55
56
57
58
59
60
61
62
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()