Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,10 @@ import pandas as pd
|
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import os
|
| 6 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from model import predict_delay, get_weather_condition
|
| 8 |
from utils import validate_inputs, generate_heatmap
|
| 9 |
from reportlab.lib.pagesizes import letter
|
|
@@ -12,10 +16,6 @@ from reportlab.lib.styles import getSampleStyleSheet
|
|
| 12 |
from reportlab.lib.units import inch
|
| 13 |
from io import BytesIO
|
| 14 |
from simple_salesforce import Salesforce
|
| 15 |
-
import base64
|
| 16 |
-
import logging
|
| 17 |
-
import json
|
| 18 |
-
import requests
|
| 19 |
|
| 20 |
# Configure logging
|
| 21 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
@@ -28,7 +28,7 @@ st.set_page_config(page_title="Delay 🚀", layout="wide")
|
|
| 28 |
try:
|
| 29 |
sf_instance_url = os.environ.get("SF_INSTANCE_URL")
|
| 30 |
if not sf_instance_url:
|
| 31 |
-
raise ValueError("SF_INSTANCE_URL environment variable
|
| 32 |
if "lightning.force.com" in sf_instance_url:
|
| 33 |
logger.warning("SF_INSTANCE_URL contains lightning.force.com; consider using my.salesforce.com for reliable PDF downloads")
|
| 34 |
sf = Salesforce(
|
|
@@ -197,7 +197,7 @@ def save_to_salesforce(input_data, prediction, pdf_buffer):
|
|
| 197 |
if sf is None:
|
| 198 |
return "Salesforce connection not established."
|
| 199 |
try:
|
| 200 |
-
# Prepare data for
|
| 201 |
sf_data = {
|
| 202 |
"Project_Name__c": input_data["project_name"],
|
| 203 |
"Phase__c": input_data["phase"],
|
|
@@ -216,13 +216,17 @@ def save_to_salesforce(input_data, prediction, pdf_buffer):
|
|
| 216 |
"AI_Insights__c": prediction["ai_insights"],
|
| 217 |
"High_Risk_Phases__c": "; ".join(format_high_risk_phases(prediction["high_risk_phases"]))
|
| 218 |
}
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
| 221 |
if not result["success"]:
|
|
|
|
| 222 |
return f"Salesforce save failed: {result['errors']}"
|
| 223 |
|
| 224 |
# Get the record ID
|
| 225 |
record_id = result["id"]
|
|
|
|
| 226 |
|
| 227 |
# Upload PDF as ContentVersion
|
| 228 |
pdf_data = pdf_buffer.getvalue()
|
|
@@ -235,6 +239,7 @@ def save_to_salesforce(input_data, prediction, pdf_buffer):
|
|
| 235 |
}
|
| 236 |
cv_result = sf.ContentVersion.create(content_version)
|
| 237 |
if not cv_result["success"]:
|
|
|
|
| 238 |
return f"Failed to upload PDF to Salesforce: {cv_result['errors']}"
|
| 239 |
|
| 240 |
# Get the ContentVersion ID
|
|
@@ -252,8 +257,8 @@ def save_to_salesforce(input_data, prediction, pdf_buffer):
|
|
| 252 |
pdf_url = f"{sf_instance_url}/sfc/servlet.shepherd/document/download/{content_document_id}"
|
| 253 |
logger.info(f"Generated PDF URL: {pdf_url}")
|
| 254 |
|
| 255 |
-
# Update the
|
| 256 |
-
update_result = sf.
|
| 257 |
if update_result != 204:
|
| 258 |
logger.error(f"Failed to update PDF_Report__c with URL: {pdf_url}")
|
| 259 |
return f"Failed to update PDF_Report__c field: {update_result}"
|
|
@@ -326,7 +331,7 @@ if submit_button:
|
|
| 326 |
else:
|
| 327 |
input_data["weather_impact_score"] = weather_data["weather_impact_score"]
|
| 328 |
input_data["weather_condition"] = weather_data["weather_condition"]
|
| 329 |
-
st.write(f"**Weather Data for {project_location} on {input_data['weather_forecast_date']}
|
| 330 |
st.write(f"- Condition: {weather_data['weather_condition']}")
|
| 331 |
st.write(f"- Impact Score: {weather_data['weather_impact_score']}")
|
| 332 |
st.write(f"- Temperature: {weather_data['temperature']}°C")
|
|
|
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import os
|
| 6 |
from datetime import datetime
|
| 7 |
+
import json
|
| 8 |
+
import requests
|
| 9 |
+
import base64
|
| 10 |
+
import logging
|
| 11 |
from model import predict_delay, get_weather_condition
|
| 12 |
from utils import validate_inputs, generate_heatmap
|
| 13 |
from reportlab.lib.pagesizes import letter
|
|
|
|
| 16 |
from reportlab.lib.units import inch
|
| 17 |
from io import BytesIO
|
| 18 |
from simple_salesforce import Salesforce
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# Configure logging
|
| 21 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
| 28 |
try:
|
| 29 |
sf_instance_url = os.environ.get("SF_INSTANCE_URL")
|
| 30 |
if not sf_instance_url:
|
| 31 |
+
raise ValueError("SF_INSTANCE_URL environment variable not set")
|
| 32 |
if "lightning.force.com" in sf_instance_url:
|
| 33 |
logger.warning("SF_INSTANCE_URL contains lightning.force.com; consider using my.salesforce.com for reliable PDF downloads")
|
| 34 |
sf = Salesforce(
|
|
|
|
| 197 |
if sf is None:
|
| 198 |
return "Salesforce connection not established."
|
| 199 |
try:
|
| 200 |
+
# Prepare data for Delay_Predictor__c object
|
| 201 |
sf_data = {
|
| 202 |
"Project_Name__c": input_data["project_name"],
|
| 203 |
"Phase__c": input_data["phase"],
|
|
|
|
| 216 |
"AI_Insights__c": prediction["ai_insights"],
|
| 217 |
"High_Risk_Phases__c": "; ".join(format_high_risk_phases(prediction["high_risk_phases"]))
|
| 218 |
}
|
| 219 |
+
logger.info(f"Attempting to save to Salesforce Delay_Predictor__c: {sf_data}")
|
| 220 |
+
|
| 221 |
+
# Create a new record in Delay_Predictor__c
|
| 222 |
+
result = sf.Delay_Predictor__c.create(sf_data)
|
| 223 |
if not result["success"]:
|
| 224 |
+
logger.error(f"Salesforce save failed: {result['errors']}")
|
| 225 |
return f"Salesforce save failed: {result['errors']}"
|
| 226 |
|
| 227 |
# Get the record ID
|
| 228 |
record_id = result["id"]
|
| 229 |
+
logger.info(f"Created Salesforce record ID: {record_id}")
|
| 230 |
|
| 231 |
# Upload PDF as ContentVersion
|
| 232 |
pdf_data = pdf_buffer.getvalue()
|
|
|
|
| 239 |
}
|
| 240 |
cv_result = sf.ContentVersion.create(content_version)
|
| 241 |
if not cv_result["success"]:
|
| 242 |
+
logger.error(f"Failed to upload PDF to Salesforce: {cv_result['errors']}")
|
| 243 |
return f"Failed to upload PDF to Salesforce: {cv_result['errors']}"
|
| 244 |
|
| 245 |
# Get the ContentVersion ID
|
|
|
|
| 257 |
pdf_url = f"{sf_instance_url}/sfc/servlet.shepherd/document/download/{content_document_id}"
|
| 258 |
logger.info(f"Generated PDF URL: {pdf_url}")
|
| 259 |
|
| 260 |
+
# Update the Delay_Predictor__c record with the PDF URL
|
| 261 |
+
update_result = sf.Delay_Predictor__c.update(record_id, {"PDF_Report__c": pdf_url})
|
| 262 |
if update_result != 204:
|
| 263 |
logger.error(f"Failed to update PDF_Report__c with URL: {pdf_url}")
|
| 264 |
return f"Failed to update PDF_Report__c field: {update_result}"
|
|
|
|
| 331 |
else:
|
| 332 |
input_data["weather_impact_score"] = weather_data["weather_impact_score"]
|
| 333 |
input_data["weather_condition"] = weather_data["weather_condition"]
|
| 334 |
+
st.write(f"**Weather Data for {project_location} on {input_data['weather_forecast_date']}**:")
|
| 335 |
st.write(f"- Condition: {weather_data['weather_condition']}")
|
| 336 |
st.write(f"- Impact Score: {weather_data['weather_impact_score']}")
|
| 337 |
st.write(f"- Temperature: {weather_data['temperature']}°C")
|