File size: 11,382 Bytes
dd93e44 e1e4345 3812759 dd93e44 |
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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
"""
ASR Audio Analysis API Server
Enterprise-grade REST API for audio processing:
- Diarization (stereo/mono)
- Whisper Transcription
- Professional Audio Analysis
"""
import os
import json
import uuid
import threading
from pathlib import Path
from datetime import datetime
from flask import Flask, jsonify, send_from_directory, request
from flask_cors import CORS
from werkzeug.utils import secure_filename
app = Flask(__name__)
CORS(app)
# Configuration
BASE_DIR = Path(os.environ.get("APP_DIR", "/app"))
OUTPUT_FOLDER = BASE_DIR / "output"
UPLOAD_FOLDER = BASE_DIR / "uploads"
WHISPER_MODEL = os.environ.get("WHISPER_MODEL", "ramalMr/whisper-small-az")
ALLOWED_EXTENSIONS = {'wav', 'mp3', 'm4a', 'flac', 'ogg', 'opus', 'webm'}
# Job tracking
processing_jobs = {}
job_lock = threading.Lock()
# Create folders
OUTPUT_FOLDER.mkdir(exist_ok=True)
UPLOAD_FOLDER.mkdir(exist_ok=True)
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def process_audio_file(job_id, audio_path, output_dir):
"""Process audio: diarization + transcription + analysis"""
try:
with job_lock:
processing_jobs[job_id]['status'] = 'processing'
processing_jobs[job_id]['stage'] = 'initializing'
from stereo_diarizer import StereoCallDiarizer
from whisper_transcriber import WhisperTranscriber
from audio_analyzer import AudioAnalyzer
# Step 1: Diarization
with job_lock:
processing_jobs[job_id]['stage'] = 'diarization'
diarizer = StereoCallDiarizer(str(audio_path), verbose=False)
diarizer.load_audio()
with job_lock:
processing_jobs[job_id]['is_stereo'] = diarizer.is_stereo
left_seg, right_seg = diarizer.detect_speech_segments()
diarizer.create_timeline(left_seg, right_seg)
segment_files = diarizer.export_segments(str(output_dir))
diarizer.export_full_speakers(str(output_dir))
diarizer.export_transcript_txt(str(output_dir))
diarizer.export_transcript_json(str(output_dir))
# Step 2: Transcription
with job_lock:
processing_jobs[job_id]['stage'] = 'transcription'
whisper = WhisperTranscriber(WHISPER_MODEL, device="cpu", verbose=False)
transcribed = whisper.transcribe_segments(segment_files, diarizer.timeline)
whisper.export_transcription(transcribed, str(output_dir))
# Step 3: Audio Analysis
with job_lock:
processing_jobs[job_id]['stage'] = 'audio_analysis'
analyzer = AudioAnalyzer(verbose=False)
analysis = analyzer.analyze_call(
segment_files=segment_files,
timeline=diarizer.timeline,
call_id=output_dir.name,
is_stereo=diarizer.is_stereo
)
analyzer.export_analysis(analysis, str(output_dir))
# Success
with job_lock:
processing_jobs[job_id]['status'] = 'completed'
processing_jobs[job_id]['stage'] = 'done'
processing_jobs[job_id]['result'] = {
'call_name': output_dir.name,
'is_stereo': diarizer.is_stereo,
'quality_score': analysis.overall_quality_score
}
except Exception as e:
with job_lock:
processing_jobs[job_id]['status'] = 'failed'
processing_jobs[job_id]['error'] = str(e)
@app.route('/')
def index():
return send_from_directory('.', 'dashboard.html')
@app.route('/api/calls')
def get_calls():
try:
output_path = Path(OUTPUT_FOLDER)
if not output_path.exists():
return jsonify([])
calls = []
for item in output_path.iterdir():
if item.is_dir():
analysis_file = item / 'audio_analysis.json'
if analysis_file.exists():
calls.append(item.name)
calls.sort(reverse=True)
return jsonify(calls)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/analysis/<call_name>')
def get_analysis(call_name):
try:
call_path = Path(OUTPUT_FOLDER) / call_name
if not call_path.exists():
return jsonify({'error': 'Call not found'}), 404
# Load audio analysis
analysis_file = call_path / 'audio_analysis.json'
if not analysis_file.exists():
return jsonify({'error': 'Analysis not found'}), 404
with open(analysis_file, 'r', encoding='utf-8') as f:
analysis = json.load(f)
# Load transcription
transcription = None
trans_file = call_path / 'transcription.json'
if trans_file.exists():
with open(trans_file, 'r', encoding='utf-8') as f:
transcription = json.load(f)
# Load metadata
stats = None
stats_file = call_path / 'transcript.json'
if stats_file.exists():
with open(stats_file, 'r', encoding='utf-8') as f:
data = json.load(f)
stats = data.get('metadata')
return jsonify({
'call_name': call_name,
'analysis': analysis,
'transcription': transcription,
'statistics': stats
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/audio/<call_name>/<filename>')
def get_audio(call_name, filename):
try:
call_path = Path(OUTPUT_FOLDER) / call_name
return send_from_directory(call_path, filename)
except Exception as e:
return jsonify({'error': str(e)}), 404
@app.route('/api/statistics')
def get_statistics():
try:
output_path = Path(OUTPUT_FOLDER)
if not output_path.exists():
return jsonify({'error': 'Output folder not found'}), 404
stats = {
'total_calls': 0,
'stereo_calls': 0,
'mono_calls': 0,
'avg_quality_score': 0,
'avg_duration': 0,
'avg_clarity': 0,
'avg_confidence': 0,
'total_segments': 0,
'emotion_distribution': {},
'communication_styles': {}
}
quality_scores = []
durations = []
clarities = []
confidences = []
emotions = []
styles = []
for item in output_path.iterdir():
if item.is_dir():
analysis_file = item / 'audio_analysis.json'
if analysis_file.exists():
with open(analysis_file, 'r', encoding='utf-8') as f:
analysis = json.load(f)
stats['total_calls'] += 1
if analysis.get('audio_type') == 'stereo':
stats['stereo_calls'] += 1
else:
stats['mono_calls'] += 1
if analysis.get('overall_quality_score'):
quality_scores.append(float(analysis['overall_quality_score']))
if analysis.get('audio_duration'):
durations.append(float(analysis['audio_duration']))
segments = analysis.get('segments', [])
stats['total_segments'] += len(segments)
for seg in segments:
if seg.get('voice_quality', {}).get('clarity_score'):
clarities.append(float(seg['voice_quality']['clarity_score']))
if seg.get('emotion', {}).get('confidence_score'):
confidences.append(float(seg['emotion']['confidence_score']))
if seg.get('emotion', {}).get('primary_emotion'):
emotions.append(seg['emotion']['primary_emotion'])
for profile in analysis.get('speaker_profiles', {}).values():
if profile.get('communication_style'):
styles.append(profile['communication_style'])
if quality_scores:
stats['avg_quality_score'] = round(sum(quality_scores) / len(quality_scores), 1)
if durations:
stats['avg_duration'] = round(sum(durations) / len(durations), 1)
if clarities:
stats['avg_clarity'] = round(sum(clarities) / len(clarities), 1)
if confidences:
stats['avg_confidence'] = round(sum(confidences) / len(confidences), 1)
for e in set(emotions):
stats['emotion_distribution'][e] = emotions.count(e)
for s in set(styles):
stats['communication_styles'][s] = styles.count(s)
return jsonify(stats)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/upload', methods=['POST'])
def upload_file():
try:
if 'file' not in request.files:
return jsonify({'error': 'No file provided'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No file selected'}), 400
if not allowed_file(file.filename):
return jsonify({'error': f'Invalid file type. Allowed: {", ".join(ALLOWED_EXTENSIONS)}'}), 400
job_id = str(uuid.uuid4())
filename = secure_filename(file.filename)
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
unique_filename = f"{timestamp}_{filename}"
audio_path = UPLOAD_FOLDER / unique_filename
file.save(str(audio_path))
output_dir = OUTPUT_FOLDER / audio_path.stem
output_dir.mkdir(exist_ok=True)
with job_lock:
processing_jobs[job_id] = {
'job_id': job_id,
'filename': filename,
'status': 'queued',
'stage': 'pending',
'created_at': datetime.now().isoformat(),
'audio_path': str(audio_path),
'output_dir': str(output_dir),
'is_stereo': None
}
thread = threading.Thread(
target=process_audio_file,
args=(job_id, audio_path, output_dir)
)
thread.daemon = True
thread.start()
return jsonify({
'job_id': job_id,
'filename': filename,
'status': 'queued',
'message': 'File uploaded. Processing started.'
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/jobs/<job_id>')
def get_job_status(job_id):
with job_lock:
if job_id not in processing_jobs:
return jsonify({'error': 'Job not found'}), 404
job = processing_jobs[job_id].copy()
return jsonify(job)
@app.route('/api/jobs')
def get_all_jobs():
with job_lock:
jobs = list(processing_jobs.values())
return jsonify(jobs)
@app.route('/health')
def health():
return jsonify({'status': 'healthy', 'service': 'ASR Audio Intelligence Platform', 'version': '2.0'})
if __name__ == '__main__':
OUTPUT_FOLDER.mkdir(exist_ok=True)
print("="*60)
print("ASR Audio Intelligence Platform")
print("="*60)
print(f"Output: {OUTPUT_FOLDER}")
print(f"Whisper: {WHISPER_MODEL}")
print(f"Server: http://localhost:7860")
print("="*60)
app.run(host='0.0.0.0', port=7860, debug=False, threaded=True)
|