VanguardAI commited on
Commit
625d84b
1 Parent(s): cab275d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -79
app.py CHANGED
@@ -1,27 +1,27 @@
1
- import gradio as gr
2
  import torch
3
- import spaces
4
- from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
5
  from datasets import load_dataset
6
- from openvoice.api import ToneColorConverter
7
  from openvoice import se_extractor
8
- from melo.api import TTS
9
- import pyaudio
10
- import wave
11
- import numpy as np
12
 
13
- # Load ASR model and processor
14
  torch_dtype = torch.float16
15
 
16
- asr_model_id = "openai/whisper-large-v3"
17
- asr_model = AutoModelForSpeechSeq2Seq.from_pretrained(asr_model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True)
18
- asr_processor = AutoProcessor.from_pretrained(asr_model_id)
19
-
20
- asr_pipeline = pipeline(
 
 
21
  "automatic-speech-recognition",
22
- model=asr_model,
23
- tokenizer=asr_processor.tokenizer,
24
- feature_extractor=asr_processor.feature_extractor,
25
  max_new_tokens=128,
26
  chunk_length_s=30,
27
  batch_size=16,
@@ -30,77 +30,64 @@ asr_pipeline = pipeline(
30
  device=device,
31
  )
32
 
33
- # Load text-to-text model and tokenizer
34
- text_model_id = "meta-llama/Meta-Llama-3-8B"
35
- text_model = AutoModelForSeq2SeqLM.from_pretrained(text_model_id)
36
- text_tokenizer = AutoTokenizer.from_pretrained(text_model_id)
37
-
38
- # Load TTS model and vocoder
39
- tts_converter_ckpt = 'checkpoints_v2/converter'
40
- tts_output_dir = 'outputs_v2'
41
- os.makedirs(tts_output_dir, exist_ok=True)
42
 
43
- tts_converter = ToneColorConverter(f'{tts_converter_ckpt}/config.json')
44
- tts_converter.load_ckpt(f'{tts_converter_ckpt}/checkpoint.pth')
 
 
45
 
46
- reference_speaker = 'resources/example_reference.mp3' # This is the voice you want to clone
47
- target_se, _ = se_extractor.get_se(reference_speaker, tts_converter, vad=False)
48
 
49
- def process_audio(input_audio):
50
- # Perform ASR
51
- asr_result = asr_pipeline(input_audio)["text"]
52
 
53
- # Perform text-to-text processing
54
- input_ids = text_tokenizer(asr_result, return_tensors="pt").input_ids.to(device)
55
- generated_ids = text_model.generate(input_ids, max_length=512)
56
- response_text = text_tokenizer.decode(generated_ids[0], skip_special_tokens=True)
57
 
58
- # Perform TTS
59
- tts_model = TTS(language='EN', device=device)
60
- speaker_id = list(tts_model.hps.data.spk2id.values())[0]
61
- tts_model.tts_to_file(response_text, speaker_id, f'{tts_output_dir}/tmp.wav')
62
- save_path = f'{tts_output_dir}/output_v2.wav'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
- source_se = torch.load(f'checkpoints_v2/base_speakers/ses/english-american.pth', map_location=device)
65
- tts_converter.convert(audio_src_path=f'{tts_output_dir}/tmp.wav', src_se=source_se, tgt_se=target_se, output_path=save_path, message="@MyShell")
66
-
67
  return save_path
68
-
69
- # Real-time audio processing
70
-
71
- def real_time_audio_processing():
72
- p = pyaudio.PyAudio()
73
- stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024)
74
 
75
- frames = []
76
- print("Listening...")
77
-
78
- while True:
79
- data = stream.read(1024)
80
- frames.append(data)
81
- audio_data = np.frombuffer(data, dtype=np.int16)
82
- if np.max(audio_data) > 3000: # Simple VAD threshold
83
- wf = wave.open("input_audio.wav", 'wb')
84
- wf.setnchannels(1)
85
- wf.setsampwidth(p.get_sample_size(pyaudio.paInt16))
86
- wf.setframerate(16000)
87
- wf.writeframes(b''.join(frames))
88
- wf.close()
89
- return "input_audio.wav"
90
-
91
- # Gradio Interface
92
- @spaces.GPU(duration=300)
93
- def main():
94
- input_audio_path = real_time_audio_processing()
95
- if input_audio_path:
96
- output_audio_path = process_audio(input_audio_path)
97
- return output_audio_path
98
 
 
99
  iface = gr.Interface(
100
- fn=main,
101
- inputs=None,
102
- outputs=gr.Audio(type="filepath"),
103
- live=True
 
 
104
  )
