File size: 8,646 Bytes
bfe1eb1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | import os
import sqlite3
import json
import time
import random
import requests
import pandas as pd
import numpy as np
from flask import Flask, render_template, jsonify, request
app = Flask(__name__)
app.config['SECRET_KEY'] = 'dev-secret-key'
app.config['DATABASE'] = os.path.join(app.instance_path, 'maintenance.db')
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max upload size
# Ensure instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
SILICONFLOW_API_KEY = "sk-vimuseiptfbomzegyuvmebjzooncsqbyjtlddrfodzcdskgi"
SILICONFLOW_API_URL = "https://api.siliconflow.cn/v1/chat/completions"
def get_db():
conn = sqlite3.connect(app.config['DATABASE'])
conn.row_factory = sqlite3.Row
return conn
def init_db():
conn = get_db()
c = conn.cursor()
# Assets Table
c.execute('''
CREATE TABLE IF NOT EXISTS assets (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
type TEXT NOT NULL,
location TEXT,
status TEXT DEFAULT 'operational',
health_score INTEGER DEFAULT 100,
last_maintenance DATE
)
''')
# Anomalies Table
c.execute('''
CREATE TABLE IF NOT EXISTS anomalies (
id INTEGER PRIMARY KEY AUTOINCREMENT,
asset_id INTEGER,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
metric TEXT,
value REAL,
threshold REAL,
severity TEXT,
diagnosis TEXT,
recommendation TEXT,
status TEXT DEFAULT 'new',
FOREIGN KEY (asset_id) REFERENCES assets (id)
)
''')
# Seed Data
c.execute('SELECT count(*) FROM assets')
if c.fetchone()[0] == 0:
assets = [
('CNC-Milling-01', 'CNC Machine', 'Zone A', 'operational', 95, '2025-01-15'),
('Hydraulic-Pump-04', 'Pump', 'Zone B', 'warning', 78, '2024-12-10'),
('Conveyor-Belt-Main', 'Conveyor', 'Zone A', 'operational', 98, '2025-02-01'),
('Robot-Arm-Welder', 'Robot', 'Zone C', 'critical', 45, '2024-11-20')
]
c.executemany('INSERT INTO assets (name, type, location, status, health_score, last_maintenance) VALUES (?,?,?,?,?,?)', assets)
conn.commit()
conn.commit()
conn.close()
init_db()
# --- Helpers ---
def call_siliconflow(prompt, system_prompt="You are an expert Industrial AI assistant."):
headers = {
"Authorization": f"Bearer {SILICONFLOW_API_KEY}",
"Content-Type": "application/json"
}
payload = {
"model": "Qwen/Qwen2.5-7B-Instruct",
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt}
],
"temperature": 0.7
}
try:
response = requests.post(SILICONFLOW_API_URL, json=payload, headers=headers, timeout=30)
response.raise_for_status()
return response.json()['choices'][0]['message']['content']
except Exception as e:
print(f"AI Error: {e}")
return f"Error generating diagnosis: {str(e)}"
# --- Routes ---
@app.route('/')
def index():
return render_template('index.html')
@app.route('/api/assets', methods=['GET'])
def get_assets():
conn = get_db()
assets = conn.execute('SELECT * FROM assets').fetchall()
conn.close()
return jsonify([dict(a) for a in assets])
@app.route('/api/sensor_data/<int:asset_id>', methods=['GET'])
def get_sensor_data(asset_id):
# Mock sensor data generation based on asset health
conn = get_db()
asset = conn.execute('SELECT * FROM assets WHERE id = ?', (asset_id,)).fetchone()
conn.close()
if not asset:
return jsonify({'error': 'Asset not found'}), 404
# Generate 50 points
now = time.time()
timestamps = []
vibration = []
temperature = []
base_vib = 2.0 if asset['health_score'] > 80 else (5.0 if asset['health_score'] > 50 else 8.0)
base_temp = 45.0 if asset['health_score'] > 80 else (65.0 if asset['health_score'] > 50 else 85.0)
for i in range(50):
t = now - (50 - i) * 60 # Past 50 minutes
timestamps.append(time.strftime('%H:%M', time.localtime(t)))
# Add noise and occasional spikes
vib_noise = np.random.normal(0, 0.5)
temp_noise = np.random.normal(0, 2.0)
v = base_vib + vib_noise
tm = base_temp + temp_noise
if asset['status'] in ['warning', 'critical'] and i > 40:
# Recent spike
v += random.uniform(2.0, 5.0)
tm += random.uniform(5.0, 10.0)
vibration.append(round(max(0, v), 2))
temperature.append(round(max(20, tm), 2))
return jsonify({
'timestamps': timestamps,
'vibration': vibration,
'temperature': temperature,
'asset_name': asset['name']
})
@app.route('/api/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
if file:
try:
# Simple mock processing of uploaded file (e.g. historical data)
# In a real app, we would parse CSV/JSON and update DB
filename = file.filename
return jsonify({'status': 'success', 'message': f'File {filename} uploaded and processed successfully'})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/anomalies', methods=['GET', 'POST'])
def handle_anomalies():
conn = get_db()
if request.method == 'POST':
data = request.json
conn.execute('''
INSERT INTO anomalies (asset_id, metric, value, threshold, severity, status)
VALUES (?, ?, ?, ?, ?, 'new')
''', (data['asset_id'], data['metric'], data['value'], data['threshold'], data['severity']))
conn.commit()
conn.close()
return jsonify({'status': 'recorded'})
else:
anomalies = conn.execute('''
SELECT a.*, asst.name as asset_name
FROM anomalies a
JOIN assets asst ON a.asset_id = asst.id
ORDER BY a.timestamp DESC
''').fetchall()
conn.close()
return jsonify([dict(a) for a in anomalies])
@app.route('/api/diagnose', methods=['POST'])
def diagnose_anomaly():
data = request.json
anomaly_id = data.get('id')
asset_context = data.get('context', {})
# Construct prompt
prompt = f"""
Analyze the following industrial equipment anomaly and provide a diagnosis and maintenance recommendation.
Asset: {asset_context.get('name')} ({asset_context.get('type')})
Metric: {asset_context.get('metric')}
Current Value: {asset_context.get('value')}
Threshold: {asset_context.get('threshold')}
Severity: {asset_context.get('severity')}
Please provide:
1. Potential Root Cause (Diagnosis)
2. Recommended Action (Maintenance Plan)
3. Estimated Urgency (High/Medium/Low)
Return the response in JSON format with keys: 'diagnosis', 'recommendation', 'urgency'.
ENSURE THE VALUES ARE IN CHINESE (SIMPLIFIED).
"""
system_prompt = "You are an expert Industrial Maintenance Engineer AI. Output strictly valid JSON. Use Chinese for diagnosis and recommendation."
ai_response = call_siliconflow(prompt, system_prompt)
# Parse JSON from AI (cleanup if needed)
try:
# Strip code blocks if present
if "```json" in ai_response:
ai_response = ai_response.split("```json")[1].split("```")[0].strip()
elif "```" in ai_response:
ai_response = ai_response.split("```")[1].split("```")[0].strip()
result = json.loads(ai_response)
# Update DB
if anomaly_id:
conn = get_db()
conn.execute('''
UPDATE anomalies
SET diagnosis = ?, recommendation = ?, status = 'diagnosed'
WHERE id = ?
''', (result.get('diagnosis'), result.get('recommendation'), anomaly_id))
conn.commit()
conn.close()
return jsonify(result)
except Exception as e:
print(f"Parse Error: {e}, Response: {ai_response}")
return jsonify({'error': 'Failed to parse AI response', 'raw': ai_response}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860, debug=True)
|