dmatekenya
commited on
Commit
•
431c006
1
Parent(s):
335a859
added transcription app
Browse files- app.py +60 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
from transformers import WhisperForConditionalGeneration, WhisperProcessor, WhisperFeatureExtractor
|
3 |
+
import gradio as gr
|
4 |
+
import librosa
|
5 |
+
|
6 |
+
# Prepare model for prediction
|
7 |
+
MODEL_SPECS_ID = "dmatekenya/whisper-small_finetuned_sh_chich"
|
8 |
+
MODEL_SPECS_BASE_ID = "openai/whisper-small"
|
9 |
+
MODEL_SPECS_BASE_LAN_SW = "swahili"
|
10 |
+
MODEL_SPECS_BASE_LAN_SH = "shona"
|
11 |
+
FEATURE_EXTRACTOR = WhisperFeatureExtractor.from_pretrained(MODEL_SPECS_ID)
|
12 |
+
PROCESSOR_SH = WhisperProcessor.from_pretrained(MODEL_SPECS_BASE_ID,
|
13 |
+
language=MODEL_SPECS_BASE_LAN_SH, task="transcribe")
|
14 |
+
MODEL = WhisperForConditionalGeneration.from_pretrained(MODEL_SPECS_ID)
|
15 |
+
|
16 |
+
|
17 |
+
def transcribe(audio_file):
|
18 |
+
y, sr = librosa.load(audio_file, sr=16000)
|
19 |
+
|
20 |
+
input_features = PROCESSOR_SH(y, return_tensors="pt", sampling_rate=sr).input_features
|
21 |
+
generated_ids = MODEL.generate(inputs=input_features)
|
22 |
+
|
23 |
+
transcription = PROCESSOR_SH.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
24 |
+
|
25 |
+
return transcription
|
26 |
+
|
27 |
+
|
28 |
+
def transcribe_audio(mic=None, file=None):
|
29 |
+
if mic is not None:
|
30 |
+
audio = mic
|
31 |
+
elif file is not None:
|
32 |
+
audio = file
|
33 |
+
else:
|
34 |
+
return "You must either provide a mic recording or a file"
|
35 |
+
transcription = transcribe(audio_file=audio)
|
36 |
+
return transcription
|
37 |
+
|
38 |
+
title = "Transcribe Chichewa Audio"
|
39 |
+
description = """
|
40 |
+
<img src="https://i.ibb.co/5nQdGSs/logo.png">
|
41 |
+
In this demo, test the first Automated Speech Recognition (ASR) model for Chichewa by transcribing your Chichewa voice notes.
|
42 |
+
For audio files, please upload short voice notes only (no longer than 30 sec).
|
43 |
+
"""
|
44 |
+
|
45 |
+
article = "Read more about the [ChichewaSpeech2Text](https://dmatekenya.github.io/Chichewa-Speech2Text/README.html) project \
|
46 |
+
and make sure to sign-up for our first [voice note donation event](https://forms.gle/fHLESutofVvb2YFM9) on July 22. \
|
47 |
+
You stand a chance to win Airtel or TNM units if you choose to participate in the raffle after the event"
|
48 |
+
|
49 |
+
gr.Interface(
|
50 |
+
fn=transcribe_audio,
|
51 |
+
theme='grass',
|
52 |
+
title=title,
|
53 |
+
description=description,
|
54 |
+
article=article,
|
55 |
+
inputs=[
|
56 |
+
gr.Audio(source="microphone", type="filepath", optional=True),
|
57 |
+
gr.Audio(source="upload", type="filepath", optional=True),
|
58 |
+
],
|
59 |
+
outputs="text",
|
60 |
+
).launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
librosa
|
3 |
+
torch
|
4 |
+
'transformers[torch]'
|