Coverage for lib/lib_speechtotext.py: 58%

101 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2026-01-26 23:58 +0100

1__author__ = 'moilerat' 

2 

3#import openai 

4import os 

5 

6def speech_to_text(in_file, 

7 openai_token, 

8 condition_on_previous_text=False, 

9 no_speech_threshold=0.1, 

10 language="fr", 

11 verbose = False): 

12 import openai 

13 try : 

14 model = "whisper-1" 

15 audio_file= open(in_file, "rb") 

16 openai.api_key = openai_token 

17 

18 print(" openai.api_key : " + str(openai.api_key)) 

19 

20# hack useless 

21# import os 

22# os.environ["PATH"]=os.environ["PATH"]+":/usr/bin" 

23 

24 # For pricing 

25 try : 

26 from pydub import AudioSegment 

27 if in_file.endswith(".mp3") : 

28 sound = AudioSegment.from_mp3(in_file) 

29 elif in_file.endswith(".m4a") : 

30 sound = AudioSegment.from_file(in_file) 

31 else : 

32 sound = AudioSegment.from_file(in_file) 

33 size_part = len(sound) 

34 except Exception as e: 

35 print(str(e)) 

36 print("Problem in estimating cost") 

37 size_part = 0 

38 

39 transcript = openai.Audio.transcribe(model, audio_file, condition_on_previous_text=False, 

40 no_speech_threshold=0.1, language=language) 

41 

42 if verbose : 

43 print(transcript) 

44 text_transcript = correct(transcript["text"]) 

45 transcript["text"] = "PRIVACY" 

46 

47 if verbose : 

48 print(transcript) 

49 

50 if verbose : 

51 print(" size_part : " + str(size_part/1000) + " seconds") 

52 

53 if verbose: 

54 print(text_transcript) 

55 

56 return text_transcript, (size_part / 1000), model 

57 except Exception as e: 

58 print(str(e)) 

59 return "", 0, model 

60 

61def correct(to_change): 

62 

63 change = (("\u00e9","é"), 

64 ("\u00e8","è"), 

65 ("\u0153","oœ"), 

66 ("\u00f9","ù"), 

67 ("\u00e0","à"), 

68 ("\u00e7","ç"), 

69 ("\u00ea","ê"), 

70 ("\u00ef","ï"), 

71 ("\u00c7","Ç"), 

72 ("\u00e2","â"), 

73 ("\u00fb","û"), 

74 ("\u00f4","ô"), 

75 ("\u00c0","Â"), 

76 ("\u00ab","\""), 

77 ("\u00bb","\""), 

78 ("\u2014","—"), 

79 ("\u00ee","î")) 

80 

81 for id in change: 

82 to_change.replace(id[0], id[1]) 

83 

84 return to_change 

85 

86 

87 

88 

89def remove_extension(in_file): 

90 

91 if in_file is None: 

92 print(" remove_extension called with None ") 

93 return "None" 

94 

95 # TODO VR 26-4-23 : a améliorer 

96 basebasename = os.path.basename(in_file)[:-4] 

97 

98 dir = os.path.dirname(in_file) 

99 

100 return os.path.join(dir, basebasename) 

101 

102 

103 

104def convert_file(in_file, out_format = "mp3"): 

105 try : 

106 import os 

107 print(os.environ["PATH"]) 

108 from pydub import AudioSegment 

109 

110# hack useless 

111# os.environ["PATH"]=os.environ["PATH"]+":/usr/bin" 

112 

113 import ftransc 

114 

115 # files 

116 #src = "transcript.mp3" 

117 #dst = "test.wav" 

118 

119 dst = remove_extension(in_file) + "." + out_format 

120 

121 import os 

122 print("display PATH") 

123 print(os.environ["PATH"]) 

124 if "PATHTEST" in os.environ: 

125 print("PATHTEST : " + str(os.environ["PATHTEST"])) 

126 

127 # convert wav or ogg or amr to mp3 

