Spaces:
Runtime error
Runtime error
import gradio as gr | |
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor, GPT2LMHeadModel, GPT2Tokenizer, VitsProcessor, VitsForConditionalGeneration | |
# Load the ASR model and processor | |
asr_processor = Wav2Vec2Processor.from_pretrained("/path/to/canary/processor") | |
asr_model = Wav2Vec2ForCTC.from_pretrained("/path/to/canary/model") | |
# Load the text processing model and tokenizer | |
proc_tokenizer = GPT2Tokenizer.from_pretrained("/path/to/phi3/tokenizer") | |
proc_model = GPT2LMHeadModel.from_pretrained("/path/to/phi3/model") | |
# Load the TTS model and processor | |
tts_processor = VitsProcessor.from_pretrained("facebook/vits-base") | |
tts_model = VitsForConditionalGeneration.from_pretrained("facebook/vits-base") | |
def process_speech(speech): | |
# Convert the speech to text | |
inputs = asr_processor(speech, sampling_rate=16_000, return_tensors="pt", padding=True) | |
with torch.no_grad(): | |
logits = asr_model(inputs.input_values, attention_mask=inputs.attention_mask).logits | |
predicted_ids = torch.argmax(logits, dim=-1) | |
transcription = asr_processor.decode(predicted_ids[0]) | |
# 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) | |
processed_text = proc_tokenizer.decode(outputs[0], skip_special_tokens=True) | |
# Convert the processed text to speech | |
inputs = tts_processor(processed_text, return_tensors="pt") | |
with torch.no_grad(): | |
logits = tts_model(inputs["input_ids"]).logits | |
predicted_ids = torch.argmax(logits, dim=-1) | |
audio = tts_processor.decode(predicted_ids) | |
return audio | |
iface = gr.Interface(fn=process_speech, inputs=gr.inputs.Audio(source="microphone"), outputs="audio") | |
iface.launch() |