File size: 3,120 Bytes
80cb67e
a1b63cd
 
 
 
 
80cb67e
 
a1b63cd
 
 
 
 
 
 
80cb67e
71926eb
 
 
b055baf
71926eb
 
e97eba8
71926eb
 
 
 
 
2cd6dcc
71926eb
 
 
 
 
 
 
 
da4ee79
71926eb
 
 
 
 
 
 
 
 
 
 
 
 
 
0bcec1b
71926eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
efa9a83
71926eb
 
 
 
 
 
 
80cb67e
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
import gradio as gr
from huggingface_hub import InferenceClient


client = InferenceClient("google/gemma-2-27b-it")


def greet(name):
    messages = [{"role": "user", "content": name}]
    generated = ""
    for token in client.chat_completion(messages, max_tokens=100,stream=True):
        content = (token.choices[0].delta.content)
        generated+=content
        print(generated)
        yield generated

head = '''
<script src="https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.webgpu.min.js" ></script>
<script type="module">
        import { matcha_tts,env } from "https://akjava.github.io/Matcha-TTS-Japanese/js-esm/v001-20240921/matcha_tts_onnx_en.js";
        window.MatchaTTSEn = matcha_tts
</script>

<script>
let last_chatbot_size = 0
let tts_text_index = 0
let tts_texts = []

const interval = 100
async function start_multi_line_tts() {
  //console.log("start_multi_line_tts")
  //console.log(tts_texts.length)
  if (tts_texts.length > tts_text_index){
  const tts_text = tts_texts[tts_text_index]
  tts_text_index += 1
  console.log(tts_text)
  if (tts_text!=""){
    await window.MatchaTTSEn(tts_text,"https://huggingface.co/spaces/Akjava/gemma-2-27b-it-matcha-tts-en/resolve/main/ljspeech_sim.onnx")
  }

  
  }
  setTimeout(start_multi_line_tts, interval);
}


function reset_tts_text(){
    console.log("reset tts text")
    tts_text_index = 0
    tts_texts = []
}
function replaceSpecialChars(text) {
    const pattern = /[^a-zA-Z0-9,.!?-_']/g;
    return text.replace(pattern, ' ');
}


function update_tts_texts(text){
    //console.log(text)
    const replaced_text = replaceSpecialChars(text)
    const new_texts = []
    const splited = replaced_text.split(/[.!?]+\s/);
    for (let i = 0; i < splited.length; i++) {
    const value = splited[i].trim();
    
    if (i === splited.length - 1) {
        if (value.endsWith(".") || value.endsWith("?") || value.endsWith("!")){
            new_texts.push(value);
        }
        console.log("Last element:", value);
    } else {
        // その他の要素に対する処理
        new_texts.push(value);
    }
    }
    tts_texts=new_texts
    
}

function update_chatbot(chatbot){
    //console.log(chatbot)
    if (chatbot.length!=last_chatbot_size){
        last_chatbot_size = chatbot.length
        reset_tts_text()
    }
    text = (chatbot[chatbot.length -1])["content"]
    update_tts_texts(text)
    
}
window.update_chatbot = update_chatbot
window.update_tts_texts = update_tts_texts
window.reset_tts_text = reset_tts_text  
start_multi_line_tts();
</script>
'''

input=gr.Textbox()
textbox=gr.Textbox()
#submit = gr.Button("Submit")
with gr.Interface(fn=greet, inputs=input, outputs=textbox,head=head) as demo: #submit_btn=submit
    input.submit(None,[],[],js="function(){console.log('submit');window.reset_tts_text()}")
    textbox.change(None,[textbox],[],js="""function(text){
        window.update_tts_texts(text)
        }""")
    #submit click not catch.
    #print(demo.inputs)
    #input.submit(None,[],[],js="function(){console.log('submit')}")
    #demo.inputs=input
    #demo.outputs=textbox
    
demo.launch()