-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
284 lines (237 loc) · 9.27 KB
/
app.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import io
import json
import mmap
import numpy
import torchaudio
import torch
#import spaces #comment out if you using Zero-GPU
import datetime
from collections import defaultdict
from pathlib import Path
from seamless_communication.inference import Translator
from transformers import pipeline
from transformers.pipelines.audio_utils import ffmpeg_read
import tempfile
import os
print(os.system("python -m pip install --upgrade pip"))
# print(os.system("pip install torch==2.1.1 torchvision==0.16.1 torchaudio==2.1.1 --index-url https://download.pytorch.org/whl/cu118"))
from huggingface_hub import InferenceClient
import gradio as gr
import warnings
warnings.filterwarnings("ignore")
# Initialize a Translator object with a multitask model, vocoder on the GPU.
model_name = "seamlessM4T_v2_large"
vocoder_name = "vocoder_v2" if model_name == "seamlessM4T_v2_large" else "vocoder_36langs"
AUDIO_SAMPLE_RATE = 16000.0
MAX_INPUT_AUDIO_LENGTH = 60 # in seconds
MODEL_NAME = "openai/whisper-large-v3"
BATCH_SIZE = 8
FILE_LIMIT_MB = 1000
YT_LENGTH_LIMIT_S = 3600 # limit to 1 hour YouTube files
if torch.cuda.is_available():
device = torch.device("cuda:0")
dtype = torch.float16
else:
device = torch.device("cpu")
dtype = torch.float32
translator = Translator(
model_name_or_card="seamlessM4T_v2_large",
vocoder_name_or_card="vocoder_v2",
device=device,
dtype=dtype,
apply_mintox=True,
)
device = 0 if torch.cuda.is_available() else "cpu"
pipe = pipeline(
task="automatic-speech-recognition",
model=MODEL_NAME,
chunk_length_s=30,
device=device,
)
client = InferenceClient(
"mistralai/Mixtral-8x7B-Instruct-v0.1"
)
def translate_seamless(input_text):
# tgt_langs = ("spa", "fra", "deu", "ita", "hin", "cmn")
text_output, speech_output = translator.predict(
input=input_text,
task_str="t2st",
tgt_lang="eng",
src_lang="eng",
)
return text_output,speech_output
device = 0 if torch.cuda.is_available() else "cpu"
# Initialize a Translator object with a multitask model, vocoder on the GPU.
model_name = "seamlessM4T_v2_large"
vocoder_name = "vocoder_v2" if model_name == "seamlessM4T_v2_large" else "vocoder_36langs"
AUDIO_SAMPLE_RATE = 16000.0
MAX_INPUT_AUDIO_LENGTH = 60 # in seconds
MODEL_NAME = "openai/whisper-large-v3"
BATCH_SIZE = 8
FILE_LIMIT_MB = 1000
YT_LENGTH_LIMIT_S = 3600 # limit to 1 hour YouTube files
def format_prompt(message, history):
prompt = "<s>"
for user_prompt, bot_response in history:
prompt += f"[INST] {user_prompt} [/INST]"
prompt += f" {bot_response}</s> "
prompt += f"[INST] {message} [/INST]"
return prompt
# @spaces.GPU() #comment out if you using Zero-GPU
def generate(
prompt, history, system_prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0,
):
temperature = float(temperature)
if temperature < 1e-2:
temperature = 1e-2
top_p = float(top_p)
generate_kwargs = dict(
temperature=temperature,
max_new_tokens=max_new_tokens,
top_p=top_p,
repetition_penalty=repetition_penalty,
do_sample=True,
seed=42,
)
formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
# print("formatted_prompt ",formatted_prompt)
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
output = ""
response_list=[]
dummy_list=""
for response in stream:
output += response.token.text
if len(dummy_list.split(" ")) < 50:
dummy_list+=response.token.text
else:
response_list.append(dummy_list)
dummy_list=""
# yield [output]
response_list.append(dummy_list)
voice_stream=[]
from tqdm import tqdm
for stream_response in tqdm(response_list):
text_output, speech_output= translate_seamless(stream_response)
voice_stream.append(speech_output.audio_wavs[0][0].to(torch.float32).cpu())
# text_output, speech_output= translate_seamless(output)
out_file = f"./voice.wav"
# print(type(speech_output.audio_wavs[0][0]))
torchaudio.save(out_file, torch.cat(voice_stream,1), speech_output.sample_rate)
print("audio Saved")
# torchaudio.save(out_file, speech_output.audio_wavs[0][0].to(torch.float32).cpu(), speech_output.sample_rate)
# return history,sentence,out_file
history[-1][1]=output
return history,out_file
def add_text(history, text):
history = [] if history is None else history
history = history + [(text, None)]
return history, gr.update(value="", interactive=False)
def generate_speech(history):
# yield (history, sentence,"/home/rnd/Documents/Ameer/Dream/voice.wav")
return generate(history[-1][0],history, system_prompt="Machine learning Based Answer in 50 words")
def transcribe(wav_path):
text = pipe(wav_path, batch_size=BATCH_SIZE, generate_kwargs={"task": "translate"}, return_timestamps=True)["text"]
return text
def add_file(history, file):
history = [] if history is None else history
try:
text = transcribe(file)
print("Transcribed text:", text)
except Exception as e:
print(str(e))
gr.Warning("There was an issue with transcription, please try writing for now")
# Apply a null text on error
text = "Transcription seems failed, please tell me a joke about chickens"
history = history + [(text, None)]
return history, gr.update(value="", interactive=False)
EXAMPLES = [
[[],"What is the difference between supervised and unsupervised machine learning?"],
[[],"What is the difference between a generative and discriminative model?"],
[[],"What is the difference between a probability and a likelihood?"],
]
MODELS = ["Mistral 7B Instruct"]
OTHER_HTML=f"""<div>
</div>
"""
DESCRIPTION = """# ML QA Voice ChatBot"""
with gr.Blocks(title="ML QA") as demo:
gr.Markdown(DESCRIPTION)
gr.Markdown(OTHER_HTML)
# with gr.Row():
# model_selected = gr.Dropdown(
# label="Select Instuct LLM Model to Use",
# info="Mistral, Zephyr: Mistral uses inference endpoint, Zephyr is 5 bit GGUF",
# choices=MODELS,
# max_choices=1,
# value=MODELS[0],
# )
chatbot = gr.Chatbot(
[],
elem_id="chatbot",
bubble_full_width=False,
)
with gr.Row():
txt = gr.Textbox(
scale=3,
show_label=False,
placeholder="Enter text and press enter, or speak to your microphone",
container=False,
interactive=True,
)
txt_btn = gr.Button(value="Submit text", scale=1)
btn = gr.Audio(source="microphone", type="filepath", scale=4)
def stop():
print("Audio STOP")
set_audio_playing(False)
with gr.Row():
sentence = gr.Textbox(visible=False)
audio = gr.Audio(
value=None,
label="Generated audio response",
streaming=True,
autoplay=True,
interactive=False,
show_label=True,
)
audio.end(stop)
with gr.Row():
gr.Examples(
EXAMPLES,
[chatbot, txt],
[chatbot, txt],
add_text,
cache_examples=False,
run_on_click=False, # Will not work , user should submit it
)
def clear_inputs(chatbot):
return None
clear_btn = gr.ClearButton([chatbot, audio])
# chatbot_role.change(fn=clear_inputs, inputs=[chatbot], outputs=[chatbot])
# model_selected.change(fn=clear_inputs, inputs=[chatbot], outputs=[chatbot])
txt_msg = txt_btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
generate_speech, [chatbot], [chatbot,audio]
)
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
generate_speech, [chatbot], [chatbot,audio]
)
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
file_msg = btn.stop_recording(
add_file, [chatbot, btn], [chatbot, txt], queue=False
).then(
generate_speech, [chatbot], [chatbot,audio]
)
file_msg.then(lambda: (gr.update(interactive=True),gr.update(interactive=True,value=None)), None, [txt, btn], queue=False)
gr.Markdown(
"""
This Space demonstrates how to speak to a chatbot, based solely on open accessible models.
It relies on following models :
Speech to Text : [Whisper-large-v3](https://huggingface.co/spaces/openai/whisper) as an ASR model, to transcribe recorded audio to text. It is called through a [gradio client](https://www.gradio.app/docs/client).
LLM Mistral : [Mixtral-8x7B-Instruct-v0.1](https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1) as the chat model.
Text to Speech : [Facebook Seamless T2ST](https://github.com/facebookresearch/seamless_communication) as a Multilingual TTS model, to generate the chatbot answers. This time, the model is hosted locally.
Note:
- By using this demo you agree to the terms of the Open Source Not for Comercial Use Just Demo Purpuse
- Responses generated by chat model should not be assumed correct or taken serious, as this is a demonstration example only"""
)
demo.queue()
demo.launch(debug=True,share=True)