Update app.py
Browse files
app.py
CHANGED
|
@@ -84,7 +84,7 @@ def init_salesforce():
|
|
| 84 |
raise
|
| 85 |
|
| 86 |
# Create Salesforce record in custom object Project_Closure_Handover__c
|
| 87 |
-
def create_salesforce_record(
|
| 88 |
if not SALESFORCE_AVAILABLE:
|
| 89 |
logging.error("Salesforce library not available. Skipping record creation.")
|
| 90 |
return "Salesforce library not available"
|
|
@@ -95,8 +95,13 @@ def create_salesforce_record(project_id, score, checklist_summary, missing_summa
|
|
| 95 |
logging.error(f"Skipping Salesforce record creation due to connection failure: {connection_message}")
|
| 96 |
return connection_message
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
# Ensure inputs are properly formatted
|
| 99 |
-
project_id = str(project_id) if project_id else "" # Project ID is not summarized
|
| 100 |
score = float(score) if score is not None else 0.0
|
| 101 |
checklist_summary = str(checklist_summary) if checklist_summary else ""
|
| 102 |
missing_summary = str(missing_summary) if missing_summary else ""
|
|
@@ -104,24 +109,23 @@ def create_salesforce_record(project_id, score, checklist_summary, missing_summa
|
|
| 104 |
logs = str(logs) if logs else ""
|
| 105 |
qa_report = str(qa_report) if qa_report else ""
|
| 106 |
punch_list_text = str(punch_list_text) if punch_list_text else ""
|
| 107 |
-
missing_documents = len(missing_summary.split(", ")) if missing_summary and missing_summary != "None" else 0
|
| 108 |
open_punch_items = int(open_punch_items) if open_punch_items is not None else 0
|
| 109 |
|
| 110 |
# Set timezone to Asia/Kolkata (IST) and get current time
|
| 111 |
-
|
| 112 |
-
evaluated_at = datetime.now(ist_tz).strftime("%Y-%m-%dT%H:%M:%SZ") # Salesforce Date/Time format in UTC with Z
|
| 113 |
alert_sent = str(bool(escalated)).lower() # Converts True/False to "true"/"false"
|
| 114 |
logging.info(f"Setting Alert_Sent__c to: {alert_sent}")
|
| 115 |
escalation_flag = str(bool(escalated)).lower() # Ensure this is also a proper boolean string
|
| 116 |
|
| 117 |
# Create the record in Project_Closure_Handover__c without the PDF URL for now
|
| 118 |
record = {
|
| 119 |
-
"Project_ID__c": project_id, # Project ID
|
| 120 |
"Readiness_Score__c": score,
|
| 121 |
"Checklist_Summary__c": checklist_summary,
|
| 122 |
"Missing_Documents__c": missing_documents,
|
| 123 |
"Status__c": status,
|
| 124 |
-
"Summarized_Missing_Items__c": missing_summary,
|
| 125 |
"Alert_Sent__c": alert_sent,
|
| 126 |
"Client_PDF_Pack_URL__c": "", # Will be updated after attachment is created
|
| 127 |
"Closure_Pack_URL__c": "", # Placeholder; update if you have a closure pack URL
|
|
@@ -193,10 +197,10 @@ def sanitize_input(text):
|
|
| 193 |
return bleach.clean(text.strip())
|
| 194 |
|
| 195 |
# Rule-based completeness engine with weighted scoring
|
| 196 |
-
def evaluate_readiness(
|
| 197 |
try:
|
| 198 |
# Log inputs for debugging
|
| 199 |
-
logging.info(f"Inputs -
|
| 200 |
|
| 201 |
# Initialize score and lists for tracking
|
| 202 |
score = 0
|
|
@@ -209,7 +213,6 @@ def evaluate_readiness(project_id, logs, qa_report, punch_list_text):
|
|
| 209 |
PUNCH_WEIGHT = 30 # 30% weight for punch list
|
| 210 |
|
| 211 |
# Sanitize inputs
|
| 212 |
-
project_id = sanitize_input(project_id)
|
| 213 |
logs = sanitize_input(logs)
|
| 214 |
qa_report = sanitize_input(qa_report)
|
| 215 |
punch_list_text = sanitize_input(punch_list_text)
|
|
@@ -360,7 +363,7 @@ with gr.Blocks(css="""
|
|
| 360 |
)
|
| 361 |
with gr.Row():
|
| 362 |
with gr.Column(scale=2):
|
| 363 |
-
|
| 364 |
logs_input = gr.Textbox(label="Project Logs", lines=5, placeholder="Enter project logs (e.g., 'Project complete, handover done')")
|
| 365 |
qa_input = gr.Dropdown(
|
| 366 |
label="QA Report",
|
|
@@ -391,7 +394,7 @@ with gr.Blocks(css="""
|
|
| 391 |
# Chain the evaluation, PDF generation, and Salesforce record creation
|
| 392 |
submit_btn.click(
|
| 393 |
fn=evaluate_readiness,
|
| 394 |
-
inputs=[
|
| 395 |
outputs=[
|
| 396 |
score_output, checklist_output, missing_output, status_output, progress_output,
|
| 397 |
gr.State(), gr.State(), gr.State(), gr.State(), open_punch_items_output, status_output
|
|
@@ -403,7 +406,7 @@ with gr.Blocks(css="""
|
|
| 403 |
).then(
|
| 404 |
fn=create_salesforce_record,
|
| 405 |
inputs=[
|
| 406 |
-
|
| 407 |
gr.State(), gr.State(), gr.State(), gr.State(), open_punch_items_output, pdf_output
|
| 408 |
],
|
| 409 |
outputs=None
|
|
|
|
| 84 |
raise
|
| 85 |
|
| 86 |
# Create Salesforce record in custom object Project_Closure_Handover__c
|
| 87 |
+
def create_salesforce_record(score, checklist_summary, missing_summary, status, escalated, logs, qa_report, punch_list_text, open_punch_items, pdf_path=None):
|
| 88 |
if not SALESFORCE_AVAILABLE:
|
| 89 |
logging.error("Salesforce library not available. Skipping record creation.")
|
| 90 |
return "Salesforce library not available"
|
|
|
|
| 95 |
logging.error(f"Skipping Salesforce record creation due to connection failure: {connection_message}")
|
| 96 |
return connection_message
|
| 97 |
|
| 98 |
+
# Generate a unique Project ID based on the current timestamp
|
| 99 |
+
ist_tz = pytz.timezone("Asia/Kolkata")
|
| 100 |
+
current_time = datetime.now(ist_tz)
|
| 101 |
+
project_id = f"PROJ-{current_time.strftime('%Y%m%d-%H%M%S')}" # e.g., PROJ-20250526-103015
|
| 102 |
+
logging.info(f"Generated Project ID: {project_id}")
|
| 103 |
+
|
| 104 |
# Ensure inputs are properly formatted
|
|
|
|
| 105 |
score = float(score) if score is not None else 0.0
|
| 106 |
checklist_summary = str(checklist_summary) if checklist_summary else ""
|
| 107 |
missing_summary = str(missing_summary) if missing_summary else ""
|
|
|
|
| 109 |
logs = str(logs) if logs else ""
|
| 110 |
qa_report = str(qa_report) if qa_report else ""
|
| 111 |
punch_list_text = str(punch_list_text) if punch_list_text else ""
|
| 112 |
+
missing_documents = len(missing_summary.split(", ")) Mads if missing_summary and missing_summary != "None" else 0
|
| 113 |
open_punch_items = int(open_punch_items) if open_punch_items is not None else 0
|
| 114 |
|
| 115 |
# Set timezone to Asia/Kolkata (IST) and get current time
|
| 116 |
+
evaluated_at = current_time.strftime("%Y-%m-%dT%H:%M:%SZ") # Salesforce Date/Time format in UTC with Z
|
|
|
|
| 117 |
alert_sent = str(bool(escalated)).lower() # Converts True/False to "true"/"false"
|
| 118 |
logging.info(f"Setting Alert_Sent__c to: {alert_sent}")
|
| 119 |
escalation_flag = str(bool(escalated)).lower() # Ensure this is also a proper boolean string
|
| 120 |
|
| 121 |
# Create the record in Project_Closure_Handover__c without the PDF URL for now
|
| 122 |
record = {
|
| 123 |
+
"Project_ID__c": project_id, # Use the generated Project ID
|
| 124 |
"Readiness_Score__c": score,
|
| 125 |
"Checklist_Summary__c": checklist_summary,
|
| 126 |
"Missing_Documents__c": missing_documents,
|
| 127 |
"Status__c": status,
|
| 128 |
+
"Summarized_Missing_Items__c": missing_summary,
|
| 129 |
"Alert_Sent__c": alert_sent,
|
| 130 |
"Client_PDF_Pack_URL__c": "", # Will be updated after attachment is created
|
| 131 |
"Closure_Pack_URL__c": "", # Placeholder; update if you have a closure pack URL
|
|
|
|
| 197 |
return bleach.clean(text.strip())
|
| 198 |
|
| 199 |
# Rule-based completeness engine with weighted scoring
|
| 200 |
+
def evaluate_readiness(logs, qa_report, punch_list_text):
|
| 201 |
try:
|
| 202 |
# Log inputs for debugging
|
| 203 |
+
logging.info(f"Inputs - Logs: {logs}, QA Report: {qa_report}, Punch List: {punch_list_text}")
|
| 204 |
|
| 205 |
# Initialize score and lists for tracking
|
| 206 |
score = 0
|
|
|
|
| 213 |
PUNCH_WEIGHT = 30 # 30% weight for punch list
|
| 214 |
|
| 215 |
# Sanitize inputs
|
|
|
|
| 216 |
logs = sanitize_input(logs)
|
| 217 |
qa_report = sanitize_input(qa_report)
|
| 218 |
punch_list_text = sanitize_input(punch_list_text)
|
|
|
|
| 363 |
)
|
| 364 |
with gr.Row():
|
| 365 |
with gr.Column(scale=2):
|
| 366 |
+
# Removed project_id_input
|
| 367 |
logs_input = gr.Textbox(label="Project Logs", lines=5, placeholder="Enter project logs (e.g., 'Project complete, handover done')")
|
| 368 |
qa_input = gr.Dropdown(
|
| 369 |
label="QA Report",
|
|
|
|
| 394 |
# Chain the evaluation, PDF generation, and Salesforce record creation
|
| 395 |
submit_btn.click(
|
| 396 |
fn=evaluate_readiness,
|
| 397 |
+
inputs=[logs_input, qa_input, punch_input],
|
| 398 |
outputs=[
|
| 399 |
score_output, checklist_output, missing_output, status_output, progress_output,
|
| 400 |
gr.State(), gr.State(), gr.State(), gr.State(), open_punch_items_output, status_output
|
|
|
|
| 406 |
).then(
|
| 407 |
fn=create_salesforce_record,
|
| 408 |
inputs=[
|
| 409 |
+
score_output, checklist_output, missing_output, status_output,
|
| 410 |
gr.State(), gr.State(), gr.State(), gr.State(), open_punch_items_output, pdf_output
|
| 411 |
],
|
| 412 |
outputs=None
|