sevvaliclal commited on
Commit
b465ef2
·
verified ·
1 Parent(s): 2a45904

Update src/app.py

Browse files
Files changed (1) hide show
  1. src/app.py +43 -10
src/app.py CHANGED
@@ -1,22 +1,55 @@
1
  import streamlit as st
2
  import pandas as pd
 
3
  import joblib
4
 
 
 
 
5
  model = joblib.load("src/final_model.pkl")
6
- features = joblib.load("src/model_features.pkl")
7
 
8
- st.title("🚦 Accident Risk Prediction")
9
 
10
- input_data = {}
 
11
 
12
- for feature in features:
13
- input_data[feature] = st.number_input(feature, value=0.0)
 
 
14
 
15
- input_df = pd.DataFrame([input_data])
16
-
17
- # CRITICAL FIX
18
- input_df = input_df.reindex(columns=features, fill_value=0)
 
 
19
 
 
 
 
20
  if st.button("Predict"):
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  prediction = model.predict(input_df)[0]
22
- st.success(f"Predicted Accident Risk: {prediction:.4f}")
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import pandas as pd
3
+ import numpy as np
4
  import joblib
5
 
6
+ # -------------------------
7
+ # Load Model & Features
8
+ # -------------------------
9
  model = joblib.load("src/final_model.pkl")
10
+ model_features = joblib.load("src/model_features.pkl")
11
 
12
+ st.set_page_config(page_title="Prediction App", layout="centered")
13
 
14
+ st.title("🚀 ML Prediction App")
15
+ st.write("Enter the feature values below:")
16
 
17
+ # -------------------------
18
+ # Dynamic Input Fields
19
+ # -------------------------
20
+ user_input = {}
21
 
22
+ for feature in model_features:
23
+ user_input[feature] = st.number_input(
24
+ f"{feature}",
25
+ value=0.0,
26
+ format="%.4f"
27
+ )
28
 
29
+ # -------------------------
30
+ # Predict Button
31
+ # -------------------------
32
  if st.button("Predict"):
33
+
34
+ # Convert to DataFrame
35
+ input_df = pd.DataFrame([user_input])
36
+
37
+ # Ensure all expected columns exist
38
+ for col in model_features:
39
+ if col not in input_df.columns:
40
+ input_df[col] = 0
41
+
42
+ # Remove unexpected columns
43
+ input_df = input_df[model_features]
44
+
45
+ # Prediction
46
  prediction = model.predict(input_df)[0]
47
+
48
+ st.subheader("Prediction Result")
49
+
50
+ if hasattr(model, "predict_proba"):
51
+ probability = model.predict_proba(input_df)[0][1]
52
+ st.success(f"Prediction: {prediction}")
53
+ st.info(f"Probability: {probability:.4f}")
54
+ else:
55
+ st.success(f"Prediction: {prediction}")