Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import datetime | |
| from simple_salesforce import Salesforce | |
| from model_predictor import predict_next_service | |
| import logging | |
| # Setup logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| # Salesforce credentials | |
| SALESFORCE_USERNAME = "abhi.reddyleburu@gmail.com" | |
| SALESFORCE_PASSWORD = "Abhilash@325" | |
| SALESFORCE_SECURITY_TOKEN = "UMzBWbvBz2KjOOzOtlZxLxTQt" | |
| # Connect to Salesforce | |
| def connect_to_salesforce(): | |
| try: | |
| sf = Salesforce( | |
| username=SALESFORCE_USERNAME, | |
| password=SALESFORCE_PASSWORD, | |
| security_token=SALESFORCE_SECURITY_TOKEN | |
| ) | |
| logger.info("✅ Connected to Salesforce") | |
| return sf | |
| except Exception as e: | |
| logger.error(f"❌ Salesforce connection failed: {e}") | |
| return None | |
| # Streamlit UI | |
| st.set_page_config(page_title="Predictive AMC Notifier", page_icon="🧪") | |
| st.title("🧪 Predictive AMC Notification Engine") | |
| # Input fields | |
| equipment_id = st.text_input("Enter Equipment ID") | |
| last_service = st.date_input("Select Last Service Date") | |
| usage_hours = st.slider("Avg. Usage (Hours/Day)", 1, 24, 8) | |
| days_per_week = st.slider("Operational Days/Week", 1, 7, 5) | |
| # Prediction and Salesforce interaction | |
| if st.button("🔮 Predict Next AMC Date"): | |
| if equipment_id and last_service: | |
| try: | |
| # Step 1: Predict next AMC date | |
| predicted = predict_next_service(last_service, usage_hours, days_per_week, "Generic") | |
| days_left = (predicted - datetime.date.today()).days | |
| st.success(f"✅ Predicted Next AMC: `{predicted}` ({days_left} days left)") | |
| # Step 2: Connect to Salesforce | |
| sf = connect_to_salesforce() | |
| if not sf: | |
| st.error("❌ Could not connect to Salesforce.") | |
| st.stop() | |
| # Step 3: Check if AMC_Record__c already exists | |
| query = f"SELECT Id FROM AMC_Record__c WHERE Equipment_ID__c = '{equipment_id}' LIMIT 1" | |
| existing_records = sf.query(query) | |
| if existing_records['totalSize'] > 0: | |
| amc_record_id = existing_records['records'][0]['Id'] | |
| st.info(f"ℹ️ Existing AMC_Record__c found (ID: {amc_record_id})") | |
| else: | |
| # Step 4: Create new AMC_Record__c | |
| amc_record_data = { | |
| 'Equipment_ID__c': equipment_id, | |
| 'Last_Service_Date__c': str(last_service), | |
| 'Predicted_Next_Service__c': str(predicted) | |
| } | |
| record_result = sf.AMC_Record__c.create(amc_record_data) | |
| if not record_result.get("success"): | |
| raise Exception("❌ Failed to create AMC_Record__c") | |
| amc_record_id = record_result.get("id") | |
| st.success(f"📦 New record created in AMC_Record__c (ID: `{amc_record_id}`)") | |
| # Step 5: Create log in AMC_Logs__c | |
| log_data = { | |
| 'Event_Type__c': 'Predicted', | |
| 'Event_Timestamp__c': datetime.datetime.now().isoformat(), | |
| 'Raw_Request__c': f"Equipment ID: {equipment_id}, Usage: {usage_hours} hrs/day, {days_per_week} days/week", | |
| 'Raw_Response__c': f"Predicted Date: {predicted}, Days Left: {days_left}", | |
| 'Is_Success__c': True | |
| } | |
| log_result = sf.AMC_Logs__c.create(log_data) | |
| if log_result.get("success"): | |
| st.info(f"📝 Log saved in AMC_Logs__c (ID: `{log_result.get('id')}`)") | |
| else: | |
| st.error("❌ Failed to save to AMC_Logs__c") | |
| except Exception as e: | |
| st.error(f"🚨 Error: {e}") | |
| else: | |
| st.warning("⚠️ Please enter all required fields.") |