zainulabedin949 commited on
Commit
ae177e0
·
verified ·
1 Parent(s): 9236793

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -85
app.py CHANGED
@@ -5,7 +5,6 @@ import librosa
5
  import soundfile as sf
6
  from transformers import AutoFeatureExtractor, AutoModelForAudioClassification
7
  import matplotlib.pyplot as plt
8
- from matplotlib.colors import Normalize
9
  import tempfile
10
  import os
11
 
@@ -14,63 +13,62 @@ SAMPLING_RATE = 16000
14
  MODEL_NAME = "MIT/ast-finetuned-audioset-10-10-0.4593"
15
  DEFAULT_THRESHOLD = 0.7
16
 
17
- # Load model and feature extractor
18
- feature_extractor = AutoFeatureExtractor.from_pretrained(MODEL_NAME)
19
- model = AutoModelForAudioClassification.from_pretrained(MODEL_NAME)
 
 
 
20
 
21
  # Equipment knowledge base
22
  EQUIPMENT_RECOMMENDATIONS = {
23
  "bearing": {
24
- "high_frequency": "Recommend bearing replacement. High-frequency noise indicates wear or lubrication issues.",
25
- "low_frequency": "Check for improper installation or contamination in bearings.",
26
- "irregular": "Possible bearing cage damage. Schedule vibration analysis."
27
  },
28
  "pump": {
29
- "cavitation": "Pump cavitation detected. Check suction conditions and NPSH.",
30
- "impeller": "Impeller damage likely. Inspect and balance if needed.",
31
- "misalignment": "Misalignment detected. Perform laser shaft alignment."
32
  },
33
  "motor": {
34
- "electrical": "Electrical fault suspected. Check windings and connections.",
35
- "mechanical": "Mechanical imbalance detected. Perform dynamic balancing.",
36
- "bearing": "Motor bearing wear detected. Schedule replacement."
37
- },
38
- "compressor": {
39
- "valve": "Compressor valve leakage suspected. Perform valve test.",
40
- "pulsation": "Pulsation issues detected. Check dampeners and piping.",
41
- "surge": "Compressor surge condition. Review control settings."
42
  }
43
  }
44
 
45
  def analyze_frequency_patterns(audio, sr):
46
  """Analyze frequency patterns to identify potential issues"""
47
  patterns = []
 
48
 
49
  # Spectral analysis
50
  spectral_centroid = librosa.feature.spectral_centroid(y=audio, sr=sr)[0]
51
  spectral_rolloff = librosa.feature.spectral_rolloff(y=audio, sr=sr)[0]
52
 
53
- mean_centroid = np.mean(spectral_centroid)
54
- mean_rolloff = np.mean(spectral_rolloff)
55
 
56
- if mean_centroid > 3000: # High frequency components
57
  patterns.append("high_frequency")
58
- elif mean_centroid < 1000: # Low frequency components
59
  patterns.append("low_frequency")
60
 
61
- if mean_rolloff > 8000: # Rich in harmonics
62
  patterns.append("harmonic_rich")
63
-
64
- return patterns
65
 
66
  def generate_recommendation(prediction, confidence, audio, sr):
67
  """Generate maintenance recommendations based on analysis"""
68
  if prediction == "Normal":
69
- return "No immediate action required. Equipment operating within normal parameters."
70
 
71
- patterns = analyze_frequency_patterns(audio, sr)
72
 
73
- # Simple equipment type classifier based on frequency profile
74
  spectral_flatness = librosa.feature.spectral_flatness(y=audio)[0]
75
  mean_flatness = np.mean(spectral_flatness)
76
 
@@ -79,78 +77,77 @@ def generate_recommendation(prediction, confidence, audio, sr):
79
  elif 0.2 <= mean_flatness < 0.6:
80
  equipment_type = "pump"
81
  else:
82
- equipment_type = "motor" if np.mean(audio) < 0.1 else "compressor"
83
 
84
- # Generate specific recommendations
85
- recommendations = ["🔧 Maintenance Recommendations:"]
86
- recommendations.append(f"Detected issues in {equipment_type} with {confidence:.1%} confidence")
 
 
 
 
87
 
88
  for pattern in patterns:
89
  if pattern in EQUIPMENT_RECOMMENDATIONS.get(equipment_type, {}):
90
- recommendations.append(f"→ {EQUIPMENT_RECOMMENDATIONS[equipment_type][pattern]}")
91
 
92
- # General recommendations
93
  if prediction == "Anomaly":
