Update app.py
Browse files
app.py
CHANGED
|
@@ -1,109 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
from sklearn.preprocessing import StandardScaler
|
| 5 |
|
| 6 |
-
|
| 7 |
-
SIMILARITY_THRESHOLD = 0.75
|
| 8 |
|
| 9 |
-
|
| 10 |
-
features = []
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
seg = y[i:i+frame_len]
|
| 28 |
-
if len(seg) < frame_len:
|
| 29 |
-
continue
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
times.append((i/sr, (i+frame_len)/sr))
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
y, sr = librosa.load(file_path, sr=None)
|
| 39 |
-
total_duration = len(y) / sr
|
| 40 |
|
| 41 |
-
all_segments = []
|
| 42 |
speaker_embeddings = []
|
| 43 |
speaker_labels = []
|
| 44 |
-
speaker_count = 0
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
while current_time < total_duration:
|
| 49 |
-
start = int(current_time * sr)
|
| 50 |
-
end = int(min((current_time + CHUNK_DURATION) * sr, len(y)))
|
| 51 |
chunk = y[start:end]
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
np.linalg.norm(feat_norm) * np.linalg.norm(emb)
|
| 72 |
)
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
"start": round(current_time + s, 2),
|
| 87 |
-
"end": round(current_time + e, 2)
|
| 88 |
-
})
|
| 89 |
-
|
| 90 |
-
current_time += CHUNK_DURATION
|
| 91 |
-
|
| 92 |
-
return {"segments": all_segments}
|
| 93 |
-
|
| 94 |
-
# 🎯 Gradio UI
|
| 95 |
-
def run(audio):
|
| 96 |
-
if audio is None:
|
| 97 |
-
return {"error": "Upload audio"}
|
| 98 |
-
|
| 99 |
-
return process_audio(audio)
|
| 100 |
-
|
| 101 |
-
demo = gr.Interface(
|
| 102 |
-
fn=run,
|
| 103 |
-
inputs=gr.Audio(type="filepath"),
|
| 104 |
-
outputs=gr.JSON(),
|
| 105 |
-
title="Speaker Diarization (CPU)",
|
| 106 |
-
description="Upload audio → get speaker labels with timestamps"
|
| 107 |
-
)
|
| 108 |
|
| 109 |
-
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, Form
|
| 2 |
+
import tempfile
|
| 3 |
+
import shutil
|
| 4 |
+
import uvicorn
|
| 5 |
+
import whisperx
|
| 6 |
+
import torch
|
| 7 |
import numpy as np
|
| 8 |
+
import soundfile as sf
|
| 9 |
+
from speechbrain.pretrained import EncoderClassifier
|
|
|
|
| 10 |
|
| 11 |
+
app = FastAPI()
|
|
|
|
| 12 |
|
| 13 |
+
device = "cpu"
|
|
|
|
| 14 |
|
| 15 |
+
# Load models (light)
|
| 16 |
+
asr_model = whisperx.load_model("small", device)
|
| 17 |
|
| 18 |
+
speaker_model = EncoderClassifier.from_hparams(
|
| 19 |
+
source="speechbrain/spkrec-ecapa-voxceleb",
|
| 20 |
+
run_opts={"device": device}
|
| 21 |
+
)
|
| 22 |
|
| 23 |
+
@app.post("/transcribe")
|
| 24 |
+
async def transcribe(audio: UploadFile = File(...), lang: str = Form("en")):
|
| 25 |
|
| 26 |
+
temp = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
| 27 |
+
with temp as buffer:
|
| 28 |
+
shutil.copyfileobj(audio.file, buffer)
|
| 29 |
|
| 30 |
+
audio_path = temp.name
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# Load audio
|
| 33 |
+
audio_data = whisperx.load_audio(audio_path)
|
|
|
|
| 34 |
|
| 35 |
+
# Transcribe
|
| 36 |
+
result = asr_model.transcribe(audio_data, language=lang)
|
| 37 |
+
segments = result["segments"]
|
| 38 |
|
| 39 |
+
y, sr = sf.read(audio_path)
|
|
|
|
|
|
|
| 40 |
|
|
|
|
| 41 |
speaker_embeddings = []
|
| 42 |
speaker_labels = []
|
|
|
|
| 43 |
|
| 44 |
+
final_segments = []
|
| 45 |
+
|
| 46 |
+
for i, seg in enumerate(segments):
|
| 47 |
+
|
| 48 |
+
start = int(seg["start"] * sr)
|
| 49 |
+
end = int(seg["end"] * sr)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
| 51 |
chunk = y[start:end]
|
| 52 |
|
| 53 |
+
if len(chunk) < sr * 0.5: # skip very short
|
| 54 |
+
continue
|
| 55 |
+
|
| 56 |
+
chunk_tensor = torch.tensor(chunk).unsqueeze(0)
|
| 57 |
+
|
| 58 |
+
emb = speaker_model.encode_batch(chunk_tensor)
|
| 59 |
+
emb = emb.squeeze().detach().cpu().numpy()
|
| 60 |
+
|
| 61 |
+
# Assign speakers
|
| 62 |
+
if len(speaker_embeddings) < 2:
|
| 63 |
+
speaker_id = f"SPEAKER_{len(speaker_embeddings)+1}"
|
| 64 |
+
speaker_embeddings.append(emb)
|
| 65 |
+
speaker_labels.append(speaker_id)
|
| 66 |
+
else:
|
| 67 |
+
sims = []
|
| 68 |
+
for e in speaker_embeddings:
|
| 69 |
+
sim = np.dot(emb, e) / (
|
| 70 |
+
np.linalg.norm(emb) * np.linalg.norm(e)
|
|
|
|
| 71 |
)
|
| 72 |
+
sims.append(sim)
|
| 73 |
+
|
| 74 |
+
speaker_id = speaker_labels[np.argmax(sims)]
|
| 75 |
+
|
| 76 |
+
final_segments.append({
|
| 77 |
+
"speaker": speaker_id,
|
| 78 |
+
"start": round(seg["start"], 2),
|
| 79 |
+
"end": round(seg["end"], 2),
|
| 80 |
+
"text": seg["text"]
|
| 81 |
+
})
|
| 82 |
+
|
| 83 |
+
return {"segments": final_segments}
|
| 84 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|