Mohamed Aymane Farhi
commited on
Commit
•
0b452e3
1
Parent(s):
cd36912
Initial commit.
Browse files- app.py +40 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import Wav2Vec2ForCTC, AutoProcessor
|
3 |
+
import torch
|
4 |
+
import numpy as np
|
5 |
+
import librosa
|
6 |
+
|
7 |
+
model_id = "facebook/mms-1b-all"
|
8 |
+
|
9 |
+
def transcribe(audio_file_mic=None, audio_file_upload=None):
|
10 |
+
if audio_file_mic:
|
11 |
+
audio_file = audio_file_mic
|
12 |
+
elif audio_file_upload:
|
13 |
+
audio_file = audio_file_upload
|
14 |
+
else:
|
15 |
+
return "Please upload an audio file or record one"
|
16 |
+
|
17 |
+
speech, sample_rate = librosa.load(audio_file)
|
18 |
+
if sample_rate != 16000:
|
19 |
+
speech = librosa.resample(speech, orig_sr=sample_rate, target_sr=16000)
|
20 |
+
|
21 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
22 |
+
model = Wav2Vec2ForCTC.from_pretrained(model_id)
|
23 |
+
|
24 |
+
inputs = processor(speech, sampling_rate=16_000, return_tensors="pt")
|
25 |
+
|
26 |
+
with torch.no_grad():
|
27 |
+
outputs = model(**inputs).logits
|
28 |
+
|
29 |
+
ids = torch.argmax(outputs, dim=-1)[0]
|
30 |
+
transcription = processor.decode(ids)
|
31 |
+
return transcription
|
32 |
+
|
33 |
+
iface = gr.Interface(fn=transcribe,
|
34 |
+
inputs=[
|
35 |
+
gr.Audio(source="microphone", type="filepath"),
|
36 |
+
gr.Audio(source="upload", type="filepath")
|
37 |
+
],
|
38 |
+
outputs=["textbox"],
|
39 |
+
)
|
40 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
git+https://github.com/huggingface/transformers.git
|
2 |
+
torch
|
3 |
+
librosa
|