File size: 2,861 Bytes
046eea1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
from datasets import load_dataset
from datasets import load_dataset
from transformers import pipeline
import numpy as np
import gradio as gr




device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
asr_model = pipeline(task = "automatic-speech-recognition",
               model = "distil-whisper/distil-small.en")





llm_model = AutoModelForCausalLM.from_pretrained("OuteAI/Lite-Mistral-150M-v2-Instruct").to(device)
tokenizer = AutoTokenizer.from_pretrained("OuteAI/Lite-Mistral-150M-v2-Instruct")

def generate_response(message: str, temperature: float = 0.3, repetition_penalty: float = 1.2) -> str:
    # Apply the chat template and convert to PyTorch tensors
    messages = [
        {"role": "system",
         "content": "You are my best close friend. you are full of human emotions and you have feelings. provide short responses."},
         {"role": "user", "content": message}
    ]
    input_ids = tokenizer.apply_chat_template(
        messages, add_generation_prompt=True, return_tensors="pt"
    ).to(device)

    # Generate the response
    output = llm_model.generate(
        input_ids,
        max_length = 150,
        pad_token_id=tokenizer.eos_token_id,
        temperature=temperature,
        repetition_penalty=repetition_penalty,
        do_sample=True
    ) 

    # Decode the generated output
    generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
    return generated_text.split('\n assistant\n')[1]





processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
tts_model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
speaker_embeddings = torch.tensor(embeddings_dataset[7300]["xvector"]).unsqueeze(0)

def text_to_speech(input_text):
    inputs = processor(text=input_text, return_tensors="pt")
    speech = tts_model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
    return speech





def spoken_llm(input_voice_file):
    if input_voice_file is None:
        gr.Warning("No input audio")
        return None
    asr_text = asr_model(input_voice_file)['text']
    print("\n\nASR\n\n")
    print(asr_text)
    llm_response = generate_response(asr_text)
    print("\n\nLLM\n\n")
    print(llm_response)
    audio_out = text_to_speech(llm_response)
    print("\n\nTTS\n\n")
    rate = 17000
    return rate, (audio_out.cpu().numpy().reshape(-1)*2e4).astype(np.int16)



interface = gr.Interface(fn = spoken_llm, inputs = gr.Audio(sources = "microphone", type = "filepath"), outputs = "audio")
interface.launch()