Chatty_Ashe / temp_store.py
gdnartea's picture
Update temp_store.py
2973f2a verified
raw
history blame
1.76 kB
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, VitsModel
from nemo.collections.asr.models import EncDecMultiTaskModel
# load speech to text model
canary_model = EncDecMultiTaskModel.from_pretrained('nvidia/canary-1b')
canary_model.eval()
canary_model.to('cpu')
# update decode params
canary_model.change_decoding_strategy(None)
decode_cfg = canary_model.cfg.decoding
decode_cfg.beam.beam_size = 1
canary_model.change_decoding_strategy(decode_cfg)
# Load the text processing model and tokenizer
proc_tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct")
proc_model = AutoModelForCausalLM.from_pretrained(
"microsoft/Phi-3-mini-4k-instruct",
trust_remote_code=True,
)
proc_model.eval()
proc_model.to('cpu')
# Load the TTS model
tts_model = VitsModel.from_pretrained("facebook/mms-tts-eng")
tts_tokenizer = AutoTokenizer.from_pretrained("facebook/mms-tts-eng")
tts_model.eval()
tts_model.to('cpu')
def process_speech(speech):
# Convert the speech to text
transcription = canary_model.transcribe(
speech,
logprobs=False,
)
# Process the text
inputs = proc_tokenizer.encode(transcription + proc_tokenizer.eos_token, return_tensors='pt')
outputs = proc_model.generate(inputs, max_length=100, temperature=0.7, pad_token_id=proc_tokenizer.eos_token_id)
text = proc_tokenizer.decode(outputs[0], skip_special_tokens=True)
processed_text = tts_tokenizer(text, return_tensors="pt")
# Convert the processed text to speech
with torch.no_grad():
audio = tts_model(**inputs).waveform
return audio
iface = gr.Interface(fn=process_speech, inputs=gr.Audio(source="microphone"), outputs="audio")
iface.launch()