Nahiyan14 commited on
Commit
f95702c
·
verified ·
1 Parent(s): 47bc3d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +123 -42
app.py CHANGED
@@ -1,85 +1,166 @@
1
  import streamlit as st
2
  import pandas as pd
3
  import joblib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  # Load the model
6
  @st.cache_resource
7
  def load_model():
8
- return joblib.load('best_model.pkl')
 
9
 
10
  model = load_model()
11
 
12
- # Streamlit app title and description
13
- st.title("Retention Probability Predictor")
14
- st.markdown("""
15
- Predict the probability of retention based on patient and treatment details.
16
- Provide the required inputs below to see the prediction.
17
- """)
18
 
19
- # Sidebar for inputs
20
- st.sidebar.header("Input Features")
 
21
 
22
  # Input fields for features
23
- st.sidebar.subheader("Episode Details")
24
  previous_bup_episodes = st.sidebar.number_input(
25
- "Number of Previous Buprenorphine Episodes:", min_value=0, value=20, step=1
26
  )
27
  previous_failed_bup_episodes = st.sidebar.number_input(
28
- "Number of Previous Failed Buprenorphine Episodes:", min_value=0, value=20, step=1
29
  )
30
 
31
- st.sidebar.subheader("Days Details")
32
  days_covered = st.sidebar.number_input(
33
- "Days Covered in Current Episode:", min_value=0, value=30, step=1
34
  )
35
  days_since_start = st.sidebar.number_input(
36
- "Days Since Start of the Episode:", min_value=1, value=90, step=1
37
  )
38
 
39
  st.sidebar.subheader("Other Features")
40
  prior_BUP_days_supply = st.sidebar.number_input(
41
- "Prior BUP Days Supply:", min_value=0, value=30, step=1
42
  )
43
  YearOfInitiation = st.sidebar.number_input(
44
- "Year of Initiation:", min_value=1900, max_value=2100, value=2023, step=1
45
  )
46
  Previous_Meth_Episodes = st.sidebar.radio(
47
- "Previous Methadone Episodes (Yes/No):",
48
- options=[0, 1], # Binary options
49
- format_func=lambda x: "Yes" if x == 1 else "No" # Display "Yes" or "No" instead of 0/1
50
  )
51
 
52
- # Calculate derived features
53
- if st.sidebar.button("Predict Retention Probability"):
54
- # Calculate Failed Buprenorphine Rate
55
  if previous_bup_episodes > 0:
56
  Failed_Bup_Rate = previous_failed_bup_episodes / previous_bup_episodes
57
  else:
58
- Failed_Bup_Rate = 0.0 # No prior episodes, so no failures
59
 
60
- # Calculate Proportion of Days Covered
61
  if days_since_start > 0:
62
  proportionofDaysCovered = days_covered / days_since_start
63
  else:
64
- proportionofDaysCovered = 0.0 # Avoid division by zero
65
 
66
- # Prepare input data for prediction
67
- input_data = pd.DataFrame({
68
- 'proportionofDaysCovered': [proportionofDaysCovered],
69
- 'Failed_Bup_Rate': [Failed_Bup_Rate],
70
- 'prior_BUP_days_supply': [prior_BUP_days_supply],
71
- 'YearOfInitiation': [YearOfInitiation],
72
- 'Previous_Meth_Episodes': [Previous_Meth_Episodes]
73
- })
 
 
74
 
75
  # Make prediction
76
- retention_probability = model.predict_proba(input_data)[:, 1][0]
 
77
 
78
- # Display the prediction
79
- st.subheader("Prediction Result")
80
- st.success(f"The predicted retention probability is: **{retention_probability:.2f}**")
 
81
 
