Alidr79 commited on
Commit
046eea1
1 Parent(s): 3a3d562

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
4
+ from datasets import load_dataset
5
+ from datasets import load_dataset
6
+ from transformers import pipeline
7
+ import numpy as np
8
+ import gradio as gr
9
+
10
+
11
+
12
+
13
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
+ asr_model = pipeline(task = "automatic-speech-recognition",
15
+ model = "distil-whisper/distil-small.en")
16
+
17
+
18
+
19
+
20
+
21
+ llm_model = AutoModelForCausalLM.from_pretrained("OuteAI/Lite-Mistral-150M-v2-Instruct").to(device)
22
+ tokenizer = AutoTokenizer.from_pretrained("OuteAI/Lite-Mistral-150M-v2-Instruct")
23
+
24
+ def generate_response(message: str, temperature: float = 0.3, repetition_penalty: float = 1.2) -> str:
25
+ # Apply the chat template and convert to PyTorch tensors
26
+ messages = [
27
+ {"role": "system",
28
+ "content": "You are my best close friend. you are full of human emotions and you have feelings. provide short responses."},
29
+ {"role": "user", "content": message}
30
+ ]
31
+ input_ids = tokenizer.apply_chat_template(
32
+ messages, add_generation_prompt=True, return_tensors="pt"
33
+ ).to(device)
34
+
35
+ # Generate the response
36
+ output = llm_model.generate(
37
+ input_ids,
38
+ max_length = 150,
39
+ pad_token_id=tokenizer.eos_token_id,
40
+ temperature=temperature,
41
+ repetition_penalty=repetition_penalty,
42
+ do_sample=True
43
+ )
44
+
45
+ # Decode the generated output
46
+ generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
47
+ return generated_text.split('\n assistant\n')[1]
48
+
49
+
50
+
51
+
52
+
53
+ processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
54
+ tts_model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
55
+ vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
56
+ embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
57
+ speaker_embeddings = torch.tensor(embeddings_dataset[7300]["xvector"]).unsqueeze(0)
58
+
59
+ def text_to_speech(input_text):
60
+ inputs = processor(text=input_text, return_tensors="pt")
61
+ speech = tts_model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
62
+ return speech
63
+
64
+
65
+
66
+
67
+
68
+ def spoken_llm(input_voice_file):
69
+ if input_voice_file is None:
70
+ gr.Warning("No input audio")
71
+ return None
72
+ asr_text = asr_model(input_voice_file)['text']
73
+ print("\n\nASR\n\n")
74
+ print(asr_text)
75
+ llm_response = generate_response(asr_text)
76
+ print("\n\nLLM\n\n")
77
+ print(llm_response)
78
+ audio_out = text_to_speech(llm_response)
79
+ print("\n\nTTS\n\n")
80
+ rate = 17000
81
+ return rate, (audio_out.cpu().numpy().reshape(-1)*2e4).astype(np.int16)
82
+
83
+
84
+
85
+ interface = gr.Interface(fn = spoken_llm, inputs = gr.Audio(sources = "microphone", type = "filepath"), outputs = "audio")
86
+ interface.launch()