94
- recommendations.append("\n🛠️ Suggested Actions:")
95
- recommendations.append("1. Isolate equipment if possible")
96
- recommendations.append("2. Perform visual inspection")
97
- recommendations.append("3. Schedule detailed diagnostics")
98
- recommendations.append(f"4. Review last maintenance records ({equipment_type})")
 
 
99
 
100
  if confidence > 0.8:
101
- recommendations.append("\n🚨 Urgent: High confidence abnormality detected. Recommend immediate inspection!")
102
-
103
  return "\n".join(recommendations)
104
 
 
 
 
 
 
 
 
 
105
  def analyze_audio(audio_input, threshold=DEFAULT_THRESHOLD):
106
- """Process audio and detect anomalies"""
107
  try:
108
  # Handle file upload
109
  if isinstance(audio_input, str):
110
- audio, sr = sf.read(audio_input)
111
- else: # Gradio file object
112
  with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as tmp:
113
  tmp.write(audio_input.read())
114
  tmp_path = tmp.name
115
- audio, sr = sf.read(tmp_path)
116
  os.unlink(tmp_path)
117
 
118
- # Convert to mono and resample if needed
119
- if len(audio.shape) > 1:
120
- audio = np.mean(audio, axis=1)
121
- if sr != SAMPLING_RATE:
122
- audio = librosa.resample(audio, orig_sr=sr, target_sr=SAMPLING_RATE)
123
-
124
- # Feature extraction and prediction
125
  inputs = feature_extractor(audio, sampling_rate=SAMPLING_RATE, return_tensors="pt")
126
  with torch.no_grad():
127
  outputs = model(**inputs)
128
  probs = torch.softmax(outputs.logits, dim=-1)
129
 
130
- # Get results
131
  predicted_class = "Normal" if probs[0][0] > threshold else "Anomaly"
132
  confidence = probs[0][0].item() if predicted_class == "Normal" else 1 - probs[0][0].item()
133
 
134
- # Generate spectrogram
135
- spectrogram = librosa.feature.melspectrogram(y=audio, sr=SAMPLING_RATE, n_mels=64, fmax=8000)
136
- db_spec = librosa.power_to_db(spectrogram, ref=np.max)
137
-
138
- fig, ax = plt.subplots(figsize=(10, 4))
139
- librosa.display.specshow(db_spec, x_axis='time', y_axis='mel', sr=SAMPLING_RATE, fmax=8000, ax=ax)
140
  plt.colorbar(format='%+2.0f dB')
141
- plt.title('Mel Spectrogram with Anomaly Detection')
142
-
143
- # Mark anomalies on plot
144
- if predicted_class == "Anomaly":
145
- plt.text(0.5, 0.9, 'ANOMALY DETECTED', color='red',
146
- ha='center', va='center', transform=ax.transAxes,
147
- fontsize=14, bbox=dict(facecolor='white', alpha=0.8))
148
 
149
  spec_path = os.path.join(tempfile.gettempdir(), 'spec.png')
150
  plt.savefig(spec_path, bbox_inches='tight')
151
  plt.close()
152
 
153
- # Generate detailed recommendations
154
  recommendations = generate_recommendation(predicted_class, confidence, audio, SAMPLING_RATE)
155
 
156
  return (
@@ -159,36 +156,35 @@ def analyze_audio(audio_input, threshold=DEFAULT_THRESHOLD):
159
  spec_path,
160
  recommendations
161
  )
162
-
163
  except Exception as e:
164
  return f"Error: {str(e)}", "", None, ""
165
 
166
  # Gradio Interface
167
- with gr.Blocks(title="Industrial Diagnostic Assistant 👨��🔧", theme=gr.themes.Soft()) as demo:
168
  gr.Markdown("""
169
- # 🏭 Industrial Equipment Diagnostic Assistant
170
- ## Acoustic Anomaly Detection & Maintenance Recommendation System
171
  """)
172
 
173
  with gr.Row():
174
  with gr.Column():
175
  audio_input = gr.Audio(
176
- label="Upload Equipment Recording (.wav)",
177
- type="filepath",
178
- source="upload"
179
  )
180
  threshold = gr.Slider(
181
  minimum=0.5, maximum=0.95, step=0.05, value=DEFAULT_THRESHOLD,
182
- label="Detection Sensitivity", interactive=True
183
  )
184
  analyze_btn = gr.Button("🔍 Analyze & Diagnose", variant="primary")
185
 
186
  with gr.Column():
187
  result_label = gr.Label(label="Diagnosis Result")
188
  confidence = gr.Textbox(label="Confidence Score")
189
- spectrogram = gr.Image(label="Acoustic Analysis")
190
  recommendations = gr.Textbox(
191
- label="Maintenance Recommendations",
192
  lines=10,
193
  interactive=False
194
  )
