dev update
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ from huggingface_hub import InferenceClient
|
|
3 |
|
4 |
|
5 |
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
|
|
|
6 |
|
7 |
|
8 |
def generate_text(messages):
|
@@ -17,7 +18,7 @@ def generate_text(messages):
|
|
17 |
yield generated
|
18 |
#print(token.choices[0].delta)
|
19 |
|
20 |
-
#return generated+"
|
21 |
|
22 |
def call_generate_text(message, history):
|
23 |
#if len(message) == 0:
|
@@ -46,101 +47,8 @@ def call_generate_text(message, history):
|
|
46 |
head = '''
|
47 |
<script src="https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.webgpu.min.js" ></script>
|
48 |
<script type="module">
|
49 |
-
import {
|
50 |
-
|
51 |
-
window.start_threads = async function(){
|
52 |
-
//sadly onload unstable.only me or gradle
|
53 |
-
//console.log("start_threads")
|
54 |
-
await start_thread_play_tts();
|
55 |
-
await start_multi_line_tts();
|
56 |
-
}
|
57 |
-
</script>
|
58 |
-
|
59 |
-
<script>
|
60 |
-
let is_start_threads = false
|
61 |
-
let last_chatbot_size = 0
|
62 |
-
let tts_text_index = 0
|
63 |
-
let tts_texts = []
|
64 |
-
|
65 |
-
const interval = 10
|
66 |
-
|
67 |
-
async function start_multi_line_tts() {
|
68 |
-
//console.log("start_multi_line_tts")
|
69 |
-
//console.log(tts_texts.length)
|
70 |
-
if (tts_texts.length > tts_text_index){
|
71 |
-
const tts_text = tts_texts[tts_text_index]
|
72 |
-
tts_text_index += 1
|
73 |
-
console.log(tts_text)
|
74 |
-
if (tts_text!=""){
|
75 |
-
//console.log("statar await load")
|
76 |
-
await window.MatchaTTSEn(tts_text,"/file=models/ljspeech_sim.onnx")
|
77 |
-
//console.log("end await ")
|
78 |
-
}
|
79 |
-
|
80 |
-
|
81 |
-
}
|
82 |
-
setTimeout(start_multi_line_tts, interval);
|
83 |
-
}
|
84 |
-
|
85 |
-
|
86 |
-
function reset_tts_text(){
|
87 |
-
if (is_start_threads == false){
|
88 |
-
window.start_threads()
|
89 |
-
is_start_threads = true
|
90 |
-
}
|
91 |
-
console.log("new messages")
|
92 |
-
tts_text_index = 0
|
93 |
-
tts_texts = []
|
94 |
-
}
|
95 |
-
function replaceSpecialChars(text) {
|
96 |
-
const pattern = /[^a-zA-Z0-9,.!?-_']/g;
|
97 |
-
return text.replace(pattern, ' ');
|
98 |
-
}
|
99 |
-
|
100 |
-
|
101 |
-
function update_tts_texts(text){
|
102 |
-
//console.log(text)
|
103 |
-
const replaced_text = replaceSpecialChars(text)
|
104 |
-
const new_texts = []
|
105 |
-
//const splited = replaced_text.split(/[.!?]+\s*/);
|
106 |
-
const splited = replaced_text.match(/[^.?!,]+[.?!,]+\s?/g);
|
107 |
-
if(!splited){
|
108 |
-
return
|
109 |
-
}
|
110 |
-
for (let i = 0; i < splited.length; i++) {
|
111 |
-
const value = splited[i].trim();
|
112 |
-
|
113 |
-
if (i === splited.length - 1) {
|
114 |
-
if (value.endsWith(".") || value.endsWith("?") || value.endsWith("!")){
|
115 |
-
new_texts.push(value);
|
116 |
-
}
|
117 |
-
//console.log("Last element:", value);
|
118 |
-
} else {
|
119 |
-
// その他の要素に対する処理
|
120 |
-
new_texts.push(value);
|
121 |
-
}
|
122 |
-
}
|
123 |
-
tts_texts=new_texts
|
124 |
-
|
125 |
-
}
|
126 |
-
|
127 |
-
function update_chatbot(chatbot){
|
128 |
-
//console.log(chatbot)
|
129 |
-
if (chatbot.length!=last_chatbot_size){
|
130 |
-
last_chatbot_size = chatbot.length
|
131 |
-
reset_tts_text()
|
132 |
-
}
|
133 |
-
text = (chatbot[chatbot.length -1])["content"]
|
134 |
-
update_tts_texts(text)
|
135 |
-
|
136 |
-
}
|
137 |
-
|
138 |
-
window.replaceSpecialChars = replaceSpecialChars
|
139 |
-
|
140 |
-
window.update_chatbot = update_chatbot
|
141 |
-
window.update_tts_texts = update_tts_texts
|
142 |
-
window.reset_tts_text = reset_tts_text
|
143 |
-
|
144 |
</script>
|
145 |
'''
|
146 |
|
@@ -151,16 +59,12 @@ with gr.Blocks(title="LLM with TTS",head=head) as demo:
|
|
151 |
|
152 |
js = """
|
153 |
function(chatbot){
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
//text = (chatbot[chatbot.length -1])["content"]
|
161 |
-
//tts_text = window.replaceSpecialChars(text)
|
162 |
-
//console.log(tts_text)
|
163 |
-
//window.MatchaTTSEn(tts_text,"/file=models/ljspeech_sim.onnx")
|
164 |
}
|
165 |
"""
|
166 |
chatbot = gr.Chatbot(type="messages",elem_id="gr-chatbot")
|
@@ -194,6 +98,9 @@ with gr.Blocks(title="LLM with TTS",head=head) as demo:
|
|
194 |
msg.submit(call_generate_text, [msg, chatbot], [msg, chatbot])
|
195 |
|
196 |
import os
|
197 |
-
|
198 |
-
|
199 |
-
|
|
|
|
|
|
|
|
3 |
|
4 |
|
5 |
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
|
6 |
+
#client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")
|
7 |
|
8 |
|
9 |
def generate_text(messages):
|
|
|
18 |
yield generated
|
19 |
#print(token.choices[0].delta)
|
20 |
|
21 |
+
#return generated+"\n" #no stram version
|
22 |
|
23 |
def call_generate_text(message, history):
|
24 |
#if len(message) == 0:
|
|
|
47 |
head = '''
|
48 |
<script src="https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.webgpu.min.js" ></script>
|
49 |
<script type="module">
|
50 |
+
import { env } from "http://localhost:8000/js-esm/matcha_tts_onnx_en.js";
|
51 |
+
env.matcha_tts_model_path = "/file=models/ljspeech_sim.onnx"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
</script>
|
53 |
'''
|
54 |
|
|
|
59 |
|
60 |
js = """
|
61 |
function(chatbot){
|
62 |
+
window.update_chatbot(chatbot)
|
63 |
+
//auto scroll
|
64 |
+
var chatElement = document.getElementById('gr-chatbot');
|
65 |
+
chatElement.scrollTop = chatElement.scrollHeight;
|
66 |
+
var logElement = chatElement.querySelector('div[role="log"]');
|
67 |
+
logElement.scrollTop = logElement.scrollHeight;
|
|
|
|
|
|
|
|
|
68 |
}
|
69 |
"""
|
70 |
chatbot = gr.Chatbot(type="messages",elem_id="gr-chatbot")
|
|
|
98 |
msg.submit(call_generate_text, [msg, chatbot], [msg, chatbot])
|
99 |
|
100 |
import os
|
101 |
+
remote_dir ="/home/user/app/"
|
102 |
+
local_dir = "C:\\Users\\owner\\Documents\\pythons\\huggingface\\mistral-7b-v0.3-matcha-tts-en" #sorry this is my develop env
|
103 |
+
#print(os.path.join(dir,"js-esm","matcha_tts_onnx_en.js"))
|
104 |
+
# set not dir but file
|
105 |
+
#demo.launch(allowed_paths=[os.path.join(remote_dir,"models","ljspeech_sim.onnx")])
|
106 |
+
demo.launch(allowed_paths=[os.path.join(local_dir,"models","ljspeech_sim.onnx")])
|