KristofferSolberg commited on
Commit
e02ba83
·
verified ·
1 Parent(s): e0df518

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +49 -0
  2. le_fartoy.pkl +3 -0
  3. requirements.txt +5 -0
  4. xgb_model.pkl +3 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ import pickle
4
+ import numpy as np
5
+ import pandas as pd
6
+
7
+ # Load the saved model and LabelEncoder
8
+ with open('xgb_model.pkl', 'rb') as f:
9
+ model = pickle.load(f)
10
+
11
+ with open('le_fartoy.pkl', 'rb') as f:
12
+ le_fartoy = pickle.load(f)
13
+
14
+ # Define the prediction function
15
+ def predict_accidents(fartoy_gruppe, year):
16
+ try:
17
+ # Encode the selected vessel group
18
+ fartoy_encoded = le_fartoy.transform([fartoy_gruppe])[0]
19
+
20
+ # Features must be in the exact order: ['FartøyGruppe', 'Ulykke_Year']
21
+ features = np.array([[fartoy_encoded, year]])
22
+
23
+ # Get model prediction
24
+ prediction = model.predict(features)[0]
25
+
26
+ # Round and ensure non-negative
27
+ predicted_count = max(0, int(round(prediction)))
28
+ return f"Predicted Number of Accidents: {predicted_count}"
29
+ except Exception as e:
30
+ return f"Error during prediction: {str(e)}"
31
+
32
+ # Get the list of vessel groups for the dropdown
33
+ vessel_groups = sorted(list(le_fartoy.classes_))
34
+
35
+ # Build Gradio UI with custom inputs (Dropdown and Slider)
36
+ interface = gr.Interface(
37
+ fn=predict_accidents,
38
+ inputs=[
39
+ gr.Dropdown(choices=vessel_groups, label="FartøyGruppe (Vessel Group)", value=vessel_groups[0]),
40
+ gr.Slider(minimum=1981, maximum=2032, step=1, label="Ulykke_Year (Accident Year)", value=2024)
41
+ ],
42
+ outputs=gr.Textbox(label="Prediction Output"),
43
+ title="Maritime Accident Predictor (XGBoost)",
44
+ description="Select a vessel category and year to predict the annual aggregated number of accidents using the optimized XGBoost Regressor model.",
45
+ theme="soft"
46
+ )
47
+
48
+ if __name__ == '__main__':
49
+ interface.launch()
le_fartoy.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b3222315d74e25b836dd6fe78bfe9df1b860a99d5f8647ce3b6847881969d6fe
3
+ size 772
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ xgboost
2
+ scikit-learn
3
+ pandas
4
+ numpy
5
+ gradio
xgb_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2a3d52b75480fbd91dde74b517e408f6a3d086bb6628a490a54eaa785acb7974
3
+ size 1094908