|
20 | 20 | from fastapi import Depends, HTTPException |
21 | 21 | from fastapi.exceptions import RequestValidationError |
22 | 22 | from fastapi.middleware.cors import CORSMiddleware |
23 | | -from fastapi.responses import StreamingResponse, JSONResponse |
| 23 | +from fastapi.responses import StreamingResponse, JSONResponse, FileResponse |
24 | 24 | from fastapi.security.http import HTTPAuthorizationCredentials, HTTPBearer |
25 | 25 | import httpx |
26 | 26 |
|
@@ -699,7 +699,31 @@ async def generate_completion(payload: Dict[str, Any], worker_addr: str): |
699 | 699 | CustomEmbeddingsRequest, |
700 | 700 | RerankRequest, |
701 | 701 | ModerationsRequest, |
| 702 | + SpeechRequest, |
702 | 703 | ) |
| 704 | +import edge_tts |
| 705 | +import uuid |
| 706 | + |
| 707 | +OUTPUT_DIR = "./edge_tts_cache" |
| 708 | + |
| 709 | + |
| 710 | +@app.post("/v1/audio/speech", dependencies=[Depends(check_api_key)]) |
| 711 | +async def speech(request: SpeechRequest): |
| 712 | + os.makedirs(OUTPUT_DIR, exist_ok=True) # 即使存在也不会报错 |
| 713 | + list_voices = await edge_tts.list_voices() |
| 714 | + support_list_voices = [i["ShortName"] for i in list_voices] |
| 715 | + if request.voice not in support_list_voices: |
| 716 | + return JSONResponse( |
| 717 | + ErrorResponse( |
| 718 | + message=f"不支持voice:{request.voice}", code=ErrorCode.INVALID_MODEL |
| 719 | + ).dict(), |
| 720 | + status_code=400, |
| 721 | + ) |
| 722 | + filename = f"{uuid.uuid4()}.mp3" |
| 723 | + output_path = os.path.join(OUTPUT_DIR, filename) |
| 724 | + communicate = edge_tts.Communicate(text=request.input, voice=request.voice) |
| 725 | + await communicate.save(output_path) |
| 726 | + return FileResponse(output_path, media_type="audio/mpeg", filename=filename) |
703 | 727 |
|
704 | 728 |
|
705 | 729 | @app.post("/v1/moderations", dependencies=[Depends(check_api_key)]) |
|
0 commit comments