StormblessedKal commited on
Commit
508fd98
1 Parent(s): fc16268

try two methods

Browse files
Files changed (2) hide show
  1. src/predict.py +81 -42
  2. src/rp_handler.py +4 -1
src/predict.py CHANGED
@@ -206,7 +206,7 @@ class Predictor:
206
  return {"url": file_url}
207
 
208
 
209
- def predict(self,s3_url,passage,process_audio,method_type='voice_clone'):
210
  output_dir = 'processed'
211
  gen_id = str(uuid.uuid4())
212
  os.makedirs(output_dir,exist_ok=True)
@@ -223,48 +223,43 @@ class Predictor:
223
  bucket_name = 'demovidelyuseruploads'
224
  local_file_path = os.path.join(raw_dir,s3_key)
225
  self.download_file_from_s3(self.s3_client,bucket_name,s3_key,local_file_path)
226
-
227
- se_extractor.generate_voice_segments(local_file_path,segments_dir,vad=True)
228
-
229
- if method_type == 'voice_clone':
230
- #voice_clone with styletts2
231
- model,sampler = self.model,self.sampler
232
- processed_seg_dir = os.path.join(segments_dir,s3_key.split('.')[0],'wavs')
233
- result = self.process_audio_file(local_file_path,passage,model,sampler)
234
- final_output = os.path.join(results_dir,f"{gen_id}-voice-clone-1.wav")
235
-
236
- sf.write(final_output,result,24000)
237
- if process_audio:
238
- (new_sr, wav1) = self._fn(final_output,"Midpoint",32,0.5)
239
- sf.write(final_output,wav1,new_sr)
240
 
241
- base_speaker_tts,tone_color_converter = self.base_speaker_tts,self.tone_color_converter
242
- reference_speaker = local_file_path
243
- target_se, audio_name = se_extractor.get_se(reference_speaker, tone_color_converter, target_dir=openvoice_dir, vad=False)
244
- src_path = os.path.join(results_dir,f"{gen_id}-tmp.wav")
245
- openvoice_output = os.path.join(results_dir,f"{gen_id}-voice-clone-2.wav")
246
- base_speaker_tts.tts(passage,src_path,speaker='default',language='English',speed=1.0)
247
-
248
- source_se = torch.load(f'{self.ckpt_base}/en_default_se.pth').to(self.device)
249
- tone_color_converter.convert(audio_src_path=src_path,src_se=source_se,tgt_se=target_se,output_path=openvoice_output,message='')
250
- if process_audio:
251
- (new_sr, wav1) = self._fn(openvoice_output,"Midpoint",32,0.5)
252
- sf.write(openvoice_output,wav1,new_sr)
253
-
254
-
255
- mp3_final_output_1 = str(final_output).replace('wav','mp3')
256
- mp3_final_output_2 = str(openvoice_output).replace('wav','mp3')
257
- self.convert_wav_to_mp3(final_output,mp3_final_output_1)
258
- self.convert_wav_to_mp3(openvoice_output,mp3_final_output_2)
259
- print(mp3_final_output_1)
260
- print(mp3_final_output_2)
261
-
262
- self.upload_file_to_s3(mp3_final_output_1,'demovidelyusergenerations',f"{gen_id}-voice-clone-1.mp3")
263
- self.upload_file_to_s3(mp3_final_output_2,'demovidelyusergenerations',f"{gen_id}-voice-clone-2.mp3")
264
- shutil.rmtree(os.path.join(output_dir,gen_id))
265
- return {"voice_clone_1":f"https://demovidelyusergenerations.s3.amazonaws.com/{gen_id}-voice-clone-1.mp3",
266
- "voice_clone_2":f"https://demovidelyusergenerations.s3.amazonaws.com/{gen_id}-voice-clone-2.mp3"
267
- }
 
 
 
 
 
268
  if method_type == 'voice_clone_with_emotions':
269
  try:
270
  print("INSIDE emotions")
@@ -324,6 +319,50 @@ class Predictor:
324
  }
325
 
326
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
  def _fn(self,path, solver, nfe, tau):
328
  if path is None:
329
  return None, None
 
206
  return {"url": file_url}
207
 
208
 
209
+ def predict(self,s3_url,passage,process_audio):
210
  output_dir = 'processed'
211
  gen_id = str(uuid.uuid4())
212
  os.makedirs(output_dir,exist_ok=True)
 
223
  bucket_name = 'demovidelyuseruploads'
224
  local_file_path = os.path.join(raw_dir,s3_key)
225
  self.download_file_from_s3(self.s3_client,bucket_name,s3_key,local_file_path)
226
+ #voice_clone with styletts2
227
+ model,sampler = self.model,self.sampler
228
+ result = self.process_audio_file(local_file_path,passage,model,sampler)
229
+ final_output = os.path.join(results_dir,f"{gen_id}-voice-clone-1.wav")
 
 
 
 
 
 
 
 
 
 
230
 
