preetam8 commited on
Commit
9e2a87c
·
1 Parent(s): 8732ac5

Revert to asr_pipeline

Browse files
Files changed (1) hide show
  1. app.py +8 -25
app.py CHANGED
@@ -3,42 +3,25 @@ import librosa
3
  import logging
4
  import numpy as np
5
  import torch
6
- from datasets import load_dataset
7
 
8
- from transformers import VitsModel, VitsTokenizer
9
- from transformers import WhisperForConditionalGeneration, WhisperProcessor
10
 
11
 
12
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
13
 
14
- target_language = "french"
 
15
  # load speech translation checkpoint
16
- whisper_model_name = "openai/whisper-base"
17
- whisper_processor = WhisperProcessor.from_pretrained(whisper_model_name)
18
- whisper_model = WhisperForConditionalGeneration.from_pretrained(whisper_model_name)
19
- decoder_ids = whisper_processor.get_decoder_prompt_ids(language=target_language, task="transcribe")
20
 
21
- # load text-to-speech checkpoint and speaker embeddings
22
  model = VitsModel.from_pretrained("facebook/mms-tts-fra")
23
  tokenizer = VitsTokenizer.from_pretrained("facebook/mms-tts-fra")
24
 
25
 
26
-
27
-
28
  def translate(audio):
29
- if isinstance(audio, str):
30
- # Account for recorded audio
31
- audio = {
32
- "path": audio,
33
- "sampling_rate": 16_000,
34
- "array": librosa.load(audio, sr=16_000)[0]
35
- }
36
- elif audio["sampling_rate"] != 16_000:
37
- audio["array"] = librosa.resample(audio["array"], audio["sampling_rate"], 16_000)
38
- input_features = whisper_processor(audio["array"], sampling_rate=16000, return_tensors="pt").input_features
39
- predicted_ids = whisper_model.generate(input_features, forced_decoder_ids=decoder_ids)
40
- translated_text = whisper_processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
41
- return translated_text
42
 
43
 
44
  def synthesise(text):
@@ -88,4 +71,4 @@ with demo:
88
  gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
89
 
90
  logging.getLogger().setLevel(logging.INFO)
91
- demo.launch()
 
3
  import logging
4
  import numpy as np
5
  import torch
 
6
 
7
+ from transformers import VitsModel, VitsTokenizer, pipeline
 
8
 
9
 
10
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
11
 
12
+ target_language = "fr"
13
+
14
  # load speech translation checkpoint
15
+ asr_pipe = pipeline("automatic-speech-recognition", model="openai/whisper-base", device=device)
 
 
 
16
 
17
+ # load text-to-speech checkpoint
18
  model = VitsModel.from_pretrained("facebook/mms-tts-fra")
19
  tokenizer = VitsTokenizer.from_pretrained("facebook/mms-tts-fra")
20
 
21
 
 
 
22
  def translate(audio):
23
+ outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "transcribe", "language": target_language})
24
+ return outputs["text"]
 
 
 
 
 
 
 
 
 
 
 
25
 
26
 
27
  def synthesise(text):
 
71
  gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
72
 
73
  logging.getLogger().setLevel(logging.INFO)
74
+ demo.launch()