Upload 2 files
Browse files- Hausa_model.py +91 -0
- requirements.txt +7 -0
Hausa_model.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import WhisperForConditionalGeneration, WhisperProcessor
|
| 4 |
+
import librosa
|
| 5 |
+
import numpy as np
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Page configuration
|
| 9 |
+
st.set_page_config(page_title="Hausa Speech Transcription", page_icon="🎙️")
|
| 10 |
+
|
| 11 |
+
# Load model and processor
|
| 12 |
+
@st.cache_resource
|
| 13 |
+
def load_model():
|
| 14 |
+
st.info("Loading the transcription model, please wait...")
|
| 15 |
+
model = WhisperForConditionalGeneration.from_pretrained(
|
| 16 |
+
"therealbee/whisper-small-ha-bible-tts",
|
| 17 |
+
ignore_mismatched_sizes=True
|
| 18 |
+
)
|
| 19 |
+
processor = WhisperProcessor.from_pretrained("therealbee/whisper-small-ha-bible-tts")
|
| 20 |
+
return model, processor
|
| 21 |
+
|
| 22 |
+
# Transcription function
|
| 23 |
+
def transcribe_audio(audio_path, model, processor):
|
| 24 |
+
# Load and resample audio
|
| 25 |
+
audio, sampling_rate = librosa.load(audio_path, sr=None)
|
| 26 |
+
if sampling_rate != 16000:
|
| 27 |
+
audio = librosa.resample(audio, orig_sr=sampling_rate, target_sr=16000)
|
| 28 |
+
|
| 29 |
+
# Prepare inputs
|
| 30 |
+
inputs = processor(
|
| 31 |
+
audio,
|
| 32 |
+
sampling_rate=16000,
|
| 33 |
+
return_tensors="pt",
|
| 34 |
+
language="ha"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# Generate transcription
|
| 38 |
+
with torch.no_grad():
|
| 39 |
+
outputs = model.generate(inputs.input_features, task="transcribe")
|
| 40 |
+
|
| 41 |
+
# Decode transcription
|
| 42 |
+
transcription = processor.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 43 |
+
return transcription
|
| 44 |
+
|
| 45 |
+
# Streamlit app
|
| 46 |
+
def main():
|
| 47 |
+
st.title("Hausa Speech Transcription")
|
| 48 |
+
st.write("Upload a Hausa language audio file for transcription.")
|
| 49 |
+
|
| 50 |
+
# Load the model and processor
|
| 51 |
+
model, processor = load_model()
|
| 52 |
+
|
| 53 |
+
# File uploader
|
| 54 |
+
uploaded_file = st.file_uploader(
|
| 55 |
+
"Choose an audio file",
|
| 56 |
+
type=['wav', 'mp3', 'ogg'],
|
| 57 |
+
help="Upload a Hausa language audio file."
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
if uploaded_file is not None:
|
| 61 |
+
# Get the file extension
|
| 62 |
+
file_extension = uploaded_file.name.split('.')[-1]
|
| 63 |
+
temp_audio_path = f"temp_audio_file.{file_extension}"
|
| 64 |
+
|
| 65 |
+
# Save the uploaded file
|
| 66 |
+
with open(temp_audio_path, "wb") as f:
|
| 67 |
+
f.write(uploaded_file.getbuffer())
|
| 68 |
+
|
| 69 |
+
# Display the audio player
|
| 70 |
+
st.audio(temp_audio_path)
|
| 71 |
+
|
| 72 |
+
# Transcription button
|
| 73 |
+
if st.button("Transcribe"):
|
| 74 |
+
with st.spinner("Transcribing audio..."):
|
| 75 |
+
try:
|
| 76 |
+
transcription = transcribe_audio(temp_audio_path, model, processor)
|
| 77 |
+
st.success("Transcription complete!")
|
| 78 |
+
st.write(transcription)
|
| 79 |
+
except FileNotFoundError:
|
| 80 |
+
st.error("Audio file not found. Please try uploading again.")
|
| 81 |
+
except ValueError as ve:
|
| 82 |
+
st.error(f"Value error: {ve}")
|
| 83 |
+
except Exception as e:
|
| 84 |
+
st.error(f"An unexpected error occurred: {e}")
|
| 85 |
+
finally:
|
| 86 |
+
# Clean up temporary file
|
| 87 |
+
os.remove(temp_audio_path)
|
| 88 |
+
|
| 89 |
+
# Run the app
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
torch
|
| 3 |
+
transformers
|
| 4 |
+
librosa
|
| 5 |
+
numpy
|
| 6 |
+
pydub
|
| 7 |
+
soundfile
|