| |
| """ASR_Deployment.ipynb |
| |
| Automatically generated by Colab. |
| |
| Original file is located at |
| https://colab.research.google.com/drive/1MmePYOn1Ho2FhILi00u9UbvsujEoHhot |
| """ |
|
|
| import gradio as gr |
| from transformers import WhisperForConditionalGeneration, WhisperProcessor, GenerationConfig |
| import torch |
| import librosa |
| import os |
|
|
| |
| |
| MODEL_PATH = "MaryWambo/whisper-base-kikuyu4" |
| device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
| |
| print(f"Loading model to {device}...") |
| try: |
| processor = WhisperProcessor.from_pretrained(MODEL_PATH) |
| model = WhisperForConditionalGeneration.from_pretrained(MODEL_PATH).to(device) |
|
|
| |
| |
| gen_config = GenerationConfig.from_pretrained(MODEL_PATH) |
| gen_config.language = "swahili" |
| gen_config.task = "transcribe" |
| gen_config.forced_decoder_ids = None |
| gen_config.suppress_tokens = [] |
|
|
| model.generation_config = gen_config |
|
|
| except Exception as e: |
| print(f"Error loading model: {e}") |
|
|
| |
| custom_css = """ |
| body, .gradio-container { background-color: white !important; } |
| #title-text h1 { color: #8b0000 !important; font-weight: 900 !important; text-align: center; } |
| .upload-button svg, .mic-button svg, .clear-button svg, .record-button svg { |
| transform: scale(1.5) !important; |
| color: #8b0000 !important; |
| } |
| #predict-box textarea { |
| font-size: 1.6rem !important; |
| font-weight: 800 !important; |
| color: #000000 !important; |
| border: 3px solid #8b0000 !important; |
| } |
| #run-btn { |
| background: #8b0000 !important; |
| color: white !important; |
| font-weight: bold !important; |
| font-size: 1.4rem !important; |
| } |
| """ |
|
|
| |
| def transcribe_kikuyu(audio): |
| if audio is None: |
| return "Please record or upload audio." |
|
|
| try: |
| |
| speech_array, sampling_rate = librosa.load(audio, sr=16000) |
|
|
| |
| inputs = processor(speech_array, sampling_rate=sampling_rate, return_tensors="pt") |
| input_features = inputs.input_features.to(device) |
|
|
| with torch.no_grad(): |
| |
| |
| generated_ids = model.generate( |
| input_features=input_features, |
| num_beams=5, |
| max_new_tokens=255 |
| ) |
|
|
| |
| prediction = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] |
| return prediction |
|
|
| except Exception as e: |
| return f"Error during transcription: {str(e)}" |
|
|
| |
| with gr.Blocks(theme=gr.themes.Default(), css=custom_css) as demo: |
| gr.Markdown("# ποΈ Kikuyu ASR ", elem_id="title-text") |
|
|
| with gr.Row(): |
| with gr.Column(scale=1): |
| audio_input = gr.Audio( |
| sources=["microphone", "upload"], |
| type="filepath", |
| label="π€ Record/Upload Kikuyu Speech" |
| ) |
| submit_btn = gr.Button("π RUN TRANSCRIPTION", elem_id="run-btn") |
|
|
| with gr.Column(scale=1): |
| text_out = gr.Textbox( |
| label="π€ AI Prediction", |
| elem_id="predict-box", |
| lines=8 |
| ) |
|
|
| submit_btn.click( |
| fn=transcribe_kikuyu, |
| inputs=[audio_input], |
| outputs=[text_out] |
| ) |
|
|
| |
| if __name__ == "__main__": |
| |
| demo.launch(share=True, debug=True) |
|
|
|
|