Skip to content

Commit

Permalink
feat: add docker build
Browse files Browse the repository at this point in the history
  • Loading branch information
ultrasev committed May 30, 2024
1 parent 59bf38d commit 6026003
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Build Docker chattts image

on:
push:
branches:
- master

jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Log in to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.CR_PAT }}

- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
push: true
tags: ghcr.io/${{ github.repository_owner }}/chattts:latest
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.8-slim

RUN apt-get update && apt-get install -y gcc libgomp1 && apt-get clean

WORKDIR /app

COPY requirements.txt .

RUN pip3 install --no-cache-dir -r requirements.txt

COPY . .

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3.8'

services:
chattts:
build:
context: .
dockerfile: Dockerfile
ports:
- "8000:8000"
environment:
- ENV=production
56 changes: 56 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python3
import wave
import numpy as np
from fastapi import FastAPI, HTTPException, Depends
import pydantic
import ChatTTS

app = FastAPI()


class TTSInput(pydantic.BaseModel):
text: str
output_path: str
seed: int = 42


def get_chat_model() -> ChatTTS.Chat:
chat = ChatTTS.Chat()
chat.load_models()
return chat


@app.post("/tts")
def tts(input: TTSInput, chat: ChatTTS.Chat = Depends(get_chat_model)):
try:
texts = [input.text]
r = chat.sample_random_speaker(seed=input.seed)

params_infer_code = {
'spk_emb': r, # add sampled speaker
'temperature': .3, # using customtemperature
'top_P': 0.7, # top P decode
'top_K': 20, # top K decode
}

params_refine_text = {
'prompt': '[oral_2][laugh_0][break_6]'
}

wavs = chat.infer(texts,
params_infer_code=params_infer_code,
params_refine_text=params_refine_text, use_decoder=True)

audio_data = np.array(wavs[0], dtype=np.float32)
sample_rate = 24000
audio_data = (audio_data * 32767).astype(np.int16)

with wave.open(input.output_path, "w") as wf:
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(sample_rate)
wf.writeframes(audio_data.tobytes())
return {"output_path": input.output_path}

except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ einops
vector_quantize_pytorch
transformers~=4.41.1
vocos
fastapi
uvicorn
starlette
pydantic

0 comments on commit 6026003

Please sign in to comment.