File size: 2,296 Bytes
431c006
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13e50c7
 
431c006
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4eef125
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from transformers import pipeline
from transformers import WhisperForConditionalGeneration, WhisperProcessor, WhisperFeatureExtractor
import gradio as gr
import librosa

# Prepare model for prediction
MODEL_SPECS_ID = "dmatekenya/whisper-small_finetuned_sh_chich"
MODEL_SPECS_BASE_ID = "openai/whisper-small"
MODEL_SPECS_BASE_LAN_SW = "swahili"
MODEL_SPECS_BASE_LAN_SH = "shona"
FEATURE_EXTRACTOR = WhisperFeatureExtractor.from_pretrained(MODEL_SPECS_ID)
PROCESSOR_SH = WhisperProcessor.from_pretrained(MODEL_SPECS_BASE_ID, 
                                                language=MODEL_SPECS_BASE_LAN_SH, task="transcribe")
MODEL = WhisperForConditionalGeneration.from_pretrained(MODEL_SPECS_ID)


def transcribe(audio_file):
  y, sr = librosa.load(audio_file, sr=16000)

  input_features = PROCESSOR_SH(y, return_tensors="pt", sampling_rate=sr).input_features
  generated_ids = MODEL.generate(inputs=input_features)

  transcription = PROCESSOR_SH.batch_decode(generated_ids, skip_special_tokens=True)[0]

  return transcription


def transcribe_audio(mic=None, file=None):
    if mic is not None:
        audio = mic
    elif file is not None:
        audio = file
    else:
        return "You must either provide a mic recording or a file"
    transcription = transcribe(audio_file=audio)
    return transcription

title = "Transcribe Chichewa Audio"
description = """
<img src="https://i.ibb.co/5nQdGSs/logo.png">
IN THIS DEMO, TEST THE FIRST AUTOMATED SPEECH RECOGNITION (ASR) MODEL FOR CHICHEWA BY TRANSCRIBING YOUR CHICHEWA VOICE NOTES. 
FOR AUDIO FILES, PLEASE UPLOAD SHORT VOICE NOTES ONLY (NO LONGER THAN 30 SEC).
"""

article = "Read more about the [ChichewaSpeech2Text](https://dmatekenya.github.io/Chichewa-Speech2Text/README.html) project \
and make sure to sign-up for our first [voice note donation event](https://forms.gle/fHLESutofVvb2YFM9) on July 22. \
You stand a chance to win Airtel or TNM units if you choose to participate in the raffle after the event"

gr.Interface(
    fn=transcribe_audio,
    theme='grass',
    title=title,
    description=description,
    article=article,
    inputs=[
        gr.Audio(source="microphone", type="filepath", optional=True),
        gr.Audio(source="upload", type="filepath", optional=True),
    ],
    outputs="text",
).launch()