-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrv_pipeline_cascade.sh
executable file
·269 lines (221 loc) · 5.97 KB
/
srv_pipeline_cascade.sh
1
2
3
4
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/bin/bash
# ARGS:
#1: wavFile
#2: srcLang
#3: tgtLang
#4: outSrt
#5: stateFile
wDir=$(cd $(dirname $0) ; pwd)
maxCharacters=40
maxLines=2
function fail_exit() {
stateFile=$1
outSrt=$2
# write stateFile
echo fail > $stateFile
# write empty srt
cat - <<EOF > $outSrt
1
00:00:00,000 --> 00:00:01,000
EMPTY_SUBTITLES
EOF
exit 1
}
function translateWithHelsinkiWrapper() {
sl=$1
tl=$2
inF=$3
outF=$4
tmpOut=/tmp/wh.$$.out
: > $tmpOut
Nin=$(wc -l < $inF)
Nout=$(wc -l < $tmpOut)
while [ $Nout -lt $Nin ] ; do
doneN=$(( $Nout + 1 ))
tail -n +$doneN $inF | python $exe8 $sl $tl >> $tmpOut 2> /dev/null
Nout=$(wc -l < $tmpOut)
if [ $Nout -lt $Nin ] ; then
echo "Translation Error" >> $tmpOut
Nout=$(wc -l < $tmpOut)
fi
done
cat $tmpOut > $outF
\rm -f $tmpOut
}
function srtFinalCheckAndFillEmpty() {
srt=$1
size=$(wc -c < $srt 2>/dev/null | awk '{print $1}' )
if ! test -f $srt ; then size=0 ; fi
if test $size -lt 35
then
echo fixing empty $srt:
cat - <<EOF > $srt
1
00:00:00,000 --> 00:00:01,000
EMPTY_SUBTITLES
EOF
fi
}
test $# -ge 5 || { echo 'ARGS: wav srcLang tgtLang outSrt stateFile' ; exit 1 ; }
wav=$1
src=$2
tgt=$3
outSrt=$4
stateFile=$5
cat << EOF
args:
wav $wav
srcLang $src
tgtLang $tgt
outSrc $outSrt
stateFile $stateFile
EOF
test -f $wav || { echo cannot find wav $wav ; fail_exit $stateFile $outSrt ; }
case $src in
en|es|fr|it|pt)
prefix=$src
;;
*)
prefix=multi
;;
esac
ckpt=$HOME/.cache/shas/${prefix}.checkpoint
test -f $ckpt || { echo cannot find chkpt $ckpt ; fail_exit $stateFile $outSrt ; }
_dd=$(dirname $outSrt)
test -d $_dd || { echo cannot find directory of $outSrt ; fail_exit $stateFile $outSrt ; }
test -w $_dd || { echo directory of $outSrt is not writable ; fail_exit $stateFile $outSrt ; }
unset _dd
if ! test -z "$logDir"
then
_dd=$(dirname $logDir)
test -d $_dd || { echo cannot find directory of $logDir ; fail_exit $stateFile $outSrt ; }
test -w $_dd || { echo directory of $logDir is not writable ; fail_exit $stateFile $outSrt ; }
unset _dd
fi
scriptDir=$wDir/scripts
exe1=${SHAS_ROOT}/src/supervised_hybrid/segment.py
exe2=$scriptDir/createWavFromShasSegm.py
exe3=faster-whisper
exe4_1=$scriptDir/srtFixTS.pl
exe4_2=$scriptDir/srt_fix_duration.pl
exe4_3=$scriptDir/rmHallucinations.pl
exe5=$scriptDir/joinSrtFromShasSegm.py
exe6=$scriptDir/srtPostprocess.pl
exe7=$scriptDir/srt2txt.pl
exe8=$scriptDir/helsinki_opus_mt.py
exe9=$scriptDir/src2trgSrt.pl
exe10=$scriptDir/splitSentences4matesub_naive.pl
for f in $exe1 $exe2 $exe4_1 $exe4_2 $exe4_3 $exe5 $exe6 $exe7 $exe8 $exe9 $exe10
do
test -f $f || { echo cannot find exe $f ; fail_exit $stateFile $outSrt ; }
done
echo run $0 on $(uname -n) ; echo
echo START $(date +%s)
tmpWavD1=/tmp/psfw.$$.audio.1
! test -d $tmpWavD1 || rm -rf ${tmpWavD1}
mkdir ${tmpWavD1}
#
tmpOutY=/tmp/psfw.$$.yaml
! test -f $tmpOutY || rm -f ${tmpOutY}
#
tmpWavD2=/tmp/psfw.$$.audio.2
! test -d $tmpWavD2 || rm -rf ${tmpWavD2}
mkdir ${tmpWavD2}
#
tmpSrtD1=/tmp/psfw.$$.srt.1
! test -d $tmpSrtD1 || rm -rf ${tmpSrtD1}
mkdir ${tmpSrtD1}
#
tmpSrtD2=/tmp/psfw.$$.srt.2
! test -d $tmpSrtD2 || rm -rf ${tmpSrtD2}
mkdir ${tmpSrtD2}
#
tmpJoinSrtD=/tmp/psfw.$$.joinSrt
! test -d $tmpJoinSrtD || rm -rf ${tmpJoinSrtD}
mkdir ${tmpJoinSrtD}
#
tmpSrtF=/tmp/psfw.$$.srt
! test -f $tmpSrtF || rm -f ${tmpSrtF}
cp $wav $tmpWavD1
bName=$(basename $wav .wav)
yamlF=${tmpOutY}
maxSegLen=15
# STEP 1
#
echo doing step 1 $(date +%s)
source /opt/env/shas/bin/activate
echo python $exe1 -wavs $tmpWavD1 -ckpt $ckpt -yaml $yamlF -max $maxSegLen
python $exe1 -wavs $tmpWavD1 -ckpt $ckpt -yaml $yamlF -max $maxSegLen
# STEP 2
#
echo doing step 2 $(date +%s)
$exe2 --segmentation-yaml $yamlF --wav-dir $tmpWavD1 --out-dir $tmpWavD2
# STEPS 3 and 4
#
echo doing steps 3 and 4 $(date +%s)
deactivate
source /opt/env/fw/bin/activate
export HF_DATASETS_OFFLINE=1
args="--language $src --task transcribe --model_size_or_path large-v3"
args="$args --vad_filter True"
for wav in $tmpWavD2/*wav
do
fn=$(basename $wav .wav)
srtF=$tmpSrtD1/${fn}.srt
startSec=$(date +%s)
echo $exe3 $args $wav -o $srtF
$exe3 $args $wav -o $srtF
endSec=$(date +%s)
runSecs=$(expr $endSec - $startSec)
echo runSecs $runSecs for $wav
# skip empty srt
if test $(wc -c < $srtF) -eq 0 ; then continue ; fi
rmh=$tmpSrtD2/${fn}.srt
cat $srtF | $exe4_1 | $exe4_2 | $exe4_3 --inType srt > $rmh
done
echo
# STEP 5
#
echo doing step 5 $(date +%s)
# join the segments srt into a single srt
$exe5 --segmentation-yaml $yamlF --srt-dir $tmpSrtD2 --out-dir $tmpJoinSrtD
srt=$tmpJoinSrtD/${bName}.srt
# clean the srt
cat $srt | $exe4_3 --inType srt | $exe6 > $tmpSrtF
cat $tmpSrtF > $srt
# STEP 6
#
echo doing step 6 $(date +%s)
srt=$tmpJoinSrtD/${bName}.srt
txtSrc=${srt}.txt.src
txtTgt=${srt}.txt.tgt
# extract the src text
$exe7 < $srt > $txtSrc
# translate the text with helsinki wrapper
deactivate
source /opt/env/helsinki/bin/activate
echo translateWithHelsinkiWrapper $src $tgt $txtSrc $txtTgt
translateWithHelsinkiWrapper $src $tgt $txtSrc $txtTgt
# re-compose the srt with the translated text
srtTgt=${txtTgt}.srt
$exe9 $srt $txtTgt > $srtTgt
# split blocks into lines (naive approach)
cat $srtTgt | $exe10 -c $maxCharacters -b $maxLines \
| perl -pe 's/\s*\$\{DNT1\}\s*/\n/g' > $tmpSrtF
cat $tmpSrtF > $srtTgt
# check and fix final srt
srtFinalCheckAndFillEmpty $srtTgt
# cp the final srt into the outSrt
cat $srtTgt > $outSrt
echo END $(date +%s)
echo
if ! test -z "$logDir"
then
test -d $logDir || mkdir -p $logDir
cp -rf $tmpWavD1 $tmpWavD2 $tmpSrtD1 $tmpSrtD2 $tmpJoinSrtD $yamlF $logDir
echo copied $tmpWavD1 $tmpWavD2 $tmpSrtD1 $tmpSrtD2 $tmpJoinSrtD $yamlF in $logDir
fi
rm -rf $tmpWavD1 $tmpWavD2 $tmpSrtD1 $tmpSrtD2 $tmpJoinSrtD $yamlF $tmpSrtF
echo done rm -rf $tmpWavD1 $tmpWavD2 $tmpSrtD1 $tmpSrtD2 $tmpJoinSrtD $yamlF $tmpSrtF
echo ready > $stateFile
echo updated $stateFile with state \"ready\"