not-lain commited on
Commit
7d63d59
·
verified ·
1 Parent(s): b4e25b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -9
app.py CHANGED
@@ -4,33 +4,34 @@ import torch
4
  from datasets import load_dataset
5
 
6
  from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
7
- import spaces
 
8
 
9
 
10
  # load speech translation checkpoint
11
- asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-base",device="cuda")
12
 
13
  # load text-to-speech checkpoint and speaker embeddings
14
  processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
15
 
16
- model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts").to("cuda")
17
- vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to("cuda")
18
 
19
  embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
20
  speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
21
 
22
- @spaces.GPU
23
  def translate(audio):
24
- outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "transcribe", "language": "es"})
25
  return outputs["text"]
26
 
27
- @spaces.GPU
28
  def synthesise(text):
29
  inputs = processor(text=text, return_tensors="pt")
30
- speech = model.generate_speech(inputs["input_ids"].to("cuda"), speaker_embeddings.to("cuda"), vocoder=vocoder)
31
  return speech.cpu()
32
 
33
- @spaces.GPU
34
  def speech_to_speech_translation(audio):
35
  translated_text = translate(audio)
36
  synthesised_speech = synthesise(translated_text)
 
4
  from datasets import load_dataset
5
 
6
  from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
7
+
8
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
9
 
10
 
11
  # load speech translation checkpoint
12
+ asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-base",device=device)
13
 
14
  # load text-to-speech checkpoint and speaker embeddings
15
  processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
16
 
17
+ model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts").to(device)
18
+ vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(device)
19
 
20
  embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
21
  speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
22
 
23
+
24
  def translate(audio):
25
+ outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "transcribe", "language": "fr"})
26
  return outputs["text"]
27
 
28
+
29
  def synthesise(text):
30
  inputs = processor(text=text, return_tensors="pt")
31
+ speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
32
  return speech.cpu()
33
 
34
+
35
  def speech_to_speech_translation(audio):
36
  translated_text = translate(audio)
37
  synthesised_speech = synthesise(translated_text)