82
- # Display calculated features
83
- st.subheader("Calculated Features")
84
- st.write(f"**Failed Buprenorphine Rate:** {Failed_Bup_Rate:.2%}")
85
- st.write(f"**Proportion of Days Covered:** {proportionofDaysCovered:.2%}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import pandas as pd
3
  import joblib
4
+ from PIL import Image
5
+
6
+ # Custom styling for the app
7
+ st.markdown(
8
+ """
9
+ <style>
10
+ .main {
11
+ background-color: #f4f4f9;
12
+ }
13
+ .stButton > button {
14
+ background-color: #4CAF50;
15
+ color: white;
16
+ border-radius: 8px;
17
+ padding: 10px 20px;
18
+ font-size: 16px;
19
+ }
20
+ .stButton > button:hover {
21
+ background-color: #45a049;
22
+ color: white;
23
+ }
24
+ .stSidebar {
25
+ background-color: #F7F7F7;
26
+ }
27
+ .header {
28
+ font-size: 36px;
29
+ font-weight: bold;
30
+ color: #4CAF50;
31
+ text-align: center;
32
+ }
33
+ .subheader {
34
+ font-size: 20px;
35
+ color: #333;
36
+ margin-top: 0;
37
+ }
38
+ .footer {
39
+ font-size: 14px;
40
+ color: #666;
41
+ text-align: center;
42
+ margin-top: 30px;
43
+ }
44
+ </style>
45
+ """,
46
+ unsafe_allow_html=True,
47
+ )
48
 
49
  # Load the model
50
  @st.cache_resource
51
  def load_model():
52
+ return joblib.load("best_model.pkl")
53
+
54
 
55
  model = load_model()
56
 
57
+ # Sidebar customization
58
+ with st.sidebar:
59
+ st.image("MAT_primary-logo@2x.jpg", use_container_width=True)
60
+ st.header("Retention Predictor")
61
+ st.write("Enter the required details below to predict retention probability.")
 
62
 
63
+ # Main app title
64
+ st.markdown('<div class="header">Medication Retention/Discontinuation Probability Predictor</div>', unsafe_allow_html=True)
65
+ #st.markdown('<p class="subheader">Analyze patient retention using advanced ML models.</p>', unsafe_allow_html=True)
66
 
67
  # Input fields for features
68
+ st.sidebar.subheader("Patient Episode Details")
69
  previous_bup_episodes = st.sidebar.number_input(
70
+ "Number of Previous Buprenorphine Episodes", min_value=0, value=20, step=1
71
  )
72
  previous_failed_bup_episodes = st.sidebar.number_input(
73
+ "Number of Previous Failed Buprenorphine Episodes", min_value=0, value=20, step=1
74
  )
75
 
76
+ st.sidebar.subheader("Duration Details")
77
  days_covered = st.sidebar.number_input(
78
+ "Days Covered in Current Episode", min_value=0, value=30, step=1
79
  )
80
  days_since_start = st.sidebar.number_input(
81
+ "Days Since Start of the Episode", min_value=1, value=90, step=1
82
  )
83
 
84
  st.sidebar.subheader("Other Features")
85
  prior_BUP_days_supply = st.sidebar.number_input(
86
+ "Prior BUP Days Supply", min_value=0, value=30, step=1
87
  )
88
  YearOfInitiation = st.sidebar.number_input(
89
+ "Year of Initiation", min_value=1900, max_value=2100, value=2023, step=1
90
  )
91
  Previous_Meth_Episodes = st.sidebar.radio(
92
+ "Previous Methadone Episodes (Yes/No)",
93
+ options=[0, 1],
94
+ format_func=lambda x: "Yes" if x == 1 else "No",
95
  )
96
 
97
+ # Calculate derived features and predict
98
+ if st.sidebar.button("🔍 Predict Retention Probability"):
99
+ # Calculate derived features
100
  if previous_bup_episodes > 0:
101
  Failed_Bup_Rate = previous_failed_bup_episodes / previous_bup_episodes
102
  else:
103
+ Failed_Bup_Rate = 0.0
104
 
 
105
  if days_since_start > 0:
106
  proportionofDaysCovered = days_covered / days_since_start
107
  else:
108
+ proportionofDaysCovered = 0.0
109
 
110
+ # Prepare input data
111
+ input_data = pd.DataFrame(
112
+ {
113
+ "proportionofDaysCovered": [proportionofDaysCovered],
114
+ "Failed_Bup_Rate": [Failed_Bup_Rate],
115
+ "prior_BUP_days_supply": [prior_BUP_days_supply],
116
+ "YearOfInitiation": [YearOfInitiation],
117
+ "Previous_Meth_Episodes": [Previous_Meth_Episodes],
118
+ }
119
+ )
120
 
121
  # Make prediction
122
+ proba_output = model.predict_proba(input_data)
123
+ print("Predict_proba output shape:", proba_output.shape)
124
 
125
+ # Handle both 1D and 2D outputs
126
+ retention_probability = (
127
+ proba_output[:, 1][0] if proba_output.ndim == 2 else proba_output[0]
128
+ )
129
 
130
+ # Display the prediction result in a styled card
131
+ st.markdown(
132
+ f"""
133
+ <div style="padding: 20px; border: 1px solid #4CAF50; border-radius: 8px; background-color: #FFFFFF;">
134
+ <h2 style="color: #4CAF50; text-align: center;">Prediction Result</h2>
135
+ <p style="font-size: 18px; color: #333; text-align: center;">
136
+ The predicted retention probability is:
137
+ </p>
138
+ <p style="font-size: 28px; font-weight: bold; color: #4CAF50; text-align: center;">
139
+ {retention_probability:.2f}
140
+ </p>
141
+ </div>
142
+ """,
143
+ unsafe_allow_html=True,
144
+ )
145
+
146
+ # Display calculated features in a visually appealing manner
147
+ st.markdown("### **Calculated Features**")
148
+ st.markdown(
149
+ f"""
150
+ <ul style="font-size: 18px; color: #333;">
151
+ <li><strong>Failed Buprenorphine Rate:</strong> {Failed_Bup_Rate:.2%}</li>
152
+ <li><strong>Proportion of Days Covered:</strong> {proportionofDaysCovered:.2%}</li>
153
+ </ul>
154
+ """,
155
+ unsafe_allow_html=True,
156
+ )
157
+
158
+ # Footer section
159
+ st.markdown(
160
+ """
161
+ <div style="text-align: center; font-size: 14px; color: #666; margin-top: 30px;">
162
+ Developed by <strong>Nahiyan Noor</strong>
163
+ </div>
164
+ """,
165
+ unsafe_allow_html=True,
166
+ )