浏览代码

fix: respect proxy and timeout settings in audio-related aiohttp requests

Liu Yue 2 月之前
父节点
当前提交
90d9cdacfa
共有 1 个文件被更改,包括 7 次插入3 次删除
  1. 7 3
      backend/open_webui/routers/audio.py

+ 7 - 3
backend/open_webui/routers/audio.py

@@ -37,6 +37,7 @@ from open_webui.config import (
 
 
 from open_webui.constants import ERROR_MESSAGES
 from open_webui.constants import ERROR_MESSAGES
 from open_webui.env import (
 from open_webui.env import (
+    AIOHTTP_CLIENT_TIMEOUT,
     ENV,
     ENV,
     SRC_LOG_LEVELS,
     SRC_LOG_LEVELS,
     DEVICE_TYPE,
     DEVICE_TYPE,
@@ -266,7 +267,8 @@ async def speech(request: Request, user=Depends(get_verified_user)):
 
 
         try:
         try:
             # print(payload)
             # print(payload)
-            async with aiohttp.ClientSession() as session:
+            timeout=aiohttp.ClientTimeout(total=AIOHTTP_CLIENT_TIMEOUT)
+            async with aiohttp.ClientSession(timeout=timeout, trust_env=True) as session:
                 async with session.post(
                 async with session.post(
                     url=f"{request.app.state.config.TTS_OPENAI_API_BASE_URL}/audio/speech",
                     url=f"{request.app.state.config.TTS_OPENAI_API_BASE_URL}/audio/speech",
                     json=payload,
                     json=payload,
@@ -323,7 +325,8 @@ async def speech(request: Request, user=Depends(get_verified_user)):
             )
             )
 
 
         try:
         try:
-            async with aiohttp.ClientSession() as session:
+            timeout=aiohttp.ClientTimeout(total=AIOHTTP_CLIENT_TIMEOUT)
+            async with aiohttp.ClientSession(timeout=timeout, trust_env=True) as session:
                 async with session.post(
                 async with session.post(
                     f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}",
                     f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}",
                     json={
                     json={
@@ -380,7 +383,8 @@ async def speech(request: Request, user=Depends(get_verified_user)):
             data = f"""<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="{locale}">
             data = f"""<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xml:lang="{locale}">
                 <voice name="{language}">{payload["input"]}</voice>
                 <voice name="{language}">{payload["input"]}</voice>
             </speak>"""
             </speak>"""
-            async with aiohttp.ClientSession() as session:
+            timeout=aiohttp.ClientTimeout(total=AIOHTTP_CLIENT_TIMEOUT)
+            async with aiohttp.ClientSession(timeout=timeout, trust_env=True) as session:
                 async with session.post(
                 async with session.post(
                     f"https://{region}.tts.speech.microsoft.com/cognitiveservices/v1",
                     f"https://{region}.tts.speech.microsoft.com/cognitiveservices/v1",
                     headers={
                     headers={