roman commited on
Commit
654bae8
·
1 Parent(s): 6adb90e
Files changed (2) hide show
  1. app.py +92 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import soundfile as sf
3
+ from transformers import AutoModelForCTC, Wav2Vec2BertProcessor
4
+ from pydub import AudioSegment
5
+ import streamlit as st
6
+ import tempfile
7
+
8
+ # Define available models
9
+ available_models = ['Yehor/w2v-bert-2.0-uk']
10
+
11
+ st.title("Voice Recognition App")
12
+
13
+ # Model selection dropdown
14
+ model_name = st.selectbox("Choose a model", available_models)
15
+
16
+ # # Config
17
+ # device = 'cpu' # 'cuda:0' # or cpu
18
+ # sampling_rate = 16_000
19
+
20
+ # Load the model
21
+ asr_model = AutoModelForCTC.from_pretrained(model_name).to(device)
22
+ processor = Wav2Vec2BertProcessor.from_pretrained(model_name)
23
+
24
+ # paths = [
25
+ # 'short_1_16k.wav',
26
+ # ]
27
+
28
+ def map_to_pred(file_path, sampling_rate = 16_000, device = 'cpu'):
29
+ audio_inputs = []
30
+
31
+ # # load audio file
32
+ # audio, _ = librosa.load(file_path)
33
+ #
34
+ # # preprocess audio and generate standard
35
+ # input_features = processor([audio], return_tensors="pt", sampling_rate=16000).input_features
36
+ # generated_ids = model.generate(inputs=input_features)
37
+ # transcription = processor.batch_decode(generated_ids, normalize=True, skip_special_tokens=True)
38
+ # text = processor.tokenizer._normalize(transcription[0])
39
+
40
+ audio_input, _ = sf.read(file_path)
41
+ audio_inputs.append(audio_input)
42
+ # Transcribe the audio
43
+ inputs = processor(audio_inputs, sampling_rate=sampling_rate).input_features
44
+ features = torch.tensor(inputs).to(device)
45
+
46
+ with torch.no_grad():
47
+ logits = asr_model(features).logits
48
+
49
+ predicted_ids = torch.argmax(logits, dim=-1)
50
+ predictions = processor.batch_decode(predicted_ids)
51
+
52
+ # Log results
53
+ print('Predictions:')
54
+
55
+ return predictions
56
+
57
+
58
+ # Extract audio
59
+ # audio_inputs = []
60
+ # for path in paths:
61
+ # audio_input, _ = sf.read(path)
62
+ # audio_inputs.append(audio_input)
63
+
64
+ # # Transcribe the audio
65
+ # inputs = processor(audio_inputs, sampling_rate=sampling_rate).input_features
66
+ # features = torch.tensor(inputs).to(device)
67
+
68
+ uploaded_file = st.file_uploader("Choose file", type=["wav", "mp3"])
69
+
70
+ if uploaded_file is not None:
71
+ # convert file object to file path
72
+ file_path = './temp.wav'
73
+ with open(file_path, 'wb') as f:
74
+ f.write(uploaded_file.getbuffer())
75
+
76
+ # Save the uploaded file temporarily
77
+ with tempfile.NamedTemporaryFile(delete=False) as temp_file:
78
+ temp_file.write(uploaded_file.read())
79
+ temp_file_path = temp_file.name
80
+
81
+ # Convert audio file to a format supported by Whisper (if necessary)
82
+ audio = AudioSegment.from_file(temp_file_path)
83
+ temp_wav_path = tempfile.mktemp(suffix=".wav")
84
+ audio.export(temp_wav_path, format="wav")
85
+
86
+ st.audio(uploaded_file, format="audio/wav")
87
+
88
+ text = map_to_pred(file_path)
89
+
90
+ # display results
91
+ st.write('Input audio:', uploaded_file.name)
92
+ st.write('Predicted standard:', text)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ streamlit
2
+ transformers
3
+ torch
4
+ soundfile
5
+ pydub