Skip to content

Commit 0db6709

Browse files
authored
Merge pull request #15 from PyThaiNLP/dev
PyThaiTTS 0.4.0
2 parents fd98dcb + 6da092b commit 0db6709

16 files changed

Lines changed: 2546 additions & 74 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/

.github/workflows/test.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: CI Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- dev
8+
pull_request:
9+
branches:
10+
- main
11+
- dev
12+
13+
jobs:
14+
test:
15+
runs-on: ${{ matrix.os }}
16+
permissions:
17+
contents: read
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
os: [ubuntu-latest]
22+
python-version: ['3.8', '3.9', '3.10', '3.11']
23+
24+
steps:
25+
- uses: actions/checkout@v4
26+
27+
- name: Set up Python ${{ matrix.python-version }}
28+
uses: actions/setup-python@v5
29+
with:
30+
python-version: ${{ matrix.python-version }}
31+
cache: 'pip'
32+
33+
- name: Install dependencies
34+
run: |
35+
python -m pip install --upgrade pip
36+
pip install -r requirements.txt
37+
pip install -e .
38+
39+
- name: Run tests
40+
run: |
41+
python -m unittest discover -s tests -p "test_*.py" -v

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"]

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Install by pip:
1414
1515
## Usage
1616

17+
### Basic Usage
18+
1719
```python
1820
from pythaitts import TTS
1921

@@ -22,4 +24,61 @@ file = tts.tts("ภาษาไทย ง่าย มาก มาก", filenam
2224
wave = tts.tts("ภาษาไทย ง่าย มาก มาก",return_type="waveform") # It will get waveform.
2325
```
2426

27+
### Using Different TTS Models
28+
29+
PyThaiTTS supports multiple TTS models. You can specify which model to use:
30+
31+
```python
32+
from pythaitts import TTS
33+
34+
# Use VachanaTTS (default voices: th_f_1, th_m_1, th_f_2, th_m_2)
35+
tts = TTS(pretrained="vachana")
36+
file = tts.tts("สวัสดีครับ", speaker_idx="th_f_1", filename="output.wav")
37+
38+
# Use Lunarlist ONNX (default)
39+
tts = TTS(pretrained="lunarlist_onnx")
40+
file = tts.tts("ภาษาไทย ง่าย มาก", filename="output.wav")
41+
42+
# Use KhanomTan
43+
tts = TTS(pretrained="khanomtan")
44+
file = tts.tts("ภาษาไทย", speaker_idx="Linda", filename="output.wav")
45+
```
46+
47+
### Text Preprocessing
48+
49+
PyThaiTTS includes automatic text preprocessing to improve TTS quality:
50+
- **Number to Thai text conversion**: Converts digits (e.g., "123") to Thai text (e.g., "หนึ่งร้อยยี่สิบสาม")
51+
- **Mai yamok (ๆ) expansion**: Expands the Thai repetition character (e.g., "ดีๆ" becomes "ดีดี")
52+
53+
Preprocessing is enabled by default:
54+
55+
```python
56+
from pythaitts import TTS
57+
58+
tts = TTS()
59+
# Automatic preprocessing: "มี 5 คนๆ" becomes "มี ห้า คนคน"
60+
file = tts.tts("มี 5 คนๆ", filename="output.wav")
61+
```
62+
63+
You can disable preprocessing if needed:
64+
65+
```python
66+
file = tts.tts("มี 5 คนๆ", preprocess=False, filename="output.wav")
67+
```
68+
69+
You can also use preprocessing functions directly:
70+
71+
```python
72+
from pythaitts import num_to_thai, expand_maiyamok, preprocess_text
73+
74+
# Convert numbers to Thai text
75+
print(num_to_thai("123")) # Output: หนึ่งร้อยยี่สิบสาม
76+
77+
# Expand mai yamok
78+
print(expand_maiyamok("ดีๆ")) # Output: ดีดี
79+
80+
# Full preprocessing
81+
print(preprocess_text("มี 5 คนๆ")) # Output: มี ห้า คนคน
82+
```
83+
2584
You can see more at [https://pythainlp.github.io/PyThaiTTS/](https://pythainlp.github.io/PyThaiTTS/).

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())

docs/index.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,15 @@ PyThaiTTS
55
Open Source Thai Text-to-speech library in Python
66

77
.. autoclass:: TTS
8-
:members:
8+
:members:
9+
10+
Text Preprocessing
11+
------------------
12+
13+
PyThaiTTS provides text preprocessing functions to improve TTS quality.
14+
15+
.. autofunction:: preprocess_text
16+
17+
.. autofunction:: num_to_thai
18+
19+
.. autofunction:: expand_maiyamok

0 commit comments

Comments
 (0)