128 if in_file.endswith(".mp4"): 

129 print(" If this fail we should maybe use webm extension !") 

130 from moviepy.editor import VideoFileClip #, video_fps 

131 folder = os.getcwd() 

132 video = VideoFileClip(os.path.join(folder, in_file)) 

133 print("Exporting sound from mp4 (TODO treat cost I think or not) ") 

134 video.audio.write_audiofile(dst) 

135 

136 else : 

137 print("building AudioSegment.from_file") 

138 sound = AudioSegment.from_file(in_file) 

139 print("Exporting " + str(sound.duration_seconds)) 

140 sound.export(dst, format=out_format, bitrate="128k") 

141 print("return destination") 

142 

143 return dst 

144 except Exception as e: 

145 print(str(e)) 

146 return "" 

147 

148 

149 

150def split_mp3(in_file, nb_to_split = 10, verbose = False): # pragma no cover scale long test TODO VR 23-6-23 

151 from pydub import AudioSegment 

152 

153 sound = AudioSegment.from_mp3(in_file) 

154 

155 list_files = [] 

156 

157 size_part = len(sound) / nb_to_split 

158 for i in range(nb_to_split): 

159 file_i = remove_extension(in_file) + "_" + str(i) + ".mp3" 

160 list_files.append(file_i) 

161 # len() and slicing are in milliseconds 

162 part_i = sound[size_part * i:size_part * (i + 1)] 

163 

164 # Concatenation is just adding 

165# second_half_3_times = second_half + second_half + second_half 

166 

167 # writing mp3 files is a one liner 

168 part_i.export(file_i, format="mp3") 

169 

170 return list_files 

171 

172 

173 

174def force_cudnn_initialization(): 

175 import torch 

176 s = 32 

177 dev = torch.device('cuda') 

178 torch.nn.functional.conv2d(torch.zeros(s, s, s, s, device=dev), torch.zeros(s, s, s, s, device=dev)) 

179 

180 

181def speech_to_text_local(file, verbose = False): 

182 from faster_whisper import WhisperModel 

183 import time 

184 

185 model_size = "large-v2" 

186 model_size = "tiny" 

187 

188 # Run on GPU with FP16 

189# model = WhisperModel(model_size, device="cuda", compute_type="float16") 

190 

191 # or run on GPU with INT8 

192# model = WhisperModel(model_size, device="cuda", compute_type="int8_float16") 

193 # or run on CPU with INT8 

194 model = WhisperModel(model_size, device="cpu", compute_type="int8") 

195 

196 

197 

198 start = time.time() 

199 segments, info = model.transcribe(file, beam_size=5) 

200 segments = list(segments) 

201 end = time.time() 

202 duration = end - start 

203 print(" Duration transcribe : " + str(duration)) 

204 

205 print("Detected language '%s' with probability %f" % (info.language, info.language_probability)) 

206 

207 for segment in segments: 

208 print("[%.2fs -> %.2fs] %s" % (segment.start, segment.end, segment.text)) 

209 

210 

211def speech_to_text_local_oai(file, verbose = False, language="fr"): # pragma no cover 

212 import whisper 

213 import torch 

214 cpu = False 

215 language=None 

216 

217 import ssl 

218 if hasattr(ssl, '_create_unverified_context'): 

219 ssl._create_default_https_context = ssl._create_unverified_context 

220 

221 if not cpu: 

222# import torch 

223# torch.cuda.init() 

224## force_cudnn_initialization() 

225 device = "cuda" if torch.cuda.is_available() else "cpu" 

226 else : 

227 device = "cpu" 

228# print(" device : " + str(device)) 

229 

230 # list models https://blog.ml6.eu/whisper-deployment-decisions-part-i-evaluating-latency-costs-and-performance-metrics-d07f6edc9ec0 

231 

232 model = whisper.load_model("tiny").to(device) 

233 

234 print("trying to read mais voilr") 

235 

236# import soundfile as sf 

