File size: 1,764 Bytes
c0451b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2973f2a
c0451b6
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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

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()