ibombonato commited on
Commit
31a07fa
1 Parent(s): 11ea060

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -0
app.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import matplotlib.pyplot as plt
3
+ import subprocess
4
+ import re
5
+ import logging
6
+ import os
7
+ import numpy as np
8
+ import matplotlib
9
+ import scipy.io
10
+ import scipy.io.wavfile
11
+
12
+ matplotlib.use('Agg')
13
+
14
+ logging.basicConfig(level=logging.INFO)
15
+ logging.getLogger()
16
+
17
+ def get_chunk_times(in_filename, silence_threshold, silence_duration=1):
18
+
19
+ silence_duration_re = re.compile('silence_duration: (\d+.\d+)')
20
+ silence_end_re = re.compile('silence_end: (\d+.\d+)\s')
21
+
22
+ command = f"ffmpeg -i {in_filename} -af silencedetect=n=-{silence_threshold}dB:d={silence_duration} -f null - "
23
+ out = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
24
+
25
+ stdout, stderr = out.communicate()
26
+
27
+ lines = stdout.splitlines()
28
+
29
+ ts = 0
30
+ chunks = []
31
+ for line in lines:
32
+ match = silence_duration_re.search(str(line))
33
+ if(match):
34
+ chunk_time = float(match.group(1))
35
+ ts = ts + chunk_time
36
+ end = silence_end_re.search(str(line))
37
+ if(end):
38
+ t_end = float(end.group(1))
39
+ t_start = t_end - chunk_time
40
+ chunks.append([t_start, t_end, chunks])
41
+
42
+ logging.info(f"TS audio {os.path.basename(in_filename)} = {ts}")
43
+ return ts, chunks
44
+
45
+ def get_plot(a):
46
+ x = [1, 2, 3]
47
+ y = np.array([[1, 2], [3, 4], [5, 6]])
48
+ plt.plot(x, y)
49
+ return plt.gcf()
50
+
51
+ def get_audio_plot(filename, chunks):
52
+ fig, ax = plt.subplots()
53
+
54
+ fig.set_size_inches(18.5, 10.5)
55
+
56
+ sampleRate, audioBuffer = scipy.io.wavfile.read(filename)
57
+
58
+ duration = len(audioBuffer)/sampleRate
59
+
60
+ time = np.arange(0,duration,1/sampleRate) #time vector
61
+
62
+ ax.plot(time,audioBuffer)
63
+ y1 = min(audioBuffer)
64
+ y2 = max(audioBuffer)
65
+
66
+ for c in chunks:
67
+ ax.fill_between(c[0:2], y1, y2,
68
+ color='gray', alpha=0.5)
69
+
70
+ plt.xlabel('Time [s]')
71
+ plt.ylabel('Amplitude')
72
+ plt.title(os.path.basename(filename))
73
+
74
+ #plt.show()
75
+ return plt.gcf()
76
+
77
+ def get_audio_info(audio):
78
+ ts, chunks = get_chunk_times(audio.name, 30, 1)
79
+ p = get_audio_plot(audio.name, chunks)
80
+ return str(ts), p
81
+
82
+ otext = gr.outputs.Textbox(type="auto", label="Silence time")
83
+
84
+ oplot = gr.outputs.Image(type="plot", label=None)
85
+
86
+ iaudio = gr.inputs.Audio(source="upload", type="file", label=None)
87
+
88
+ #iface = gr.Interface(audio, iaudio, [otext, oplot])
89
+
90
+ iface = gr.Interface(
91
+ get_audio_info,
92
+ iaudio,
93
+ [otext, oplot],
94
+ description="Enter .WAV audio to view silence areas",
95
+ )
96
+
97
+ iface.test_launch()
98
+ iface.launch()
99
+
100
+ # import matplotlib.pyplot as plt
101
+ # import numpy as np
102
+ # import pandas as pd
103
+
104
+ # import gradio as gr
105
+ # import matplotlib
106
+
107
+ # matplotlib.use('Agg')
108
+
109
+
110
+ # iaudio = gr.inputs.Audio(source="upload", type="file", label=None)
111
+
112
+ # def sales_projections(employee_data):
113
+ # sales_data = employee_data.iloc[:, 1:4].astype("int").to_numpy()
114
+ # regression_values = np.apply_along_axis(
115
+ # lambda row: np.array(np.poly1d(np.polyfit([0, 1, 2], row, 2))), 0, sales_data
116
+ # )
117
+ # projected_months = np.repeat(
118
+ # np.expand_dims(np.arange(3, 12), 0), len(sales_data), axis=0
119
+ # )
120
+ # projected_values = np.array(
121
+ # [
122
+ # month * month * regression[0] + month * regression[1] + regression[2]
123
+ # for month, regression in zip(projected_months, regression_values)
124
+ # ]
125
+ # )
126
+ # # x = [1, 2, 3]
127
+ # # y = np.array([[1, 2], [3, 4], [5, 6]])
128
+ # # plt.plot(x, y)
129
+ # #plt.plot(projected_values.T)
130
+ # #plt.legend(employee_data["Name"])
131
+ # return employee_data, get_plot(1), regression_values
132
+
133
+
134
+ # iface = gr.Interface(
135
+ # get_plot,
136
+ # iaudio,
137
+ # ["plot"],
138
+ # description="Enter sales figures for employees to predict sales trajectory over year.",
139
+ # )
140
+ # iface.launch()