Akjava commited on
Commit
15dc7eb
·
verified ·
1 Parent(s): 4cf2940

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -88
app.py CHANGED
@@ -5,14 +5,31 @@ from huggingface_hub import InferenceClient
5
  client = InferenceClient("google/gemma-2-27b-it")
6
 
7
 
8
- def greet(name):
9
- messages = [{"role": "user", "content": name}]
10
  generated = ""
11
  for token in client.chat_completion(messages, max_tokens=100,stream=True):
12
  content = (token.choices[0].delta.content)
13
  generated+=content
14
  print(generated)
15
- yield generated
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  head = '''
18
  <script src="https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.webgpu.min.js" ></script>
@@ -20,92 +37,39 @@ head = '''
20
  import { matcha_tts,env } from "https://akjava.github.io/Matcha-TTS-Japanese/js-esm/v001-20240921/matcha_tts_onnx_en.js";
21
  window.MatchaTTSEn = matcha_tts
22
  </script>
23
-
24
- <script>
25
- let last_chatbot_size = 0
26
- let tts_text_index = 0
27
- let tts_texts = []
28
-
29
- const interval = 100
30
- async function start_multi_line_tts() {
31
- //console.log("start_multi_line_tts")
32
- //console.log(tts_texts.length)
33
- if (tts_texts.length > tts_text_index){
34
- const tts_text = tts_texts[tts_text_index]
35
- tts_text_index += 1
36
- console.log(tts_text)
37
- if (tts_text!=""){
38
- await window.MatchaTTSEn(tts_text,"https://huggingface.co/spaces/Akjava/gemma-2-27b-it-matcha-tts-en/resolve/main/ljspeech_sim.onnx")
39
- }
40
-
41
-
42
- }
43
- setTimeout(start_multi_line_tts, interval);
44
- }
45
-
46
-
47
- function reset_tts_text(){
48
- console.log("reset tts text")
49
- tts_text_index = 0
50
- tts_texts = []
51
- }
52
- function replaceSpecialChars(text) {
53
- const pattern = /[^a-zA-Z0-9,.!?-_']/g;
54
- return text.replace(pattern, ' ');
55
- }
56
-
57
-
58
- function update_tts_texts(text){
59
- //console.log(text)
60
- const replaced_text = replaceSpecialChars(text)
61
- const new_texts = []
62
- const splited = replaced_text.split(/[.!?]+\s/);
63
- for (let i = 0; i < splited.length; i++) {
64
- const value = splited[i].trim();
65
-
66
- if (i === splited.length - 1) {
67
- if (value.endsWith(".") || value.endsWith("?") || value.endsWith("!")){
68
- new_texts.push(value);
69
- }
70
- console.log("Last element:", value);
71
- } else {
72
- // その他の要素に対する処理
73
- new_texts.push(value);
74
- }
75
- }
76
- tts_texts=new_texts
77
-
78
- }
79
-
80
- function update_chatbot(chatbot){
81
- //console.log(chatbot)
82
- if (chatbot.length!=last_chatbot_size){
83
- last_chatbot_size = chatbot.length
84
- reset_tts_text()
85
- }
86
  text = (chatbot[chatbot.length -1])["content"]
87
- update_tts_texts(text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
- }
90
- window.update_chatbot = update_chatbot
91
- window.update_tts_texts = update_tts_texts
92
- window.reset_tts_text = reset_tts_text
93
- start_multi_line_tts();
94
- </script>
95
- '''
96
-
97
- input=gr.Textbox()
98
- textbox=gr.Textbox()
99
- #submit = gr.Button("Submit")
100
- with gr.Interface(fn=greet, inputs=input, outputs=textbox,head=head) as demo: #submit_btn=submit
101
- input.submit(None,[],[],js="function(){console.log('submit');window.reset_tts_text()}")
102
- textbox.change(None,[textbox],[],js="""function(text){
103
- window.update_tts_texts(text)
104
- }""")
105
- #submit click not catch.
106
- #print(demo.inputs)
107
- #input.submit(None,[],[],js="function(){console.log('submit')}")
108
- #demo.inputs=input
109
- #demo.outputs=textbox
110
 
111
  demo.launch()
 
5
  client = InferenceClient("google/gemma-2-27b-it")
6
 
7
 
8
+ def generate_text(messages):
 
9
  generated = ""
10
  for token in client.chat_completion(messages, max_tokens=100,stream=True):
11
  content = (token.choices[0].delta.content)
12
  generated+=content
13
  print(generated)
14
+
15
+ generated
16
+
17
+ def call_generate_text(message, history):
18
+ if len(message) == 0:
19
+ message.append({"role": "system", "content": "you response around 10 words"})
20
+ # history.append({"role": "user", "content": message})
21
+ print(message)
22
+ print(history)
23
+
24
+ messages = history+[{"role":"user","content":message}]
25
+ try:
26
+ text = generate_text(messages)
27
+ messages += [{"role":"assistant","content":text}]
28
+ return "",messages
29
+ except RuntimeError as e:
30
+ print(f"An unexpected error occurred: {e}")
31
+
32
+ return "",history
33
 
34
  head = '''
35
  <script src="https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.webgpu.min.js" ></script>
 
37
  import { matcha_tts,env } from "https://akjava.github.io/Matcha-TTS-Japanese/js-esm/v001-20240921/matcha_tts_onnx_en.js";
38
  window.MatchaTTSEn = matcha_tts
39
  </script>
40
+ '''
41
+ with gr.Blocks(title="LLM with TTS",head=head) as demo:
42
+ gr.Markdown("## Please be patient, the first response may have a delay of up to 20 seconds while loading.")
43
+ gr.Markdown("**Qwen2.5-0.5B-Instruct/LJSpeech-q8**.LLM and TTS models will change without notice.")
44
+ js = """
45
+ function(chatbot){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  text = (chatbot[chatbot.length -1])["content"]
47
+ window.MatchaTTSEn(text)
48
+ }
49
+ """
50
+ chatbot = gr.Chatbot(type="messages")
51
+ chatbot.change(None,[chatbot],[],js=js)
52
+ msg = gr.Textbox()
53
+ clear = gr.ClearButton([msg, chatbot])
54
+ gr.HTML("""
55
+ <br>
56
+ <div id="footer">
57
+ <b>Spaces</b><br>
58
+ <a href="https://huggingface.co/spaces/Akjava/matcha-tts_vctk-onnx" style="font-size: 9px" target="link">Match-TTS VCTK-ONNX</a> |
59
+ <a href="https://huggingface.co/spaces/Akjava/matcha-tts-onnx-benchmarks" style="font-size: 9px" target="link">Match-TTS ONNX-Benchmark</a> |
60
+ <br><br>
61
+ <b>Credits</b><br>
62
+ <a href="https://github.com/akjava/Matcha-TTS-Japanese" style="font-size: 9px" target="link">Matcha-TTS-Japanese</a> |
63
+ <a href = "http://www.udialogue.org/download/cstr-vctk-corpus.html" style="font-size: 9px" target="link">CSTR VCTK Corpus</a> |
64
+ <a href = "https://github.com/cmusphinx/cmudict" style="font-size: 9px" target="link">CMUDict</a> |
65
+ <a href = "https://huggingface.co/docs/transformers.js/index" style="font-size: 9px" target="link">Transformer.js</a> |
66
+ <a href = "https://huggingface.co/cisco-ai/mini-bart-g2p" style="font-size: 9px" target="link">mini-bart-g2p</a> |
67
+ <a href = "https://onnxruntime.ai/docs/get-started/with-javascript/web.html" style="font-size: 9px" target="link">ONNXRuntime-Web</a> |
68
+ <a href = "https://github.com/akjava/English-To-IPA-Collections" style="font-size: 9px" target="link">English-To-IPA-Collections</a> |
69
+ <a href ="https://huggingface.co/papers/2309.03199" style="font-size: 9px" target="link">Matcha-TTS Paper</a>
70
+ </div>
71
+ """)
72
 
73
+ msg.submit(call_generate_text, [msg, chatbot], [msg, chatbot])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  demo.launch()