Akjava commited on
Commit
71926eb
·
1 Parent(s): e97eba8

add tts script

Browse files
Files changed (1) hide show
  1. app.py +94 -3
app.py CHANGED
@@ -15,9 +15,100 @@ def greet(name):
15
  print(generated)
16
  yield generated
17
 
 
 
 
 
 
 
18
 
19
- with gr.Interface(fn=greet, inputs="text", outputs="text") as demo:
20
- textbox=gr.Textbox()
21
- #textbox.change(None,[],[],js="function(){console.log('hello')}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  demo.launch()
 
15
  print(generated)
16
  yield generated
17
 
18
+ head = '''
19
+ <script src="https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.webgpu.min.js" ></script>
20
+ <script type="module">
21
+ import { matcha_tts,env } from "https://akjava.github.io/Matcha-TTS-Japanese/js-esm/matcha_tts_onnx_en_dev.js";
22
+ window.MatchaTTSEn = matcha_tts
23
+ </script>
24
 
25
+ <script>
26
+ let last_chatbot_size = 0
27
+ let tts_text_index = 0
28
+ let tts_texts = []
29
+
30
+ const interval = 50
31
+ async function start_multi_line_tts() {
32
+ //console.log("start_multi_line_tts")
33
+ //console.log(tts_texts.length)
34
+ if (tts_texts.length > tts_text_index){
35
+ const tts_text = tts_texts[tts_text_index]
36
+ tts_text_index += 1
37
+ console.log(tts_text)
38
+ if (tts_text!=""){
39
+ await window.MatchaTTSEn(tts_text)
40
+ console.log("await")
41
+ }
42
+
43
+
44
+ }
45
+ setTimeout(start_multi_line_tts, interval);
46
+ }
47
+
48
+
49
+ function reset_tts_text(){
50
+ console.log("reset tts text")
51
+ tts_text_index = 0
52
+ tts_texts = []
53
+ }
54
+ function replaceSpecialChars(text) {
55
+ const pattern = /[^a-zA-Z0-9,.!?-_]/g;
56
+ return text.replace(pattern, ' ');
57
+ }
58
+
59
+
60
+ function update_tts_texts(text){
61
+ //console.log(text)
62
+ const replaced_text = replaceSpecialChars(text)
63
+ const new_texts = []
64
+ const splited = replaced_text.split(/[.!?]+\s/);
65
+ for (let i = 0; i < splited.length; i++) {
66
+ const value = splited[i].trim();
67
+
68
+ if (i === splited.length - 1) {
69
+ if (value.endsWith(".") || value.endsWith("?") || value.endsWith("!")){
70
+ new_texts.push(value);
71
+ }
72
+ console.log("Last element:", value);
73
+ } else {
74
+ // その他の要素に対する処理
75
+ new_texts.push(value);
76
+ }
77
+ }
78
+ tts_texts=new_texts
79
+
80
+ }
81
+
82
+ function update_chatbot(chatbot){
83
+ //console.log(chatbot)
84
+ if (chatbot.length!=last_chatbot_size){
85
+ last_chatbot_size = chatbot.length
86
+ reset_tts_text()
87
+ }
88
+ text = (chatbot[chatbot.length -1])["content"]
89
+ update_tts_texts(text)
90
+
91
+ }
92
+ window.update_chatbot = update_chatbot
93
+ window.update_tts_texts = update_tts_texts
94
+ window.reset_tts_text = reset_tts_text
95
+ start_multi_line_tts();
96
+ </script>
97
+ '''
98
+
99
+ input=gr.Textbox()
100
+ textbox=gr.Textbox()
101
+ #submit = gr.Button("Submit")
102
+ with gr.Interface(fn=greet, inputs=input, outputs=textbox,head=head) as demo: #submit_btn=submit
103
+ input.submit(None,[],[],js="function(){console.log('submit');window.reset_tts_text()}")
104
+ textbox.change(None,[textbox],[],js="""function(text){
105
+ window.update_tts_texts(text)
106
+ }""")
107
+ #submit click not catch.
108
+ #print(demo.inputs)
109
+ #input.submit(None,[],[],js="function(){console.log('submit')}")
110
+ #demo.inputs=input
111
+ #demo.outputs=textbox
112
+
113
 
114
  demo.launch()