swan387 commited on
Commit
87dc097
·
verified ·
1 Parent(s): dbfdf1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -14
app.py CHANGED
@@ -3,33 +3,35 @@ import numpy as np
3
  import torch
4
  from datasets import load_dataset
5
 
6
- from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
7
 
8
 
9
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
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": "translate"})
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):
@@ -69,4 +71,4 @@ file_translate = gr.Interface(
69
  with demo:
70
  gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
71
 
72
- demo.launch()
 
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"
10
 
11
+ asr_model_id = "openai/whisper-base"
12
+ asr_processor = WhisperProcessor.from_pretrained(asr_model_id, language="es", task="transcribe")
13
  # load speech translation checkpoint
14
+ asr_pipe = pipeline(
15
+ "automatic-speech-recognition",
16
+ model=asr_model_id,
17
+ feature_extractor=asr_processor.feature_extractor,
18
+ tokenizer=asr_processor.tokenizer,
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):
27
+ outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "transcribe", "language": "es", "forced_decoder_ids": asr_processor.tokenizer.get_decoder_prompt_ids(language="es", task="translate")})
28
  return outputs["text"]
29
 
30
 
31
  def synthesise(text):
32
+ inputs = processor(text, voice_preset="v2/es_speaker_3")
33
+ speech = model.generate(**inputs).cpu().numpy()
34
+ return speech
35
 
36
 
37
  def speech_to_speech_translation(audio):
 
71
  with demo:
72
  gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
73
 
74
+ demo.launch()