import gradio as gr
import pandas as pd
import joblib

random_forest = joblib.load('Models/forest_model.joblib')

def predict_survival(name, Pclass, Sex, Age, SibSp, PaCh, Fare):
    
    input_data = pd.DataFrame({
        'Pclass': [Pclass],
        'Sex': [Sex],
        'Age': [Age],
        'SibSp': [SibSp],
        'PaCh': [PaCh],
        'Fare': [Fare],
    })
    
    
    prediction = random_forest.predict(input_data)[0]
    probabilities = random_forest.predict_proba(input_data)[0]

    if prediction == 0:
        result = f'Wooo 😮 {name.title()}, you Died 😢'
    else:
        result = f'Cheers 🥂 {name.title()}, you Survived 😁'
    # gr.label expects a dict of {Class name: probability} to display the probabilities of each class
    probs = {'Died': float(probabilities[0]), 'Survived': float(probabilities[1])}
    return result, probs

# PILLAR @ THE INPUTS(ONE PER FUNCTION PARAMETER, SAME ORDER)
inputs = [
    gr.Textbox(label="Name", placeholder="e.g Olanrewaju Esther"),
    gr.Radio([1, 2, 3], label="Passenger Class(1 = First)", value=3),
    gr.Radio([("male", 0), ("female", 1)], label="Sex", value=0),
    gr.Slider(0, 80, value=30, step=1, label="Age"),
    gr.Number(label="Siblings/Spouses aboard", value=0, precision=0),
    gr.Number(label="Parents/Children aboard", value=0, precision=0),
    gr.Number(label="Fare Paid ($)", value=30.0)
]

# PILLAR 3 @ THE OUTPUTS(ONE PER FUNCTION RETURN VALUE, SAME ORDER)
outputs = [
    gr.Textbox(label="Survival Prediction"),
    gr.Label(label="Prediction Probabilities")
]

#Wire the three pillars together to create the interface
demo = gr.Interface(
    fn=predict_survival, 
    inputs=inputs, 
    outputs=outputs, 
    title="Titanic Survival Prediction", 
    description="Enter a passenger's details and see if our random forest thinks they survived the Titanic disaster or not. The model was trained on the Titanic dataset from Kaggle.",
    examples=[
        ["John Doe", 3, 0, 25, 0, 0, 8],
        ["Tomi Jesu", 1, 1, 17, 0, 1, 150],
    ],
)
#If __name__ == "__main__":
demo.launch()