| import gradio as gr |
| import pandas as pd |
| from sklearn.ensemble import RandomForestClassifier |
| from sklearn.preprocessing import LabelEncoder |
|
|
| |
| df = pd.read_csv('cleaned_titanic_dataset.csv') |
|
|
| |
| le = LabelEncoder() |
| df['Sex'] = le.fit_transform(df['Sex']) |
|
|
| |
| features = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare'] |
| X = df[features] |
| y = df['Survived'] |
|
|
| |
| model = RandomForestClassifier(random_state=42) |
| model.fit(X, y) |
|
|
| |
| def predict_survival(pclass, sex, age, sibsp, parch, fare): |
| |
| sex_encoded = 1 if sex == "Male" else 0 |
| input_data = pd.DataFrame([[pclass, sex_encoded, age, sibsp, parch, fare]], columns=features) |
| |
| |
| prediction = model.predict(input_data)[0] |
| probability = model.predict_proba(input_data)[0][prediction] |
| |
| result = "Survived" if prediction == 1 else "Did Not Survive" |
| return f"Prediction: {result}\nConfidence: {probability:.2%}" |
|
|
| |
| interface = gr.Interface( |
| fn=predict_survival, |
| inputs=[ |
| gr.Radio(choices=[1, 2, 3], label="Passenger Class (1=1st, 2=2nd, 3=3rd)"), |
| gr.Radio(choices=["Male", "Female"], label="Sex"), |
| |
| gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Normalized Age (0 to 1)"), |
| gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Normalized SibSp (0 to 1)"), |
| gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Normalized Parch (0 to 1)"), |
| gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Normalized Fare (0 to 1)") |
| ], |
| outputs=gr.Text(label="Survival Prediction"), |
| title="Titanic Survival Predictor", |
| description="Enter normalized passenger details to predict if they would have survived the Titanic disaster." |
| ) |
|
|
| |
| if __name__ == "__main__": |
| interface.launch() |