@@ -200,15 +196,14 @@ with gr.Blocks(title="Industrial Diagnostic Assistant 👨‍🔧", theme=gr.the
200
  )
201
 
202
  gr.Markdown("""
203
- ### System Capabilities:
204
- - Automatic anomaly detection in industrial equipment sounds
205
- - Frequency pattern analysis to identify failure modes
206
- - Equipment-specific maintenance recommendations
207
- - Confidence-based urgency classification
208
-
209
- **Tip:** For best results, use 5-10 second recordings of steady operation
210
  """)
211
 
212
  if __name__ == "__main__":
213
  demo.launch()
214
-
 
5
  import soundfile as sf
6
  from transformers import AutoFeatureExtractor, AutoModelForAudioClassification
7
  import matplotlib.pyplot as plt
 
8
  import tempfile
9
  import os
10
 
 
13
  MODEL_NAME = "MIT/ast-finetuned-audioset-10-10-0.4593"
14
  DEFAULT_THRESHOLD = 0.7
15
 
16
+ # Load model components
17
+ try:
18
+ feature_extractor = AutoFeatureExtractor.from_pretrained(MODEL_NAME)
19
+ model = AutoModelForAudioClassification.from_pretrained(MODEL_NAME)
20
+ except Exception as e:
21
+ print(f"Error loading model: {str(e)}")
22
 
23
  # Equipment knowledge base
24
  EQUIPMENT_RECOMMENDATIONS = {
25
  "bearing": {
26
+ "high_frequency": " Replace bearings immediately\n• Check lubrication system\n• Monitor vibration levels",
27
+ "low_frequency": " Inspect bearing installation\n• Check for contamination\n• Verify lubrication",
28
+ "irregular": " Perform vibration analysis\n• Schedule bearing replacement\n• Check alignment"
29
  },
30
  "pump": {
31
+ "cavitation": " Check NPSH available\n• Inspect suction strainer\n• Adjust operating speed",
32
+ "impeller": " Inspect impeller for damage\n• Perform dynamic balancing\n• Check wear rings",
33
+ "misalignment": " Perform laser alignment\n• Check coupling condition\n• Verify baseplate level"
34
  },
35
  "motor": {
36
+ "electrical": " Megger test windings\n• Check connections\n• Inspect starter contacts",
37
+ "mechanical": " Perform dynamic balancing\n• Check alignment\n• Inspect cooling fins",
38
+ "bearing": " Replace motor bearings\n• Check lubrication\n• Monitor temperature"
 
 
 
 
 
39
  }
40
  }
41
 
42
  def analyze_frequency_patterns(audio, sr):
43
  """Analyze frequency patterns to identify potential issues"""
44
  patterns = []
45
+ features = {}
46
 
47
  # Spectral analysis
48
  spectral_centroid = librosa.feature.spectral_centroid(y=audio, sr=sr)[0]
49
  spectral_rolloff = librosa.feature.spectral_rolloff(y=audio, sr=sr)[0]
50
 
51
+ features['centroid_mean'] = np.mean(spectral_centroid)
52
+ features['rolloff_mean'] = np.mean(spectral_rolloff)
53
 
54
+ if features['centroid_mean'] > 3000:
55
  patterns.append("high_frequency")
56
+ elif features['centroid_mean'] < 1000:
57
  patterns.append("low_frequency")
58
 
59
+ if features['rolloff_mean'] > 8000:
60
  patterns.append("harmonic_rich")
61
+
62
+ return patterns, features
63
 
64
  def generate_recommendation(prediction, confidence, audio, sr):
65
  """Generate maintenance recommendations based on analysis"""
66
  if prediction == "Normal":
67
+ return "No immediate action required. Equipment operating within normal parameters."
68
 
69
+ patterns, features = analyze_frequency_patterns(audio, sr)
70
 
71
+ # Equipment classification
72
  spectral_flatness = librosa.feature.spectral_flatness(y=audio)[0]
73
  mean_flatness = np.mean(spectral_flatness)
74
 
 
77
  elif 0.2 <= mean_flatness < 0.6:
78
  equipment_type = "pump"
79
  else:
80
+ equipment_type = "motor"
81
 
82
+ # Generate recommendations
83
+ recommendations = [
84
+ "🔧 MAINTENANCE RECOMMENDATIONS",
85
+ f"Equipment Type: {equipment_type.upper()}",
86
+ f"Confidence: {confidence:.1%}",
87
+ ""
88
+ ]
89
 
90
  for pattern in patterns:
91
  if pattern in EQUIPMENT_RECOMMENDATIONS.get(equipment_type, {}):
