Update app.py
Browse files
app.py
CHANGED
|
@@ -141,8 +141,7 @@ def create_salesforce_record(score, checklist_summary, missing_summary, status,
|
|
| 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 |
-
|
| 145 |
-
evaluated_at = "2025-05-24T09:07:00Z" # Converted 02:37 PM IST to UTC (IST is UTC+5:30)
|
| 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 |
escalation_flag = str(bool(escalated)).lower() # Ensure this is also a proper boolean string
|
|
@@ -227,13 +226,8 @@ def sanitize_input(text):
|
|
| 227 |
# Rule-based completeness engine with weighted scoring
|
| 228 |
def evaluate_readiness(logs, qa_report, punch_list_text):
|
| 229 |
try:
|
| 230 |
-
# Define keywords for completed and incomplete statuses
|
| 231 |
-
complete_keywords = r"completed|handover done|finished|closed|successful|done"
|
| 232 |
-
incomplete_keywords = r"not completed|incomplete|pending|not finished|not done|still in progress|delayed|on hold|issues remaining"
|
| 233 |
-
|
| 234 |
# Log inputs for debugging
|
| 235 |
logging.info(f"Inputs - Logs: {logs}, QA Report: {qa_report}, Punch List: {punch_list_text}")
|
| 236 |
-
logging.info(f"Complete Keywords: {complete_keywords}, Incomplete Keywords: {incomplete_keywords}")
|
| 237 |
|
| 238 |
# Initialize score and lists for tracking
|
| 239 |
score = 0
|
|
@@ -249,22 +243,26 @@ def evaluate_readiness(logs, qa_report, punch_list_text):
|
|
| 249 |
logs = sanitize_input(logs)
|
| 250 |
qa_report = sanitize_input(qa_report)
|
| 251 |
punch_list_text = sanitize_input(punch_list_text)
|
| 252 |
-
complete_keywords = sanitize_input(complete_keywords)
|
| 253 |
-
incomplete_keywords = sanitize_input(incomplete_keywords)
|
| 254 |
|
| 255 |
# Process Project Logs (30% weight)
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
if logs_pass:
|
| 262 |
score += LOGS_WEIGHT
|
| 263 |
checklist_details.append("Logs: Completed")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
else:
|
| 265 |
missing_items.append("Project Logs Incomplete")
|
| 266 |
checklist_details.append("Logs: Pending")
|
| 267 |
-
|
| 268 |
|
| 269 |
# Process QA Report (40% weight)
|
| 270 |
qa_keywords = r"approved|passed|cleared"
|
|
@@ -292,16 +290,11 @@ def evaluate_readiness(logs, qa_report, punch_list_text):
|
|
| 292 |
open_punch_items = 0 if punch_pass else 1 # Simplified for dropdown input
|
| 293 |
|
| 294 |
# Enhanced escalation logic
|
| 295 |
-
escalated = score <
|
| 296 |
-
logging.info(f"Escalation Check: Score <
|
| 297 |
-
|
| 298 |
-
# Map
|
| 299 |
-
if escalated
|
| 300 |
-
status = "Escalated"
|
| 301 |
-
elif "Logs: Pending" in checklist_details:
|
| 302 |
-
status = "In Progress" # If logs are pending, status should be "In Progress"
|
| 303 |
-
else:
|
| 304 |
-
status = "Completed" if not missing_items else "In Progress"
|
| 305 |
checklist_status = "Escalated" if escalated else ("Completed" if not missing_items else "Pending") # For UI display
|
| 306 |
|
| 307 |
# Build summaries
|
|
@@ -324,7 +317,7 @@ def evaluate_readiness(logs, qa_report, punch_list_text):
|
|
| 324 |
logging.error(f"Error in evaluate_readiness: {str(e)}")
|
| 325 |
raise
|
| 326 |
|
| 327 |
-
# Generate PDF report with signature slots
|
| 328 |
def generate_pdf(score, checklist_summary, missing_summary, checklist_status, logs, qa_report, punch_list_text):
|
| 329 |
try:
|
| 330 |
# Sanitize inputs
|
|
@@ -341,19 +334,15 @@ def generate_pdf(score, checklist_summary, missing_summary, checklist_status, lo
|
|
| 341 |
pdf_path = "readiness_report.pdf"
|
| 342 |
logging.info(f"Creating PDF at {pdf_path}")
|
| 343 |
|
| 344 |
-
# Use the provided date and time for the report
|
| 345 |
-
evaluation_datetime = "02:37 PM IST, May 24, 2025"
|
| 346 |
-
|
| 347 |
# Create the PDF
|
| 348 |
c = canvas.Canvas(pdf_path, pagesize=letter)
|
| 349 |
c.setFont("Times-Bold", 16)
|
| 350 |
c.drawString(50, 750, "Project Closure Readiness Report")
|
| 351 |
c.setFont("Times-Roman", 12)
|
| 352 |
-
c.drawString(50,
|
| 353 |
-
c.drawString(50,
|
| 354 |
-
c.drawString(50,
|
| 355 |
-
|
| 356 |
-
y = 650
|
| 357 |
BOTTOM_MARGIN = 50
|
| 358 |
for line in checklist_summary.split("\n"):
|
| 359 |
if y < BOTTOM_MARGIN:
|
|
@@ -401,7 +390,7 @@ with gr.Blocks(css="""
|
|
| 401 |
)
|
| 402 |
with gr.Row():
|
| 403 |
with gr.Column(scale=2):
|
| 404 |
-
logs_input = gr.Textbox(label="Project Logs", lines=5, placeholder="Enter project logs (e.g., 'Project
|
| 405 |
qa_input = gr.Dropdown(
|
| 406 |
label="QA Report",
|
| 407 |
choices=["Approved", "Passed", "Cleared", "Pending", "Not Started"],
|
|
|
|
| 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
|
|
|
|
| 226 |
# Rule-based completeness engine with weighted scoring
|
| 227 |
def evaluate_readiness(logs, qa_report, punch_list_text):
|
| 228 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
# Log inputs for debugging
|
| 230 |
logging.info(f"Inputs - Logs: {logs}, QA Report: {qa_report}, Punch List: {punch_list_text}")
|
|
|
|
| 231 |
|
| 232 |
# Initialize score and lists for tracking
|
| 233 |
score = 0
|
|
|
|
| 243 |
logs = sanitize_input(logs)
|
| 244 |
qa_report = sanitize_input(qa_report)
|
| 245 |
punch_list_text = sanitize_input(punch_list_text)
|
|
|
|
|
|
|
| 246 |
|
| 247 |
# Process Project Logs (30% weight)
|
| 248 |
+
log_keywords = r"complete|handover done|finished|closed|successful"
|
| 249 |
+
negative_keywords = r"issue|pending|incomplete|problem|delay"
|
| 250 |
+
logs_pass = logs and re.search(log_keywords, logs.lower())
|
| 251 |
+
logs_negative = logs and re.search(negative_keywords, logs.lower())
|
| 252 |
+
|
| 253 |
+
if logs_pass and not logs_negative:
|
| 254 |
score += LOGS_WEIGHT
|
| 255 |
checklist_details.append("Logs: Completed")
|
| 256 |
+
logging.info(f"Logs Check: Pass (positive keywords found, no negative keywords), Score: {score}%")
|
| 257 |
+
elif logs_pass and logs_negative:
|
| 258 |
+
score += LOGS_WEIGHT // 2 # Partial score for mixed signals
|
| 259 |
+
missing_items.append("Issues detected in logs")
|
| 260 |
+
checklist_details.append("Logs: Partially Completed (issues detected)")
|
| 261 |
+
logging.info(f"Logs Check: Partial Pass (positive and negative keywords found), Score: {score}%")
|
| 262 |
else:
|
| 263 |
missing_items.append("Project Logs Incomplete")
|
| 264 |
checklist_details.append("Logs: Pending")
|
| 265 |
+
logging.info(f"Logs Check: Fail (no positive keywords or only negative keywords), Score: {score}%")
|
| 266 |
|
| 267 |
# Process QA Report (40% weight)
|
| 268 |
qa_keywords = r"approved|passed|cleared"
|
|
|
|
| 290 |
open_punch_items = 0 if punch_pass else 1 # Simplified for dropdown input
|
| 291 |
|
| 292 |
# Enhanced escalation logic
|
| 293 |
+
escalated = score < 70 or open_punch_items > 2
|
| 294 |
+
logging.info(f"Escalation Check: Score < 70 ({score < 70}), Open Punch Items > 2 ({open_punch_items > 2}), Escalated: {escalated}")
|
| 295 |
+
|
| 296 |
+
# Map "Pending" to "In Progress" for Salesforce Status__c picklist
|
| 297 |
+
status = "Escalated" if escalated else ("Completed" if not missing_items else "In Progress")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
checklist_status = "Escalated" if escalated else ("Completed" if not missing_items else "Pending") # For UI display
|
| 299 |
|
| 300 |
# Build summaries
|
|
|
|
| 317 |
logging.error(f"Error in evaluate_readiness: {str(e)}")
|
| 318 |
raise
|
| 319 |
|
| 320 |
+
# Generate PDF report with signature slots
|
| 321 |
def generate_pdf(score, checklist_summary, missing_summary, checklist_status, logs, qa_report, punch_list_text):
|
| 322 |
try:
|
| 323 |
# Sanitize inputs
|
|
|
|
| 334 |
pdf_path = "readiness_report.pdf"
|
| 335 |
logging.info(f"Creating PDF at {pdf_path}")
|
| 336 |
|
|
|
|
|
|
|
|
|
|
| 337 |
# Create the PDF
|
| 338 |
c = canvas.Canvas(pdf_path, pagesize=letter)
|
| 339 |
c.setFont("Times-Bold", 16)
|
| 340 |
c.drawString(50, 750, "Project Closure Readiness Report")
|
| 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 |
+
y = 660
|
|
|
|
| 346 |
BOTTOM_MARGIN = 50
|
| 347 |
for line in checklist_summary.split("\n"):
|
| 348 |
if y < BOTTOM_MARGIN:
|
|
|
|
| 390 |
)
|
| 391 |
with gr.Row():
|
| 392 |
with gr.Column(scale=2):
|
| 393 |
+
logs_input = gr.Textbox(label="Project Logs", lines=5, placeholder="Enter project logs (e.g., 'Project complete, handover done')")
|
| 394 |
qa_input = gr.Dropdown(
|
| 395 |
label="QA Report",
|
| 396 |
choices=["Approved", "Passed", "Cleared", "Pending", "Not Started"],
|