Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ from reportlab.pdfgen import canvas
|
|
| 5 |
import bleach
|
| 6 |
import logging
|
| 7 |
import os
|
|
|
|
| 8 |
|
| 9 |
# Print statement to confirm script initialization
|
| 10 |
print("Starting Project Closure Readiness Evaluator app...")
|
|
@@ -33,6 +34,15 @@ SF_PASSWORD = os.getenv("SF_PASSWORD")
|
|
| 33 |
SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN")
|
| 34 |
SF_INSTANCE_URL = os.getenv("SF_INSTANCE_URL")
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# Initialize Salesforce connection
|
| 37 |
def init_salesforce():
|
| 38 |
if not SALESFORCE_AVAILABLE:
|
|
@@ -51,7 +61,27 @@ def init_salesforce():
|
|
| 51 |
logging.error(f"Failed to initialize Salesforce connection: {str(e)}")
|
| 52 |
return None, f"Salesforce connection failed: {str(e)}"
|
| 53 |
|
| 54 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
def create_salesforce_record(score, checklist_summary, missing_summary, status):
|
| 56 |
if not SALESFORCE_AVAILABLE:
|
| 57 |
logging.error("Salesforce library not available. Skipping record creation.")
|
|
@@ -63,22 +93,23 @@ def create_salesforce_record(score, checklist_summary, missing_summary, status):
|
|
| 63 |
logging.error(f"Skipping Salesforce record creation due to connection failure: {connection_message}")
|
| 64 |
return
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
sf_status = "Closed"
|
| 70 |
|
| 71 |
-
# Create a
|
| 72 |
record = {
|
| 73 |
-
"
|
| 74 |
-
"
|
| 75 |
-
"
|
| 76 |
-
"
|
| 77 |
-
"
|
|
|
|
| 78 |
}
|
| 79 |
-
sf.
|
|
|
|
| 80 |
except Exception as e:
|
| 81 |
-
logging.error(f"Failed to create Salesforce
|
| 82 |
pass
|
| 83 |
|
| 84 |
# Clean input to prevent injection attacks
|
|
@@ -272,7 +303,7 @@ with gr.Blocks(css="""
|
|
| 272 |
"""
|
| 273 |
# Project Closure Readiness Evaluator
|
| 274 |
Evaluate project readiness and generate a PDF report.
|
| 275 |
-
Results are automatically saved to Salesforce.
|
| 276 |
"""
|
| 277 |
)
|
| 278 |
with gr.Row():
|
|
|
|
| 5 |
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...")
|
|
|
|
| 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:
|
| 40 |
+
logging.error("Hugging Face API token (HF_API_TOKEN) not found. Hugging Face functionality will be disabled.")
|
| 41 |
+
HF_AVAILABLE = False
|
| 42 |
+
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:
|
|
|
|
| 61 |
logging.error(f"Failed to initialize Salesforce connection: {str(e)}")
|
| 62 |
return None, f"Salesforce connection failed: {str(e)}"
|
| 63 |
|
| 64 |
+
# Summarize text using Hugging Face Inference API
|
| 65 |
+
def summarize_text(text, max_length=100, min_length=30):
|
| 66 |
+
if not HF_AVAILABLE:
|
| 67 |
+
logging.error("Hugging Face API not available. Returning original text.")
|
| 68 |
+
return text
|
| 69 |
+
|
| 70 |
+
if not text or text == "None":
|
| 71 |
+
return "No summary available"
|
| 72 |
+
|
| 73 |
+
try:
|
| 74 |
+
summary = hf_client.summarization(
|
| 75 |
+
text,
|
| 76 |
+
model="facebook/bart-large-cnn",
|
| 77 |
+
parameters={"max_length": max_length, "min_length": min_length}
|
| 78 |
+
)
|
| 79 |
+
return summary
|
| 80 |
+
except Exception as e:
|
| 81 |
+
logging.error(f"Failed to summarize text with Hugging Face: {str(e)}")
|
| 82 |
+
return text # Fallback to original text
|
| 83 |
+
|
| 84 |
+
# Create Salesforce record in custom object Project_Closure_Handover__c
|
| 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.")
|
|
|
|
| 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_Items__c": missing_summary,
|
| 105 |
+
"Overall_Status__c": status,
|
| 106 |
+
"Summarized_Checklist__c": summarized_checklist,
|
| 107 |
+
"Summarized_Missing_Items__c": summarized_missing
|
| 108 |
}
|
| 109 |
+
sf.Project_Closure_Handover__c.create(record)
|
| 110 |
+
logging.info("Successfully created record in Project_Closure_Handover__c")
|
| 111 |
except Exception as e:
|
| 112 |
+
logging.error(f"Failed to create Salesforce Project_Closure_Handover__c record: {str(e)}")
|
| 113 |
pass
|
| 114 |
|
| 115 |
# Clean input to prevent injection attacks
|
|
|
|
| 303 |
"""
|
| 304 |
# Project Closure Readiness Evaluator
|
| 305 |
Evaluate project readiness and generate a PDF report.
|
| 306 |
+
Results are automatically saved to Salesforce in Project_Closure_Handover__c.
|
| 307 |
"""
|
| 308 |
)
|
| 309 |
with gr.Row():
|