Chris Bracegirdle
commited on
Commit
·
9fbfc52
1
Parent(s):
079863b
First go
Browse files- app.py +50 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
import librosa
|
| 5 |
+
import json
|
| 6 |
+
# Load model directly
|
| 7 |
+
from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq
|
| 8 |
+
|
| 9 |
+
processor = AutoProcessor.from_pretrained("dmatekenya/whisper-large-v3-chichewa")
|
| 10 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained("dmatekenya/whisper-large-v3-chichewa")
|
| 11 |
+
|
| 12 |
+
def transcribe(audio_file_mic=None, audio_file_upload=None, language="English (eng)"):
|
| 13 |
+
if audio_file_mic:
|
| 14 |
+
audio_file = audio_file_mic
|
| 15 |
+
elif audio_file_upload:
|
| 16 |
+
audio_file = audio_file_upload
|
| 17 |
+
else:
|
| 18 |
+
return "Please upload an audio file or record one"
|
| 19 |
+
|
| 20 |
+
# Make sure audio is 16kHz
|
| 21 |
+
speech, sample_rate = librosa.load(audio_file)
|
| 22 |
+
if sample_rate != 16000:
|
| 23 |
+
speech = librosa.resample(speech, orig_sr=sample_rate, target_sr=16000)
|
| 24 |
+
|
| 25 |
+
# Keep the same model in memory and simply switch out the language adapters by calling load_adapter() for the model and set_target_lang() for the tokenizer
|
| 26 |
+
# language_code = iso_codes[language]
|
| 27 |
+
# processor.tokenizer.set_target_lang(language_code)
|
| 28 |
+
# model.load_adapter(language_code)
|
| 29 |
+
|
| 30 |
+
inputs = processor(speech, sampling_rate=16_000, return_tensors="pt")
|
| 31 |
+
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
outputs = model(**inputs).logits
|
| 34 |
+
|
| 35 |
+
ids = torch.argmax(outputs, dim=-1)[0]
|
| 36 |
+
transcription = processor.decode(ids)
|
| 37 |
+
return transcription
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
description = ''''''
|
| 41 |
+
|
| 42 |
+
iface = gr.Interface(fn=transcribe,
|
| 43 |
+
inputs=[
|
| 44 |
+
gr.Audio(source="microphone", type="filepath", label="Record Audio"),
|
| 45 |
+
gr.Audio(source="upload", type="filepath", label="Upload Audio"),
|
| 46 |
+
],
|
| 47 |
+
outputs=gr.Textbox(label="Transcription"),
|
| 48 |
+
description=description
|
| 49 |
+
)
|
| 50 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
git+https://github.com/huggingface/transformers.git
|
| 2 |
+
torch
|
| 3 |
+
librosa
|