Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,17 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
from datetime import datetime
|
| 4 |
import logging
|
| 5 |
-
from
|
|
|
|
|
|
|
| 6 |
import os
|
|
|
|
| 7 |
import requests
|
| 8 |
from requests.exceptions import Timeout
|
| 9 |
-
import time
|
| 10 |
|
| 11 |
-
# Configure logging
|
| 12 |
logging.basicConfig(
|
| 13 |
level=logging.INFO,
|
| 14 |
format='%(asctime)s - %(levelname)s - %(message)s',
|
|
@@ -21,25 +24,22 @@ logging.basicConfig(
|
|
| 21 |
# Initialize Flask app
|
| 22 |
app = Flask(__name__)
|
| 23 |
|
| 24 |
-
# Salesforce credentials
|
| 25 |
-
SF_USERNAME = os.getenv('SF_USERNAME', '
|
| 26 |
-
SF_PASSWORD = os.getenv('SF_PASSWORD', '
|
| 27 |
-
SF_SECURITY_TOKEN = os.getenv('SF_SECURITY_TOKEN', '
|
| 28 |
SF_INSTANCE_URL = os.getenv('SF_INSTANCE_URL', 'https://login.salesforce.com')
|
| 29 |
|
| 30 |
-
# Global Salesforce connection
|
| 31 |
sf = None
|
|
|
|
| 32 |
|
| 33 |
-
# Health check endpoint
|
| 34 |
@app.route('/health', methods=['GET'])
|
| 35 |
def health_check():
|
| 36 |
-
return jsonify({"status": "App is running"
|
| 37 |
|
| 38 |
-
# Connect to Salesforce
|
| 39 |
def connect_to_salesforce():
|
| 40 |
global sf
|
| 41 |
-
logging.info("
|
| 42 |
-
start_time = time.time()
|
| 43 |
try:
|
| 44 |
session = requests.Session()
|
| 45 |
adapter = requests.adapters.HTTPAdapter(max_retries=3)
|
|
@@ -53,122 +53,111 @@ def connect_to_salesforce():
|
|
| 53 |
instance_url=SF_INSTANCE_URL,
|
| 54 |
session=session
|
| 55 |
)
|
| 56 |
-
logging.info(
|
| 57 |
-
return True
|
| 58 |
except Timeout:
|
| 59 |
-
logging.error("Salesforce connection timed out
|
| 60 |
sf = None
|
| 61 |
-
return False
|
| 62 |
except Exception as e:
|
| 63 |
-
logging.error(f"
|
| 64 |
sf = None
|
| 65 |
-
return False
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
def fetch_smartlog_records(lab_site=None, start_date=None, end_date=None, equipment_type=None):
|
| 69 |
if sf is None:
|
| 70 |
-
raise Exception("Salesforce connection not established")
|
|
|
|
| 71 |
try:
|
| 72 |
logging.info("Fetching SmartLog records...")
|
| 73 |
-
query = "
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
conditions = []
|
| 75 |
-
params = {}
|
| 76 |
if lab_site:
|
| 77 |
-
conditions.append("Lab_Site__c =
|
| 78 |
-
params['lab_site'] = lab_site
|
| 79 |
if start_date:
|
| 80 |
-
conditions.append("Timestamp__c >=
|
| 81 |
-
params['start_date'] = start_date
|
| 82 |
if end_date:
|
| 83 |
-
conditions.append("Timestamp__c <=
|
| 84 |
-
params['end_date'] = end_date
|
| 85 |
if equipment_type:
|
| 86 |
-
conditions.append("Log_Type__c =
|
| 87 |
-
params['equipment_type'] = equipment_type
|
| 88 |
|
| 89 |
if conditions:
|
| 90 |
query += " WHERE " + " AND ".join(conditions)
|
| 91 |
|
| 92 |
-
result = sf.query_all(query
|
| 93 |
records = result['records']
|
| 94 |
-
if not records:
|
| 95 |
-
logging.info("No records found matching the criteria")
|
| 96 |
-
return pd.DataFrame()
|
| 97 |
-
|
| 98 |
data = [{
|
| 99 |
'device_id': r['Device_Id__c'],
|
| 100 |
'log_type': r['Log_Type__c'],
|
| 101 |
'status': r['Status__c'],
|
| 102 |
'timestamp': r['Timestamp__c'],
|
| 103 |
-
'usage_hours': r['Usage_Hours__c']
|
| 104 |
-
'downtime': r['Downtime__c']
|
| 105 |
'amc_date': r['AMC_Date__c']
|
| 106 |
} for r in records]
|
|
|
|
| 107 |
df = pd.DataFrame(data)
|
| 108 |
df['timestamp'] = pd.to_datetime(df['timestamp'], errors='coerce')
|
| 109 |
df['amc_date'] = pd.to_datetime(df['amc_date'], errors='coerce')
|
| 110 |
-
logging.info(f"Fetched {len(df)} SmartLog records")
|
| 111 |
return df
|
|
|
|
| 112 |
except Exception as e:
|
| 113 |
-
logging.error(f"
|
| 114 |
-
raise
|
| 115 |
|
| 116 |
-
# Summarize logs (simplified without Hugging Face)
|
| 117 |
def summarize_logs(df):
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
total_downtime = df['downtime'].sum() if 'downtime' in df else 0
|
| 129 |
-
status_counts = df['status'].value_counts().to_dict()
|
| 130 |
-
|
| 131 |
-
summary_text = (
|
| 132 |
-
f"Analyzed {total_records} SmartLog records. "
|
| 133 |
-
f"Found {unique_devices} unique devices. "
|
| 134 |
-
f"Average usage hours: {avg_usage_hours:.2f} hours. "
|
| 135 |
-
f"Total downtime: {total_downtime:.2f} hours. "
|
| 136 |
-
f"Status distribution: {status_counts}"
|
| 137 |
-
)
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
"total_records": total_records,
|
| 143 |
-
"unique_devices": unique_devices,
|
| 144 |
-
"avg_usage_hours": float(avg_usage_hours),
|
| 145 |
-
"total_downtime": float(total_downtime),
|
| 146 |
-
"status_counts": status_counts
|
| 147 |
-
}
|
| 148 |
-
}
|
| 149 |
except Exception as e:
|
| 150 |
-
logging.error(f"
|
| 151 |
-
return
|
| 152 |
|
| 153 |
-
# Main endpoint to fetch and summarize logs
|
| 154 |
@app.route('/summarize', methods=['POST'])
|
| 155 |
-
def
|
| 156 |
-
if not connect_to_salesforce():
|
| 157 |
-
return jsonify({"error": "Failed to connect to Salesforce"}), 500
|
| 158 |
-
|
| 159 |
try:
|
| 160 |
-
data = request.
|
| 161 |
-
lab_site = data.get(
|
| 162 |
-
start_date = data.get(
|
| 163 |
-
end_date = data.get(
|
| 164 |
-
equipment_type = data.get(
|
| 165 |
|
| 166 |
df = fetch_smartlog_records(lab_site, start_date, end_date, equipment_type)
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
except Exception as e:
|
| 170 |
-
logging.error(f"
|
| 171 |
return jsonify({"error": str(e)}), 500
|
| 172 |
|
| 173 |
if __name__ == '__main__':
|
| 174 |
-
|
|
|
|
|
|
| 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 |
+
import time
|
| 11 |
import requests
|
| 12 |
from requests.exceptions import Timeout
|
|
|
|
| 13 |
|
| 14 |
+
# Configure logging
|
| 15 |
logging.basicConfig(
|
| 16 |
level=logging.INFO,
|
| 17 |
format='%(asctime)s - %(levelname)s - %(message)s',
|
|
|
|
| 24 |
# Initialize Flask app
|
| 25 |
app = Flask(__name__)
|
| 26 |
|
| 27 |
+
# Salesforce credentials
|
| 28 |
+
SF_USERNAME = os.getenv('SF_USERNAME', 'your_username')
|
| 29 |
+
SF_PASSWORD = os.getenv('SF_PASSWORD', 'your_password')
|
| 30 |
+
SF_SECURITY_TOKEN = os.getenv('SF_SECURITY_TOKEN', 'your_token')
|
| 31 |
SF_INSTANCE_URL = os.getenv('SF_INSTANCE_URL', 'https://login.salesforce.com')
|
| 32 |
|
|
|
|
| 33 |
sf = None
|
| 34 |
+
summarizer = None
|
| 35 |
|
|
|
|
| 36 |
@app.route('/health', methods=['GET'])
|
| 37 |
def health_check():
|
| 38 |
+
return jsonify({"status": "App is running"}), 200
|
| 39 |
|
|
|
|
| 40 |
def connect_to_salesforce():
|
| 41 |
global sf
|
| 42 |
+
logging.info("Connecting to Salesforce...")
|
|
|
|
| 43 |
try:
|
| 44 |
session = requests.Session()
|
| 45 |
adapter = requests.adapters.HTTPAdapter(max_retries=3)
|
|
|
|
| 53 |
instance_url=SF_INSTANCE_URL,
|
| 54 |
session=session
|
| 55 |
)
|
| 56 |
+
logging.info("Connected to Salesforce successfully.")
|
|
|
|
| 57 |
except Timeout:
|
| 58 |
+
logging.error("Salesforce connection timed out.")
|
| 59 |
sf = None
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
+
logging.error(f"Salesforce connection error: {e}")
|
| 62 |
sf = None
|
|
|
|
| 63 |
|
| 64 |
+
def load_huggingface_model():
|
| 65 |
+
global summarizer
|
| 66 |
+
if summarizer is None:
|
| 67 |
+
logging.info("Loading Hugging Face model...")
|
| 68 |
+
try:
|
| 69 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 70 |
+
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6", device=device)
|
| 71 |
+
logging.info("Model loaded.")
|
| 72 |
+
except Exception as e:
|
| 73 |
+
logging.error(f"Model load error: {e}")
|
| 74 |
+
summarizer = None
|
| 75 |
+
|
| 76 |
def fetch_smartlog_records(lab_site=None, start_date=None, end_date=None, equipment_type=None):
|
| 77 |
if sf is None:
|
| 78 |
+
raise Exception("Salesforce connection not established.")
|
| 79 |
+
|
| 80 |
try:
|
| 81 |
logging.info("Fetching SmartLog records...")
|
| 82 |
+
query = """
|
| 83 |
+
SELECT Device_Id__c, Log_Type__c, Status__c, Timestamp__c,
|
| 84 |
+
Usage_Hours__c, Downtime__c, AMC_Date__c
|
| 85 |
+
FROM SmartLog__c
|
| 86 |
+
"""
|
| 87 |
conditions = []
|
|
|
|
| 88 |
if lab_site:
|
| 89 |
+
conditions.append(f"Lab_Site__c = '{lab_site}'")
|
|
|
|
| 90 |
if start_date:
|
| 91 |
+
conditions.append(f"Timestamp__c >= {start_date}")
|
|
|
|
| 92 |
if end_date:
|
| 93 |
+
conditions.append(f"Timestamp__c <= {end_date}")
|
|
|
|
| 94 |
if equipment_type:
|
| 95 |
+
conditions.append(f"Log_Type__c = '{equipment_type}'")
|
|
|
|
| 96 |
|
| 97 |
if conditions:
|
| 98 |
query += " WHERE " + " AND ".join(conditions)
|
| 99 |
|
| 100 |
+
result = sf.query_all(query)
|
| 101 |
records = result['records']
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
data = [{
|
| 103 |
'device_id': r['Device_Id__c'],
|
| 104 |
'log_type': r['Log_Type__c'],
|
| 105 |
'status': r['Status__c'],
|
| 106 |
'timestamp': r['Timestamp__c'],
|
| 107 |
+
'usage_hours': r['Usage_Hours__c'],
|
| 108 |
+
'downtime': r['Downtime__c'],
|
| 109 |
'amc_date': r['AMC_Date__c']
|
| 110 |
} for r in records]
|
| 111 |
+
|
| 112 |
df = pd.DataFrame(data)
|
| 113 |
df['timestamp'] = pd.to_datetime(df['timestamp'], errors='coerce')
|
| 114 |
df['amc_date'] = pd.to_datetime(df['amc_date'], errors='coerce')
|
|
|
|
| 115 |
return df
|
| 116 |
+
|
| 117 |
except Exception as e:
|
| 118 |
+
logging.error(f"Error fetching records: {e}")
|
| 119 |
+
raise e
|
| 120 |
|
|
|
|
| 121 |
def summarize_logs(df):
|
| 122 |
+
load_huggingface_model()
|
| 123 |
+
if summarizer is None:
|
| 124 |
+
return "Model not available"
|
| 125 |
+
|
| 126 |
+
text = ""
|
| 127 |
+
for _, row in df.iterrows():
|
| 128 |
+
text += f"Device {row['device_id']} had status {row['status']} on {row['timestamp'].strftime('%Y-%m-%d')}.\n"
|
| 129 |
+
|
| 130 |
+
if len(text) < 20:
|
| 131 |
+
return "Not enough log data for summarization."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
+
try:
|
| 134 |
+
summary = summarizer(text[:1024], max_length=100, min_length=30, do_sample=False)[0]['summary_text']
|
| 135 |
+
return summary
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
except Exception as e:
|
| 137 |
+
logging.error(f"Summarization failed: {e}")
|
| 138 |
+
return "Error summarizing logs"
|
| 139 |
|
|
|
|
| 140 |
@app.route('/summarize', methods=['POST'])
|
| 141 |
+
def summarize_endpoint():
|
|
|
|
|
|
|
|
|
|
| 142 |
try:
|
| 143 |
+
data = request.json
|
| 144 |
+
lab_site = data.get("lab_site")
|
| 145 |
+
start_date = data.get("start_date")
|
| 146 |
+
end_date = data.get("end_date")
|
| 147 |
+
equipment_type = data.get("equipment_type")
|
| 148 |
|
| 149 |
df = fetch_smartlog_records(lab_site, start_date, end_date, equipment_type)
|
| 150 |
+
summary = summarize_logs(df)
|
| 151 |
+
|
| 152 |
+
return jsonify({
|
| 153 |
+
"summary": summary,
|
| 154 |
+
"records_fetched": len(df)
|
| 155 |
+
})
|
| 156 |
+
|
| 157 |
except Exception as e:
|
| 158 |
+
logging.error(f"API error: {e}")
|
| 159 |
return jsonify({"error": str(e)}), 500
|
| 160 |
|
| 161 |
if __name__ == '__main__':
|
| 162 |
+
connect_to_salesforce()
|
| 163 |
+
app.run(host='0.0.0.0', port=5000)
|