92
+ recommendations.append(EQUIPMENT_RECOMMENDATIONS[equipment_type][pattern])
93
 
 
94
  if prediction == "Anomaly":
95
+ recommendations.extend([
96
+ "",
97
+ "🛠️ GENERAL ACTIONS:",
98
+ "1. Isolate equipment if possible",
99
+ "2. Perform visual inspection",
100
+ "3. Schedule detailed diagnostics",
101
+ ])
102
 
103
  if confidence > 0.8:
104
+ recommendations.append("\n🚨 URGENT: High-confidence abnormality detected!")
105
+
106
  return "\n".join(recommendations)
107
 
108
+ def process_audio(file_path):
109
+ """Handle audio file processing"""
110
+ try:
111
+ audio, sr = librosa.load(file_path, sr=SAMPLING_RATE, mono=True)
112
+ return audio, sr
113
+ except Exception as e:
114
+ raise RuntimeError(f"Audio processing error: {str(e)}")
115
+
116
  def analyze_audio(audio_input, threshold=DEFAULT_THRESHOLD):
117
+ """Main analysis function"""
118
  try:
119
  # Handle file upload
120
  if isinstance(audio_input, str):
121
+ audio, sr = process_audio(audio_input)
122
+ else: # Handle file object
123
  with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as tmp:
124
  tmp.write(audio_input.read())
125
  tmp_path = tmp.name
126
+ audio, sr = process_audio(tmp_path)
127
  os.unlink(tmp_path)
128
 
129
+ # Model prediction
 
 
 
 
 
 
130
  inputs = feature_extractor(audio, sampling_rate=SAMPLING_RATE, return_tensors="pt")
131
  with torch.no_grad():
132
  outputs = model(**inputs)
133
  probs = torch.softmax(outputs.logits, dim=-1)
134
 
 
135
  predicted_class = "Normal" if probs[0][0] > threshold else "Anomaly"
136
  confidence = probs[0][0].item() if predicted_class == "Normal" else 1 - probs[0][0].item()
137
 
138
+ # Generate visualization
139
+ plt.figure(figsize=(10, 4))
140
+ S = librosa.feature.melspectrogram(y=audio, sr=SAMPLING_RATE, n_mels=64)
141
+ S_db = librosa.power_to_db(S, ref=np.max)
142
+ librosa.display.specshow(S_db, x_axis='time', y_axis='mel', sr=SAMPLING_RATE, fmax=8000)
 
143
  plt.colorbar(format='%+2.0f dB')
144
+ plt.title('Mel Spectrogram')
 
 
 
 
 
 
145
 
146
  spec_path = os.path.join(tempfile.gettempdir(), 'spec.png')
147
  plt.savefig(spec_path, bbox_inches='tight')
148
  plt.close()
149
 
150
+ # Generate recommendations
151
  recommendations = generate_recommendation(predicted_class, confidence, audio, SAMPLING_RATE)
152
 
153
  return (
 
156
  spec_path,
157
  recommendations
158
  )
159
+
160
  except Exception as e:
161
  return f"Error: {str(e)}", "", None, ""
162
 
163
  # Gradio Interface
164
+ with gr.Blocks(title="Industrial Audio Analyzer", theme=gr.themes.Soft()) as demo:
165
  gr.Markdown("""
166
+ # 🏭 Industrial Equipment Sound Analyzer
167
+ ### Acoustic Anomaly Detection & Maintenance Recommendation System
168
  """)
169
 
170
  with gr.Row():
171
  with gr.Column():
172
  audio_input = gr.Audio(
173
+ label="Upload Equipment Audio (.wav)",
174
+ type="filepath"
 
175
  )
176
  threshold = gr.Slider(
177
  minimum=0.5, maximum=0.95, step=0.05, value=DEFAULT_THRESHOLD,
178
+ label="Detection Sensitivity"
179
  )
180
  analyze_btn = gr.Button("🔍 Analyze & Diagnose", variant="primary")
181
 
182
  with gr.Column():
183
  result_label = gr.Label(label="Diagnosis Result")
184
  confidence = gr.Textbox(label="Confidence Score")
185
+ spectrogram = gr.Image(label="Spectrogram Analysis")
186
  recommendations = gr.Textbox(
187
+ label="Maintenance Recommendations",
188
  lines=10,
189
  interactive=False
190
  )
 
196
  )
197
 
198
  gr.Markdown("""
199
+ **Instructions:**
200
+ - Upload 5-10 second .wav recordings
201
+ - Results include:
202
+ Anomaly detection
203
+ Equipment classification
204
+ ✓ Maintenance recommendations
205
+ Spectrogram visualization
206
  """)
207
 
208
  if __name__ == "__main__":
209
  demo.launch()