File size: 963 Bytes
76d8c82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import asyncio
import aiohttp
import io
import os

BASE_URL = os.getenv("BASE_URL")

async def generate_api(voice_ids, text):
    timeout = aiohttp.ClientTimeout(total=10)  # 设置10秒的总超时时间
    try:
        async with aiohttp.ClientSession(timeout=timeout) as session:
            async with session.post(BASE_URL+"tts", json={"ids": voice_ids, "text": text}) as response:
                if response.status == 200:
                    # 读取响应内容
                    audio_data = await response.read()
                    # print(type(audio_data))
                    # 创建一个字节流对象
                    return audio_data
                else:
                    print(response)
                    return f"合成失败: {response.status}"
    except asyncio.TimeoutError:
        return "请求超时,请稍后重试"
    except aiohttp.ClientError as e:
        return f"网络错误: {str(e)}"