Update app.py
Browse files
app.py
CHANGED
|
@@ -5,11 +5,11 @@ from reportlab.pdfgen import canvas
|
|
| 5 |
import bleach
|
| 6 |
import logging
|
| 7 |
import os
|
|
|
|
| 8 |
from retry import retry
|
| 9 |
import time
|
| 10 |
from datetime import datetime
|
| 11 |
import base64
|
| 12 |
-
import pytz # For proper timezone handling
|
| 13 |
|
| 14 |
# Print statement to confirm script initialization
|
| 15 |
print("Starting Project Closure Readiness Evaluator app...")
|
|
@@ -49,6 +49,16 @@ if not all([SF_USERNAME, SF_PASSWORD, SF_SECURITY_TOKEN, SF_INSTANCE_URL]):
|
|
| 49 |
logging.error("Please set these variables in Hugging Face Space Settings > Secrets.")
|
| 50 |
SALESFORCE_AVAILABLE = False
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
# Initialize Salesforce connection with retry mechanism
|
| 53 |
@retry(tries=3, delay=2, backoff=2, logger=logger)
|
| 54 |
def init_salesforce():
|
|
@@ -83,6 +93,26 @@ def init_salesforce():
|
|
| 83 |
logging.error("Check your Salesforce org settings, network restrictions, or API access.")
|
| 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:
|
|
@@ -95,37 +125,34 @@ def create_salesforce_record(score, checklist_summary, missing_summary, status,
|
|
| 95 |
logging.error(f"Skipping Salesforce record creation due to connection failure: {connection_message}")
|
| 96 |
return connection_message
|
| 97 |
|
| 98 |
-
#
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
project_id = f"PROJ-{current_time.strftime('%Y%m%d-%H%M%S')}" # e.g., PROJ-20250526-103315
|
| 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 ""
|
|
|
|
| 108 |
status = str(status) if status 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(", ")) 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":
|
| 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
|
|
@@ -349,7 +376,7 @@ def generate_pdf(score, checklist_summary, missing_summary, checklist_status, lo
|
|
| 349 |
|
| 350 |
# Gradio interface with updated UI
|
| 351 |
with gr.Blocks(css="""
|
| 352 |
-
.progress-container { background-color: #f0f0f0; width: 100%; height: 20px; border-radius: 5px;
|
| 353 |
.progress-bar { height: 100%; text-align: center; line-height: 20px; color: #000; font-size: 12px; }
|
| 354 |
.progress-bar.red { background-color: #FF0000; }
|
| 355 |
.progress-bar.yellow { background-color: #FFFF00; }
|
|
@@ -363,7 +390,6 @@ with gr.Blocks(css="""
|
|
| 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",
|
|
|
|
| 5 |
import bleach
|
| 6 |
import logging
|
| 7 |
import os
|
| 8 |
+
from huggingface_hub import InferenceClient
|
| 9 |
from retry import retry
|
| 10 |
import time
|
| 11 |
from datetime import datetime
|
| 12 |
import base64
|
|
|
|
| 13 |
|
| 14 |
# Print statement to confirm script initialization
|
| 15 |
print("Starting Project Closure Readiness Evaluator app...")
|
|
|
|
| 49 |
logging.error("Please set these variables in Hugging Face Space Settings > Secrets.")
|
| 50 |
SALESFORCE_AVAILABLE = False
|
| 51 |
|
| 52 |
+
# Hugging Face configuration
|
| 53 |
+
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 54 |
+
if not HF_API_TOKEN:
|
| 55 |
+
logging.error("Hugging Face API token (HF_API_TOKEN) not found. Hugging Face functionality will be disabled.")
|
| 56 |
+
logging.error("Please set HF_API_TOKEN in Hugging Face Space Settings > Secrets.")
|
| 57 |
+
HF_AVAILABLE = False
|
| 58 |
+
else:
|
| 59 |
+
HF_AVAILABLE = True
|
| 60 |
+
hf_client = InferenceClient(token=HF_API_TOKEN)
|
| 61 |
+
|
| 62 |
# Initialize Salesforce connection with retry mechanism
|
| 63 |
@retry(tries=3, delay=2, backoff=2, logger=logger)
|
| 64 |
def init_salesforce():
|
|
|
|
| 93 |
logging.error("Check your Salesforce org settings, network restrictions, or API access.")
|
| 94 |
raise
|
| 95 |
|
| 96 |
+
# Summarize text using Hugging Face Inference API
|
| 97 |
+
def summarize_text(text, max_length=100, min_length=30):
|
| 98 |
+
if not HF_AVAILABLE:
|
| 99 |
+
logging.error("Hugging Face API not available. Returning original text.")
|
| 100 |
+
return text
|
| 101 |
+
|
| 102 |
+
if not text or text == "None":
|
| 103 |
+
return "No summary available"
|
| 104 |
+
|
| 105 |
+
try:
|
| 106 |
+
summary = hf_client.summarization(
|
| 107 |
+
text,
|
| 108 |
+
model="facebook/bart-large-cnn",
|
| 109 |
+
parameters={"max_length": max_length, "min_length": min_length}
|
| 110 |
+
)
|
| 111 |
+
return summary
|
| 112 |
+
except Exception as e:
|
| 113 |
+
logging.error(f"Failed to summarize text with Hugging Face: {str(e)}")
|
| 114 |
+
return text # Fallback to original text
|
| 115 |
+
|
| 116 |
# Create Salesforce record in custom object Project_Closure_Handover__c
|
| 117 |
def create_salesforce_record(score, checklist_summary, missing_summary, status, escalated, logs, qa_report, punch_list_text, open_punch_items, pdf_path=None):
|
| 118 |
if not SALESFORCE_AVAILABLE:
|
|
|
|
| 125 |
logging.error(f"Skipping Salesforce record creation due to connection failure: {connection_message}")
|
| 126 |
return connection_message
|
| 127 |
|
| 128 |
+
# Summarize checklist_summary and missing_summary using Hugging Face
|
| 129 |
+
summarized_checklist = summarize_text(checklist_summary)
|
| 130 |
+
summarized_missing = summarize_text(missing_summary)
|
|
|
|
|
|
|
| 131 |
|
| 132 |
# Ensure inputs are properly formatted
|
| 133 |
score = float(score) if score is not None else 0.0
|
| 134 |
checklist_summary = str(checklist_summary) if checklist_summary else ""
|
| 135 |
+
summarized_checklist = str(summarized_checklist) if summarized_checklist else ""
|
| 136 |
missing_summary = str(missing_summary) if missing_summary else ""
|
| 137 |
+
summarized_missing = str(summarized_missing) if summarized_missing else ""
|
| 138 |
status = str(status) if status else ""
|
| 139 |
logs = str(logs) if logs else ""
|
| 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
|
| 150 |
record = {
|
|
|
|
| 151 |
"Readiness_Score__c": score,
|
| 152 |
"Checklist_Summary__c": checklist_summary,
|
| 153 |
"Missing_Documents__c": missing_documents,
|
| 154 |
"Status__c": status,
|
| 155 |
+
"Summarized_Missing_Items__c": summarized_missing,
|
| 156 |
"Alert_Sent__c": alert_sent,
|
| 157 |
"Client_PDF_Pack_URL__c": "", # Will be updated after attachment is created
|
| 158 |
"Closure_Pack_URL__c": "", # Placeholder; update if you have a closure pack URL
|
|
|
|
| 376 |
|
| 377 |
# Gradio interface with updated UI
|
| 378 |
with gr.Blocks(css="""
|
| 379 |
+
.progress-container { background-color: #f0f0f0; width: 100%; height: 20px; border-radius: 5px; overflow: hidden; position: relative; }
|
| 380 |
.progress-bar { height: 100%; text-align: center; line-height: 20px; color: #000; font-size: 12px; }
|
| 381 |
.progress-bar.red { background-color: #FF0000; }
|
| 382 |
.progress-bar.yellow { background-color: #FFFF00; }
|
|
|
|
| 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",
|