Smartlizardpy commited on
Commit
e52bce6
·
verified ·
1 Parent(s): 399e7e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -3
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import pandas as pd
3
  import gradio as gr
4
  from sklearn.ensemble import RandomForestClassifier
@@ -32,6 +31,12 @@ model.fit(X_train, y_train)
32
 
33
  # Gradio interface function
34
  def predict_survival(Pclass, Gender, Age, SibSp, Parch, Fare, Embarked):
 
 
 
 
 
 
35
  # Encode Gender and Embarked
36
  Gender_encoded = 1 if Gender.lower() == 'female' else 0
37
  Embarked_encoded = {'s': 0, 'c': 1, 'q': 2}.get(Embarked.lower(), 0)
@@ -42,7 +47,7 @@ def predict_survival(Pclass, Gender, Age, SibSp, Parch, Fare, Embarked):
42
 
43
  # Predict
44
  prediction = model.predict(input_data)
45
- return "Survived" if prediction[0] == 1 else "Did Not Survive"
46
 
47
  # Gradio inputs and outputs
48
  inputs = [
@@ -55,7 +60,7 @@ inputs = [
55
  gr.Radio(["S (Southampton)", "C (Cherbourg)", "Q (Queenstown)"], label="Port of Embarkation (Embarked)")
56
  ]
57
 
58
- outputs = gr.Textbox(label="Prediction")
59
 
60
  # Launch Gradio interface
61
  gr.Interface(fn=predict_survival, inputs=inputs, outputs=outputs, title="Titanic Survival Predictor").launch()
 
 
1
  import pandas as pd
2
  import gradio as gr
3
  from sklearn.ensemble import RandomForestClassifier
 
31
 
32
  # Gradio interface function
33
  def predict_survival(Pclass, Gender, Age, SibSp, Parch, Fare, Embarked):
34
+ # Handle missing or invalid inputs
35
+ if not Gender:
36
+ return "⚠️ Error: Please select a Gender."
37
+ if not Embarked:
38
+ return "⚠️ Error: Please select a Port of Embarkation."
39
+
40
  # Encode Gender and Embarked
41
  Gender_encoded = 1 if Gender.lower() == 'female' else 0
42
  Embarked_encoded = {'s': 0, 'c': 1, 'q': 2}.get(Embarked.lower(), 0)
 
47
 
48
  # Predict
49
  prediction = model.predict(input_data)
50
+ return "Survived" if prediction[0] == 1 else "Did Not Survive"
51
 
52
  # Gradio inputs and outputs
53
  inputs = [
 
60
  gr.Radio(["S (Southampton)", "C (Cherbourg)", "Q (Queenstown)"], label="Port of Embarkation (Embarked)")
61
  ]
62
 
63
+ outputs = gr.Textbox(label="Prediction or Error Message")
64
 
65
  # Launch Gradio interface
66
  gr.Interface(fn=predict_survival, inputs=inputs, outputs=outputs, title="Titanic Survival Predictor").launch()