Spaces:
Build error
Build error
VanguardAI
commited on
Commit
•
cab275d
1
Parent(s):
b29b41c
Update app.py
Browse files
app.py
CHANGED
@@ -1,121 +1,106 @@
|
|
1 |
-
import torch
|
2 |
-
import torchaudio
|
3 |
import gradio as gr
|
4 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
import wave
|
6 |
import numpy as np
|
7 |
-
from transformers import WhisperForCTC, WhisperProcessor, AutoModelForSeq2SeqLM, AutoTokenizer
|
8 |
-
from transformers import OpenVoiceV2Processor, OpenVoiceV2
|
9 |
|
10 |
# Load ASR model and processor
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
# Load text-to-text model and tokenizer
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
#
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
wake_word_detected = True
|
80 |
-
print("Wake word detected. Processing audio...")
|
81 |
-
|
82 |
-
while wake_word_detected:
|
83 |
-
# Capture audio here (this is a simplified example, you need actual audio capture logic)
|
84 |
-
time.sleep(2) # Simulate 2 seconds of audio capture
|
85 |
-
|
86 |
-
# Save the captured audio to the temp file for ASR
|
87 |
-
data, sample_rate = sf.read(tmp_wav_path)
|
88 |
-
sf.write(tmp_wav_path, data, sample_rate)
|
89 |
-
|
90 |
-
# Step 1: Transcribe audio to text
|
91 |
-
transcription = transcribe(tmp_wav_path)
|
92 |
-
|
93 |
-
# Step 2: Generate response using text-to-text model
|
94 |
-
response = generate_response(transcription)
|
95 |
-
|
96 |
-
# Step 3: Synthesize speech from text
|
97 |
-
synthesized_audio = synthesize_speech(response)
|
98 |
-
|
99 |
-
# Save the synthesized audio to a temporary file
|
100 |
-
output_path = "output.wav"
|
101 |
-
torchaudio.save(output_path, synthesized_audio.squeeze(1), 22050)
|
102 |
-
|
103 |
-
# Play the synthesized audio using simpleaudio
|
104 |
-
wave_obj = sa.WaveObject.from_wave_file(output_path)
|
105 |
-
play_obj = wave_obj.play()
|
106 |
-
play_obj.wait_done()
|
107 |
-
except KeyboardInterrupt:
|
108 |
-
print("Stopping...")
|
109 |
-
|
110 |
-
# Gradio interface
|
111 |
-
gr_interface = gr.Interface(
|
112 |
-
fn=real_time_pipeline,
|
113 |
-
inputs=None,
|
114 |
-
outputs=None,
|
115 |
-
live=True,
|
116 |
-
title="Real-Time Audio-to-Audio Model",
|
117 |
-
description="ASR + Text-to-Text Model + TTS with Human-like Voice and Emotions"
|
118 |
)
|
119 |
|
120 |
-
|
121 |
-
iface.launch(inline=False)
|
|
|
|
|
|
|
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,
|
28 |
+
return_timestamps=True,
|
29 |
+
torch_dtype=torch_dtype,
|
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()
|
|