Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,25 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
from simple_salesforce import Salesforce
|
| 3 |
import pandas as pd
|
| 4 |
-
from datetime import datetime
|
| 5 |
import logging
|
| 6 |
from sklearn.ensemble import IsolationForest
|
| 7 |
from transformers import pipeline
|
| 8 |
import torch
|
| 9 |
import os
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# Configure logging
|
| 12 |
-
logging.basicConfig(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Initialize Flask app
|
| 15 |
app = Flask(__name__)
|
|
@@ -20,32 +30,64 @@ SF_PASSWORD = os.getenv('SF_PASSWORD', 'your_salesforce_password')
|
|
| 20 |
SF_SECURITY_TOKEN = os.getenv('SF_SECURITY_TOKEN', 'your_security_token')
|
| 21 |
SF_INSTANCE_URL = os.getenv('SF_INSTANCE_URL', 'https://login.salesforce.com')
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
logging.info("
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# Fetch SmartLog records from Salesforce
|
| 47 |
def fetch_smartlog_records(lab_site, start_date, end_date, equipment_type):
|
|
|
|
|
|
|
| 48 |
try:
|
|
|
|
| 49 |
query = "SELECT Device_Id__c, Log_Type__c, Status__c, Timestamp__c, Usage_Hours__c, Downtime__c, AMC_Date__c FROM SmartLog__c WHERE "
|
| 50 |
conditions = []
|
| 51 |
params = {}
|
|
@@ -86,6 +128,7 @@ def fetch_smartlog_records(lab_site, start_date, end_date, equipment_type):
|
|
| 86 |
df = pd.DataFrame(data)
|
| 87 |
df['timestamp'] = pd.to_datetime(df['timestamp'], errors='coerce')
|
| 88 |
df['amc_date'] = pd.to_datetime(df['amc_date'], errors='coerce')
|
|
|
|
| 89 |
return df
|
| 90 |
except Exception as e:
|
| 91 |
logging.error(f"Failed to fetch SmartLog records: {str(e)}")
|
|
@@ -93,6 +136,9 @@ def fetch_smartlog_records(lab_site, start_date, end_date, equipment_type):
|
|
| 93 |
|
| 94 |
# Format summary prompt and generate report
|
| 95 |
def summarize_logs(df):
|
|
|
|
|
|
|
|
|
|
| 96 |
try:
|
| 97 |
total_devices = df["device_id"].nunique()
|
| 98 |
most_used = df.groupby("device_id")["usage_hours"].sum().idxmax() if not df.empty else "N/A"
|
|
@@ -153,6 +199,9 @@ def check_amc_reminders(df, current_date):
|
|
| 153 |
|
| 154 |
# Dashboard Insights
|
| 155 |
def generate_dashboard_insights(df):
|
|
|
|
|
|
|
|
|
|
| 156 |
try:
|
| 157 |
total_devices = df["device_id"].nunique()
|
| 158 |
avg_usage = df["usage_hours"].mean() if "usage_hours" in df.columns else 0
|
|
@@ -185,6 +234,9 @@ def create_usage_chart_data(df):
|
|
| 185 |
@app.route('/process_logs', methods=['POST'])
|
| 186 |
def process_logs():
|
| 187 |
try:
|
|
|
|
|
|
|
|
|
|
| 188 |
data = request.get_json()
|
| 189 |
lab_site = data.get('lab_site')
|
| 190 |
start_date = data.get('start_date')
|
|
@@ -245,4 +297,8 @@ def process_logs():
|
|
| 245 |
return jsonify({"error": str(e)}), 500
|
| 246 |
|
| 247 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
app.run(host="0.0.0.0", port=5000, debug=True)
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
from simple_salesforce import Salesforce
|
| 3 |
import pandas as pd
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
import logging
|
| 6 |
from sklearn.ensemble import IsolationForest
|
| 7 |
from transformers import pipeline
|
| 8 |
import torch
|
| 9 |
import os
|
| 10 |
+
import time
|
| 11 |
+
import requests
|
| 12 |
+
from requests.exceptions import Timeout
|
| 13 |
|
| 14 |
+
# Configure logging to a file and console for better debugging
|
| 15 |
+
logging.basicConfig(
|
| 16 |
+
level=logging.INFO,
|
| 17 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
| 18 |
+
handlers=[
|
| 19 |
+
logging.FileHandler('app.log'), # Log to a file for persistence
|
| 20 |
+
logging.StreamHandler() # Log to console for real-time visibility
|
| 21 |
+
]
|
| 22 |
+
)
|
| 23 |
|
| 24 |
# Initialize Flask app
|
| 25 |
app = Flask(__name__)
|
|
|
|
| 30 |
SF_SECURITY_TOKEN = os.getenv('SF_SECURITY_TOKEN', 'your_security_token')
|
| 31 |
SF_INSTANCE_URL = os.getenv('SF_INSTANCE_URL', 'https://login.salesforce.com')
|
| 32 |
|
| 33 |
+
# Global variable for Salesforce connection
|
| 34 |
+
sf = None
|
| 35 |
+
|
| 36 |
+
# Global variable for Hugging Face model (lazy initialization)
|
| 37 |
+
summarizer = None
|
| 38 |
+
|
| 39 |
+
# Health check endpoint to confirm the app is running
|
| 40 |
+
@app.route('/health', methods=['GET'])
|
| 41 |
+
def health_check():
|
| 42 |
+
return jsonify({"status": "App is running"}), 200
|
| 43 |
+
|
| 44 |
+
# Connect to Salesforce with a timeout
|
| 45 |
+
def connect_to_salesforce():
|
| 46 |
+
global sf
|
| 47 |
+
logging.info("Attempting to connect to Salesforce...")
|
| 48 |
+
start_time = time.time()
|
| 49 |
+
try:
|
| 50 |
+
# Use a timeout to prevent hanging
|
| 51 |
+
session = requests.Session()
|
| 52 |
+
adapter = requests.adapters.HTTPAdapter(max_retries=3)
|
| 53 |
+
session.mount('https://', adapter)
|
| 54 |
+
session.request('GET', SF_INSTANCE_URL, timeout=10) # Test connectivity
|
| 55 |
+
|
| 56 |
+
sf = Salesforce(
|
| 57 |
+
username=SF_USERNAME,
|
| 58 |
+
password=SF_PASSWORD,
|
| 59 |
+
security_token=SF_SECURITY_TOKEN,
|
| 60 |
+
instance_url=SF_INSTANCE_URL,
|
| 61 |
+
session=session
|
| 62 |
+
)
|
| 63 |
+
logging.info(f"Connected to Salesforce successfully in {time.time() - start_time:.2f} seconds")
|
| 64 |
+
except Timeout:
|
| 65 |
+
logging.error("Salesforce connection timed out after 10 seconds")
|
| 66 |
+
sf = None
|
| 67 |
+
except Exception as e:
|
| 68 |
+
logging.error(f"Failed to connect to Salesforce: {str(e)}")
|
| 69 |
+
sf = None
|
| 70 |
+
|
| 71 |
+
# Lazy load the Hugging Face model
|
| 72 |
+
def load_huggingface_model():
|
| 73 |
+
global summarizer
|
| 74 |
+
if summarizer is None:
|
| 75 |
+
logging.info("Loading Hugging Face model...")
|
| 76 |
+
start_time = time.time()
|
| 77 |
+
try:
|
| 78 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 79 |
+
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", device=device)
|
| 80 |
+
logging.info(f"Hugging Face model loaded successfully in {time.time() - start_time:.2f} seconds on device: {'GPU' if device == 0 else 'CPU'}")
|
| 81 |
+
except Exception as e:
|
| 82 |
+
logging.error(f"Failed to load Hugging Face model: {str(e)}")
|
| 83 |
+
summarizer = None
|
| 84 |
|
| 85 |
# Fetch SmartLog records from Salesforce
|
| 86 |
def fetch_smartlog_records(lab_site, start_date, end_date, equipment_type):
|
| 87 |
+
if sf is None:
|
| 88 |
+
raise Exception("Salesforce connection not established")
|
| 89 |
try:
|
| 90 |
+
logging.info("Fetching SmartLog records from Salesforce...")
|
| 91 |
query = "SELECT Device_Id__c, Log_Type__c, Status__c, Timestamp__c, Usage_Hours__c, Downtime__c, AMC_Date__c FROM SmartLog__c WHERE "
|
| 92 |
conditions = []
|
| 93 |
params = {}
|
|
|
|
| 128 |
df = pd.DataFrame(data)
|
| 129 |
df['timestamp'] = pd.to_datetime(df['timestamp'], errors='coerce')
|
| 130 |
df['amc_date'] = pd.to_datetime(df['amc_date'], errors='coerce')
|
| 131 |
+
logging.info(f"Fetched {len(df)} SmartLog records")
|
| 132 |
return df
|
| 133 |
except Exception as e:
|
| 134 |
logging.error(f"Failed to fetch SmartLog records: {str(e)}")
|
|
|
|
| 136 |
|
| 137 |
# Format summary prompt and generate report
|
| 138 |
def summarize_logs(df):
|
| 139 |
+
load_huggingface_model()
|
| 140 |
+
if summarizer is None:
|
| 141 |
+
return "Failed to load summarization model."
|
| 142 |
try:
|
| 143 |
total_devices = df["device_id"].nunique()
|
| 144 |
most_used = df.groupby("device_id")["usage_hours"].sum().idxmax() if not df.empty else "N/A"
|
|
|
|
| 199 |
|
| 200 |
# Dashboard Insights
|
| 201 |
def generate_dashboard_insights(df):
|
| 202 |
+
load_huggingface_model()
|
| 203 |
+
if summarizer is None:
|
| 204 |
+
return "Failed to load summarization model."
|
| 205 |
try:
|
| 206 |
total_devices = df["device_id"].nunique()
|
| 207 |
avg_usage = df["usage_hours"].mean() if "usage_hours" in df.columns else 0
|
|
|
|
| 234 |
@app.route('/process_logs', methods=['POST'])
|
| 235 |
def process_logs():
|
| 236 |
try:
|
| 237 |
+
if sf is None:
|
| 238 |
+
return jsonify({"error": "Salesforce connection not established. Check server logs for details."}), 500
|
| 239 |
+
|
| 240 |
data = request.get_json()
|
| 241 |
lab_site = data.get('lab_site')
|
| 242 |
start_date = data.get('start_date')
|
|
|
|
| 297 |
return jsonify({"error": str(e)}), 500
|
| 298 |
|
| 299 |
if __name__ == "__main__":
|
| 300 |
+
logging.info("Starting Flask application...")
|
| 301 |
+
start_time = time.time()
|
| 302 |
+
connect_to_salesforce() # Attempt to connect to Salesforce at startup
|
| 303 |
+
logging.info(f"Flask application startup completed in {time.time() - start_time:.2f} seconds")
|
| 304 |
app.run(host="0.0.0.0", port=5000, debug=True)
|