forked from hkgood/Ollama_ChatTTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoice.py
More file actions
63 lines (49 loc) · 1.46 KB
/
voice.py
File metadata and controls
63 lines (49 loc) · 1.46 KB
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
import scipy
import streamlit as st
import re
import requests
from streamlit_mic_recorder import speech_to_text
import ChatTTS
from IPython.display import Audio
chat = ChatTTS.Chat()
chat.load_models()
PATH = 'chattts.wav'
# Record voice base on you lang
def record_voice(language="zh"):
state = st.session_state
if "text_received" not in state:
state.text_received = []
text = speech_to_text(
start_prompt="Click to speak",
stop_prompt="Stop recording",
language=language,
use_container_width=True,
just_once=True,
)
if text:
state.text_received.append(text)
result = ""
for text in state.text_received:
result += text
state.text_received = []
return result if result else None
# Save Audio to Mp3
def saveFile(res):
contentType = res.headers['Content-Type']
if 'audio' in contentType:
fo = open(PATH, 'wb')
fo.write(res.content)
fo.close()
print('save file path: ' + PATH)
else:
print(str(res.content, 'utf-8'))
# Convert text to audio
def createRequest(text):
wavs = chat.infer(text, use_decoder=True)
Audio(wavs[0], rate=24_000, autoplay=True)
scipy.io.wavfile.write(filename = "./chattts.wav", rate = 24_000, data = wavs[0].T)
def doCall(url, header, params, method):
if 'get' == method:
return requests.get(url, params)
elif 'post' == method:
return requests.post(url, params, header)