Update app.py
Browse files
app.py
CHANGED
|
@@ -6,16 +6,18 @@ import bleach
|
|
| 6 |
import logging
|
| 7 |
import os
|
| 8 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Print statement to confirm script initialization
|
| 11 |
print("Starting Project Closure Readiness Evaluator app...")
|
| 12 |
|
| 13 |
# Set up console logging for debugging
|
| 14 |
-
logging.basicConfig(level=logging.
|
| 15 |
logger = logging.getLogger()
|
| 16 |
logger.handlers = [] # Clear existing handlers
|
| 17 |
console_handler = logging.StreamHandler()
|
| 18 |
-
console_handler.setLevel(logging.
|
| 19 |
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
|
| 20 |
console_handler.setFormatter(formatter)
|
| 21 |
logger.addHandler(console_handler)
|
|
@@ -34,6 +36,11 @@ SF_PASSWORD = os.getenv("SF_PASSWORD")
|
|
| 34 |
SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN")
|
| 35 |
SF_INSTANCE_URL = os.getenv("SF_INSTANCE_URL")
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
# Hugging Face configuration
|
| 38 |
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 39 |
if not HF_API_TOKEN:
|
|
@@ -43,7 +50,8 @@ else:
|
|
| 43 |
HF_AVAILABLE = True
|
| 44 |
hf_client = InferenceClient(token=HF_API_TOKEN)
|
| 45 |
|
| 46 |
-
# Initialize Salesforce connection
|
|
|
|
| 47 |
def init_salesforce():
|
| 48 |
if not SALESFORCE_AVAILABLE:
|
| 49 |
logging.error("Salesforce library not available. Skipping connection.")
|
|
@@ -56,10 +64,14 @@ def init_salesforce():
|
|
| 56 |
security_token=SF_SECURITY_TOKEN,
|
| 57 |
instance_url=SF_INSTANCE_URL
|
| 58 |
)
|
|
|
|
| 59 |
return sf, "Salesforce connected successfully"
|
|
|
|
|
|
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
logging.error(f"Failed to initialize Salesforce connection: {str(e)}")
|
| 62 |
-
|
| 63 |
|
| 64 |
# Summarize text using Hugging Face Inference API
|
| 65 |
def summarize_text(text, max_length=100, min_length=30):
|
|
@@ -85,32 +97,44 @@ def summarize_text(text, max_length=100, min_length=30):
|
|
| 85 |
def create_salesforce_record(score, checklist_summary, missing_summary, status):
|
| 86 |
if not SALESFORCE_AVAILABLE:
|
| 87 |
logging.error("Salesforce library not available. Skipping record creation.")
|
| 88 |
-
return
|
| 89 |
|
| 90 |
try:
|
| 91 |
sf, connection_message = init_salesforce()
|
| 92 |
if not sf:
|
| 93 |
logging.error(f"Skipping Salesforce record creation due to connection failure: {connection_message}")
|
| 94 |
-
return
|
| 95 |
|
| 96 |
# Summarize checklist_summary and missing_summary using Hugging Face
|
| 97 |
summarized_checklist = summarize_text(checklist_summary)
|
| 98 |
summarized_missing = summarize_text(missing_summary)
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
# Create a record in the custom object Project_Closure_Handover__c
|
| 101 |
record = {
|
| 102 |
"Readiness_Score__c": score,
|
| 103 |
"Checklist_Summary__c": checklist_summary,
|
| 104 |
"Missing_Documents__c": missing_summary,
|
| 105 |
"Status__c": status,
|
| 106 |
-
"Checklist_Summary__c": summarized_checklist,
|
| 107 |
"Summarized_Missing_Items__c": summarized_missing
|
| 108 |
}
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
except Exception as e:
|
| 112 |
logging.error(f"Failed to create Salesforce Project_Closure_Handover__c record: {str(e)}")
|
| 113 |
-
|
| 114 |
|
| 115 |
# Clean input to prevent injection attacks
|
| 116 |
def sanitize_input(text):
|
|
@@ -192,7 +216,8 @@ def evaluate_readiness(logs, qa_report, punch_list_text):
|
|
| 192 |
"""
|
| 193 |
|
| 194 |
# Automatically create Salesforce record
|
| 195 |
-
create_salesforce_record(score, checklist_summary, missing_summary, status)
|
|
|
|
| 196 |
|
| 197 |
return score, checklist_summary, missing_summary, status, progress_bar
|
| 198 |
except Exception as e:
|
|
@@ -339,8 +364,6 @@ with gr.Blocks(css="""
|
|
| 339 |
outputs=[pdf_output, pdf_debug]
|
| 340 |
)
|
| 341 |
|
| 342 |
-
#
|
| 343 |
-
#
|
| 344 |
-
|
| 345 |
-
if __name__ == "__main__":
|
| 346 |
-
demo.launch()
|
|
|
|
| 6 |
import logging
|
| 7 |
import os
|
| 8 |
from huggingface_hub import InferenceClient
|
| 9 |
+
from retry import retry
|
| 10 |
+
import time
|
| 11 |
|
| 12 |
# Print statement to confirm script initialization
|
| 13 |
print("Starting Project Closure Readiness Evaluator app...")
|
| 14 |
|
| 15 |
# Set up console logging for debugging
|
| 16 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 17 |
logger = logging.getLogger()
|
| 18 |
logger.handlers = [] # Clear existing handlers
|
| 19 |
console_handler = logging.StreamHandler()
|
| 20 |
+
console_handler.setLevel(logging.DEBUG)
|
| 21 |
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
|
| 22 |
console_handler.setFormatter(formatter)
|
| 23 |
logger.addHandler(console_handler)
|
|
|
|
| 36 |
SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN")
|
| 37 |
SF_INSTANCE_URL = os.getenv("SF_INSTANCE_URL")
|
| 38 |
|
| 39 |
+
# Validate Salesforce environment variables
|
| 40 |
+
if not all([SF_USERNAME, SF_PASSWORD, SF_SECURITY_TOKEN, SF_INSTANCE_URL]):
|
| 41 |
+
logging.error("One or more Salesforce environment variables are missing. Salesforce functionality will be disabled.")
|
| 42 |
+
SALESFORCE_AVAILABLE = False
|
| 43 |
+
|
| 44 |
# Hugging Face configuration
|
| 45 |
HF_API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 46 |
if not HF_API_TOKEN:
|
|
|
|
| 50 |
HF_AVAILABLE = True
|
| 51 |
hf_client = InferenceClient(token=HF_API_TOKEN)
|
| 52 |
|
| 53 |
+
# Initialize Salesforce connection with retry mechanism
|
| 54 |
+
@retry(tries=3, delay=2, backoff=2, logger=logger)
|
| 55 |
def init_salesforce():
|
| 56 |
if not SALESFORCE_AVAILABLE:
|
| 57 |
logging.error("Salesforce library not available. Skipping connection.")
|
|
|
|
| 64 |
security_token=SF_SECURITY_TOKEN,
|
| 65 |
instance_url=SF_INSTANCE_URL
|
| 66 |
)
|
| 67 |
+
logging.info("Salesforce connected successfully")
|
| 68 |
return sf, "Salesforce connected successfully"
|
| 69 |
+
except SalesforceError as e:
|
| 70 |
+
logging.error(f"Salesforce authentication failed: {str(e)}")
|
| 71 |
+
raise
|
| 72 |
except Exception as e:
|
| 73 |
logging.error(f"Failed to initialize Salesforce connection: {str(e)}")
|
| 74 |
+
raise
|
| 75 |
|
| 76 |
# Summarize text using Hugging Face Inference API
|
| 77 |
def summarize_text(text, max_length=100, min_length=30):
|
|
|
|
| 97 |
def create_salesforce_record(score, checklist_summary, missing_summary, status):
|
| 98 |
if not SALESFORCE_AVAILABLE:
|
| 99 |
logging.error("Salesforce library not available. Skipping record creation.")
|
| 100 |
+
return "Salesforce library not available"
|
| 101 |
|
| 102 |
try:
|
| 103 |
sf, connection_message = init_salesforce()
|
| 104 |
if not sf:
|
| 105 |
logging.error(f"Skipping Salesforce record creation due to connection failure: {connection_message}")
|
| 106 |
+
return connection_message
|
| 107 |
|
| 108 |
# Summarize checklist_summary and missing_summary using Hugging Face
|
| 109 |
summarized_checklist = summarize_text(checklist_summary)
|
| 110 |
summarized_missing = summarize_text(missing_summary)
|
| 111 |
|
| 112 |
+
# Ensure inputs are strings and sanitized
|
| 113 |
+
score = float(score) if score is not None else 0.0
|
| 114 |
+
checklist_summary = str(checklist_summary) if checklist_summary else ""
|
| 115 |
+
summarized_checklist = str(summarized_checklist) if summarized_checklist else ""
|
| 116 |
+
missing_summary = str(missing_summary) if missing_summary else ""
|
| 117 |
+
summarized_missing = str(summarized_missing) if summarized_missing else ""
|
| 118 |
+
status = str(status) if status else ""
|
| 119 |
+
|
| 120 |
# Create a record in the custom object Project_Closure_Handover__c
|
| 121 |
record = {
|
| 122 |
"Readiness_Score__c": score,
|
| 123 |
"Checklist_Summary__c": checklist_summary,
|
| 124 |
"Missing_Documents__c": missing_summary,
|
| 125 |
"Status__c": status,
|
|
|
|
| 126 |
"Summarized_Missing_Items__c": summarized_missing
|
| 127 |
}
|
| 128 |
+
logging.debug(f"Attempting to create Salesforce record: {record}")
|
| 129 |
+
result = sf.Project_Closure_Handover__c.create(record)
|
| 130 |
+
logging.info(f"Successfully created Salesforce record: {result}")
|
| 131 |
+
return "Record created successfully"
|
| 132 |
+
except SalesforceError as e:
|
| 133 |
+
logging.error(f"Salesforce error while creating Project_Closure_Handover__c record: {str(e)}")
|
| 134 |
+
return f"Salesforce error: {str(e)}"
|
| 135 |
except Exception as e:
|
| 136 |
logging.error(f"Failed to create Salesforce Project_Closure_Handover__c record: {str(e)}")
|
| 137 |
+
return f"Error creating record: {str(e)}"
|
| 138 |
|
| 139 |
# Clean input to prevent injection attacks
|
| 140 |
def sanitize_input(text):
|
|
|
|
| 216 |
"""
|
| 217 |
|
| 218 |
# Automatically create Salesforce record
|
| 219 |
+
sf_result = create_salesforce_record(score, checklist_summary, missing_summary, status)
|
| 220 |
+
logging.info(f"Salesforce record creation result: {sf_result}")
|
| 221 |
|
| 222 |
return score, checklist_summary, missing_summary, status, progress_bar
|
| 223 |
except Exception as e:
|
|
|
|
| 364 |
outputs=[pdf_output, pdf_debug]
|
| 365 |
)
|
| 366 |
|
| 367 |
+
# Comment out demo.launch() for Hugging Face Spaces deployment
|
| 368 |
+
# if __name__ == "__main__":
|
| 369 |
+
# demo.launch()
|
|
|
|
|
|