cherif54 commited on
Commit
2725cfe
1 Parent(s): bce05e2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -0
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import subprocess
4
+ import tempfile
5
+ import matlab.engine
6
+ from st_audiorec import st_audiorec
7
+ import os.path
8
+ import numpy as np
9
+ import sounddevice as sd
10
+ from scipy.io.wavfile import write
11
+ from scipy.io import wavfile
12
+
13
+
14
+
15
+ def record(duration):
16
+ fs = 16000
17
+ seconds = duration
18
+ myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=1)
19
+ sd.wait()
20
+ write('clean_waveform.wav', fs, myrecording)
21
+
22
+
23
+ def main():
24
+ st.title("Upload WAV File, make the file noisy, run enhancement and transcribe")
25
+ uploaded_wav_file = st.file_uploader("Upload a WAV file", type=["wav"])
26
+ uploaded_noise_file = st.file_uploader("Upload a noise file", type = ["wav"])
27
+ snr = st.text_input("Enter SNR", "")
28
+
29
+ temp_dir = tempfile.mkdtemp()
30
+ if st.button("Record"):
31
+ record(5)
32
+ if st.button("Add Noise"):
33
+ wav_file_path = os.path.join(temp_dir, uploaded_wav_file.name)
34
+ with open(wav_file_path, "wb") as f1:
35
+ f1.write(uploaded_wav_file.getvalue())
36
+
37
+ noise_file_path = os.path.join(temp_dir, uploaded_noise_file.name)
38
+ with open(noise_file_path, "wb") as f2:
39
+ f2.write(uploaded_noise_file.getvalue())
40
+
41
+ #run_matlab_script(snr)
42
+ samplerate, signal = wavfile.read(wav_file_path)
43
+ samplerate, noise = wavfile.read(noise_file_path)
44
+ mix_audio(signal, noise, snr)
45
+
46
+ if st.button("Enhance"):
47
+ run_batch_script()
48
+
49
+ if st.button("Transcribe_zeroshot"):
50
+ transcribe_zeroshot()
51
+ if st.button("Transcribe_trained"):
52
+ transcribe_trained()
53
+
54
+
55
+ def run_matlab_script(snr):
56
+
57
+ read_fd, write_fd = os.pipe()
58
+
59
+ matlab_executable = 'matlab'
60
+
61
+ # Path to your MATLAB script
62
+ matlab_script = 'mixFiles.m'
63
+
64
+ # Data to send to MATLAB (replace with your actual data)
65
+ data_to_send = snr
66
+
67
+ # Run the MATLAB script
68
+ process = subprocess.Popen([matlab_executable, '-nodesktop', '-nosplash', '-r', f'run("{matlab_script}");exit;'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
69
+
70
+ # Send data to MATLAB
71
+ process.stdin.write(data_to_send)
72
+ process.stdin.close()
73
+
74
+
75
+
76
+ def run_batch_script():
77
+ command = r"OmniClear_cloud_demo noisy_waveform.wav enhanced_waveform.wav"
78
+ subprocess.run(command, shell=True)
79
+
80
+ def transcribe_zeroshot():
81
+ command = r"streamlit run loadlocal_zeroshot.py"
82
+ subprocess.run(command)
83
+
84
+ def transcribe_trained():
85
+ command = r"streamlit run loadlocal_trained.py"
86
+ subprocess.run(command)
87
+
88
+
89
+ def mix_audio(signal, noise, snr):
90
+ noise = noise[np.arange(len(signal)) % len(noise)]
91
+ noise = noise.astype(np.float32)
92
+ signal = signal.astype(np.float32)
93
+ signal_energy = np.mean(signal**2)
94
+ noise_energy = np.mean(noise**2)
95
+ snr = float(snr)
96
+ g = np.sqrt(10.0 ** (-snr/10) * signal_energy / noise_energy)
97
+ a = np.sqrt(1 / (1 + g**2))
98
+ b = np.sqrt(g**2 / (1 + g**2))
99
+ # mix the signals
100
+ rate = 16000
101
+ noisy_signal = a * signal + b * noise
102
+ scaled = np.int16(noisy_signal / np.max(np.abs(noisy_signal)) * 32767)
103
+ # Write the array to a WAV file
104
+ #print(scaled.shape)
105
+ write('noisy_waveform.wav', rate, scaled)
106
+
107
+
108
+ if __name__ == "__main__":
109
+ main()