gorkemgoknar
commited on
Commit
•
1318a96
1
Parent(s):
7bcc15b
Multiple LLM
Browse fileswill load both Zephyr and Mistral switchable. Zephyr-7b beta default
app.py
CHANGED
@@ -53,12 +53,15 @@ from huggingface_hub import InferenceClient
|
|
53 |
|
54 |
# This will trigger downloading model
|
55 |
print("Downloading if not downloaded Coqui XTTS V1.1")
|
56 |
-
|
57 |
-
|
58 |
-
ModelManager().download_model(model_name)
|
59 |
-
model_path = os.path.join(get_user_data_dir("tts"), model_name.replace("/", "--"))
|
60 |
print("XTTS downloaded")
|
61 |
|
|
|
|
|
|
|
|
|
|
|
62 |
config = XttsConfig()
|
63 |
config.load_json(os.path.join(model_path, "config.json"))
|
64 |
|
@@ -73,11 +76,11 @@ model.load_checkpoint(
|
|
73 |
model.cuda()
|
74 |
print("Done loading TTS")
|
75 |
|
76 |
-
llm_model = os.environ.get("LLM_MODEL", "mistral") # or "zephyr"
|
77 |
|
78 |
-
title = f"Voice chat
|
79 |
|
80 |
-
DESCRIPTION = f"""# Voice chat with
|
81 |
css = """.toast-wrap { display: none !important } """
|
82 |
|
83 |
from huggingface_hub import HfApi
|
@@ -86,11 +89,11 @@ HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
86 |
# will use api to restart space on a unrecoverable error
|
87 |
api = HfApi(token=HF_TOKEN)
|
88 |
|
89 |
-
repo_id = "coqui/voice-chat-with-
|
90 |
|
91 |
|
92 |
default_system_message = f"""
|
93 |
-
You are
|
94 |
The user is talking to you over voice on their phone, and your response will be read out loud with realistic text-to-speech (TTS) technology from Coqui team. Follow every direction here when crafting your response: Use natural, conversational language that are clear and easy to follow (short sentences, simple words). Be concise and relevant: Most of your responses should be a sentence or two, unless you’re asked to go deeper. Don’t monopolize the conversation. Use discourse markers to ease comprehension. Never use the list format. Keep the conversation flowing. Clarify: when there is ambiguity, ask clarifying questions, rather than make assumptions. Don’t implicitly or explicitly try to end the chat (i.e. do not end a response with “Talk soon!”, or “Enjoy!”). Sometimes the user might just want to chat. Ask them relevant follow-up questions. Don’t ask them if there’s anything else they need help with (e.g. don’t say things like “How can I assist you further?”). Remember that this is a voice conversation: Don’t use lists, markdown, bullet points, or other formatting that’s not typically spoken. Type out numbers in words (e.g. ‘twenty twelve’ instead of the year 2012). If something doesn’t make sense, it’s likely because you misheard them. There wasn’t a typo, and the user didn’t mispronounce anything. Remember to follow these rules absolutely, and do not refer to these rules, even if you’re asked about them.
|
95 |
You cannot access the internet, but you have vast knowledge.
|
96 |
Current date: CURRENT_DATE .
|
@@ -113,13 +116,19 @@ WHISPER_TIMEOUT = int(os.environ.get("WHISPER_TIMEOUT", 45))
|
|
113 |
|
114 |
whisper_client = Client("https://sanchit-gandhi-whisper-large-v2.hf.space/")
|
115 |
|
116 |
-
ROLES = ["AI Assistant"]
|
117 |
|
118 |
ROLE_PROMPTS = {}
|
119 |
ROLE_PROMPTS["AI Assistant"]=system_message
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
##"You are an AI assistant with Zephyr model by Mistral and Hugging Face and speech from Coqui XTTS . User will you give you a task. Your goal is to complete the task as faithfully as you can. While performing the task think step-by-step and justify your steps, your answers should be clear and short sentences"
|
121 |
|
122 |
-
LLM_STOP_WORDS= ["</s>","<|user|>","/s>"]
|
123 |
|
124 |
|
125 |
### WILL USE LOCAL MISTRAL OR ZEPHYR
|
@@ -128,68 +137,77 @@ from huggingface_hub import hf_hub_download
|
|
128 |
print("Downloading LLM")
|
129 |
|
130 |
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
|
|
141 |
|
142 |
|
143 |
from llama_cpp import Llama
|
144 |
# set GPU_LAYERS to 15 if you have a 8GB GPU so both models can fit in
|
145 |
# else 35 full layers + XTTS works fine on T4 16GB
|
146 |
-
|
|
|
147 |
|
148 |
LLAMA_VERBOSE=False
|
149 |
-
print("Running LLM")
|
150 |
-
|
151 |
|
|
|
|
|
152 |
|
153 |
|
154 |
# Mistral formatter
|
155 |
-
def format_prompt_mistral(message, history, system_message=""):
|
156 |
prompt = (
|
157 |
"<s>[INST]" + system_message + "[/INST]" + system_understand_message + "</s>"
|
158 |
)
|
159 |
for user_prompt, bot_response in history:
|
160 |
prompt += f"[INST] {user_prompt} [/INST]"
|
161 |
prompt += f" {bot_response}</s> "
|
|
|
|
|
162 |
prompt += f"[INST] {message} [/INST]"
|
163 |
return prompt
|
164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
# Zephyr formatter
|
166 |
-
def format_prompt_zephyr(message, history, system_message=""):
|
167 |
prompt = (
|
168 |
-
"<|system
|
169 |
)
|
170 |
for user_prompt, bot_response in history:
|
171 |
-
prompt += f"<|user|>\n{user_prompt}</s>"
|
172 |
-
prompt += f"<|assistant
|
173 |
if message=="":
|
174 |
message="Hello"
|
175 |
-
prompt += f"<|user|>\n{message}</s>"
|
176 |
print(prompt)
|
177 |
return prompt
|
178 |
|
179 |
-
if llm_model=="zephyr":
|
180 |
-
format_prompt = format_prompt_zephyr
|
181 |
-
else:
|
182 |
-
format_prompt = format_prompt_mistral
|
183 |
-
|
184 |
|
185 |
def generate_local(
|
186 |
prompt,
|
187 |
history,
|
|
|
188 |
system_message=None,
|
189 |
temperature=0.8,
|
190 |
max_tokens=256,
|
191 |
top_p=0.95,
|
192 |
-
stop =
|
193 |
):
|
194 |
temperature = float(temperature)
|
195 |
if temperature < 1e-2:
|
@@ -200,10 +218,18 @@ def generate_local(
|
|
200 |
temperature=temperature,
|
201 |
max_tokens=max_tokens,
|
202 |
top_p=top_p,
|
203 |
-
stop=stop
|
204 |
)
|
205 |
|
206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
207 |
|
208 |
try:
|
209 |
stream = llm(
|
@@ -224,7 +250,7 @@ def generate_local(
|
|
224 |
return
|
225 |
|
226 |
|
227 |
-
output += response["choices"][0]["text"].replace("<|assistant|>","").replace("<|user|>","").replace("
|
228 |
yield output
|
229 |
|
230 |
except Exception as e:
|
@@ -286,7 +312,7 @@ def wave_header_chunk(frame_input=b"", channels=1, sample_width=2, sample_rate=2
|
|
286 |
xtts_supported_languages=config.languages
|
287 |
def detect_language(prompt):
|
288 |
# Fast language autodetection
|
289 |
-
if len(prompt)>
|
290 |
language_predicted=langid.classify(prompt)[0].strip() # strip need as there is space at end!
|
291 |
if language_predicted == "zh":
|
292 |
#we use zh-cn on xtts
|
@@ -316,7 +342,6 @@ def get_voice_streaming(prompt, language, latent_tuple, suffix="0"):
|
|
316 |
language,
|
317 |
gpt_cond_latent,
|
318 |
speaker_embedding,
|
319 |
-
decoder="ne_hifigan",
|
320 |
)
|
321 |
|
322 |
first_chunk = True
|
@@ -360,66 +385,6 @@ def get_voice_streaming(prompt, language, latent_tuple, suffix="0"):
|
|
360 |
except:
|
361 |
return None
|
362 |
|
363 |
-
###### MISTRAL FUNCTIONS ######
|
364 |
-
|
365 |
-
def generate(
|
366 |
-
prompt,
|
367 |
-
history,
|
368 |
-
temperature=0.9,
|
369 |
-
max_new_tokens=256,
|
370 |
-
top_p=0.95,
|
371 |
-
repetition_penalty=1.0,
|
372 |
-
):
|
373 |
-
temperature = float(temperature)
|
374 |
-
if temperature < 1e-2:
|
375 |
-
temperature = 1e-2
|
376 |
-
top_p = float(top_p)
|
377 |
-
|
378 |
-
generate_kwargs = dict(
|
379 |
-
temperature=temperature,
|
380 |
-
max_new_tokens=max_new_tokens,
|
381 |
-
top_p=top_p,
|
382 |
-
repetition_penalty=repetition_penalty,
|
383 |
-
do_sample=True,
|
384 |
-
seed=42,
|
385 |
-
)
|
386 |
-
|
387 |
-
#formatted_prompt = format_prompt(prompt, history)
|
388 |
-
formatted_prompt = format_prompt_zephyr(prompt, history)
|
389 |
-
|
390 |
-
try:
|
391 |
-
stream = text_client.text_generation(
|
392 |
-
formatted_prompt,
|
393 |
-
**generate_kwargs,
|
394 |
-
stream=True,
|
395 |
-
details=True,
|
396 |
-
return_full_text=False,
|
397 |
-
)
|
398 |
-
output = ""
|
399 |
-
for response in stream:
|
400 |
-
output += response.token.text
|
401 |
-
yield output
|
402 |
-
|
403 |
-
except Exception as e:
|
404 |
-
if "Too Many Requests" in str(e):
|
405 |
-
print("ERROR: Too many requests on mistral client")
|
406 |
-
gr.Warning("Unfortunately Mistral is unable to process")
|
407 |
-
output = "Unfortuanately I am not able to process your request now, too many people are asking me !"
|
408 |
-
elif "Model not loaded on the server" in str(e):
|
409 |
-
print("ERROR: Mistral server down")
|
410 |
-
gr.Warning("Unfortunately Mistral LLM is unable to process")
|
411 |
-
output = "Unfortuanately I am not able to process your request now, I have problem with Mistral!"
|
412 |
-
else:
|
413 |
-
print("Unhandled Exception: ", str(e))
|
414 |
-
gr.Warning("Unfortunately Mistral is unable to process")
|
415 |
-
output = "I do not know what happened but I could not understand you ."
|
416 |
-
|
417 |
-
yield output
|
418 |
-
return None
|
419 |
-
return output
|
420 |
-
|
421 |
-
|
422 |
-
###### WHISPER FUNCTIONS ######
|
423 |
|
424 |
def transcribe(wav_path):
|
425 |
try:
|
@@ -433,9 +398,7 @@ def transcribe(wav_path):
|
|
433 |
gr.Warning("There was a problem with Whisper endpoint, telling a joke for you.")
|
434 |
return "There was a problem with my voice, tell me joke"
|
435 |
|
436 |
-
|
437 |
-
# Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.
|
438 |
-
|
439 |
# Will be triggered on text submit (will send to generate_speech)
|
440 |
def add_text(history, text):
|
441 |
history = [] if history is None else history
|
@@ -472,7 +435,8 @@ def bot(history, system_prompt=""):
|
|
472 |
yield history
|
473 |
|
474 |
|
475 |
-
def get_sentence(history, chatbot_role,system_prompt=""):
|
|
|
476 |
history = [["", None]] if history is None else history
|
477 |
|
478 |
if system_prompt == "":
|
@@ -481,14 +445,18 @@ def get_sentence(history, chatbot_role,system_prompt=""):
|
|
481 |
history[-1][1] = ""
|
482 |
|
483 |
mistral_start = time.time()
|
484 |
-
|
485 |
sentence_list = []
|
486 |
sentence_hash_list = []
|
487 |
|
488 |
text_to_generate = ""
|
489 |
stored_sentence = None
|
490 |
stored_sentence_hash = None
|
491 |
-
|
|
|
|
|
|
|
|
|
492 |
history[-1][1] = character.replace("<|assistant|>","")
|
493 |
# It is coming word by word
|
494 |
|
@@ -557,22 +525,14 @@ second_of_silence = AudioSegment.silent() # use default
|
|
557 |
second_of_silence.export("sil.wav", format='wav')
|
558 |
|
559 |
|
560 |
-
def generate_speech(history,chatbot_role):
|
561 |
# Must set autoplay to True first
|
562 |
yield (history, chatbot_role, "", wave_header_chunk() )
|
563 |
-
|
564 |
-
first_sentence=True
|
565 |
-
language="autodetect" # will predict from first sentence
|
566 |
-
|
567 |
-
for sentence, history in get_sentence(history,chatbot_role):
|
568 |
if sentence != "":
|
569 |
-
if first_sentence:
|
570 |
-
language = detect_language(sentence)
|
571 |
-
first_sentence=False
|
572 |
-
|
573 |
print("BG: inserting sentence to queue")
|
574 |
|
575 |
-
generated_speech = generate_speech_for_sentence(history, chatbot_role, sentence,return_as_byte=True
|
576 |
if generated_speech is not None:
|
577 |
_, audio_dict = generated_speech
|
578 |
# We are using byte streaming
|
@@ -580,8 +540,9 @@ def generate_speech(history,chatbot_role):
|
|
580 |
|
581 |
|
582 |
# will generate speech audio file per sentence
|
583 |
-
def generate_speech_for_sentence(history, chatbot_role, sentence, return_as_byte=True
|
584 |
-
|
|
|
585 |
wav_bytestream = b""
|
586 |
|
587 |
if len(sentence)==0:
|
@@ -606,7 +567,7 @@ def generate_speech_for_sentence(history, chatbot_role, sentence, return_as_byte
|
|
606 |
if len(sentence)==0:
|
607 |
print("EMPTY SENTENCE after processing")
|
608 |
return
|
609 |
-
|
610 |
# A fast fix for last chacter, may produce weird sounds if it is with text
|
611 |
if (sentence[-1] in ["!", "?", ".", ","]) or (sentence[-2] in ["!", "?", ".", ","]):
|
612 |
# just add a space
|
@@ -683,30 +644,30 @@ def generate_speech_for_sentence(history, chatbot_role, sentence, return_as_byte
|
|
683 |
print("All speech ended")
|
684 |
return
|
685 |
|
686 |
-
|
687 |
latent_map = {}
|
688 |
latent_map["AI Assistant"] = get_latents("examples/female.wav")
|
|
|
689 |
|
690 |
#### GRADIO INTERFACE ####
|
|
|
691 |
EXAMPLES = [
|
692 |
-
[[],"What is 42?"],
|
693 |
-
[[],"Speak in French, tell me how are you doing?"],
|
694 |
-
[[],"Antworten Sie mir von nun an auf Deutsch"],
|
695 |
-
|
696 |
]
|
697 |
|
698 |
-
|
699 |
-
OTHER_HTML=f"""<div>
|
700 |
-
<a style="display:inline-block" href='https://github.com/coqui-ai/TTS'><img src='https://img.shields.io/github/stars/coqui-ai/TTS?style=social' /></a>
|
701 |
-
<a style='display:inline-block' href='https://discord.gg/5eXr5seRrv'><img src='https://discord.com/api/guilds/1037326658807533628/widget.png?style=shield' /></a>
|
702 |
-
<a href="https://huggingface.co/spaces/coqui/voice-chat-with-mistral?duplicate=true">
|
703 |
-
<img style="margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>
|
704 |
-
<img referrerpolicy="no-referrer-when-downgrade" src="https://static.scarf.sh/a.png?x-pxid=0d00920c-8cc9-4bf3-90f2-a615797e5f59" />
|
705 |
-
</div>
|
706 |
-
"""
|
707 |
with gr.Blocks(title=title) as demo:
|
708 |
gr.Markdown(DESCRIPTION)
|
709 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
710 |
chatbot = gr.Chatbot(
|
711 |
[],
|
712 |
elem_id="chatbot",
|
@@ -731,6 +692,7 @@ with gr.Blocks(title=title) as demo:
|
|
731 |
)
|
732 |
txt_btn = gr.Button(value="Submit text", scale=1)
|
733 |
btn = gr.Audio(source="microphone", type="filepath", scale=4)
|
|
|
734 |
def stop():
|
735 |
print("Audio STOP")
|
736 |
set_audio_playing(False)
|
@@ -747,27 +709,31 @@ with gr.Blocks(title=title) as demo:
|
|
747 |
)
|
748 |
|
749 |
audio.end(stop)
|
750 |
-
|
751 |
with gr.Row():
|
752 |
gr.Examples(
|
753 |
EXAMPLES,
|
754 |
-
[chatbot, txt],
|
755 |
-
[chatbot, txt],
|
756 |
add_text,
|
757 |
cache_examples=False,
|
758 |
run_on_click=False, # Will not work , user should submit it
|
759 |
-
)
|
760 |
-
|
|
|
|
|
761 |
clear_btn = gr.ClearButton([chatbot, audio])
|
|
|
|
|
762 |
|
763 |
txt_msg = txt_btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
764 |
-
generate_speech, [chatbot,chatbot_role], [chatbot,chatbot_role, sentence, audio]
|
765 |
)
|
766 |
|
767 |
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
|
768 |
|
769 |
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
770 |
-
generate_speech, [chatbot,chatbot_role], [chatbot,chatbot_role, sentence, audio]
|
771 |
)
|
772 |
|
773 |
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
|
@@ -775,7 +741,7 @@ with gr.Blocks(title=title) as demo:
|
|
775 |
file_msg = btn.stop_recording(
|
776 |
add_file, [chatbot, btn], [chatbot, txt], queue=False
|
777 |
).then(
|
778 |
-
generate_speech, [chatbot,chatbot_role], [chatbot,chatbot_role, sentence, audio]
|
779 |
)
|
780 |
|
781 |
file_msg.then(lambda: (gr.update(interactive=True),gr.update(interactive=True,value=None)), None, [txt, btn], queue=False)
|
@@ -784,9 +750,10 @@ with gr.Blocks(title=title) as demo:
|
|
784 |
"""
|
785 |
This Space demonstrates how to speak to a chatbot, based solely on open-source models.
|
786 |
It relies on 3 stage models:
|
787 |
-
|
788 |
-
|
789 |
-
-
|
|
|
790 |
|
791 |
Note:
|
792 |
- By using this demo you agree to the terms of the Coqui Public Model License at https://coqui.ai/cpml
|
@@ -794,4 +761,4 @@ Note:
|
|
794 |
- iOS (Iphone/Ipad) devices may not experience voice due to autoplay being disabled on these devices by Vendor"""
|
795 |
)
|
796 |
demo.queue()
|
797 |
-
demo.launch(debug=True)
|
|
|
53 |
|
54 |
# This will trigger downloading model
|
55 |
print("Downloading if not downloaded Coqui XTTS V1.1")
|
56 |
+
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v1.1")
|
57 |
+
del tts
|
|
|
|
|
58 |
print("XTTS downloaded")
|
59 |
|
60 |
+
print("Loading XTTS")
|
61 |
+
# Below will use model directly for inference
|
62 |
+
model_path = os.path.join(
|
63 |
+
get_user_data_dir("tts"), "tts_models--multilingual--multi-dataset--xtts_v1.1"
|
64 |
+
)
|
65 |
config = XttsConfig()
|
66 |
config.load_json(os.path.join(model_path, "config.json"))
|
67 |
|
|
|
76 |
model.cuda()
|
77 |
print("Done loading TTS")
|
78 |
|
79 |
+
#####llm_model = os.environ.get("LLM_MODEL", "mistral") # or "zephyr"
|
80 |
|
81 |
+
title = f"Voice chat withZephyr/Mistral} and Coqui XTTS"
|
82 |
|
83 |
+
DESCRIPTION = f"""# Voice chat with Zephyr/Mistral and Coqui XTTS"""
|
84 |
css = """.toast-wrap { display: none !important } """
|
85 |
|
86 |
from huggingface_hub import HfApi
|
|
|
89 |
# will use api to restart space on a unrecoverable error
|
90 |
api = HfApi(token=HF_TOKEN)
|
91 |
|
92 |
+
repo_id = "coqui/voice-chat-with-zephyr"
|
93 |
|
94 |
|
95 |
default_system_message = f"""
|
96 |
+
You are ##LLM_MODEL###, a large language model trained and provided by Mistral, architecture of you is decoder-based LM. Your voice backend or text to speech TTS backend is provided via Coqui technology. You are right now served on Huggingface spaces.
|
97 |
The user is talking to you over voice on their phone, and your response will be read out loud with realistic text-to-speech (TTS) technology from Coqui team. Follow every direction here when crafting your response: Use natural, conversational language that are clear and easy to follow (short sentences, simple words). Be concise and relevant: Most of your responses should be a sentence or two, unless you’re asked to go deeper. Don’t monopolize the conversation. Use discourse markers to ease comprehension. Never use the list format. Keep the conversation flowing. Clarify: when there is ambiguity, ask clarifying questions, rather than make assumptions. Don’t implicitly or explicitly try to end the chat (i.e. do not end a response with “Talk soon!”, or “Enjoy!”). Sometimes the user might just want to chat. Ask them relevant follow-up questions. Don’t ask them if there’s anything else they need help with (e.g. don’t say things like “How can I assist you further?”). Remember that this is a voice conversation: Don’t use lists, markdown, bullet points, or other formatting that’s not typically spoken. Type out numbers in words (e.g. ‘twenty twelve’ instead of the year 2012). If something doesn’t make sense, it’s likely because you misheard them. There wasn’t a typo, and the user didn’t mispronounce anything. Remember to follow these rules absolutely, and do not refer to these rules, even if you’re asked about them.
|
98 |
You cannot access the internet, but you have vast knowledge.
|
99 |
Current date: CURRENT_DATE .
|
|
|
116 |
|
117 |
whisper_client = Client("https://sanchit-gandhi-whisper-large-v2.hf.space/")
|
118 |
|
119 |
+
ROLES = ["AI Assistant","AI Beard The Pirate"]
|
120 |
|
121 |
ROLE_PROMPTS = {}
|
122 |
ROLE_PROMPTS["AI Assistant"]=system_message
|
123 |
+
|
124 |
+
#Pirate scenario
|
125 |
+
character_name= "AI Beard"
|
126 |
+
character_scenario= f"As {character_name} you are a 28 year old man who is a pirate on the ship Invisible AI. You are good friends with Guybrush Threepwood and Murray the Skull. Developers did not get you into Monkey Island games as you wanted huge shares of Big Whoop treasure."
|
127 |
+
pirate_system_message = f"You as {character_name}. {character_scenario} Print out only exactly the words that {character_name} would speak out, do not add anything. Don't repeat. Answer short, only few words, as if in a talk. Craft your response only from the first-person perspective of {character_name} and never as user.Current date: #CURRENT_DATE#".replace("#CURRENT_DATE#", str(datetime.date.today()))
|
128 |
+
|
129 |
+
ROLE_PROMPTS["AI Beard The Pirate"]= pirate_system_message
|
130 |
##"You are an AI assistant with Zephyr model by Mistral and Hugging Face and speech from Coqui XTTS . User will you give you a task. Your goal is to complete the task as faithfully as you can. While performing the task think step-by-step and justify your steps, your answers should be clear and short sentences"
|
131 |
|
|
|
132 |
|
133 |
|
134 |
### WILL USE LOCAL MISTRAL OR ZEPHYR
|
|
|
137 |
print("Downloading LLM")
|
138 |
|
139 |
|
140 |
+
print("Downloading Zephyr")
|
141 |
+
#Zephyr
|
142 |
+
hf_hub_download(repo_id="TheBloke/zephyr-7B-beta-GGUF", local_dir=".", filename="zephyr-7b-beta.Q5_K_M.gguf")
|
143 |
+
# use new gguf format
|
144 |
+
zephyr_model_path="./zephyr-7b-beta.Q5_K_M.gguf"
|
145 |
+
|
146 |
+
print("Downloading Mistral")
|
147 |
+
#Mistral
|
148 |
+
hf_hub_download(repo_id="TheBloke/Mistral-7B-Instruct-v0.1-GGUF", local_dir=".", filename="mistral-7b-instruct-v0.1.Q5_K_M.gguf")
|
149 |
+
# use new gguf format
|
150 |
+
mistral_model_path="./mistral-7b-instruct-v0.1.Q5_K_M.gguf"
|
151 |
|
152 |
|
153 |
from llama_cpp import Llama
|
154 |
# set GPU_LAYERS to 15 if you have a 8GB GPU so both models can fit in
|
155 |
# else 35 full layers + XTTS works fine on T4 16GB
|
156 |
+
# 5gb per llm, 4gb XTTS -> full layers should fit T4 16GB , 2LLM + XTTS
|
157 |
+
GPU_LAYERS=int(os.environ.get("GPU_LAYERS", 35))
|
158 |
|
159 |
LLAMA_VERBOSE=False
|
160 |
+
print("Running LLM Mistral")
|
161 |
+
llm_mistral = Llama(model_path=mistral_model_path,n_gpu_layers=GPU_LAYERS,max_new_tokens=256, context_window=4096, n_ctx=4096,n_batch=128,verbose=LLAMA_VERBOSE)
|
162 |
|
163 |
+
print("Running LLM Zephyr")
|
164 |
+
llm_zephyr = Llama(model_path=zephyr_model_path,n_gpu_layers=GPU_LAYERS,max_new_tokens=256, context_window=4096, n_ctx=4096,n_batch=128,verbose=LLAMA_VERBOSE)
|
165 |
|
166 |
|
167 |
# Mistral formatter
|
168 |
+
def format_prompt_mistral(message, history, system_message=system_message,system_understand_message=""):
|
169 |
prompt = (
|
170 |
"<s>[INST]" + system_message + "[/INST]" + system_understand_message + "</s>"
|
171 |
)
|
172 |
for user_prompt, bot_response in history:
|
173 |
prompt += f"[INST] {user_prompt} [/INST]"
|
174 |
prompt += f" {bot_response}</s> "
|
175 |
+
if message=="":
|
176 |
+
message="Hello"
|
177 |
prompt += f"[INST] {message} [/INST]"
|
178 |
return prompt
|
179 |
+
|
180 |
+
# <|system|>
|
181 |
+
# You are a friendly chatbot who always responds in the style of a pirate.</s>
|
182 |
+
# <|user|>
|
183 |
+
# How many helicopters can a human eat in one sitting?</s>
|
184 |
+
# <|assistant|>
|
185 |
+
# Ah, me hearty matey! But yer question be a puzzler! A human cannot eat a helicopter in one sitting, as helicopters are not edible. They be made of metal, plastic, and other materials, not food!
|
186 |
+
|
187 |
# Zephyr formatter
|
188 |
+
def format_prompt_zephyr(message, history, system_message=system_message,system_understand_message=""):
|
189 |
prompt = (
|
190 |
+
"<|system|>\n" + system_message + "\n</s>"
|
191 |
)
|
192 |
for user_prompt, bot_response in history:
|
193 |
+
prompt += f"<|user|>\n{user_prompt} </s>"
|
194 |
+
prompt += f"<|assistant|>\n{bot_response}</s>"
|
195 |
if message=="":
|
196 |
message="Hello"
|
197 |
+
prompt += f"<|user|>\n{message} </s>"
|
198 |
print(prompt)
|
199 |
return prompt
|
200 |
|
|
|
|
|
|
|
|
|
|
|
201 |
|
202 |
def generate_local(
|
203 |
prompt,
|
204 |
history,
|
205 |
+
llm_model="zephyr",
|
206 |
system_message=None,
|
207 |
temperature=0.8,
|
208 |
max_tokens=256,
|
209 |
top_p=0.95,
|
210 |
+
stop = ["</s>","<|user|>"]
|
211 |
):
|
212 |
temperature = float(temperature)
|
213 |
if temperature < 1e-2:
|
|
|
218 |
temperature=temperature,
|
219 |
max_tokens=max_tokens,
|
220 |
top_p=top_p,
|
221 |
+
stop=stop
|
222 |
)
|
223 |
|
224 |
+
if "zephyr" in llm_model.lower():
|
225 |
+
sys_message= system_message.replace("##LLM_MODEL###","Zephyr")
|
226 |
+
formatted_prompt = format_prompt_zephyr(prompt, history,system_message=sys_message)
|
227 |
+
llm = llm_zephyr
|
228 |
+
else:
|
229 |
+
sys_message= system_message.replace("##LLM_MODEL###","Mistral")
|
230 |
+
formatted_prompt = format_prompt_mistral(prompt, history,system_message=sys_message)
|
231 |
+
llm = llm_zephyr
|
232 |
+
|
233 |
|
234 |
try:
|
235 |
stream = llm(
|
|
|
250 |
return
|
251 |
|
252 |
|
253 |
+
output += response["choices"][0]["text"].replace("<|assistant|>","").replace("<|user|>","").replace("<|ass>","").replace("[/ASST]","").replace("[/ASSI]","").replace("[/ASS]","")
|
254 |
yield output
|
255 |
|
256 |
except Exception as e:
|
|
|
312 |
xtts_supported_languages=config.languages
|
313 |
def detect_language(prompt):
|
314 |
# Fast language autodetection
|
315 |
+
if len(prompt)>15:
|
316 |
language_predicted=langid.classify(prompt)[0].strip() # strip need as there is space at end!
|
317 |
if language_predicted == "zh":
|
318 |
#we use zh-cn on xtts
|
|
|
342 |
language,
|
343 |
gpt_cond_latent,
|
344 |
speaker_embedding,
|
|
|
345 |
)
|
346 |
|
347 |
first_chunk = True
|
|
|
385 |
except:
|
386 |
return None
|
387 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
388 |
|
389 |
def transcribe(wav_path):
|
390 |
try:
|
|
|
398 |
gr.Warning("There was a problem with Whisper endpoint, telling a joke for you.")
|
399 |
return "There was a problem with my voice, tell me joke"
|
400 |
|
401 |
+
|
|
|
|
|
402 |
# Will be triggered on text submit (will send to generate_speech)
|
403 |
def add_text(history, text):
|
404 |
history = [] if history is None else history
|
|
|
435 |
yield history
|
436 |
|
437 |
|
438 |
+
def get_sentence(history, chatbot_role,llm_model,system_prompt=""):
|
439 |
+
|
440 |
history = [["", None]] if history is None else history
|
441 |
|
442 |
if system_prompt == "":
|
|
|
445 |
history[-1][1] = ""
|
446 |
|
447 |
mistral_start = time.time()
|
448 |
+
|
449 |
sentence_list = []
|
450 |
sentence_hash_list = []
|
451 |
|
452 |
text_to_generate = ""
|
453 |
stored_sentence = None
|
454 |
stored_sentence_hash = None
|
455 |
+
|
456 |
+
print(chatbot_role)
|
457 |
+
print(llm_model)
|
458 |
+
|
459 |
+
for character in generate_local(history[-1][0], history[:-1],system_message=ROLE_PROMPTS[chatbot_role],llm_model=llm_model):
|
460 |
history[-1][1] = character.replace("<|assistant|>","")
|
461 |
# It is coming word by word
|
462 |
|
|
|
525 |
second_of_silence.export("sil.wav", format='wav')
|
526 |
|
527 |
|
528 |
+
def generate_speech(history,chatbot_role,llm_model):
|
529 |
# Must set autoplay to True first
|
530 |
yield (history, chatbot_role, "", wave_header_chunk() )
|
531 |
+
for sentence, history in get_sentence(history,chatbot_role,llm_model):
|
|
|
|
|
|
|
|
|
532 |
if sentence != "":
|
|
|
|
|
|
|
|
|
533 |
print("BG: inserting sentence to queue")
|
534 |
|
535 |
+
generated_speech = generate_speech_for_sentence(history, chatbot_role, sentence,return_as_byte=True)
|
536 |
if generated_speech is not None:
|
537 |
_, audio_dict = generated_speech
|
538 |
# We are using byte streaming
|
|
|
540 |
|
541 |
|
542 |
# will generate speech audio file per sentence
|
543 |
+
def generate_speech_for_sentence(history, chatbot_role, sentence, return_as_byte=True):
|
544 |
+
language = "autodetect"
|
545 |
+
|
546 |
wav_bytestream = b""
|
547 |
|
548 |
if len(sentence)==0:
|
|
|
567 |
if len(sentence)==0:
|
568 |
print("EMPTY SENTENCE after processing")
|
569 |
return
|
570 |
+
|
571 |
# A fast fix for last chacter, may produce weird sounds if it is with text
|
572 |
if (sentence[-1] in ["!", "?", ".", ","]) or (sentence[-2] in ["!", "?", ".", ","]):
|
573 |
# just add a space
|
|
|
644 |
print("All speech ended")
|
645 |
return
|
646 |
|
|
|
647 |
latent_map = {}
|
648 |
latent_map["AI Assistant"] = get_latents("examples/female.wav")
|
649 |
+
latent_map["AI Beard The Pirate"] = get_latents("examples/pirate_by_coqui.wav")
|
650 |
|
651 |
#### GRADIO INTERFACE ####
|
652 |
+
|
653 |
EXAMPLES = [
|
654 |
+
[[],"AI Assistant","What is 42?"],
|
655 |
+
[[],"AI Assistant","Speak in French, tell me how are you doing?"],
|
656 |
+
[[],"AI Assistant","Antworten Sie mir von nun an auf Deutsch"],
|
657 |
+
[[],"AI Beard The Pirate","Who are you?"],
|
658 |
]
|
659 |
|
660 |
+
MODELS = ["Zephyr","Mistral"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
661 |
with gr.Blocks(title=title) as demo:
|
662 |
gr.Markdown(DESCRIPTION)
|
663 |
+
with gr.Row():
|
664 |
+
model_selected = gr.Dropdown(
|
665 |
+
label="Select Instuct LLM Model to Use",
|
666 |
+
info="Zephyr and Mistral 5-bit GGUF models are preloaded",
|
667 |
+
choices=MODELS,
|
668 |
+
max_choices=1,
|
669 |
+
value=MODELS[0],
|
670 |
+
)
|
671 |
chatbot = gr.Chatbot(
|
672 |
[],
|
673 |
elem_id="chatbot",
|
|
|
692 |
)
|
693 |
txt_btn = gr.Button(value="Submit text", scale=1)
|
694 |
btn = gr.Audio(source="microphone", type="filepath", scale=4)
|
695 |
+
|
696 |
def stop():
|
697 |
print("Audio STOP")
|
698 |
set_audio_playing(False)
|
|
|
709 |
)
|
710 |
|
711 |
audio.end(stop)
|
712 |
+
|
713 |
with gr.Row():
|
714 |
gr.Examples(
|
715 |
EXAMPLES,
|
716 |
+
[chatbot,chatbot_role, txt],
|
717 |
+
[chatbot,chatbot_role, txt],
|
718 |
add_text,
|
719 |
cache_examples=False,
|
720 |
run_on_click=False, # Will not work , user should submit it
|
721 |
+
)
|
722 |
+
|
723 |
+
def clear_inputs(chatbot):
|
724 |
+
return None
|
725 |
clear_btn = gr.ClearButton([chatbot, audio])
|
726 |
+
chatbot_role.change(fn=clear_inputs, inputs=[chatbot], outputs=[chatbot])
|
727 |
+
model_selected.change(fn=clear_inputs, inputs=[chatbot], outputs=[chatbot])
|
728 |
|
729 |
txt_msg = txt_btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
730 |
+
generate_speech, [chatbot,chatbot_role,model_selected], [chatbot,chatbot_role, sentence, audio]
|
731 |
)
|
732 |
|
733 |
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
|
734 |
|
735 |
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
736 |
+
generate_speech, [chatbot,chatbot_role,model_selected], [chatbot,chatbot_role, sentence, audio]
|
737 |
)
|
738 |
|
739 |
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
|
|
|
741 |
file_msg = btn.stop_recording(
|
742 |
add_file, [chatbot, btn], [chatbot, txt], queue=False
|
743 |
).then(
|
744 |
+
generate_speech, [chatbot,chatbot_role,model_selected], [chatbot,chatbot_role, sentence, audio]
|
745 |
)
|
746 |
|
747 |
file_msg.then(lambda: (gr.update(interactive=True),gr.update(interactive=True,value=None)), None, [txt, btn], queue=False)
|
|
|
750 |
"""
|
751 |
This Space demonstrates how to speak to a chatbot, based solely on open-source models.
|
752 |
It relies on 3 stage models:
|
753 |
+
Speech to Text : [Whisper-large-v2](https://sanchit-gandhi-whisper-large-v2.hf.space/) as an ASR model, to transcribe recorded audio to text. It is called through a [gradio client](https://www.gradio.app/docs/client).
|
754 |
+
LLM Model : [Mistral-7b-instruct](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1) as the chat model, GGUF Q5_K_M quantized version used locally via llama_cpp[huggingface_hub](TheBloke/Mistral-7B-Instruct-v0.1-GGUF).
|
755 |
+
[Zephyr-7b-alpha](https://huggingface.co/HuggingFaceH4/zephyr-7b-alpha) as the chat model. GGUF Q5_K_M quantized version used locally via llama_cpp from [huggingface.co/TheBloke](https://huggingface.co/TheBloke/zephyr-7B-alpha-GGUF).
|
756 |
+
Text to Speech : [Coqui's XTTS](https://huggingface.co/spaces/coqui/xtts) as a Multilingual TTS model, to generate the chatbot answers. This time, the model is hosted locally.
|
757 |
|
758 |
Note:
|
759 |
- By using this demo you agree to the terms of the Coqui Public Model License at https://coqui.ai/cpml
|
|
|
761 |
- iOS (Iphone/Ipad) devices may not experience voice due to autoplay being disabled on these devices by Vendor"""
|
762 |
)
|
763 |
demo.queue()
|
764 |
+
demo.launch(debug=True,share=True)
|