Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoFeatureExtractor, AutoModel
|
4 |
+
import numpy as np
|
5 |
+
from sklearn.linear_model import LogisticRegression
|
6 |
+
|
7 |
+
# Load HeAR model and feature extractor
|
8 |
+
MODEL_ID = "google/hear"
|
9 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(MODEL_ID)
|
10 |
+
model = AutoModel.from_pretrained(MODEL_ID)
|
11 |
+
|
12 |
+
# Dummy classifier (replace with your trained classifier)
|
13 |
+
# For demonstration, we simulate a trained classifier with random weights
|
14 |
+
# In real use, train a classifier on HeAR embeddings using your labeled dataset
|
15 |
+
clf = LogisticRegression()
|
16 |
+
clf.classes_ = np.array(["Normal", "Abnormal"])
|
17 |
+
clf.coef_ = np.random.randn(1, 768) # HeAR outputs 768-dim embeddings
|
18 |
+
clf.intercept_ = np.random.randn(1)
|
19 |
+
|
20 |
+
def extract_embedding(audio):
|
21 |
+
# audio: tuple (sr, np.array)
|
22 |
+
if audio is None:
|
23 |
+
return None
|
24 |
+
sr, y = audio
|
25 |
+
# HeAR expects 2-second clips at 16kHz; pad/truncate as needed
|
26 |
+
target_sr = 16000
|
27 |
+
if sr != target_sr:
|
28 |
+
import librosa
|
29 |
+
y = librosa.resample(y, orig_sr=sr, target_sr=target_sr)
|
30 |
+
y = y[:target_sr*2] if len(y) > target_sr*2 else np.pad(y, (0, max(0, target_sr*2-len(y))))
|
31 |
+
inputs = feature_extractor(y, sampling_rate=target_sr, return_tensors="pt")
|
32 |
+
with torch.no_grad():
|
33 |
+
emb = model(**inputs).last_hidden_state.mean(dim=1).cpu().numpy()
|
34 |
+
return emb
|
35 |
+
|
36 |
+
def predict(audio):
|
37 |
+
emb = extract_embedding(audio)
|
38 |
+
if emb is None:
|
39 |
+
return "Please upload a heart or lung sound file."
|
40 |
+
# Predict with the dummy classifier
|
41 |
+
pred = clf.predict(emb)[0]
|
42 |
+
prob = clf.predict_proba(emb)[0]
|
43 |
+
return f"Prediction: **{pred}**\n\nConfidence: {max(prob):.2%}"
|
44 |
+
|
45 |
+
description = """
|
46 |
+
# Heart & Lung Sound Classifier (Demo)
|
47 |
+
Upload a heart or lung sound (WAV, MP3, etc.).
|
48 |
+
This demo uses the [HeAR model](https://huggingface.co/google/hear) for health acoustic embeddings and a simple classifier for normal/abnormal prediction.
|
49 |
+
**Note:** For best results, use 2-second clips. For real diagnosis, a classifier trained on labeled heart/lung sound data should be used.
|
50 |
+
"""
|
51 |
+
|
52 |
+
iface = gr.Interface(
|
53 |
+
fn=predict,
|
54 |
+
inputs=gr.Audio(sources=["upload", "microphone"], type="numpy", label="Upload Heart/Lung Sound"),
|
55 |
+
outputs=gr.Markdown(),
|
56 |
+
title="Heart & Lung Sound Classifier",
|
57 |
+
description=description,
|
58 |
+
allow_flagging="never"
|
59 |
+
)
|
60 |
+
|
61 |
+
if __name__ == "__main__":
|
62 |
+
iface.launch()
|