modify tts sync request modth

This commit is contained in:
brige 2024-11-08 09:18:42 +08:00
parent 64e3f339cd
commit 1b5a6948b4
2 changed files with 54 additions and 14 deletions

View File

@ -6,6 +6,7 @@ import numpy as np
import requests import requests
import resampy import resampy
import soundfile as sf import soundfile as sf
from io import BytesIO
def download_tts(url): def download_tts(url):
@ -38,17 +39,15 @@ def __create_bytes_stream(byte_stream):
return stream return stream
def main(): def test_async_tts(url, content):
import aiohttp import aiohttp
import asyncio import asyncio
from io import BytesIO
async def fetch_audio(): async def fetch_audio():
# url = "http://localhost:8082/v1/audio/speech"
url = "https://tts.mzzsfy.eu.org/v1/audio/speech"
data = { data = {
"model": "tts-1", "model": "tts-1",
"input": "写了一个高性能tts(文本转声音)工具,5千字仅需5秒,免费使用", "input": content,
"voice": "alloy", "voice": "alloy",
"speed": 1.0 "speed": 1.0
} }
@ -69,6 +68,33 @@ def main():
asyncio.run(fetch_audio()) asyncio.run(fetch_audio())
def test_sync_tts(url, content):
data = {
"model": "tts-1",
"input": content,
"voice": "alloy",
"speed": 1.0
}
response = requests.post(url, json=data)
if response.status_code == 200:
audio_data = BytesIO(response.content)
audio_stream = __create_bytes_stream(audio_data)
# 保存为新的音频文件
sf.write("output_audio.wav", audio_stream, 16000)
print("Audio data received and saved to output_audio.wav")
else:
print("Error:", response.status_code, response.text)
def main():
# url = "http://localhost:8082/v1/audio/speech"
url = "https://tts.mzzsfy.eu.org/v1/audio/speech"
content = "写了一个高性能tts(文本转声音)工具,5千字仅需5秒,免费使用"
# test_async_tts(url, content)
test_sync_tts(url, content)
if __name__ == "__main__": if __name__ == "__main__":
try: try:
t = time.time() t = time.time()

View File

@ -4,6 +4,7 @@ from io import BytesIO
import aiohttp import aiohttp
import numpy as np import numpy as np
import requests
import soundfile as sf import soundfile as sf
import edge_tts import edge_tts
import resampy import resampy
@ -21,15 +22,7 @@ class TTSEdgeHttp(TTSBase):
self._url = 'https://tts.mzzsfy.eu.org/v1/audio/speech' self._url = 'https://tts.mzzsfy.eu.org/v1/audio/speech'
logger.info(f"TTSEdge init, {voice}") logger.info(f"TTSEdge init, {voice}")
async def _on_request(self, txt: str): async def _on_async_request(self, data):
logger.info(f'TTSEdgeHttp, _on_request, txt:{txt}')
data = {
"model": "tts-1",
"input": txt,
"voice": "alloy",
"speed": 1.0,
"thread": 10
}
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
async with session.post(self._url, json=data) as response: async with session.post(self._url, json=data) as response:
print('TTSEdgeHttp, _on_request, response:', response) print('TTSEdgeHttp, _on_request, response:', response)
@ -40,6 +33,27 @@ class TTSEdgeHttp(TTSBase):
byte_stream = None byte_stream = None
return byte_stream, None return byte_stream, None
def _on_sync_request(self, data):
response = requests.post(self._url, json=data)
if response.status_code == 200:
stream = BytesIO(response.content)
return stream
else:
return None
async def _on_request(self, txt: str):
logger.info(f'TTSEdgeHttp, _on_request, txt:{txt}')
data = {
"model": "tts-1",
"input": txt,
"voice": "alloy",
"speed": 1.0,
"thread": 10
}
# return self._on_async_request(data)
return self._on_sync_request(data)
async def _on_handle(self, stream, index): async def _on_handle(self, stream, index):
print('-------tts _on_handle') print('-------tts _on_handle')
try: try: