Xinfan Chen
2026-04-23 890ce33d291f9e7ed8d8dee7b11596ed1bd7a0be
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from fastapi import FastAPI, Request
from fastapi.responses import FileResponse
import uvicorn
import torch
import soundfile
import ChatTTS
import json
import os
 
app = FastAPI()
ROOT = "/Path to/TTS"
 
torch._dynamo.config.cache_size_limit = 64
torch._dynamo.config.suppress_errors = True
torch.set_float32_matmul_precision('high')
 
chat = ChatTTS.Chat()
# compile=True works well, but is slower
chat.load_models(source='custom', custom_path=os.path.join(ROOT, "model/ChatTTS"), compile=False)
 
 
spk = {"male": [], "female": []}
male_path = os.path.join(ROOT, "spk/male")
for file in os.listdir(male_path):
    t = torch.load(os.path.join(male_path, file))
    spk['male'].append(t)
female_path = os.path.join(ROOT, "spk/female")
for file in os.listdir(female_path):
    t = torch.load(os.path.join(female_path, file))
    spk['female'].append(t)
    
    
def cretae_new_path(path, filetype):
    if not os.path.exists(path):
        os.makedirs(path)
    files = os.listdir(path)
    if len(files) == 0:
        return os.path.join(path, f'0.{filetype}')
    else:
        max = 0
        for file in files:
            max = max if max>=int(os.path.splitext(file)[0]) else int(os.path.splitext(file)[0])
        return os.path.join(path, str(max+1) + f'.{filetype}')
 
 
 
@app.post("/")
async def AudioGenerate(request: Request):
    params = await request.json()
    speaker = spk[params['gender']][params['id']]
 
    params_refine_text = params['params_refine_text']
    params_infer_code = params['params_infer_code']
    params_infer_code['spk_emb'] = speaker
    
    print("text: ", params['text'])
    wavs = chat.infer(params['text'],
                #   do_text_normalization=True,
                  skip_refine_text=True,
                  params_refine_text=params_refine_text,
                  params_infer_code=params_infer_code)
    
    file_path = cretae_new_path(os.path.join(ROOT, "Audio"), "wav")
    soundfile.write(file_path, wavs[0][0], 24000)
 
    return FileResponse(path=file_path, filename=os.path.basename(file_path), media_type='audio/wav')
 
 
if __name__ == '__main__':
    uvicorn.run(app, host="0.0.0.0", port=8080)