Update app.py
Browse files
app.py
CHANGED
|
@@ -140,12 +140,10 @@ def create_salesforce_record(score, checklist_summary, missing_summary, status,
|
|
| 140 |
qa_report = str(qa_report) if qa_report else ""
|
| 141 |
punch_list_text = str(punch_list_text) if punch_list_text else ""
|
| 142 |
missing_documents = len(missing_summary.split(", ")) if missing_summary and missing_summary != "None" else 0
|
| 143 |
-
open_punch_items = int(open_punch_items) if open_punch_items is not None else 0
|
| 144 |
evaluated_at = datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ") # Salesforce Date/Time format
|
| 145 |
-
# Explicitly convert escalated to lowercase boolean string for Salesforce
|
| 146 |
alert_sent = str(bool(escalated)).lower() # Converts True/False to "true"/"false"
|
| 147 |
logging.info(f"Setting Alert_Sent__c to: {alert_sent}")
|
| 148 |
-
# Explicitly convert escalated to lowercase boolean string for Escalation_Flag__c
|
| 149 |
escalation_flag = str(bool(escalated)).lower() # Ensure this is also a proper boolean string
|
| 150 |
|
| 151 |
# Create the record in Project_Closure_Handover__c without the PDF URL for now
|
|
@@ -280,9 +278,9 @@ def evaluate_readiness(logs, qa_report, punch_list_text):
|
|
| 280 |
logging.info(f"Punch List Check: {'Pass' if punch_pass else 'Fail'}, Final Score: {score}%")
|
| 281 |
|
| 282 |
# Calculate open punch items
|
| 283 |
-
open_punch_items = 0 if punch_pass else
|
| 284 |
|
| 285 |
-
# Enhanced escalation logic
|
| 286 |
escalated = score < 70 or open_punch_items > 2
|
| 287 |
logging.info(f"Escalation Check: Score < 70 ({score < 70}), Open Punch Items > 2 ({open_punch_items > 2}), Escalated: {escalated}")
|
| 288 |
|
|
@@ -294,7 +292,7 @@ def evaluate_readiness(logs, qa_report, punch_list_text):
|
|
| 294 |
checklist_summary = "\n".join(checklist_details)
|
| 295 |
missing_summary = "None" if not missing_items else ", ".join(missing_items)
|
| 296 |
|
| 297 |
-
# Generate progress bar HTML
|
| 298 |
color_class = "red" if score < 70 else "yellow" if score <= 90 else "green"
|
| 299 |
logging.info(f"Readiness Score: {score}%, Color Class: {color_class}")
|
| 300 |
progress_bar = f"""
|
|
@@ -305,22 +303,21 @@ def evaluate_readiness(logs, qa_report, punch_list_text):
|
|
| 305 |
</div>
|
| 306 |
"""
|
| 307 |
|
| 308 |
-
# Return necessary values for PDF generation and Salesforce record
|
| 309 |
return (score, checklist_summary, missing_summary, status, progress_bar, escalated, logs, qa_report, punch_list_text, open_punch_items, checklist_status)
|
| 310 |
except Exception as e:
|
| 311 |
logging.error(f"Error in evaluate_readiness: {str(e)}")
|
| 312 |
raise
|
| 313 |
|
| 314 |
-
# Generate PDF report with signature slots
|
| 315 |
def generate_pdf(score, checklist_summary, missing_summary, checklist_status, logs, qa_report, punch_list_text):
|
| 316 |
try:
|
| 317 |
-
# Sanitize inputs
|
| 318 |
logging.info("Sanitizing inputs for PDF generation")
|
| 319 |
score = str(float(score)) if score is not None else "0"
|
| 320 |
checklist_summary = str(checklist_summary) if checklist_summary is not None else ""
|
| 321 |
checklist_status = str(checklist_status) if checklist_status is not None else ""
|
| 322 |
|
| 323 |
-
# Remove non-ASCII characters
|
| 324 |
checklist_summary = checklist_summary.encode('ascii', 'ignore').decode('ascii')
|
| 325 |
checklist_status = checklist_status.encode('ascii', 'ignore').decode('ascii')
|
| 326 |
|
|
@@ -328,51 +325,33 @@ def generate_pdf(score, checklist_summary, missing_summary, checklist_status, lo
|
|
| 328 |
pdf_path = "readiness_report.pdf"
|
| 329 |
logging.info(f"Creating PDF at {pdf_path}")
|
| 330 |
|
| 331 |
-
# Create the PDF
|
| 332 |
c = canvas.Canvas(pdf_path, pagesize=letter)
|
| 333 |
-
|
| 334 |
-
# Set initial font and draw title using Times-Bold
|
| 335 |
-
logging.info("Setting font to Times-Bold")
|
| 336 |
c.setFont("Times-Bold", 16)
|
| 337 |
c.drawString(50, 750, "Project Closure Readiness Report")
|
| 338 |
-
|
| 339 |
-
# Draw basic fields using Times-Roman
|
| 340 |
-
logging.info("Drawing basic fields")
|
| 341 |
c.setFont("Times-Roman", 12)
|
| 342 |
c.drawString(50, 720, f"Readiness Score: {score}%")
|
| 343 |
c.drawString(50, 700, f"Status: {checklist_status}")
|
| 344 |
c.drawString(50, 680, "Checklist Summary:")
|
| 345 |
-
|
| 346 |
-
# Draw checklist summary with Y-position overflow handling
|
| 347 |
-
logging.info("Drawing checklist summary")
|
| 348 |
y = 660
|
| 349 |
-
BOTTOM_MARGIN = 50
|
| 350 |
for line in checklist_summary.split("\n"):
|
| 351 |
if y < BOTTOM_MARGIN:
|
| 352 |
-
|
| 353 |
-
c.
|
| 354 |
-
|
| 355 |
-
y = 750 # Reset Y position
|
| 356 |
c.drawString(50, y, line)
|
| 357 |
y -= 20
|
| 358 |
-
|
| 359 |
-
# Draw signature slots
|
| 360 |
if y - 40 < BOTTOM_MARGIN:
|
| 361 |
-
logging.info("Y position below margin, creating new page")
|
| 362 |
c.showPage()
|
| 363 |
c.setFont("Times-Roman", 12)
|
| 364 |
y = 750
|
| 365 |
c.drawString(50, y - 40, "Stakeholder Signature: ____________________")
|
| 366 |
-
|
| 367 |
if y - 60 < BOTTOM_MARGIN:
|
| 368 |
-
logging.info("Y position below margin, creating new page")
|
| 369 |
c.showPage()
|
| 370 |
c.setFont("Times-Roman", 12)
|
| 371 |
y = 750
|
| 372 |
c.drawString(50, y - 60, "Date: ____________________")
|
| 373 |
-
|
| 374 |
-
# Finalize the PDF
|
| 375 |
-
logging.info("Finalizing PDF")
|
| 376 |
c.save()
|
| 377 |
|
| 378 |
# Confirm the file exists
|
|
@@ -403,8 +382,18 @@ with gr.Blocks(css="""
|
|
| 403 |
with gr.Row():
|
| 404 |
with gr.Column(scale=2):
|
| 405 |
logs_input = gr.Textbox(label="Project Logs", lines=5, placeholder="Enter project logs (e.g., 'Project complete, handover done')")
|
| 406 |
-
qa_input = gr.
|
| 407 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 408 |
submit_btn = gr.Button("Evaluate and Generate PDF")
|
| 409 |
with gr.Column(scale=3):
|
| 410 |
score_output = gr.Number(label="Readiness Score (%)")
|
|
@@ -415,10 +404,9 @@ with gr.Blocks(css="""
|
|
| 415 |
gr.Markdown("### Handover Summary")
|
| 416 |
checklist_output = gr.Textbox(label="Checklist Summary")
|
| 417 |
missing_output = gr.Textbox(label="Missing Items")
|
| 418 |
-
# Add an output for Open Punch Items for debugging
|
| 419 |
open_punch_items_output = gr.Number(label="Open Punch Items (Debug)")
|
| 420 |
-
pdf_output = gr.File(label="Download PDF Report", type="filepath", interactive=False)
|
| 421 |
-
pdf_debug = gr.Textbox(label="PDF Debug Output")
|
| 422 |
|
| 423 |
# Chain the evaluation, PDF generation, and Salesforce record creation
|
| 424 |
submit_btn.click(
|
|
@@ -438,6 +426,6 @@ with gr.Blocks(css="""
|
|
| 438 |
score_output, checklist_output, missing_output, status_output,
|
| 439 |
gr.State(), gr.State(), gr.State(), gr.State(), open_punch_items_output, pdf_output
|
| 440 |
],
|
| 441 |
-
outputs=None
|
| 442 |
)
|
| 443 |
demo.launch()
|
|
|
|
| 140 |
qa_report = str(qa_report) if qa_report else ""
|
| 141 |
punch_list_text = str(punch_list_text) if punch_list_text else ""
|
| 142 |
missing_documents = len(missing_summary.split(", ")) if missing_summary and missing_summary != "None" else 0
|
| 143 |
+
open_punch_items = int(open_punch_items) if open_punch_items is not None else 0
|
| 144 |
evaluated_at = datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ") # Salesforce Date/Time format
|
|
|
|
| 145 |
alert_sent = str(bool(escalated)).lower() # Converts True/False to "true"/"false"
|
| 146 |
logging.info(f"Setting Alert_Sent__c to: {alert_sent}")
|
|
|
|
| 147 |
escalation_flag = str(bool(escalated)).lower() # Ensure this is also a proper boolean string
|
| 148 |
|
| 149 |
# Create the record in Project_Closure_Handover__c without the PDF URL for now
|
|
|
|
| 278 |
logging.info(f"Punch List Check: {'Pass' if punch_pass else 'Fail'}, Final Score: {score}%")
|
| 279 |
|
| 280 |
# Calculate open punch items
|
| 281 |
+
open_punch_items = 0 if punch_pass else 1 # Simplified for dropdown input
|
| 282 |
|
| 283 |
+
# Enhanced escalation logic
|
| 284 |
escalated = score < 70 or open_punch_items > 2
|
| 285 |
logging.info(f"Escalation Check: Score < 70 ({score < 70}), Open Punch Items > 2 ({open_punch_items > 2}), Escalated: {escalated}")
|
| 286 |
|
|
|
|
| 292 |
checklist_summary = "\n".join(checklist_details)
|
| 293 |
missing_summary = "None" if not missing_items else ", ".join(missing_items)
|
| 294 |
|
| 295 |
+
# Generate progress bar HTML
|
| 296 |
color_class = "red" if score < 70 else "yellow" if score <= 90 else "green"
|
| 297 |
logging.info(f"Readiness Score: {score}%, Color Class: {color_class}")
|
| 298 |
progress_bar = f"""
|
|
|
|
| 303 |
</div>
|
| 304 |
"""
|
| 305 |
|
|
|
|
| 306 |
return (score, checklist_summary, missing_summary, status, progress_bar, escalated, logs, qa_report, punch_list_text, open_punch_items, checklist_status)
|
| 307 |
except Exception as e:
|
| 308 |
logging.error(f"Error in evaluate_readiness: {str(e)}")
|
| 309 |
raise
|
| 310 |
|
| 311 |
+
# Generate PDF report with signature slots
|
| 312 |
def generate_pdf(score, checklist_summary, missing_summary, checklist_status, logs, qa_report, punch_list_text):
|
| 313 |
try:
|
| 314 |
+
# Sanitize inputs
|
| 315 |
logging.info("Sanitizing inputs for PDF generation")
|
| 316 |
score = str(float(score)) if score is not None else "0"
|
| 317 |
checklist_summary = str(checklist_summary) if checklist_summary is not None else ""
|
| 318 |
checklist_status = str(checklist_status) if checklist_status is not None else ""
|
| 319 |
|
| 320 |
+
# Remove non-ASCII characters
|
| 321 |
checklist_summary = checklist_summary.encode('ascii', 'ignore').decode('ascii')
|
| 322 |
checklist_status = checklist_status.encode('ascii', 'ignore').decode('ascii')
|
| 323 |
|
|
|
|
| 325 |
pdf_path = "readiness_report.pdf"
|
| 326 |
logging.info(f"Creating PDF at {pdf_path}")
|
| 327 |
|
| 328 |
+
# Create the PDF
|
| 329 |
c = canvas.Canvas(pdf_path, pagesize=letter)
|
|
|
|
|
|
|
|
|
|
| 330 |
c.setFont("Times-Bold", 16)
|
| 331 |
c.drawString(50, 750, "Project Closure Readiness Report")
|
|
|
|
|
|
|
|
|
|
| 332 |
c.setFont("Times-Roman", 12)
|
| 333 |
c.drawString(50, 720, f"Readiness Score: {score}%")
|
| 334 |
c.drawString(50, 700, f"Status: {checklist_status}")
|
| 335 |
c.drawString(50, 680, "Checklist Summary:")
|
|
|
|
|
|
|
|
|
|
| 336 |
y = 660
|
| 337 |
+
BOTTOM_MARGIN = 50
|
| 338 |
for line in checklist_summary.split("\n"):
|
| 339 |
if y < BOTTOM_MARGIN:
|
| 340 |
+
c.showPage()
|
| 341 |
+
c.setFont("Times-Roman", 12)
|
| 342 |
+
y = 750
|
|
|
|
| 343 |
c.drawString(50, y, line)
|
| 344 |
y -= 20
|
|
|
|
|
|
|
| 345 |
if y - 40 < BOTTOM_MARGIN:
|
|
|
|
| 346 |
c.showPage()
|
| 347 |
c.setFont("Times-Roman", 12)
|
| 348 |
y = 750
|
| 349 |
c.drawString(50, y - 40, "Stakeholder Signature: ____________________")
|
|
|
|
| 350 |
if y - 60 < BOTTOM_MARGIN:
|
|
|
|
| 351 |
c.showPage()
|
| 352 |
c.setFont("Times-Roman", 12)
|
| 353 |
y = 750
|
| 354 |
c.drawString(50, y - 60, "Date: ____________________")
|
|
|
|
|
|
|
|
|
|
| 355 |
c.save()
|
| 356 |
|
| 357 |
# Confirm the file exists
|
|
|
|
| 382 |
with gr.Row():
|
| 383 |
with gr.Column(scale=2):
|
| 384 |
logs_input = gr.Textbox(label="Project Logs", lines=5, placeholder="Enter project logs (e.g., 'Project complete, handover done')")
|
| 385 |
+
qa_input = gr.Dropdown(
|
| 386 |
+
label="QA Report",
|
| 387 |
+
choices=["Approved", "Passed", "Cleared", "Pending", "Not Started"],
|
| 388 |
+
value="Pending",
|
| 389 |
+
allow_custom_value=False
|
| 390 |
+
)
|
| 391 |
+
punch_input = gr.Dropdown(
|
| 392 |
+
label="Punch List",
|
| 393 |
+
choices=["None", "Resolved", "Closed", "No Issues", "Open Items"],
|
| 394 |
+
value="Open Items",
|
| 395 |
+
allow_custom_value=False
|
| 396 |
+
)
|
| 397 |
submit_btn = gr.Button("Evaluate and Generate PDF")
|
| 398 |
with gr.Column(scale=3):
|
| 399 |
score_output = gr.Number(label="Readiness Score (%)")
|
|
|
|
| 404 |
gr.Markdown("### Handover Summary")
|
| 405 |
checklist_output = gr.Textbox(label="Checklist Summary")
|
| 406 |
missing_output = gr.Textbox(label="Missing Items")
|
|
|
|
| 407 |
open_punch_items_output = gr.Number(label="Open Punch Items (Debug)")
|
| 408 |
+
pdf_output = gr.File(label="Download PDF Report", type="filepath", interactive=False)
|
| 409 |
+
pdf_debug = gr.Textbox(label="PDF Debug Output")
|
| 410 |
|
| 411 |
# Chain the evaluation, PDF generation, and Salesforce record creation
|
| 412 |
submit_btn.click(
|
|
|
|
| 426 |
score_output, checklist_output, missing_output, status_output,
|
| 427 |
gr.State(), gr.State(), gr.State(), gr.State(), open_punch_items_output, pdf_output
|
| 428 |
],
|
| 429 |
+
outputs=None
|
| 430 |
)
|
| 431 |
demo.launch()
|