| from fastapi import FastAPI, HTTPException |
| from pydantic import BaseModel |
| import torch |
| import torchaudio |
| import ChatTTS |
|
|
| |
| torch._dynamo.config.cache_size_limit = 64 |
| torch._dynamo.config.suppress_errors = True |
| torch.set_float32_matmul_precision('high') |
|
|
| |
| app = FastAPI() |
| chat = ChatTTS.Chat() |
| chat.load(compile=False) |
|
|
| |
| class TextRequest(BaseModel): |
| text: str |
|
|
| @app.post("/synthesize/") |
| async def synthesize_speech(request: TextRequest): |
| try: |
| |
| wavs = chat.infer([request.text]) |
| |
| |
| output_file = "output.wav" |
| torchaudio.save(output_file, torch.from_numpy(wavs[0]), 24000) |
|
|
| return {"message": "Speech synthesized successfully", "audio_file": output_file} |
| except Exception as e: |
| raise HTTPException(status_code=500, detail=str(e)) |
|
|
| |
| @app.get("/") |
| def read_root(): |
| return {"message": "Welcome to the ChatTTS API. Use the /synthesize/ endpoint to generate speech."} |
|
|