Skip to content

Commit 15f5801

Browse files
authored
Merge pull request #11 from PyThaiNLP/copilot/add-dockerfile-for-project
Add Dockerfile for containerized deployment
2 parents d8db593 + 0f129f6 commit 15f5801

4 files changed

Lines changed: 252 additions & 0 deletions

File tree

.dockerignore

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Git
2+
.git
3+
.gitignore
4+
5+
# Python
6+
__pycache__/
7+
*.py[cod]
8+
*$py.class
9+
*.so
10+
*.egg
11+
*.egg-info/
12+
dist/
13+
build/
14+
.eggs/
15+
.pytest_cache/
16+
.mypy_cache/
17+
.coverage
18+
htmlcov/
19+
20+
# Virtual environments
21+
venv/
22+
env/
23+
ENV/
24+
.venv
25+
26+
# IDEs
27+
.vscode/
28+
.idea/
29+
*.swp
30+
*.swo
31+
*~
32+
33+
# Docs
34+
docs/
35+
/site
36+
37+
# Notebooks
38+
.ipynb_checkpoints
39+
notebook/
40+
41+
# GitHub
42+
.github/
43+
44+
# Output files
45+
*.wav
46+
*.mp3
47+
output.*
48+
49+
# Test files
50+
tests/
51+
test/

DOCKER.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Docker Usage Guide for PyThaiTTS
2+
3+
This guide explains how to build and run PyThaiTTS using Docker.
4+
5+
## Building the Docker Image
6+
7+
To build the Docker image, run the following command from the root directory of the repository:
8+
9+
```bash
10+
docker build -t pythaitts:latest .
11+
```
12+
13+
This will create a Docker image named `pythaitts:latest` with all dependencies installed.
14+
15+
## Running the Demo
16+
17+
To run the demo script that demonstrates Thai text-to-speech synthesis:
18+
19+
```bash
20+
docker run --rm pythaitts:latest
21+
```
22+
23+
The demo will:
24+
1. Initialize the PyThaiTTS model (default: lunarlist_onnx)
25+
2. Generate speech from Thai text
26+
3. Save the output to a WAV file
27+
4. Display the waveform information
28+
29+
## Custom Usage
30+
31+
### Interactive Shell
32+
33+
To start an interactive shell inside the container:
34+
35+
```bash
36+
docker run --rm -it pythaitts:latest /bin/bash
37+
```
38+
39+
### Run Custom Python Script
40+
41+
To run your own Python script:
42+
43+
```bash
44+
docker run --rm -v $(pwd)/your_script.py:/app/custom.py pythaitts:latest python custom.py
45+
```
46+
47+
### Save Output Files
48+
49+
To save generated audio files to your host machine:
50+
51+
```bash
52+
docker run --rm -v $(pwd)/output:/app/output pythaitts:latest python -c "
53+
from pythaitts import TTS
54+
tts = TTS()
55+
tts.tts('สวัสดีครับ', filename='output/hello.wav')
56+
"
57+
```
58+
59+
This will save the generated `hello.wav` file to the `output` directory on your host machine.
60+
61+
## Example Usage in Python
62+
63+
Inside the container, you can use PyThaiTTS as follows:
64+
65+
```python
66+
from pythaitts import TTS
67+
68+
# Initialize TTS with default model
69+
tts = TTS()
70+
71+
# Generate speech and save to file
72+
file_path = tts.tts("ภาษาไทย ง่าย มาก มาก", filename="output.wav")
73+
print(f"Audio saved to: {file_path}")
74+
75+
# Generate speech and get waveform
76+
waveform = tts.tts("ภาษาไทย ง่าย มาก มาก", return_type="waveform")
77+
print(f"Waveform shape: {waveform.shape}")
78+
```
79+
80+
## Available TTS Models
81+
82+
PyThaiTTS supports multiple models:
83+
84+
- **lunarlist_onnx** (default): ONNX-optimized model, CPU-only
85+
- **khanomtan**: KhanomTan TTS model
86+
- **lunarlist**: Original Lunarlist model
87+
88+
To use a different model:
89+
90+
```python
91+
from pythaitts import TTS
92+
93+
# Using KhanomTan model
94+
tts = TTS(pretrained="khanomtan", version="1.0")
95+
```
96+
97+
## Requirements
98+
99+
- Docker installed on your system
100+
- At least 2GB of available disk space
101+
- Internet connection for downloading models on first run
102+
103+
## Troubleshooting
104+
105+
If you encounter issues with model downloads, ensure:
106+
1. You have a stable internet connection
107+
2. The Hugging Face Hub is accessible from your network
108+
3. You have sufficient disk space for model files
109+
110+
## Notes
111+
112+
- The first run will download model files from Hugging Face Hub, which may take some time depending on your internet connection
113+
- Generated audio files are in WAV format
114+
- The default model (lunarlist_onnx) runs on CPU and doesn't require GPU support

Dockerfile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Use Python 3.11 as base image (compatible with the project requirements)
2+
FROM python:3.11-slim
3+
4+
# Set working directory
5+
WORKDIR /app
6+
7+
# Install system dependencies
8+
RUN apt-get update && apt-get install -y \
9+
git \
10+
&& rm -rf /var/lib/apt/lists/*
11+
12+
# Copy requirements and setup files
13+
COPY requirements.txt setup.py README.md ./
14+
COPY pythaitts ./pythaitts
15+
16+
# Install Python dependencies
17+
RUN pip install --no-cache-dir --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org -r requirements.txt
18+
19+
# Install the package
20+
RUN pip install --no-cache-dir -e .
21+
22+
# Copy demo script
23+
COPY demo.py ./
24+
25+
# Set environment variable to avoid Python buffering
26+
ENV PYTHONUNBUFFERED=1
27+
28+
# Run the demo script by default
29+
CMD ["python", "demo.py"]

demo.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Simple demo script for PyThaiTTS
5+
This script demonstrates Thai text-to-speech synthesis using PyThaiTTS.
6+
"""
7+
8+
from pythaitts import TTS
9+
10+
def main():
11+
print("=" * 60)
12+
print("PyThaiTTS Demo - Thai Text-to-Speech")
13+
print("=" * 60)
14+
print()
15+
16+
# Initialize TTS with default model (lunarlist_onnx)
17+
print("Initializing TTS model (lunarlist_onnx)...")
18+
try:
19+
tts = TTS()
20+
print("✓ TTS model loaded successfully!")
21+
print()
22+
23+
# Sample Thai text
24+
text = "สวัสดีครับ ยินดีต้อนรับสู่ PyThaiTTS"
25+
print(f"Input text: {text}")
26+
print()
27+
28+
# Generate speech and save to file
29+
import datetime
30+
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
31+
output_file = f"output_{timestamp}.wav"
32+
print(f"Generating speech and saving to {output_file}...")
33+
result = tts.tts(text, filename=output_file)
34+
print(f"✓ Speech generated successfully!")
35+
print(f"Output saved to: {result}")
36+
print()
37+
38+
# Also demonstrate getting waveform
39+
print("Generating waveform...")
40+
waveform = tts.tts(text, return_type="waveform")
41+
print(f"✓ Waveform generated successfully!")
42+
print(f"Waveform shape: {waveform.shape}")
43+
print()
44+
45+
print("=" * 60)
46+
print("Demo completed successfully!")
47+
print("=" * 60)
48+
49+
except Exception as e:
50+
print(f"✗ Error occurred: {e}")
51+
import traceback
52+
traceback.print_exc()
53+
return 1
54+
55+
return 0
56+
57+
if __name__ == "__main__":
58+
exit(main())

0 commit comments

Comments
 (0)