105
 
106
- iface.launch()
 
 
1
+ import os
2
  import torch
3
+ from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
4
+ from transformers import LlamaForCausalLM, LlamaTokenizer
5
  from datasets import load_dataset
 
6
  from openvoice import se_extractor
7
+ from openvoice.api import BaseSpeakerTTS, ToneColorConverter
8
+ import gradio as gr
9
+ import spaces
 
10
 
11
+ # Device setup
12
  torch_dtype = torch.float16
13
 
14
+ # Whisper setup
15
+ whisper_model_id = "openai/whisper-large-v3"
16
+ whisper_model = AutoModelForSpeechSeq2Seq.from_pretrained(
17
+ whisper_model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
18
+ )
19
+ whisper_processor = AutoProcessor.from_pretrained(whisper_model_id)
20
+ whisper_pipe = pipeline(
21
  "automatic-speech-recognition",
22
+ model=whisper_model,
23
+ tokenizer=whisper_processor.tokenizer,
24
+ feature_extractor=whisper_processor.feature_extractor,
25
  max_new_tokens=128,
26
  chunk_length_s=30,
27
  batch_size=16,
 
30
  device=device,
31
  )
32
 
33
+ # LLaMa3-8B setup
34
+ llama_model_id = "meta-llama/Meta-Llama-3-8B"
35
+ llama_tokenizer = LlamaTokenizer.from_pretrained(llama_model_id)
36
+ llama_model = LlamaForCausalLM.from_pretrained(llama_model_id, torch_dtype=torch_dtype)
 
 
 
 
 
37
 
38
+ # OpenVoiceV2 setup
39
+ ckpt_base = 'checkpoints/base_speakers/EN'
40
+ ckpt_converter = 'checkpoints/converter'
41
+ output_dir = 'outputs'
42
 
43
+ base_speaker_tts = BaseSpeakerTTS(f'{ckpt_base}/config.json',)
44
+ base_speaker_tts.load_ckpt(f'{ckpt_base}/checkpoint.pth')
45
 
46
+ tone_color_converter = ToneColorConverter(f'{ckpt_converter}/config.json',)
47
+ tone_color_converter.load_ckpt(f'{ckpt_converter}/checkpoint.pth')
 
48
 
49
+ os.makedirs(output_dir, exist_ok=True)
50
+ source_se = torch.load(f'{ckpt_base}/en_default_se.pth').to(device)
 
 
51
 
52
+ def process_audio(input_audio):
53
+ # ASR with Whisper
54
+ whisper_result = whisper_pipe(input_audio)["text"]
55
+
56
+ # Text generation with LLaMa
57
+ inputs = llama_tokenizer(whisper_result, return_tensors="pt").to(device)
58
+ outputs = llama_model.generate(**inputs, max_new_tokens=50)
59
+ generated_text = llama_tokenizer.decode(outputs[0], skip_special_tokens=True)
60
+
61
+ # TTS with OpenVoiceV2
62
+ reference_speaker = 'resources/example_reference.mp3'
63
+ target_se, _ = se_extractor.get_se(reference_speaker, tone_color_converter, target_dir='processed', vad=True)
64
+ save_path = f'{output_dir}/output_en_default.wav'
65
+ src_path = f'{output_dir}/tmp.wav'
66
+
67
+ base_speaker_tts.tts(generated_text, src_path, speaker='default', language='English', speed=1.0)
68
+ tone_color_converter.convert(
69
+ audio_src_path=src_path,
70
+ src_se=source_se,
71
+ tgt_se=target_se,
72
+ output_path=save_path,
73
+ message="@MyShell"
74
+ )
75
 
 
 
 
76
  return save_path
 
 
 
 
 
 
77
 
78
+ @spaces.GPU()
79
+ def real_time_processing(input_audio):
80
+ return process_audio(input_audio)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
 
82
+ # Gradio interface
83
  iface = gr.Interface(
84
+ fn=real_time_processing,
85
+ inputs=gr.Audio(source="microphone", type="filepath"),
86
+ outputs=gr.Audio(type="file"),
87
+ live=True,
88
+ title="ASR + Text-to-Text + TTS with Whisper, LLaMa3-8B, and OpenVoiceV2",
89
+ description="Real-time processing using Whisper for ASR, LLaMa3-8B for text generation, and OpenVoiceV2 for TTS."
90
  )
91
 
92
+ if __name__ == "__main__":
93
+ iface.launch()