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

Delete app.py

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