231
+ sf.write(final_output,result,24000)
232
+ if process_audio:
233
+ (new_sr, wav1) = self._fn(final_output,"Midpoint",32,0.5)
234
+ sf.write(final_output,wav1,new_sr)
235
+
236
+ base_speaker_tts,tone_color_converter = self.base_speaker_tts,self.tone_color_converter
237
+ reference_speaker = local_file_path
238
+ target_se, audio_name = se_extractor.get_se(reference_speaker, tone_color_converter, target_dir=openvoice_dir, vad=False)
239
+ src_path = os.path.join(results_dir,f"{gen_id}-tmp.wav")
240
+ openvoice_output = os.path.join(results_dir,f"{gen_id}-voice-clone-2.wav")
241
+ base_speaker_tts.tts(passage,src_path,speaker='default',language='English',speed=1.0)
242
+
243
+ source_se = torch.load(f'{self.ckpt_base}/en_default_se.pth').to(self.device)
244
+ tone_color_converter.convert(audio_src_path=src_path,src_se=source_se,tgt_se=target_se,output_path=openvoice_output,message='')
245
+ if process_audio:
246
+ (new_sr, wav1) = self._fn(openvoice_output,"Midpoint",32,0.5)
247
+ sf.write(openvoice_output,wav1,new_sr)
248
+
249
+
250
+ mp3_final_output_1 = str(final_output).replace('wav','mp3')
251
+ mp3_final_output_2 = str(openvoice_output).replace('wav','mp3')
252
+ self.convert_wav_to_mp3(final_output,mp3_final_output_1)
253
+ self.convert_wav_to_mp3(openvoice_output,mp3_final_output_2)
254
+ print(mp3_final_output_1)
255
+ print(mp3_final_output_2)
256
+
257
+ self.upload_file_to_s3(mp3_final_output_1,'demovidelyusergenerations',f"{gen_id}-voice-clone-1.mp3")
258
+ self.upload_file_to_s3(mp3_final_output_2,'demovidelyusergenerations',f"{gen_id}-voice-clone-2.mp3")
259
+ shutil.rmtree(os.path.join(output_dir,gen_id))
260
+ return {"voice_clone_1":f"https://demovidelyusergenerations.s3.amazonaws.com/{gen_id}-voice-clone-1.mp3",
261
+ "voice_clone_2":f"https://demovidelyusergenerations.s3.amazonaws.com/{gen_id}-voice-clone-2.mp3"
262
+ }
263
  if method_type == 'voice_clone_with_emotions':
264
  try:
265
  print("INSIDE emotions")
 
319
  }
320
 
321
 
322
+
323
+ def predict_with_emotions(self,s3_url,passage,process_audio):
324
+ output_dir = 'processed'
325
+ gen_id = str(uuid.uuid4())
326
+ os.makedirs(output_dir,exist_ok=True)
327
+ raw_dir = os.path.join(output_dir,gen_id,'raw')
328
+ segments_dir = os.path.join(output_dir,gen_id,'segments')
329
+ results_dir = os.path.join(output_dir,gen_id,'results')
330
+ openvoice_dir = os.path.join(output_dir,gen_id,'openvoice')
331
+ os.makedirs(raw_dir)
332
+ os.makedirs(segments_dir)
333
+ os.makedirs(results_dir)
334
+
335
+
336
+ s3_key = s3_url.split('/')[-1]
337
+ bucket_name = 'demovidelyuseruploads'
338
+ local_file_path = os.path.join(raw_dir,s3_key)
339
+ self.download_file_from_s3(self.s3_client,bucket_name,s3_key,local_file_path)
340
+ try:
341
+ print("INSIDE new emotions method")
342
+ base_speaker_tts,tone_color_converter = self.base_speaker_tts,self.tone_color_converter
343
+ reference_speaker = local_file_path
344
+ print("here 1")
345
+ target_se, audio_name = se_extractor.get_se(reference_speaker, tone_color_converter, target_dir=openvoice_dir, vad=False)
346
+ print("here 2")
347
+ src_path = os.path.join(results_dir,f"{gen_id}-tmp-emotions.wav")
348
+ openvoice_output = os.path.join(results_dir,f"{gen_id}-4.wav")
349
+ base_speaker_tts.tts(passage,src_path,speaker='default',language='English',speed=1.0,use_emotions=True)
350
+ source_se = torch.load(f'{self.ckpt_base}/en_style_se.pth').to(self.device)
351
+ tone_color_converter.convert(audio_src_path=src_path,src_se=source_se,tgt_se=target_se,output_path=openvoice_output,message='')
352
+ if process_audio:
353
+ (new_sr, wav1) = self._fn(openvoice_output,"Midpoint",32,0.5)
354
+ sf.write(openvoice_output,wav1,new_sr)
355
+
356
+ mp3_final_output_1 = str(openvoice_output).replace('wav','mp3')
357
+ self.convert_wav_to_mp3(openvoice_output,mp3_final_output_1)
358
+ print(mp3_final_output_1)
359
+ self.upload_file_to_s3(mp3_final_output_1,'demovidelyusergenerations',f"{gen_id}-voice-with-emotions.mp3")
360
+ shutil.rmtree(os.path.join(output_dir,gen_id))
361
+ return {"voice_clone_with_emotions":f"https://demovidelyusergenerations.s3.amazonaws.com/{gen_id}-voice-with-emotions.mp3"
362
+ }
363
+ except Exception as e:
364
+ return {"error":f"Unexpected error{e}"}
365
+
366
  def _fn(self,path, solver, nfe, tau):
367
  if path is None:
368
  return None, None
src/rp_handler.py CHANGED
@@ -49,7 +49,10 @@ def run_voice_clone_job(job):
49
  if process_audio is None:
50
  process_audio = False
51
 
52
- result = MODEL.predict(s3_url,passage,process_audio,method_type)
 
 
 
53
 
54
  return result
55
 
 
49
  if process_audio is None:
50
  process_audio = False
51
 
52
+ if method_type == 'voice_clone':
53
+ result = MODEL.predict(s3_url,passage,process_audio)
54
+ if method_type == 'voice_clone_with_emotions':
55
+ result = MODEL.predict_with_emotions(s3_url,passage,process_audio)
56
 
57
  return result
58