import gradio as gr from huggingface_hub import InferenceClient client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3") def generate_text(messages): print("generate_text") print(messages) generated = "" for token in client.chat_completion(messages, max_tokens=50,stream=True): content = (token.choices[0].delta.content) generated += content #print(content) #print(''.join(list(content))) yield generated #print(token.choices[0].delta) #return generated+"." #no stram version def call_generate_text(message, history): #if len(message) == 0: # messages.append({"role": "system", "content": "you response around 10 words"}) print(message) print(history) user_message = [{"role":"user","content":message}] messages = history + user_message try: assistant_message={"role":"assistant","content":""} text_generator = generate_text(messages) for text_chunk in text_generator: print(f"chunk={text_chunk}") assistant_message["content"] = text_chunk updated_history = messages + [assistant_message] yield "", updated_history except RuntimeError as e: print(f"An unexpected error occurred: {e}") yield "",history head = ''' ''' with gr.Blocks(title="LLM with TTS",head=head) as demo: gr.Markdown("## A LLM is unstable:The inference client used in this demo exhibits inconsistent performance. While it can provide responses in milliseconds, it sometimes becomes unresponsive and times out.") gr.Markdown("## TTS talke a long loading time:Please be patient, the first response may have a delay of up to over 20 seconds while loading.") gr.Markdown("**Mistral-7B-Instruct-v0.3/LJSpeech**.LLM and TTS models will change without notice.") js = """ function(chatbot){ window.update_chatbot(chatbot) //text = (chatbot[chatbot.length -1])["content"] //tts_text = window.replaceSpecialChars(text) //console.log(tts_text) //window.MatchaTTSEn(tts_text,"/file=models/ljspeech_sim.onnx") } """ chatbot = gr.Chatbot(type="messages") chatbot.change(None,[chatbot],[],js=js) msg = gr.Textbox() with gr.Row(): clear = gr.ClearButton([msg, chatbot]) submit = gr.Button("Submit",variant="primary").click(call_generate_text, inputs=[msg, chatbot], outputs=[msg,chatbot]) gr.HTML("""
""") msg.submit(call_generate_text, [msg, chatbot], [msg, chatbot]) import os dir ="/home/user/app/" dir = "C:\\Users\\owner\\Documents\\pythons\\huggingface\\mistral-7b-v0.3-matcha-tts-en" demo.launch(allowed_paths=[os.path.join(dir,"models","ljspeech_sim.onnx")])