Spaces:
Runtime error
Runtime error
Commit
·
70ef68d
1
Parent(s):
8ea72b6
Upload 2 files
Browse files- app.py +123 -31
- requirements.txt +3 -3
app.py
CHANGED
@@ -1,33 +1,125 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
from scipy.io
|
|
|
|
|
|
|
4 |
import numpy as np
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
import streamlit as st
|
3 |
+
import pyttsx3
|
4 |
+
from scipy.io import wavfile
|
5 |
+
from scipy import signal
|
6 |
+
from scipy.spatial import distance
|
7 |
+
import plotly.graph_objs as go
|
8 |
import numpy as np
|
9 |
+
from scipy.interpolate import interp2d
|
10 |
+
|
11 |
+
|
12 |
+
def similarity(file1, file2):
|
13 |
+
fs1, data1 = wavfile.read(file1)
|
14 |
+
fs2, data2 = wavfile.read(file2)
|
15 |
+
|
16 |
+
f1, t1, Sxx1 = signal.spectrogram(data1, fs=fs1)
|
17 |
+
f2, t2, Sxx2 = signal.spectrogram(data2, fs=fs2)
|
18 |
+
|
19 |
+
interp1 = interp2d(t1, f1, Sxx1)
|
20 |
+
interp2 = interp2d(t2, f2, Sxx2)
|
21 |
+
|
22 |
+
tmin = max(t1.min(), t2.min())
|
23 |
+
tmax = min(t1.max(), t2.max())
|
24 |
+
fmin = max(f1.min(), f2.min())
|
25 |
+
fmax = min(f1.max(), f2.max())
|
26 |
+
|
27 |
+
tnew = np.linspace(tmin, tmax, num=max(len(t1), len(t2)))
|
28 |
+
fnew = np.linspace(fmin, fmax, num=max(len(f1), len(f2)))
|
29 |
+
|
30 |
+
Sxx1new = interp1(tnew, fnew)
|
31 |
+
Sxx2new = interp2(tnew, fnew)
|
32 |
+
|
33 |
+
d = distance.euclidean(Sxx1new.flatten(), Sxx2new.flatten())
|
34 |
+
max_d = max(Sxx1new.max(), Sxx2new.max()) * len(Sxx1new.flatten())
|
35 |
+
|
36 |
+
return 100 * (1 - d / max_d)
|
37 |
+
|
38 |
+
st.title('Text to Sound Comparing')
|
39 |
+
|
40 |
+
texto1 = st.text_input('First text')
|
41 |
+
texto2 = st.text_input('Second text')
|
42 |
+
|
43 |
+
intercambiar = st.selectbox('Interchange texts', [False, True])
|
44 |
+
|
45 |
+
if intercambiar:
|
46 |
+
texto1, texto2 = texto2, texto1
|
47 |
+
|
48 |
+
if texto1=="":
|
49 |
+
texto1="a"
|
50 |
+
|
51 |
+
if texto2=="":
|
52 |
+
texto2="a"
|
53 |
+
|
54 |
+
if 1:
|
55 |
+
engine = pyttsx3.init()
|
56 |
+
engine.setProperty('voice', 'english')
|
57 |
+
engine.save_to_file(texto1, 'texto1.wav')
|
58 |
+
engine.save_to_file(texto2, 'texto2.wav')
|
59 |
+
engine.runAndWait()
|
60 |
+
|
61 |
+
fs1, data1 = wavfile.read('texto1.wav')
|
62 |
+
fs2, data2 = wavfile.read('texto2.wav')
|
63 |
+
|
64 |
+
inicio1 = st.slider('Start 1: '+str(texto1), 0, len(data1), 0)
|
65 |
+
inicio2 = st.slider('Start 2: '+str(texto2), 0, len(data2), 0)
|
66 |
+
|
67 |
+
f1, t1, Sxx1 = signal.spectrogram(data1[inicio1:], fs=fs1)
|
68 |
+
f2, t2, Sxx2 = signal.spectrogram(data2[inicio2:], fs=fs2)
|
69 |
+
|
70 |
+
fig1 = go.Figure()
|
71 |
+
fig1.add_trace(go.Scatter(y=data1[inicio1:], mode='lines', name='Text 1: '+str(texto1)))
|
72 |
+
fig1.add_trace(go.Scatter(y=data2[inicio2:], mode='lines', name='Text 2: '+str(texto2)))
|
73 |
+
st.header('Waves')
|
74 |
+
st.plotly_chart(fig1)
|
75 |
+
|
76 |
+
fig2 = go.Figure()
|
77 |
+
fig2.add_trace(go.Heatmap(x=t1, y=f1, z=Sxx1, name='Spectrogram 1', xaxis='x2', yaxis='y2'))
|
78 |
+
st.header('Spectrogram '+str(texto1))
|
79 |
+
st.plotly_chart(fig2)
|
80 |
+
|
81 |
+
fig3 = go.Figure()
|
82 |
+
fig3.add_trace(go.Heatmap(x=t2, y=f2, z=Sxx2, name='Spectrogram 2', xaxis='x3', yaxis='y3'))
|
83 |
+
st.header('Spectrogram '+str(texto2))
|
84 |
+
st.plotly_chart(fig3)
|
85 |
+
|
86 |
+
sim = similarity('texto1.wav', 'texto2.wav')
|
87 |
+
#print(f'Similarity: {sim:.2f}%')
|
88 |
+
st.header('Similarity: '+str(sim))
|
89 |
+
|
90 |
+
N = len(data1[inicio1:])
|
91 |
+
T = 1.0 / fs1
|
92 |
+
yf = np.fft.fft(data1[inicio1:])
|
93 |
+
xf = np.linspace(0.0, 1.0/(2.0*T), N//2)
|
94 |
+
fig4 = go.Figure()
|
95 |
+
fig4.add_trace(go.Scatter(x=xf, y=2.0/N * np.abs(yf[0:N//2]), mode='lines', name='Text 1: '+str(texto1)))
|
96 |
+
st.header('Fourier '+str(texto1))
|
97 |
+
st.plotly_chart(fig4)
|
98 |
+
|
99 |
+
N = len(data2[inicio2:])
|
100 |
+
T = 1.0 / fs2
|
101 |
+
yf = np.fft.fft(data2[inicio2:])
|
102 |
+
xf = np.linspace(0.0, 1.0/(2.0*T), N//2)
|
103 |
+
fig5 = go.Figure()
|
104 |
+
fig5.add_trace(go.Scatter(x=xf, y=2.0/N * np.abs(yf[0:N//2]), mode='lines', name='Text 2: '+str(texto2)))
|
105 |
+
st.header('Fourier '+str(texto2))
|
106 |
+
st.plotly_chart(fig5)
|
107 |
+
|
108 |
+
|
109 |
+
N = len(data1[inicio1:])
|
110 |
+
T = 1.0 / fs1
|
111 |
+
yf = np.fft.fft(data1[inicio1:])
|
112 |
+
xf = np.linspace(0.0, 1.0/(2.0*T), N//2)
|
113 |
+
fig6 = go.Figure()
|
114 |
+
fig6.add_trace(go.Scatter(x=xf, y=2.0/N * np.abs(yf[0:N//2]), mode='lines', name='Text 1: '+str(texto1)))
|
115 |
+
|
116 |
+
N = len(data2[inicio2:])
|
117 |
+
T = 1.0 / fs2
|
118 |
+
yf = np.fft.fft(data2[inicio2:])
|
119 |
+
xf = np.linspace(0.0, 1.0/(2.0*T), N//2)
|
120 |
+
fig6.add_trace(go.Scatter(x=xf, y=2.0/N * np.abs(yf[0:N//2]), mode='lines', name='Text 2: '+str(texto2)))
|
121 |
+
|
122 |
+
st.header('Fourier Mix')
|
123 |
+
st.plotly_chart(fig6)
|
124 |
+
|
125 |
+
|
requirements.txt
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
streamlit
|
2 |
-
|
3 |
scipy
|
4 |
-
|
5 |
-
|
|
|
1 |
streamlit
|
2 |
+
pyttsx3
|
3 |
scipy
|
4 |
+
plotly
|
5 |
+
numpy
|