Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,6 +1,4 @@
|
|
| 1 |
import matplotlib.pyplot as plt
|
| 2 |
-
from simple_salesforce import Salesforce
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
def validate_inputs(input_data):
|
| 6 |
"""
|
|
@@ -35,48 +33,4 @@ def generate_heatmap(delay_probability, label):
|
|
| 35 |
ax.set_xlabel("Delay Probability (%)")
|
| 36 |
ax.set_title("Delay Risk Heatmap")
|
| 37 |
plt.tight_layout()
|
| 38 |
-
return fig
|
| 39 |
-
|
| 40 |
-
def send_to_salesforce(input_data, prediction):
|
| 41 |
-
"""
|
| 42 |
-
Send prediction data to Salesforce as a Prediction__c custom object record.
|
| 43 |
-
"""
|
| 44 |
-
# Salesforce credentials (set these in Hugging Face Space environment variables)
|
| 45 |
-
sf_username = os.getenv("SF_USERNAME")
|
| 46 |
-
sf_password = os.getenv("SF_PASSWORD")
|
| 47 |
-
sf_security_token = os.getenv("SF_SECURITY_TOKEN")
|
| 48 |
-
sf_client_id = os.getenv("SF_CLIENT_ID")
|
| 49 |
-
sf_client_secret = os.getenv("SF_CLIENT_SECRET")
|
| 50 |
-
sf_domain = os.getenv("SF_DOMAIN", "login") # Use 'test' for sandbox, 'login' for production
|
| 51 |
-
|
| 52 |
-
if not all([sf_username, sf_password, sf_security_token, sf_client_id, sf_client_secret]):
|
| 53 |
-
raise ValueError("Missing Salesforce credentials. Set SF_USERNAME, SF_PASSWORD, SF_SECURITY_TOKEN, SF_CLIENT_ID, and SF_CLIENT_SECRET in environment variables.")
|
| 54 |
-
|
| 55 |
-
# Authenticate with Salesforce
|
| 56 |
-
sf = Salesforce(
|
| 57 |
-
username=sf_username,
|
| 58 |
-
password=sf_password,
|
| 59 |
-
security_token=sf_security_token,
|
| 60 |
-
client_id=sf_client_id,
|
| 61 |
-
client_secret=sf_client_secret,
|
| 62 |
-
domain=sf_domain
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
-
# Prepare data for Salesforce
|
| 66 |
-
high_risk_phases_text = "; ".join([f"{p['phase']}: {p['task']} ({p['risk']:.1f}%)" for p in prediction['high_risk_phases']])
|
| 67 |
-
data = {
|
| 68 |
-
"Project_Name__c": input_data["project_name"],
|
| 69 |
-
"Phase__c": input_data["phase"],
|
| 70 |
-
"Task__c": input_data["task"],
|
| 71 |
-
"Delay_Probability__c": prediction["delay_probability"],
|
| 72 |
-
"AI_Insights__c": prediction["ai_insights"],
|
| 73 |
-
"Weather_Condition__c": prediction["weather_condition"],
|
| 74 |
-
"High_Risk_Phases__c": high_risk_phases_text
|
| 75 |
-
}
|
| 76 |
-
|
| 77 |
-
# Create a new record in Prediction__c
|
| 78 |
-
try:
|
| 79 |
-
result = sf.Prediction__c.create(data)
|
| 80 |
-
return f"Successfully created Salesforce record with ID: {result['id']}"
|
| 81 |
-
except Exception as e:
|
| 82 |
-
raise Exception(f"Failed to create Salesforce record: {str(e)}")
|
|
|
|
| 1 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def validate_inputs(input_data):
|
| 4 |
"""
|
|
|
|
| 33 |
ax.set_xlabel("Delay Probability (%)")
|
| 34 |
ax.set_title("Delay Risk Heatmap")
|
| 35 |
plt.tight_layout()
|
| 36 |
+
return fig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|