CoolFace
Apppublic

lenML/ChatTTS-Forge

sourceHugging Faceagpl-3.0updated 2y agoView on Hugging Face
301likes
ssml_api.py95 linesDownload Raw Back to impl
1from fastapi import Body, HTTPException2from fastapi.responses import FileResponse, StreamingResponse3from pydantic import BaseModel4 5from modules.api.Api import APIManager6from modules.api.impl.handler.SSMLHandler import SSMLHandler7from modules.api.impl.model.audio_model import AdjustConfig, AudioFormat8from modules.api.impl.model.chattts_model import InferConfig9from modules.api.impl.model.enhancer_model import EnhancerConfig10 11 12class SSMLRequest(BaseModel):13    ssml: str14    format: AudioFormat = "mp3"15 16    # NOTE: 🤔 也许这个值应该配置成系统变量? 传进来有点奇怪17    batch_size: int = 418 19    # end of sentence20    eos: str = "[uv_break]"21 22    spliter_thr: int = 10023 24    enhancer: EnhancerConfig = EnhancerConfig()25    adjuster: AdjustConfig = AdjustConfig()26 27 28async def synthesize_ssml_api(29    request: SSMLRequest = Body(30        ..., description="JSON body with SSML string and format"31    )32):33    try:34        ssml = request.ssml35        format = request.format.lower()36        batch_size = request.batch_size37        eos = request.eos38        spliter_thr = request.spliter_thr39        enhancer = request.enhancer40        adjuster = request.adjuster41 42        if batch_size < 1:43            raise HTTPException(44                status_code=400, detail="Batch size must be greater than 0."45            )46 47        if spliter_thr < 50:48            raise HTTPException(49                status_code=400, detail="Spliter threshold must be greater than 50."50            )51 52        if not ssml or ssml == "":53            raise HTTPException(status_code=400, detail="SSML content is required.")54 55        if format not in ["mp3", "wav"]:56            raise HTTPException(57                status_code=400, detail="Format must be 'mp3' or 'wav'."58            )59 60        infer_config = InferConfig(61            batch_size=batch_size,62            spliter_threshold=spliter_thr,63            eos=eos,64        )65        adjust_config = adjuster66        enhancer_config = enhancer67 68        handler = SSMLHandler(69            ssml_content=ssml,70            infer_config=infer_config,71            adjust_config=adjust_config,72            enhancer_config=enhancer_config,73        )74 75        buffer = handler.enqueue_to_buffer(format=request.format)76 77        mime_type = f"audio/{format}"78        if format == AudioFormat.mp3:79            mime_type = "audio/mpeg"80        return StreamingResponse(buffer, media_type=mime_type)81 82    except Exception as e:83        import logging84 85        logging.exception(e)86 87        if isinstance(e, HTTPException):88            raise e89        else:90            raise HTTPException(status_code=500, detail=str(e))91 92 93def setup(api_manager: APIManager):94    api_manager.post("/v1/ssml", response_class=FileResponse)(synthesize_ssml_api)95