new_wukong/main.py
ZZY 6120e7ba57 refactor(main): 使用异步流处理替换原有的顺序执行
重构`main`函数,将原先的顺序执行模型改为基于异步流的处理方式,利用`aiostream`库实现更高效的并发数据处理。
2024-08-31 14:36:44 +08:00

35 lines
1.0 KiB
Python

# from src import Plugins
# from src import ExecutePipeline
# from src import EchoProcessor
import os
from viztracer import VizTracer
from dotenv import load_dotenv
load_dotenv()
from pathlib import Path
from src.dashscope.tongyiqianwen import CAITongYiQianWen as CAI
from src.dashscope.sambert import TTSSambert as TTS
from src.pyaudio.sounds_play_engine import SoundsPlayEngine as sounds
from aiostream import stream, pipe
async def main():
cai = CAI(api_key=os.environ.get("DASH_SCOPE_API_KEY", ""), use_stream_api=True)
tts = TTS(api_key=os.environ.get("DASH_SCOPE_API_KEY", ""))
exe_input = '给我一个100字的文章'
with VizTracer(log_async=True):
s = (
stream.iterate(await cai.execute_stream(cai.prepare_input(exe_input)))
| pipe.map(cai.transform_chunks)
| pipe.print()
| pipe.flatmap(tts.execute_stream)
| pipe.map(sounds().execute)
)
await s
if __name__ == '__main__':
import asyncio
asyncio.run(main())