Update app.py
Browse files
app.py
CHANGED
|
@@ -20,6 +20,9 @@ def generate_custom_model_points(
|
|
| 20 |
sa_min_val = float(sa_min_val)
|
| 21 |
sa_max_val = float(sa_max_val)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
| 23 |
# Issue Age
|
| 24 |
age_at_entry = rng.integers(low=age_min_val, high=age_max_val + 1, size=mp_count_val)
|
| 25 |
|
|
@@ -44,7 +47,7 @@ def generate_custom_model_points(
|
|
| 44 |
# Max duration is (term_in_months - 1). Min duration is 1.
|
| 45 |
max_duration_val = policy_term_col * 12 - 1
|
| 46 |
# Ensure max_duration_val is at least 1 to prevent issues with rng.random if term is very short
|
| 47 |
-
max_duration_val = np.maximum(1, max_duration_val)
|
| 48 |
duration_mth_col = (rng.random(size=mp_count_val) * max_duration_val).astype(int) + 1
|
| 49 |
# Ensure duration_mth does not exceed max_duration_val (especially if max_duration_val was 1)
|
| 50 |
duration_mth_col = np.minimum(duration_mth_col, max_duration_val)
|
|
@@ -58,8 +61,9 @@ def generate_custom_model_points(
|
|
| 58 |
else:
|
| 59 |
policy_count_col_val = rng.integers(low=1, high=101, size=mp_count_val) # Variable 1-100
|
| 60 |
|
| 61 |
-
# Create DataFrame
|
| 62 |
data_dict = {
|
|
|
|
| 63 |
"age_at_entry": age_at_entry,
|
| 64 |
"sex": sex_col,
|
| 65 |
"policy_term": policy_term_col,
|
|
@@ -67,9 +71,9 @@ def generate_custom_model_points(
|
|
| 67 |
"sum_assured": sum_assured_col,
|
| 68 |
"duration_mth": duration_mth_col
|
| 69 |
}
|
| 70 |
-
|
| 71 |
-
model_point_df = pd.DataFrame(data_dict
|
| 72 |
-
|
| 73 |
return model_point_df
|
| 74 |
|
| 75 |
# 2. Gradio App Definition
|
|
@@ -85,7 +89,7 @@ with gr.Blocks() as demo:
|
|
| 85 |
with gr.Row():
|
| 86 |
with gr.Column(scale=1):
|
| 87 |
gr.Markdown("### Generation Parameters")
|
| 88 |
-
|
| 89 |
mp_count_input = gr.Slider(
|
| 90 |
minimum=100, maximum=50000, value=10000, step=100,
|
| 91 |
label="Number of Model Points"
|
|
@@ -93,7 +97,7 @@ with gr.Blocks() as demo:
|
|
| 93 |
seed_input = gr.Number(
|
| 94 |
value=12345, precision=0, label="Random Seed"
|
| 95 |
)
|
| 96 |
-
|
| 97 |
gr.Markdown("#### Age Parameters")
|
| 98 |
age_min_input = gr.Slider(
|
| 99 |
minimum=18, maximum=40, value=20, step=1, label="Minimum Age at Entry"
|
|
@@ -101,7 +105,7 @@ with gr.Blocks() as demo:
|
|
| 101 |
age_max_input = gr.Slider(
|
| 102 |
minimum=41, maximum=80, value=59, step=1, label="Maximum Age at Entry"
|
| 103 |
)
|
| 104 |
-
|
| 105 |
gr.Markdown("#### Sum Assured Parameters ($)")
|
| 106 |
sum_assured_min_input = gr.Number(
|
| 107 |
value=10000, label="Minimum Sum Assured"
|
|
@@ -109,7 +113,7 @@ with gr.Blocks() as demo:
|
|
| 109 |
sum_assured_max_input = gr.Number(
|
| 110 |
value=1000000, label="Maximum Sum Assured"
|
| 111 |
)
|
| 112 |
-
|
| 113 |
gr.Markdown("#### Policy Options")
|
| 114 |
policy_terms_input = gr.CheckboxGroup(
|
| 115 |
choices=[5, 10, 15, 20, 25, 30], value=[10, 15, 20],
|
|
@@ -121,13 +125,13 @@ with gr.Blocks() as demo:
|
|
| 121 |
policy_count_fixed_input = gr.Checkbox(
|
| 122 |
value=True, label="Fixed Policy Count = 1 (Uncheck for variable count 1-100)"
|
| 123 |
)
|
| 124 |
-
|
| 125 |
generate_btn = gr.Button("Generate Model Points", variant="primary")
|
| 126 |
|
| 127 |
with gr.Column(scale=2):
|
| 128 |
model_points_display = gr.Dataframe(label="Generated Model Points")
|
| 129 |
download_excel_btn = gr.DownloadButton(
|
| 130 |
-
label="Download Excel",
|
| 131 |
value="model_points.xlsx", # Default filename
|
| 132 |
variant="secondary"
|
| 133 |
)
|
|
@@ -160,9 +164,9 @@ with gr.Blocks() as demo:
|
|
| 160 |
pd.DataFrame().to_excel(empty_excel_output, index=False)
|
| 161 |
empty_excel_output.seek(0)
|
| 162 |
return empty_excel_output
|
| 163 |
-
|
| 164 |
excel_output = io.BytesIO()
|
| 165 |
-
current_df_to_download.to_excel(excel_output, sheet_name='ModelPoints', engine='xlsxwriter', index=
|
| 166 |
excel_output.seek(0)
|
| 167 |
return excel_output
|
| 168 |
|
|
@@ -172,7 +176,7 @@ with gr.Blocks() as demo:
|
|
| 172 |
sum_assured_min_input, sum_assured_max_input, policy_terms_input,
|
| 173 |
include_sex_input, policy_count_fixed_input
|
| 174 |
]
|
| 175 |
-
|
| 176 |
generate_btn.click(
|
| 177 |
fn=handle_generate_button_click,
|
| 178 |
inputs=inputs_list,
|
|
@@ -184,16 +188,6 @@ with gr.Blocks() as demo:
|
|
| 184 |
inputs=[df_state],
|
| 185 |
outputs=[download_excel_btn]
|
| 186 |
)
|
| 187 |
-
|
| 188 |
-
# Optional: Load with default values on app start
|
| 189 |
-
# def initial_load():
|
| 190 |
-
# # Use default values from the input components
|
| 191 |
-
# # This would require getting their default values
|
| 192 |
-
# # For simplicity, we can call generate with fixed defaults or trigger a click
|
| 193 |
-
# # Or, just start with an empty table.
|
| 194 |
-
# gr.Info("App loaded. Configure parameters and click 'Generate'.")
|
| 195 |
-
# return None, None # Empty table and state
|
| 196 |
-
# demo.load(initial_load, outputs=[model_points_display, df_state])
|
| 197 |
|
| 198 |
|
| 199 |
if __name__ == "__main__":
|
|
|
|
| 20 |
sa_min_val = float(sa_min_val)
|
| 21 |
sa_max_val = float(sa_max_val)
|
| 22 |
|
| 23 |
+
# Policy ID
|
| 24 |
+
policy_id_col = np.arange(1, mp_count_val + 1)
|
| 25 |
+
|
| 26 |
# Issue Age
|
| 27 |
age_at_entry = rng.integers(low=age_min_val, high=age_max_val + 1, size=mp_count_val)
|
| 28 |
|
|
|
|
| 47 |
# Max duration is (term_in_months - 1). Min duration is 1.
|
| 48 |
max_duration_val = policy_term_col * 12 - 1
|
| 49 |
# Ensure max_duration_val is at least 1 to prevent issues with rng.random if term is very short
|
| 50 |
+
max_duration_val = np.maximum(1, max_duration_val)
|
| 51 |
duration_mth_col = (rng.random(size=mp_count_val) * max_duration_val).astype(int) + 1
|
| 52 |
# Ensure duration_mth does not exceed max_duration_val (especially if max_duration_val was 1)
|
| 53 |
duration_mth_col = np.minimum(duration_mth_col, max_duration_val)
|
|
|
|
| 61 |
else:
|
| 62 |
policy_count_col_val = rng.integers(low=1, high=101, size=mp_count_val) # Variable 1-100
|
| 63 |
|
| 64 |
+
# Create DataFrame with policy_id as the first column
|
| 65 |
data_dict = {
|
| 66 |
+
"policy_id": policy_id_col,
|
| 67 |
"age_at_entry": age_at_entry,
|
| 68 |
"sex": sex_col,
|
| 69 |
"policy_term": policy_term_col,
|
|
|
|
| 71 |
"sum_assured": sum_assured_col,
|
| 72 |
"duration_mth": duration_mth_col
|
| 73 |
}
|
| 74 |
+
|
| 75 |
+
model_point_df = pd.DataFrame(data_dict)
|
| 76 |
+
|
| 77 |
return model_point_df
|
| 78 |
|
| 79 |
# 2. Gradio App Definition
|
|
|
|
| 89 |
with gr.Row():
|
| 90 |
with gr.Column(scale=1):
|
| 91 |
gr.Markdown("### Generation Parameters")
|
| 92 |
+
|
| 93 |
mp_count_input = gr.Slider(
|
| 94 |
minimum=100, maximum=50000, value=10000, step=100,
|
| 95 |
label="Number of Model Points"
|
|
|
|
| 97 |
seed_input = gr.Number(
|
| 98 |
value=12345, precision=0, label="Random Seed"
|
| 99 |
)
|
| 100 |
+
|
| 101 |
gr.Markdown("#### Age Parameters")
|
| 102 |
age_min_input = gr.Slider(
|
| 103 |
minimum=18, maximum=40, value=20, step=1, label="Minimum Age at Entry"
|
|
|
|
| 105 |
age_max_input = gr.Slider(
|
| 106 |
minimum=41, maximum=80, value=59, step=1, label="Maximum Age at Entry"
|
| 107 |
)
|
| 108 |
+
|
| 109 |
gr.Markdown("#### Sum Assured Parameters ($)")
|
| 110 |
sum_assured_min_input = gr.Number(
|
| 111 |
value=10000, label="Minimum Sum Assured"
|
|
|
|
| 113 |
sum_assured_max_input = gr.Number(
|
| 114 |
value=1000000, label="Maximum Sum Assured"
|
| 115 |
)
|
| 116 |
+
|
| 117 |
gr.Markdown("#### Policy Options")
|
| 118 |
policy_terms_input = gr.CheckboxGroup(
|
| 119 |
choices=[5, 10, 15, 20, 25, 30], value=[10, 15, 20],
|
|
|
|
| 125 |
policy_count_fixed_input = gr.Checkbox(
|
| 126 |
value=True, label="Fixed Policy Count = 1 (Uncheck for variable count 1-100)"
|
| 127 |
)
|
| 128 |
+
|
| 129 |
generate_btn = gr.Button("Generate Model Points", variant="primary")
|
| 130 |
|
| 131 |
with gr.Column(scale=2):
|
| 132 |
model_points_display = gr.Dataframe(label="Generated Model Points")
|
| 133 |
download_excel_btn = gr.DownloadButton(
|
| 134 |
+
label="Download Excel",
|
| 135 |
value="model_points.xlsx", # Default filename
|
| 136 |
variant="secondary"
|
| 137 |
)
|
|
|
|
| 164 |
pd.DataFrame().to_excel(empty_excel_output, index=False)
|
| 165 |
empty_excel_output.seek(0)
|
| 166 |
return empty_excel_output
|
| 167 |
+
|
| 168 |
excel_output = io.BytesIO()
|
| 169 |
+
current_df_to_download.to_excel(excel_output, sheet_name='ModelPoints', engine='xlsxwriter', index=False)
|
| 170 |
excel_output.seek(0)
|
| 171 |
return excel_output
|
| 172 |
|
|
|
|
| 176 |
sum_assured_min_input, sum_assured_max_input, policy_terms_input,
|
| 177 |
include_sex_input, policy_count_fixed_input
|
| 178 |
]
|
| 179 |
+
|
| 180 |
generate_btn.click(
|
| 181 |
fn=handle_generate_button_click,
|
| 182 |
inputs=inputs_list,
|
|
|
|
| 188 |
inputs=[df_state],
|
| 189 |
outputs=[download_excel_btn]
|
| 190 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
|
| 193 |
if __name__ == "__main__":
|