Spaces:
Sleeping
Sleeping
QnxprU69yCNg8XJ
commited on
Commit
·
b7c969b
1
Parent(s):
1928315
svvhsq
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@ import tempfile
|
|
| 3 |
import warnings
|
| 4 |
from flask import Flask, request, jsonify
|
| 5 |
from inference_service import (
|
| 6 |
-
load_hear_model,
|
| 7 |
load_classifier,
|
| 8 |
preprocess_audio,
|
| 9 |
generate_embeddings,
|
|
@@ -21,8 +20,7 @@ warnings.filterwarnings("ignore", module="librosa")
|
|
| 21 |
|
| 22 |
app = Flask(__name__)
|
| 23 |
|
| 24 |
-
# Load
|
| 25 |
-
hear_infer_fn = load_hear_model()
|
| 26 |
classifier_model = load_classifier()
|
| 27 |
|
| 28 |
@app.route('/predict_pneumonia', methods=['POST'])
|
|
@@ -39,8 +37,8 @@ def predict_pneumonia_endpoint():
|
|
| 39 |
audio_file.save(temp_audio_path)
|
| 40 |
|
| 41 |
try:
|
| 42 |
-
if
|
| 43 |
-
return jsonify({"error": "
|
| 44 |
|
| 45 |
audio_clips = preprocess_audio(
|
| 46 |
temp_audio_path, SAMPLE_RATE, CLIP_DURATION, CLIP_OVERLAP_PERCENT,
|
|
@@ -50,12 +48,18 @@ def predict_pneumonia_endpoint():
|
|
| 50 |
if audio_clips.size == 0:
|
| 51 |
return jsonify({"result": "No valid audio clips", "risk_score": None}), 200
|
| 52 |
|
| 53 |
-
embeddings
|
|
|
|
| 54 |
|
| 55 |
if embeddings.size == 0:
|
| 56 |
return jsonify({"result": "No embeddings generated", "risk_score": None}), 200
|
| 57 |
|
|
|
|
| 58 |
clip_predictions, clip_probabilities = predict_pneumonia(embeddings, classifier_model)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
final_prediction, risk_score = aggregate_predictions(clip_predictions, clip_probabilities)
|
| 60 |
|
| 61 |
return jsonify({
|
|
|
|
| 3 |
import warnings
|
| 4 |
from flask import Flask, request, jsonify
|
| 5 |
from inference_service import (
|
|
|
|
| 6 |
load_classifier,
|
| 7 |
preprocess_audio,
|
| 8 |
generate_embeddings,
|
|
|
|
| 20 |
|
| 21 |
app = Flask(__name__)
|
| 22 |
|
| 23 |
+
# Load classifier globally
|
|
|
|
| 24 |
classifier_model = load_classifier()
|
| 25 |
|
| 26 |
@app.route('/predict_pneumonia', methods=['POST'])
|
|
|
|
| 37 |
audio_file.save(temp_audio_path)
|
| 38 |
|
| 39 |
try:
|
| 40 |
+
if classifier_model is None:
|
| 41 |
+
return jsonify({"error": "Classifier not loaded"}), 500
|
| 42 |
|
| 43 |
audio_clips = preprocess_audio(
|
| 44 |
temp_audio_path, SAMPLE_RATE, CLIP_DURATION, CLIP_OVERLAP_PERCENT,
|
|
|
|
| 48 |
if audio_clips.size == 0:
|
| 49 |
return jsonify({"result": "No valid audio clips", "risk_score": None}), 200
|
| 50 |
|
| 51 |
+
# Generate embeddings using OpenL3
|
| 52 |
+
embeddings = generate_embeddings(audio_clips)
|
| 53 |
|
| 54 |
if embeddings.size == 0:
|
| 55 |
return jsonify({"result": "No embeddings generated", "risk_score": None}), 200
|
| 56 |
|
| 57 |
+
# Predict pneumonia risk
|
| 58 |
clip_predictions, clip_probabilities = predict_pneumonia(embeddings, classifier_model)
|
| 59 |
+
|
| 60 |
+
if clip_predictions is None or clip_probabilities is None:
|
| 61 |
+
return jsonify({"result": "Prediction failed", "risk_score": None}), 200
|
| 62 |
+
|
| 63 |
final_prediction, risk_score = aggregate_predictions(clip_predictions, clip_probabilities)
|
| 64 |
|
| 65 |
return jsonify({
|