teamRocket123 commited on
Commit
8f5169d
·
verified ·
1 Parent(s): e3a183b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -44
app.py CHANGED
@@ -1,59 +1,46 @@
1
  import gradio as gr
2
- from transformers import Wav2Vec2ForCTC, AutoProcessor
3
  import torch
 
4
  import librosa
5
- import json
6
-
7
- with open('ISO_codes.json', 'r') as file:
8
- iso_codes = json.load(file)
9
-
10
- languages = list(iso_codes.keys())
11
 
12
  model_id = "facebook/mms-1b-all"
13
  processor = AutoProcessor.from_pretrained(model_id)
14
- model = Wav2Vec2ForCTC.from_pretrained(model_id)
15
-
16
- def transcribe(audio_file_mic=None, audio_file_upload=None, language="English (eng)"):
17
- if audio_file_mic:
18
- audio_file = audio_file_mic
19
- elif audio_file_upload:
20
- audio_file = audio_file_upload
21
- else:
22
- return "Please upload an audio file or record one"
23
-
24
- # Make sure audio is 16kHz
25
- speech, sample_rate = librosa.load(audio_file)
26
- if sample_rate != 16000:
27
- speech = librosa.resample(speech, orig_sr=sample_rate, target_sr=16000)
28
-
29
- # 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
30
- language_code = iso_codes[language]
31
- processor.tokenizer.set_target_lang(language_code)
32
- model.load_adapter(language_code)
33
-
34
- inputs = processor(speech, sampling_rate=16_000, return_tensors="pt")
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  with torch.no_grad():
37
- outputs = model(**inputs).logits
38
 
39
- ids = torch.argmax(outputs, dim=-1)[0]
40
- transcription = processor.decode(ids)
41
- return transcription
 
 
 
42
 
43
- examples = [["kab_1.mp3", None, "Amazigh (kab)"],
44
- ["kab_2.mp3", None, "Amazigh (kab)"]]
45
 
46
  description = '''Automatic Speech Recognition with [MMS](https://ai.facebook.com/blog/multilingual-model-speech-recognition/) (Massively Multilingual Speech) by Meta.
47
  Supports [1162 languages](https://dl.fbaipublicfiles.com/mms/misc/language_coverage_mms.html). Read the paper for more details: [Scaling Speech Technology to 1,000+ Languages](https://arxiv.org/abs/2305.13516).'''
48
 
49
  iface = gr.Interface(fn=transcribe,
50
- inputs=[
51
- gr.Audio(source="microphone", type="filepath", label="Record Audio"),
52
- gr.Audio(source="upload", type="filepath", label="Upload Audio"),
53
- gr.Dropdown(choices=languages, label="Language", value="English (eng)")
54
- ],
55
- outputs=gr.Textbox(label="Transcription"),
56
- examples=examples,
57
- description=description
58
- )
59
- iface.launch()
 
1
  import gradio as gr
2
+ from transformers import Wav2Vec2Model, AutoProcessor
3
  import torch
4
+ import os
5
  import librosa
6
+ import numpy as np
 
 
 
 
 
7
 
8
  model_id = "facebook/mms-1b-all"
9
  processor = AutoProcessor.from_pretrained(model_id)
10
+ model = Wav2Vec2Model.from_pretrained(model_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ def load_audio_file(file_path, db=0):
13
+ # Resample audio to 16kHz if the sample rate is different
14
+ waveform, sample_rate = librosa.load(file_path)
15
+ if len(waveform.shape) > 1 and waveform.shape[0] > 1:
16
+ waveform = librosa.to_mono(waveform)
17
+ if sample_rate != 16000:
18
+ waveform = librosa.resample(waveform, orig_sr=sample_rate, target_sr=16000)
19
+ waveform = torch.Tensor(waveform)
20
+ return waveform
21
+
22
+ def transcribe(audio_file_upload=None):
23
+ file_path = audio_file_upload
24
+ base_name = os.path.splitext(os.path.basename(file_path))[0]
25
+ waveform = load_audio_file(file_path)
26
+ input_values = processor(waveform, return_tensors="pt", sampling_rate=16000).input_values
27
  with torch.no_grad():
28
+ features = model(input_values).last_hidden_state
29
 
30
+ features = features.squeeze().cpu().detach().numpy()
31
+
32
+ # Save features to numpy file
33
+ npy_file = f"{base_name}.npy"
34
+ np.save(npy_file, features)
35
+ return npy_file
36
 
 
 
37
 
38
  description = '''Automatic Speech Recognition with [MMS](https://ai.facebook.com/blog/multilingual-model-speech-recognition/) (Massively Multilingual Speech) by Meta.
39
  Supports [1162 languages](https://dl.fbaipublicfiles.com/mms/misc/language_coverage_mms.html). Read the paper for more details: [Scaling Speech Technology to 1,000+ Languages](https://arxiv.org/abs/2305.13516).'''
40
 
41
  iface = gr.Interface(fn=transcribe,
42
+ inputs=gr.Audio(type="filepath", label="Upload Audio"),
43
+ outputs=gr.File(label="Download Features (npy)"),
44
+ description=description)
45
+
46
+ iface.launch()