237# audio_data, sample_rate = sf.read(file, always_2d=True) 

238 

239 import time 

240 start = time.time() 

241 if not cpu and torch.cuda.is_available(): 

242 with torch.cuda.device(device): 

243 end = time.time() 

244 duration = end - start 

245 print(" Duration load : " + str(duration)) 

246 start = time.time() 

247 result = model.transcribe(file, language=language, fp16=False, word_timestamps=True) 

248 end = time.time() 

249 duration = end - start 

250 print(" Duration inference : " + str(duration)) 

251 start = time.time() 

252 else: 

253 result = model.transcribe(file) 

254 

255 end = time.time() 

256 duration = end - start 

257 print(" Duration : " + str(duration)) 

258 print(result["text"]) 

259 

260 

261 

262import numpy as np 

263import sys 

264from lib.lib_plot import plot_oned 

265import random 

266import math 

267 

268# VR 26-5-23 TODO : on avait commmencer à abstraire puis plus du tout ! 

269# VR 26-5-23 TODO En fait il y a interlocuteur et l'autre/ appelant/appelé 

270# VR 26-5-23 TODO : aller plus loin : live / multi-interlocuteur 

271def analyze_sound(in_file, verbose = False): # pragma no cover 

272 from pydub import AudioSegment 

273 

274 sound = AudioSegment.from_mp3(in_file) 

275 

276 data = {} 

277 

278 print(str(len(sound))) 

279 init = True 

280 

281 param = 5000.0 

282 param2 = 100.0 

283 

284# list_segments_victor = [] 

285# list_segments_dalal = [] 

286# audio_victor = None 

287# audio_dalal = None 

288 

289 init_seg = False 

290 

291 time = 0 

292 for seg in sound: 

293 if time > 1: 

294 if init_seg: 

295 audio_victor = seg 

296 audio_dalal = seg 

297 else : 

298# if moyen_exp_dBFS > moyen2_exp_dBFS: 

299 if moyen_exp_dBFS < -50 : 

300 audio_dalal += seg 

301 else : 

302 audio_victor += seg 

303 if init: 

304 moyen_exp_dBFS = seg.max_dBFS 

305 moyen2_exp_dBFS = seg.max_dBFS 

306 init = False 

307 audio_victor = seg 

308 audio_dalal = seg 

309 else : 

310 if not math.isinf(seg.dBFS): 

311 moyen_exp_dBFS = 1/param * seg.dBFS + (1 - 1/param) * moyen_exp_dBFS # += would also work 

312 moyen2_exp_dBFS = 1/param2 * seg.dBFS + (1 - 1/param2) * moyen2_exp_dBFS # += would also work 

313 if math.isinf(moyen_exp_dBFS): 

314 print("Grosses caca") 

315 

316 if random.randint(0, 100) > 0: 

317 time += seg.duration_seconds 

318 continue 

319 

320 # max_dBFS 

321 # dBFS 

322 # max 

323 # rms 

324 # sample_width 

325 # max_possible_amplitude 

326 

327 if verbose : 

328 if random.randint(0, 1000) == 0: 

329 sys.stdout.write(str(int(time)) + ",") 

330 # CA c'est n'importequoi ! 

331 if random.randint(0, 100000) == 0: 

332 print("") 

333# if random.randint(0, 1000000) == 0: 

334# break 

335# if time > 1000: 

336# break 

337 

338# mean_intensity = np.mean(sound[t]) 

339 data[time] = (moyen_exp_dBFS, moyen2_exp_dBFS, seg.dBFS, seg.max_dBFS, seg.max, seg.rms) 

340 

341 time += seg.duration_seconds 

342 

343 print("Exporting") 

344 

345 audio_dalal.export(in_file + ".dalal.mp3", format="mp3") 

346 audio_victor.export(in_file + ".victor.mp3", format="mp3") 

347 

348 print("Ploting") 

349 

350 plot_oned(data, in_file) 

351 

352 print("DONE !") 

353 

354 

355