Update app.py
Browse files
app.py
CHANGED
|
@@ -35,28 +35,33 @@ def transcribe_audio(audio_path):
|
|
| 35 |
# Load audio
|
| 36 |
speech, sr = librosa.load(audio_path, sr=16000)
|
| 37 |
|
|
|
|
| 38 |
inputs = processor(
|
| 39 |
audios=speech,
|
| 40 |
sampling_rate=16000,
|
|
|
|
|
|
|
| 41 |
return_tensors="pt"
|
| 42 |
-
)
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
with torch.no_grad():
|
| 45 |
-
predicted_ids = asr_model.generate(
|
| 46 |
-
inputs["input_features"],
|
| 47 |
-
task="transcribe", # force transcription
|
| 48 |
-
language="yo", # Yoruba ISO-639-1 code
|
| 49 |
-
max_new_tokens=300
|
| 50 |
-
)
|
| 51 |
|
| 52 |
transcription = processor.batch_decode(
|
| 53 |
predicted_ids,
|
| 54 |
skip_special_tokens=True
|
| 55 |
)[0]
|
| 56 |
|
|
|
|
|
|
|
|
|
|
| 57 |
return transcription.strip()
|
| 58 |
|
| 59 |
|
|
|
|
| 60 |
demo = gr.Interface(
|
| 61 |
fn=transcribe_audio,
|
| 62 |
inputs=gr.Audio(type="filepath", label="Upload Speech"),
|
|
|
|
| 35 |
# Load audio
|
| 36 |
speech, sr = librosa.load(audio_path, sr=16000)
|
| 37 |
|
| 38 |
+
# Convert to batch of shape (1, seq_len)
|
| 39 |
inputs = processor(
|
| 40 |
audios=speech,
|
| 41 |
sampling_rate=16000,
|
| 42 |
+
# specify target language here
|
| 43 |
+
language="yo", # Yoruba ISO-639-1 code
|
| 44 |
return_tensors="pt"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# Move input_features to device
|
| 48 |
+
input_features = inputs["input_features"].to(DEVICE)
|
| 49 |
|
| 50 |
with torch.no_grad():
|
| 51 |
+
predicted_ids = asr_model.generate(input_features, max_new_tokens=300)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
transcription = processor.batch_decode(
|
| 54 |
predicted_ids,
|
| 55 |
skip_special_tokens=True
|
| 56 |
)[0]
|
| 57 |
|
| 58 |
+
if not transcription.strip():
|
| 59 |
+
return "Could not transcribe audio. Please try again in clear Yoruba."
|
| 60 |
+
|
| 61 |
return transcription.strip()
|
| 62 |
|
| 63 |
|
| 64 |
+
|
| 65 |
demo = gr.Interface(
|
| 66 |
fn=transcribe_audio,
|
| 67 |
inputs=gr.Audio(type="filepath", label="Upload Speech"),
|