Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -196,10 +196,17 @@ def predict_overrun(planned_cost, actual_spend, category, cement_index, labor_in
|
|
| 196 |
labor_risk = (labor_index - 100) / 100
|
| 197 |
|
| 198 |
phase_weight = {"Planning": 0.2, "Execution": 0.5, "Closure": 0.3}
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
total_risk = base_risk + (cement_risk + labor_risk) * (
|
| 202 |
-
phase_weight.get(project_phase, 0.3) + category_weight.get(category, 0.
|
| 203 |
)
|
| 204 |
total_risk = min(max(total_risk, 0), 1)
|
| 205 |
|
|
@@ -375,44 +382,51 @@ def predict_overrun(planned_cost, actual_spend, category, cement_index, labor_in
|
|
| 375 |
|
| 376 |
return readable_output, chart_image, pdf_path, csv_path
|
| 377 |
|
| 378 |
-
#
|
| 379 |
-
|
| 380 |
-
fn=predict_overrun,
|
| 381 |
-
inputs=[
|
| 382 |
-
gr.Textbox(label="Planned Cost (INR)"),
|
| 383 |
-
gr.Textbox(label="Actual Spend (INR)"),
|
| 384 |
-
gr.Dropdown(label="Budget Category", choices=[]), # Will be populated dynamically
|
| 385 |
-
gr.Textbox(label="Cement Index", info="Index (base = 100)"),
|
| 386 |
-
gr.Textbox(label="Labor Index", info="Index (base = 100)"),
|
| 387 |
-
gr.Dropdown(label="Project Phase", choices=[]) # Will be populated dynamically
|
| 388 |
-
],
|
| 389 |
-
outputs=[
|
| 390 |
-
gr.Markdown(label="Risk Summary"),
|
| 391 |
-
gr.Image(type="pil", label="Forecast Chart"),
|
| 392 |
-
gr.File(label="Download PDF"),
|
| 393 |
-
gr.File(label="Download CSV")
|
| 394 |
-
],
|
| 395 |
-
title="Budget Overrun Risk Estimator"
|
| 396 |
-
)
|
| 397 |
-
|
| 398 |
-
# Dynamically update dropdown choices with valid picklist values
|
| 399 |
-
def update_dropdowns():
|
| 400 |
sf = connect_to_salesforce()
|
| 401 |
if isinstance(sf, str):
|
| 402 |
logger.error("Failed to fetch picklist values: %s", sf)
|
| 403 |
logger.warning("Using fallback values for dropdowns due to Salesforce connection failure.")
|
| 404 |
-
|
| 405 |
-
return gr.Dropdown(choices=["Electrical", "Structural", "Civil"]), gr.Dropdown(choices=["Planning", "Execution", "Closure"])
|
| 406 |
|
| 407 |
_, _, picklist_values = fetch_salesforce_fields(sf)
|
| 408 |
-
category_choices = picklist_values.get('Category__c', ["Electrical", "
|
| 409 |
phase_choices = picklist_values.get('Project_Phase__c', ["Planning", "Execution", "Closure"])
|
| 410 |
-
logger.debug("
|
| 411 |
-
logger.debug("
|
| 412 |
-
return
|
| 413 |
|
| 414 |
-
#
|
| 415 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
|
| 417 |
if __name__ == "__main__":
|
| 418 |
-
|
|
|
|
| 196 |
labor_risk = (labor_index - 100) / 100
|
| 197 |
|
| 198 |
phase_weight = {"Planning": 0.2, "Execution": 0.5, "Closure": 0.3}
|
| 199 |
+
# Updated category weights to match Salesforce picklist values
|
| 200 |
+
category_weight = {
|
| 201 |
+
"Civil": 0.2,
|
| 202 |
+
"Electrical": 0.2,
|
| 203 |
+
"Plumbing": 0.2,
|
| 204 |
+
"Finishing": 0.2,
|
| 205 |
+
"Others": 0.2
|
| 206 |
+
}
|
| 207 |
|
| 208 |
total_risk = base_risk + (cement_risk + labor_risk) * (
|
| 209 |
+
phase_weight.get(project_phase, 0.3) + category_weight.get(category, 0.2)
|
| 210 |
)
|
| 211 |
total_risk = min(max(total_risk, 0), 1)
|
| 212 |
|
|
|
|
| 382 |
|
| 383 |
return readable_output, chart_image, pdf_path, csv_path
|
| 384 |
|
| 385 |
+
# Fetch initial dropdown choices from Salesforce
|
| 386 |
+
def get_initial_dropdown_choices():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
sf = connect_to_salesforce()
|
| 388 |
if isinstance(sf, str):
|
| 389 |
logger.error("Failed to fetch picklist values: %s", sf)
|
| 390 |
logger.warning("Using fallback values for dropdowns due to Salesforce connection failure.")
|
| 391 |
+
return ["Civil", "Electrical", "Plumbing", "Finishing", "Others"], ["Planning", "Execution", "Closure"]
|
|
|
|
| 392 |
|
| 393 |
_, _, picklist_values = fetch_salesforce_fields(sf)
|
| 394 |
+
category_choices = picklist_values.get('Category__c', ["Civil", "Electrical", "Plumbing", "Finishing", "Others"])
|
| 395 |
phase_choices = picklist_values.get('Project_Phase__c', ["Planning", "Execution", "Closure"])
|
| 396 |
+
logger.debug("Initial category choices: %s", category_choices)
|
| 397 |
+
logger.debug("Initial phase choices: %s", phase_choices)
|
| 398 |
+
return category_choices, phase_choices
|
| 399 |
|
| 400 |
+
# Get initial choices
|
| 401 |
+
initial_category_choices, initial_phase_choices = get_initial_dropdown_choices()
|
| 402 |
+
|
| 403 |
+
# Gradio Interface
|
| 404 |
+
with gr.Blocks() as app:
|
| 405 |
+
gr.Markdown("# Budget Overrun Risk Estimator")
|
| 406 |
+
|
| 407 |
+
# Inputs
|
| 408 |
+
planned_cost = gr.Textbox(label="Planned Cost (INR)")
|
| 409 |
+
actual_spend = gr.Textbox(label="Actual Spend (INR)")
|
| 410 |
+
category = gr.Dropdown(label="Budget Category", choices=initial_category_choices)
|
| 411 |
+
cement_index = gr.Textbox(label="Cement Index", info="Index (base = 100)")
|
| 412 |
+
labor_index = gr.Textbox(label="Labor Index", info="Index (base = 100)")
|
| 413 |
+
project_phase = gr.Dropdown(label="Project Phase", choices=initial_phase_choices)
|
| 414 |
+
|
| 415 |
+
# Outputs
|
| 416 |
+
risk_summary = gr.Markdown(label="Risk Summary")
|
| 417 |
+
forecast_chart = gr.Image(type="pil", label="Forecast Chart")
|
| 418 |
+
pdf_download = gr.File(label="Download PDF")
|
| 419 |
+
csv_download = gr.File(label="Download CSV")
|
| 420 |
+
|
| 421 |
+
# Submit Button
|
| 422 |
+
submit_btn = gr.Button("Submit")
|
| 423 |
+
|
| 424 |
+
# Handle submission
|
| 425 |
+
submit_btn.click(
|
| 426 |
+
fn=predict_overrun,
|
| 427 |
+
inputs=[planned_cost, actual_spend, category, cement_index, labor_index, project_phase],
|
| 428 |
+
outputs=[risk_summary, forecast_chart, pdf_download, csv_download]
|
| 429 |
+
)
|
| 430 |
|
| 431 |
if __name__ == "__main__":
|
| 432 |
+
app.launch()
|