fix: ChatTTS音频通过后端代理,解决HTTPS页面访问HTTP资源问题
This commit is contained in:
Binary file not shown.
Binary file not shown.
BIN
logs/server.log
BIN
logs/server.log
Binary file not shown.
30
server.py
30
server.py
@@ -12,7 +12,7 @@ import aiohttp
|
|||||||
from fastapi import FastAPI, UploadFile, File, HTTPException, Form
|
from fastapi import FastAPI, UploadFile, File, HTTPException, Form
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from fastapi.responses import FileResponse
|
from fastapi.responses import FileResponse, Response
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
|
|
||||||
# 导入 TTS 服务
|
# 导入 TTS 服务
|
||||||
@@ -255,6 +255,34 @@ async def synthesize_tts(text: str = Form(...), provider: Optional[str] = Form(N
|
|||||||
app.mount("/audio", StaticFiles(directory=AUDIO_DIR), name="audio")
|
app.mount("/audio", StaticFiles(directory=AUDIO_DIR), name="audio")
|
||||||
|
|
||||||
|
|
||||||
|
# ChatTTS 音频代理(解决 HTTPS 页面访问 HTTP 资源问题)
|
||||||
|
@app.get("/chattts/audio/{filename}")
|
||||||
|
async def proxy_chattts_audio(filename: str):
|
||||||
|
"""代理 ChatTTS 音频文件"""
|
||||||
|
import aiohttp
|
||||||
|
|
||||||
|
chattts_url = os.getenv("CHATTTS_URL", "http://192.168.2.5:12002")
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get(
|
||||||
|
f"{chattts_url}/audio/{filename}",
|
||||||
|
timeout=aiohttp.ClientTimeout(total=30)
|
||||||
|
) as resp:
|
||||||
|
if resp.status != 200:
|
||||||
|
raise HTTPException(status_code=404, detail="Audio not found")
|
||||||
|
|
||||||
|
audio_data = await resp.read()
|
||||||
|
return Response(
|
||||||
|
content=audio_data,
|
||||||
|
media_type="audio/wav",
|
||||||
|
headers={"Cache-Control": "public, max-age=3600"}
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Proxy audio error: {e}")
|
||||||
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import uvicorn
|
import uvicorn
|
||||||
uvicorn.run(app, host="0.0.0.0", port=PORT)
|
uvicorn.run(app, host="0.0.0.0", port=PORT)
|
||||||
@@ -134,8 +134,12 @@ class ChatTTSProvider(TTSProvider):
|
|||||||
raise Exception(f"ChatTTS error: {error}")
|
raise Exception(f"ChatTTS error: {error}")
|
||||||
|
|
||||||
data = await resp.json()
|
data = await resp.json()
|
||||||
# ChatTTS 返回的 URL 是相对路径,需要拼接
|
# ChatTTS 返回的 URL 是 /audio/xxx.wav
|
||||||
audio_url = f"{self.CHATTTS_URL}{data['audio_url']}"
|
# 改用本地代理路径(解决 HTTPS 页面访问 HTTP 问题)
|
||||||
|
original_url = data['audio_url']
|
||||||
|
# /audio/xxx.wav -> /chattts/audio/xxx.wav (通过本地代理)
|
||||||
|
filename = original_url.split('/')[-1]
|
||||||
|
audio_url = f"/chattts/audio/{filename}"
|
||||||
return None, audio_url
|
return None, audio_url
|
||||||
|
|
||||||
def get_name(self) -> str:
|
def get_name(self) -> str:
|
||||||
|
|||||||
Reference in New Issue
Block a user