Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: agent speak 方法adds tts 功能 #161

Open
tomhup opened this issue Apr 17, 2024 · 1 comment
Open

[Feature]: agent speak 方法adds tts 功能 #161

tomhup opened this issue Apr 17, 2024 · 1 comment
Labels
enhancement New feature or request todo Planned but not yet started

Comments

@tomhup
Copy link

tomhup commented Apr 17, 2024

我有一个简单的实现,
AgentBase
def speak(
self,
content: Union[str, dict],
) -> None:
if self.is_tts:
logger.chat(content)
asyncio.run(async_get_speech(content.content))
else:
logger.chat(content)

采用edget_tts 库实现语音播放

import asyncio
import io
import os
import re
import tempfile
from pydub import AudioSegment
from pydub.playback import play
import edge_tts
import logging
logger = logging.getLogger(name)

async def async_get_speech(phrase,voice="zh-CN-XiaoxiaoNeural"):
try:
# 创建一个临时文件
with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
tmpfile_name = tmpfile.name
text=phrase
text = re.sub(r'*+', '', text) # 替换所有 * 字符
text = re.sub(r'#+', '', text)
phrase=text
# print(phrase)
# 生成语音并保存到临时文件
tts = edge_tts.Communicate(text=phrase,voice=voice)
await tts.save(tmpfile_name)

    # 读取临时文件到 BytesIO 对象
    with open(tmpfile_name, "rb") as tmpfile:
        mp3_data = tmpfile.read()
    mp3_buffer = io.BytesIO(mp3_data)

    # 删除临时文件
    os.remove(tmpfile_name)

    # 从 BytesIO 对象加载音频并播放
    mp3_buffer.seek(0)  # 重置指针到开始
    audio_data = AudioSegment.from_file(mp3_buffer, format="mp3")
    play(audio_data)

except Exception as e:
    logger.error(f"Error: {e}")

async def main():
phrase = "*Hello, #this is a test."
await async_get_speech(phrase)

if name == "main":
asyncio.run(main())

@tomhup tomhup added the enhancement New feature or request label Apr 17, 2024
@rayrayraykk
Copy link
Collaborator

Thanks for your contributions to AgentScope. We will consider adding this feature to our AgentScope Studio :)

@DavdGao DavdGao added the todo Planned but not yet started label May 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request todo Planned but not yet started
Projects
None yet
Development

No branches or pull requests

3 participants