weiliming commited on
Commit
b230689
·
1 Parent(s): b99241b

support franch

Browse files
Files changed (2) hide show
  1. app.py +50 -12
  2. requirements.txt +2 -1
app.py CHANGED
@@ -3,32 +3,70 @@ 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
 
@@ -41,7 +79,7 @@ def speech_to_speech_translation(audio):
41
 
42
  title = "Cascaded STST"
43
  description = """
44
- Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any language to target speech in English. Demo uses OpenAI's [Whisper Base](https://huggingface.co/openai/whisper-base) model for speech translation, and Microsoft's
45
  [SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech:
46
 
47
  ![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
@@ -69,4 +107,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 (
7
+ SpeechT5ForTextToSpeech,
8
+ SpeechT5HifiGan,
9
+ SpeechT5Processor,
10
+ pipeline,
11
+ VitsModel,
12
+ VitsTokenizer,
13
+ )
14
 
15
 
16
  device = "cuda:0" if torch.cuda.is_available() else "cpu"
17
 
18
  # load speech translation checkpoint
19
+ asr_pipe = pipeline(
20
+ "automatic-speech-recognition", model="openai/whisper-base", device=device
21
+ )
22
 
23
+
24
+ # speecht5
25
  # load text-to-speech checkpoint and speaker embeddings
26
+ # processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
27
+ # model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts").to(device)
28
+ # vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(device)
29
 
30
+ # embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
31
+ # speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
32
 
33
+ # mms
34
+ model = VitsModel.from_pretrained("Matthijs/mms-tts-fra")
35
+ tokenizer = VitsTokenizer.from_pretrained("Matthijs/mms-tts-fra")
36
 
37
 
38
+ # 保持 main 函数 speech_to_speech_translation 不变
39
+ # 并根据需要仅更新 translate 和 synthesise 函数
40
  def translate(audio):
41
+ # outputs = asr_pipe(audio, max_new_tokens=256, generate_kwargs={"task": "translate"})
42
+ outputs = asr_pipe(
43
+ audio,
44
+ max_new_tokens=256,
45
+ generate_kwargs={"task": "transcribe", "language": "fr"},
46
+ # generate_kwargs={"task": "transcribe"},
47
+ )
48
+ print(outputs)
49
  return outputs["text"]
50
 
51
 
52
+ # speecht5
53
+ # def synthesise(text):
54
+ # inputs = processor(text=text, return_tensors="pt")
55
+ # speech = model.generate_speech(
56
+ # inputs["input_ids"].to(device), speaker_embeddings.to(device), vocoder=vocoder
57
+ # )
58
+ # return speech.cpu()
59
+
60
+
61
  def synthesise(text):
62
+ inputs = tokenizer(text, return_tensors="pt")
63
+ input_ids = inputs["input_ids"]
64
+
65
+ with torch.no_grad():
66
+ outputs = model(input_ids)
67
+
68
+ speech = outputs.audio[0]
69
+
70
  return speech.cpu()
71
 
72
 
 
79
 
80
  title = "Cascaded STST"
81
  description = """
82
+ Demo for cascaded speech-to-speech translation (STST), mapping from source speech in any language to target speech in Chinese. Demo uses OpenAI's [Whisper Base](https://huggingface.co/openai/whisper-base) model for speech translation, and Microsoft's
83
  [SpeechT5 TTS](https://huggingface.co/microsoft/speecht5_tts) model for text-to-speech:
84
 
85
  ![Cascaded STST](https://huggingface.co/datasets/huggingface-course/audio-course-images/resolve/main/s2st_cascaded.png "Diagram of cascaded speech to speech translation")
 
107
  with demo:
108
  gr.TabbedInterface([mic_translate, file_translate], ["Microphone", "Audio File"])
109
 
110
+ demo.launch(share=True)
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  torch
2
- git+https://github.com/huggingface/transformers
 
3
  datasets
4
  sentencepiece
 
1
  torch
2
+ # git+https://github.com/huggingface/transformers
3
+ git+https://github.com/hollance/transformers.git@6900e8ba6532162a8613d2270ec2286c3f58f57b
4
  datasets
5
  sentencepiece