V8.0
Browse files
app.py
CHANGED
|
@@ -1,82 +1,80 @@
|
|
| 1 |
import time
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
AutoProcessor, AutoModelForCTC, pipeline
|
| 8 |
-
)
|
| 9 |
-
from jiwer import wer, cer
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
waveform, sr = torchaudio.load(fp)
|
| 14 |
-
if sr != 16000:
|
| 15 |
-
waveform = torchaudio.transforms.Resample(sr, 16000)(waveform)
|
| 16 |
-
return waveform.squeeze(0), 16000
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
"automatic-speech-recognition",
|
| 38 |
-
model=
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
device=-1
|
| 42 |
)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
rtf = (time.time() - start) / (waveform.shape[0] / sr)
|
| 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 |
-
gr.Dropdown(choices=["hi","gu","ta"], label="Language", value="hi")
|
| 75 |
-
],
|
| 76 |
-
outputs=gr.JSON(label="Benchmark Results"),
|
| 77 |
-
title="Indic ASR Benchmark (CPU-only)",
|
| 78 |
-
description="Compare IndicConformer, AudioX-North, and MMS on WER, CER, and RTF."
|
| 79 |
-
)
|
| 80 |
|
| 81 |
-
|
| 82 |
-
demo.launch()
|
|
|
|
| 1 |
import time
|
| 2 |
+
import os
|
| 3 |
+
import evaluate
|
| 4 |
+
from datasets import load_dataset
|
| 5 |
+
from huggingface_hub import login
|
| 6 |
+
from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# 🔑 Authenticate using HF_TOKEN secret
|
| 9 |
+
login(token=os.environ.get("HF_TOKEN"))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# -----------------
|
| 12 |
+
# Load evaluation metrics
|
| 13 |
+
wer_metric = evaluate.load("wer")
|
| 14 |
+
cer_metric = evaluate.load("cer")
|
| 15 |
|
| 16 |
+
# -----------------
|
| 17 |
+
# Small sample dataset for Hindi
|
| 18 |
+
# (free Spaces can't handle large test sets)
|
| 19 |
+
test_ds = load_dataset("mozilla-foundation/common_voice_11_0", "hi", split="test[:3]")
|
| 20 |
+
|
| 21 |
+
# Extract references + audio
|
| 22 |
+
refs = [x["sentence"] for x in test_ds]
|
| 23 |
+
audio_data = [x["audio"]["array"] for x in test_ds]
|
| 24 |
+
|
| 25 |
+
results = {}
|
| 26 |
+
|
| 27 |
+
# -----------------
|
| 28 |
+
# Helper to evaluate model
|
| 29 |
+
def evaluate_model(model_name, pipeline_kwargs=None):
|
| 30 |
+
try:
|
| 31 |
+
start = time.time()
|
| 32 |
+
asr_pipeline = pipeline(
|
| 33 |
"automatic-speech-recognition",
|
| 34 |
+
model=model_name,
|
| 35 |
+
device=-1, # CPU only
|
| 36 |
+
**(pipeline_kwargs or {})
|
|
|
|
| 37 |
)
|
| 38 |
|
| 39 |
+
preds = []
|
| 40 |
+
for audio in audio_data:
|
| 41 |
+
out = asr_pipeline(audio, chunk_length_s=30, return_timestamps=False)
|
| 42 |
+
preds.append(out["text"])
|
|
|
|
| 43 |
|
| 44 |
+
end = time.time()
|
| 45 |
+
rtf = (end - start) / sum(len(a) / 16000 for a in audio_data)
|
| 46 |
|
| 47 |
+
return {
|
| 48 |
+
"Transcriptions": preds,
|
| 49 |
+
"WER": wer_metric.compute(predictions=preds, references=refs),
|
| 50 |
+
"CER": cer_metric.compute(predictions=preds, references=refs),
|
| 51 |
+
"RTF": rtf
|
| 52 |
+
}
|
| 53 |
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return {"Error": str(e)}
|
| 56 |
+
|
| 57 |
+
# -----------------
|
| 58 |
+
# Models to test
|
| 59 |
+
models = {
|
| 60 |
+
"IndicConformer (AI4Bharat)": {
|
| 61 |
+
"name": "ai4bharat/IndicConformer-Hi",
|
| 62 |
+
"pipeline_kwargs": {"trust_remote_code": True}
|
| 63 |
+
},
|
| 64 |
+
"AudioX-North (Jivi AI)": {
|
| 65 |
+
"name": "jiviai/audioX-north-v1",
|
| 66 |
+
"pipeline_kwargs": {"use_auth_token": os.environ.get("HF_TOKEN")}
|
| 67 |
+
},
|
| 68 |
+
"MMS (Facebook)": {
|
| 69 |
+
"name": "facebook/mms-1b-all",
|
| 70 |
+
"pipeline_kwargs": {}
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
|
| 74 |
+
# -----------------
|
| 75 |
+
# Run evaluations
|
| 76 |
+
for label, cfg in models.items():
|
| 77 |
+
print(f"Running {label}...")
|
| 78 |
+
results[label] = evaluate_model(cfg["name"], cfg["pipeline_kwargs"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
print(results)
|
|
|