swan387 commited on
Commit
a81d9e8
1 Parent(s): 5583619

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -10
app.py CHANGED
@@ -3,7 +3,7 @@ import numpy as np
3
  import torch
4
  from datasets import load_dataset
5
 
6
- from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline, WhisperProcessor, BarkModel, BarkProcessor
7
 
8
 
9
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
@@ -19,8 +19,12 @@ asr_pipe = pipeline(
19
  device=device)
20
 
21
  # load text-to-speech checkpoint and speaker embeddings
22
- processor = BarkProcessor.from_pretrained("suno/bark-small")
23
- model = BarkModel.from_pretrained("suno/bark-small").to(device)
 
 
 
 
24
 
25
 
26
  def translate(audio):
@@ -29,17 +33,15 @@ def translate(audio):
29
 
30
 
31
  def synthesise(text):
32
- inputs = processor(text, voice_preset="v2/es_speaker_3")
33
- speech = model.generate(**inputs).cpu()
34
- return speech
35
 
36
 
37
  def speech_to_speech_translation(audio):
38
  translated_text = translate(audio)
39
  synthesised_speech = synthesise(translated_text)
40
- synthesised_speech = synthesised_speech.numpy()
41
- synthesised_speech = np.clip(synthesised_speech, -1.0, 1.0)
42
- synthesised_speech = (synthesised_speech * 32767).astype(np.int16)
43
  return 16000, synthesised_speech
44
 
45
 
@@ -73,4 +75,4 @@ file_translate = gr.Interface(
73
  with demo:
74
  gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
75
 
76
- demo.launch()
 
3
  import torch
4
  from datasets import load_dataset
5
 
6
+ from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline, WhisperProcessor
7
 
8
 
9
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
 
19
  device=device)
20
 
21
  # load text-to-speech checkpoint and speaker embeddings
22
+ processor = SpeechT5Processor.from_pretrained("Sandiago21/speecht5_finetuned_facebook_voxpopuli_spanish")
23
+ model = SpeechT5ForTextToSpeech.from_pretrained("Sandiago21/speecht5_finetuned_facebook_voxpopuli_spanish").to(device)
24
+ vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(device)
25
+
26
+ embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
27
+ speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
28
 
29
 
30
  def translate(audio):
 
33
 
34
 
35
  def synthesise(text):
36
+ inputs = processor(text=text, return_tensors="pt")
37
+ speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
38
+ return speech.cpu()
39
 
40
 
41
  def speech_to_speech_translation(audio):
42
  translated_text = translate(audio)
43
  synthesised_speech = synthesise(translated_text)
44
+ synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
 
 
45
  return 16000, synthesised_speech
46
 
47
 
 
75
  with demo:
76
  gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
77
 
78
+ demo.launch()