Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -75,7 +75,7 @@ def generate_plot(planned_cost, actual_spend, forecast_cost):
|
|
| 75 |
return image_base64, gradio_image, buf_pdf
|
| 76 |
|
| 77 |
# Function to generate a PDF report locally
|
| 78 |
-
def generate_pdf(planned_cost, actual_spend, forecast_cost, total_risk, insights, status, top_causes, forecast_cost_inr, chart_image):
|
| 79 |
pdf_path = f"budget_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
|
| 80 |
c = canvas.Canvas(pdf_path, pagesize=letter)
|
| 81 |
width, height = letter
|
|
@@ -87,6 +87,8 @@ def generate_pdf(planned_cost, actual_spend, forecast_cost, total_risk, insights
|
|
| 87 |
# Prediction Metrics
|
| 88 |
c.setFont("Helvetica", 12)
|
| 89 |
y_position = height - 100
|
|
|
|
|
|
|
| 90 |
c.drawString(50, y_position, f"Planned Cost: ${planned_cost:,.2f}")
|
| 91 |
y_position -= 20
|
| 92 |
c.drawString(50, y_position, f"Actual Spend: ${actual_spend:,.2f}")
|
|
@@ -113,8 +115,9 @@ def generate_pdf(planned_cost, actual_spend, forecast_cost, total_risk, insights
|
|
| 113 |
return pdf_path
|
| 114 |
|
| 115 |
# Function to generate an Excel file with prediction results
|
| 116 |
-
def generate_excel(planned_cost, actual_spend, forecast_cost, total_risk, insights, status, top_causes, forecast_cost_inr):
|
| 117 |
data = {
|
|
|
|
| 118 |
"Planned Cost ($)": [planned_cost],
|
| 119 |
"Actual Spend ($)": [actual_spend],
|
| 120 |
"Forecasted Cost ($)": [forecast_cost],
|
|
@@ -216,12 +219,12 @@ def predict_risk(planned_cost, actual_spend, category, cement_index, labor_index
|
|
| 216 |
forecast_cost_inr = "N/A"
|
| 217 |
|
| 218 |
# Generate local PDF report
|
| 219 |
-
pdf_file = generate_pdf(planned_cost, actual_spend, forecast_cost, total_risk, insights, status, top_causes, forecast_cost_inr, chart_image_pdf)
|
| 220 |
|
| 221 |
# Generate Excel file
|
| 222 |
-
excel_file = generate_excel(planned_cost, actual_spend, forecast_cost, total_risk, insights, status, top_causes, forecast_cost_inr)
|
| 223 |
|
| 224 |
-
# Format the output for Gradio with enhanced summary
|
| 225 |
risk_level = "High" if total_risk == 1 else "Low"
|
| 226 |
output_text = (
|
| 227 |
f"Risk Summary\n"
|
|
@@ -230,6 +233,12 @@ def predict_risk(planned_cost, actual_spend, category, cement_index, labor_index
|
|
| 230 |
f"Risk Percentage: {risk_percentage}%\n"
|
| 231 |
f"Status: {status}\n"
|
| 232 |
f"Insights: {insights} due to {top_causes.lower()}.\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
f"Forecast Chart\n"
|
| 234 |
f"----------------------------------------\n"
|
| 235 |
f"[Chart displayed below]\n\n"
|
|
@@ -263,7 +272,7 @@ with gr.Blocks(title="Budget Overrun Risk Estimator") as demo:
|
|
| 263 |
submit_button = gr.Button("Submit", variant="primary")
|
| 264 |
|
| 265 |
with gr.Column():
|
| 266 |
-
output_text = gr.Textbox(label="Prediction Results")
|
| 267 |
output_chart = gr.Image(label="Forecast Chart")
|
| 268 |
output_pdf = gr.File(label="Download Local PDF Report")
|
| 269 |
output_excel = gr.File(label="Download Excel Report")
|
|
|
|
| 75 |
return image_base64, gradio_image, buf_pdf
|
| 76 |
|
| 77 |
# Function to generate a PDF report locally
|
| 78 |
+
def generate_pdf(planned_cost, actual_spend, forecast_cost, total_risk, insights, status, top_causes, forecast_cost_inr, category, chart_image):
|
| 79 |
pdf_path = f"budget_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
|
| 80 |
c = canvas.Canvas(pdf_path, pagesize=letter)
|
| 81 |
width, height = letter
|
|
|
|
| 87 |
# Prediction Metrics
|
| 88 |
c.setFont("Helvetica", 12)
|
| 89 |
y_position = height - 100
|
| 90 |
+
c.drawString(50, y_position, f"Category: {category}")
|
| 91 |
+
y_position -= 20
|
| 92 |
c.drawString(50, y_position, f"Planned Cost: ${planned_cost:,.2f}")
|
| 93 |
y_position -= 20
|
| 94 |
c.drawString(50, y_position, f"Actual Spend: ${actual_spend:,.2f}")
|
|
|
|
| 115 |
return pdf_path
|
| 116 |
|
| 117 |
# Function to generate an Excel file with prediction results
|
| 118 |
+
def generate_excel(planned_cost, actual_spend, forecast_cost, total_risk, insights, status, top_causes, forecast_cost_inr, category):
|
| 119 |
data = {
|
| 120 |
+
"Category": [category],
|
| 121 |
"Planned Cost ($)": [planned_cost],
|
| 122 |
"Actual Spend ($)": [actual_spend],
|
| 123 |
"Forecasted Cost ($)": [forecast_cost],
|
|
|
|
| 219 |
forecast_cost_inr = "N/A"
|
| 220 |
|
| 221 |
# Generate local PDF report
|
| 222 |
+
pdf_file = generate_pdf(planned_cost, actual_spend, forecast_cost, total_risk, insights, status, top_causes, forecast_cost_inr, category, chart_image_pdf)
|
| 223 |
|
| 224 |
# Generate Excel file
|
| 225 |
+
excel_file = generate_excel(planned_cost, actual_spend, forecast_cost, total_risk, insights, status, top_causes, forecast_cost_inr, category)
|
| 226 |
|
| 227 |
+
# Format the output for Gradio with enhanced summary, risk percentage, and category
|
| 228 |
risk_level = "High" if total_risk == 1 else "Low"
|
| 229 |
output_text = (
|
| 230 |
f"Risk Summary\n"
|
|
|
|
| 233 |
f"Risk Percentage: {risk_percentage}%\n"
|
| 234 |
f"Status: {status}\n"
|
| 235 |
f"Insights: {insights} due to {top_causes.lower()}.\n\n"
|
| 236 |
+
f"Project Details\n"
|
| 237 |
+
f"----------------------------------------\n"
|
| 238 |
+
f"Category: {category}\n"
|
| 239 |
+
f"Project Phase: {project_phase}\n"
|
| 240 |
+
f"Cement Index: {cement_index}\n"
|
| 241 |
+
f"Labor Index: {labor_index}\n\n"
|
| 242 |
f"Forecast Chart\n"
|
| 243 |
f"----------------------------------------\n"
|
| 244 |
f"[Chart displayed below]\n\n"
|
|
|
|
| 272 |
submit_button = gr.Button("Submit", variant="primary")
|
| 273 |
|
| 274 |
with gr.Column():
|
| 275 |
+
output_text = gr.Textbox(label="Prediction Results", lines=20, max_lines=30) # Increased lines to ensure full display
|
| 276 |
output_chart = gr.Image(label="Forecast Chart")
|
| 277 |
output_pdf = gr.File(label="Download Local PDF Report")
|
| 278 |
output_excel = gr.File(label="Download Excel Report")
|