preetam8 commited on
Commit
1588735
·
1 Parent(s): e970d56
Files changed (1) hide show
  1. app.py +1 -23
app.py CHANGED
@@ -1,23 +1,17 @@
1
  import gradio as gr
2
- import librosa
3
  import logging
4
  import numpy as np
5
  import torch
6
 
7
  from transformers import VitsModel, VitsTokenizer, pipeline
8
- from transformers import WhisperForConditionalGeneration, WhisperProcessor
9
 
10
 
11
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
12
 
13
- target_language = "fr"
14
 
15
  # load speech translation checkpoint
16
  asr_pipe = pipeline("automatic-speech-recognition", model="bofenghuang/whisper-small-cv11-french", device=device)
17
- # whisper_model_name = "openai/whisper-small"
18
- # whisper_processor = WhisperProcessor.from_pretrained(whisper_model_name)
19
- # whisper_model = WhisperForConditionalGeneration.from_pretrained(whisper_model_name)
20
- # decoder_ids = whisper_processor.get_decoder_prompt_ids(language=target_language, task="transcribe")
21
 
22
  # load text-to-speech checkpoint
23
  model = VitsModel.from_pretrained("facebook/mms-tts-fra")
@@ -28,21 +22,6 @@ def translate(audio):
28
  outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "transcribe", "language": target_language})
29
  return outputs["text"]
30
 
31
- # def translate(audio):
32
- # if isinstance(audio, str):
33
- # # Account for recorded audio
34
- # audio = {
35
- # "path": audio,
36
- # "sampling_rate": 16_000,
37
- # "array": librosa.load(audio, sr=16_000)[0]
38
- # }
39
- # elif audio["sampling_rate"] != 16_000:
40
- # audio["array"] = librosa.resample(audio["array"], audio["sampling_rate"], 16_000)
41
- # input_features = whisper_processor(audio["array"], sampling_rate=16000, return_tensors="pt").input_features
42
- # predicted_ids = whisper_model.generate(input_features, forced_decoder_ids=decoder_ids)
43
- # translated_text = whisper_processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
44
- # return translated_text
45
-
46
 
47
  def synthesise(text):
48
  inputs = tokenizer(text, return_tensors="pt")
@@ -55,7 +34,6 @@ def synthesise(text):
55
 
56
  def speech_to_speech_translation(audio):
57
  translated_text = translate(audio)
58
- logging.info(f"Translated Text: {translated_text}")
59
  synthesised_speech = synthesise(translated_text)
60
  synthesised_speech = (synthesised_speech.numpy() * 32767).astype(np.int16)
61
  return 16000, synthesised_speech
 
1
  import gradio as gr
 
2
  import logging
3
  import numpy as np
4
  import torch
5
 
6
  from transformers import VitsModel, VitsTokenizer, pipeline
 
7
 
8
 
9
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
10
 
11
+ target_language = "french"
12
 
13
  # load speech translation checkpoint
14
  asr_pipe = pipeline("automatic-speech-recognition", model="bofenghuang/whisper-small-cv11-french", device=device)
 
 
 
 
15
 
16
  # load text-to-speech checkpoint
17
  model = VitsModel.from_pretrained("facebook/mms-tts-fra")
 
22
  outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "transcribe", "language": target_language})
23
  return outputs["text"]
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
  def synthesise(text):
27
  inputs = tokenizer(text, return_tensors="pt")
 
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