voxxer commited on
Commit
daf595f
·
1 Parent(s): 844211b

Changed to Russian language

Browse files
Files changed (1) hide show
  1. app.py +16 -2
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  import numpy as np
3
  import torch
4
  from datasets import load_dataset
 
5
 
6
  from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
7
 
@@ -14,7 +15,7 @@ asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-base",
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")
@@ -22,7 +23,7 @@ speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze
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
 
@@ -31,9 +32,22 @@ def synthesise(text):
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)
38
  synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
39
  return 16000, synthesised_speech
 
2
  import numpy as np
3
  import torch
4
  from datasets import load_dataset
5
+ from transliterate import translit
6
 
7
  from transformers import SpeechT5ForTextToSpeech, SpeechT5HifiGan, SpeechT5Processor, pipeline
8
 
 
15
  # load text-to-speech checkpoint and speaker embeddings
16
  processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
17
 
18
+ model = SpeechT5ForTextToSpeech.from_pretrained("voxxer/speecht5_finetuned_commonvoice_ru_translit").to(device)
19
  vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(device)
20
 
21
  embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
 
23
 
24
 
25
  def translate(audio):
26
+ outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "translate", "language": "ru"})
27
  return outputs["text"]
28
 
29
 
 
32
  speech = model.generate_speech(inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder)
33
  return speech.cpu()
34
 
35
+ def cleanup_text(inputs):
36
+ replacements = [('«', '"'),
37
+ ('»', '"'),
38
+ ('‑', '-'),
39
+ ('–', '-'),
40
+ ('−', '-'),
41
+ ('…', '...'),
42
+ ]
43
+ for src, dst in replacements:
44
+ inputs = translit(inputs.replace(src, dst).lower(), 'ru', reversed=True)
45
+ return inputs
46
+
47
 
48
  def speech_to_speech_translation(audio):
49
  translated_text = translate(audio)
50
+ translated_text = cleanup_text(translated_text)
51
  synthesised_speech = synthesise(translated_text)
52
  synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
53
  return 16000, synthesised_speech