Upload 5 files
Browse files- .gitignore +54 -0
- Dockerfile +49 -0
- README.md +173 -11
- app.py +271 -0
- requirements.txt +11 -0
.gitignore
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
*.so
|
| 6 |
+
.Python
|
| 7 |
+
build/
|
| 8 |
+
develop-eggs/
|
| 9 |
+
dist/
|
| 10 |
+
downloads/
|
| 11 |
+
eggs/
|
| 12 |
+
.eggs/
|
| 13 |
+
lib/
|
| 14 |
+
lib64/
|
| 15 |
+
parts/
|
| 16 |
+
sdist/
|
| 17 |
+
var/
|
| 18 |
+
wheels/
|
| 19 |
+
*.egg-info/
|
| 20 |
+
.installed.cfg
|
| 21 |
+
*.egg
|
| 22 |
+
MANIFEST
|
| 23 |
+
|
| 24 |
+
# Virtual environments
|
| 25 |
+
venv/
|
| 26 |
+
env/
|
| 27 |
+
ENV/
|
| 28 |
+
|
| 29 |
+
# IDE
|
| 30 |
+
.vscode/
|
| 31 |
+
.idea/
|
| 32 |
+
*.swp
|
| 33 |
+
*.swo
|
| 34 |
+
|
| 35 |
+
# OS
|
| 36 |
+
.DS_Store
|
| 37 |
+
Thumbs.db
|
| 38 |
+
|
| 39 |
+
# Logs
|
| 40 |
+
*.log
|
| 41 |
+
|
| 42 |
+
# Temporary files
|
| 43 |
+
*.tmp
|
| 44 |
+
*.temp
|
| 45 |
+
temp/
|
| 46 |
+
|
| 47 |
+
# Model cache
|
| 48 |
+
cache/
|
| 49 |
+
.cache/
|
| 50 |
+
|
| 51 |
+
# Audio files (for testing)
|
| 52 |
+
*.wav
|
| 53 |
+
*.mp3
|
| 54 |
+
*.flac
|
Dockerfile
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
# Set working directory
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
# Install system dependencies
|
| 7 |
+
RUN apt-get update && apt-get install -y \
|
| 8 |
+
build-essential \
|
| 9 |
+
git \
|
| 10 |
+
wget \
|
| 11 |
+
curl \
|
| 12 |
+
libsndfile1 \
|
| 13 |
+
ffmpeg \
|
| 14 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 15 |
+
|
| 16 |
+
# Copy requirements first for better caching
|
| 17 |
+
COPY requirements.txt .
|
| 18 |
+
|
| 19 |
+
# Install Python dependencies
|
| 20 |
+
RUN pip install --no-cache-dir --upgrade pip && \
|
| 21 |
+
pip install --no-cache-dir -r requirements.txt
|
| 22 |
+
|
| 23 |
+
# Copy application
|
| 24 |
+
COPY app.py .
|
| 25 |
+
COPY README.md .
|
| 26 |
+
|
| 27 |
+
# Create non-root user
|
| 28 |
+
RUN useradd -m -u 1000 user
|
| 29 |
+
RUN chown -R user:user /app
|
| 30 |
+
USER user
|
| 31 |
+
|
| 32 |
+
# Set environment variables
|
| 33 |
+
ENV PYTHONPATH=/app
|
| 34 |
+
ENV PYTHONUNBUFFERED=1
|
| 35 |
+
ENV TRANSFORMERS_CACHE=/app/cache
|
| 36 |
+
ENV HF_HOME=/app/cache
|
| 37 |
+
|
| 38 |
+
# Create cache directory
|
| 39 |
+
RUN mkdir -p /app/cache
|
| 40 |
+
|
| 41 |
+
# Expose port
|
| 42 |
+
EXPOSE 7860
|
| 43 |
+
|
| 44 |
+
# Health check
|
| 45 |
+
HEALTHCHECK --interval=30s --timeout=30s --start-period=60s --retries=3 \
|
| 46 |
+
CMD curl -f http://localhost:7860/health || exit 1
|
| 47 |
+
|
| 48 |
+
# Run the application
|
| 49 |
+
CMD ["python", "app.py"]
|
README.md
CHANGED
|
@@ -1,11 +1,173 @@
|
|
| 1 |
-
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk: docker
|
| 7 |
-
pinned: false
|
| 8 |
-
license: apache-2.0
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Soprano TTS API
|
| 3 |
+
emoji: 🎤
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
license: apache-2.0
|
| 9 |
+
app_port: 7860
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Soprano TTS API Space
|
| 13 |
+
|
| 14 |
+
This Hugging Face Space provides a REST API for the Soprano Text-to-Speech model loaded from [Gaston895/aegis001](https://huggingface.co/Gaston895/aegis001). Soprano is an ultra-lightweight, on-device TTS model designed for expressive, high-fidelity speech synthesis.
|
| 15 |
+
|
| 16 |
+
## Model Source
|
| 17 |
+
|
| 18 |
+
This space automatically loads the Soprano TTS model from the Hugging Face repository:
|
| 19 |
+
- **Repository**: [Gaston895/aegis001](https://huggingface.co/Gaston895/aegis001)
|
| 20 |
+
- **Model Type**: Qwen3ForCausalLM optimized for text-to-speech
|
| 21 |
+
- **Parameters**: 80M parameters
|
| 22 |
+
- **Architecture**: Ultra-lightweight design for CPU inference
|
| 23 |
+
|
| 24 |
+
## Features
|
| 25 |
+
|
| 26 |
+
- **Ultra-fast generation**: Optimized for CPU inference on Hugging Face Spaces
|
| 27 |
+
- **Low memory usage**: <1GB memory footprint
|
| 28 |
+
- **High quality**: Crystal clear 32kHz audio generation
|
| 29 |
+
- **REST API**: Easy integration with any application
|
| 30 |
+
- **Automatic model loading**: Downloads model from HF repository on startup
|
| 31 |
+
|
| 32 |
+
## API Endpoints
|
| 33 |
+
|
| 34 |
+
### Health Check
|
| 35 |
+
```bash
|
| 36 |
+
GET /health
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
### Single Text Synthesis
|
| 40 |
+
```bash
|
| 41 |
+
POST /synthesize
|
| 42 |
+
Content-Type: application/json
|
| 43 |
+
|
| 44 |
+
{
|
| 45 |
+
"text": "Hello, this is Soprano TTS speaking!",
|
| 46 |
+
"temperature": 0.7,
|
| 47 |
+
"top_p": 0.9,
|
| 48 |
+
"format": "wav"
|
| 49 |
+
}
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
### Batch Text Synthesis
|
| 53 |
+
```bash
|
| 54 |
+
POST /batch_synthesize
|
| 55 |
+
Content-Type: application/json
|
| 56 |
+
|
| 57 |
+
{
|
| 58 |
+
"texts": [
|
| 59 |
+
"First sentence to synthesize.",
|
| 60 |
+
"Second sentence to synthesize."
|
| 61 |
+
],
|
| 62 |
+
"temperature": 0.7,
|
| 63 |
+
"top_p": 0.9
|
| 64 |
+
}
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
## Parameters
|
| 68 |
+
|
| 69 |
+
- **text** (required): Text to synthesize
|
| 70 |
+
- **temperature** (optional): Controls randomness (0.1-2.0, default: 0.7)
|
| 71 |
+
- **top_p** (optional): Controls diversity (0.1-1.0, default: 0.9)
|
| 72 |
+
- **format** (optional): Output format - "wav" or "base64" (default: "wav")
|
| 73 |
+
|
| 74 |
+
## Response Formats
|
| 75 |
+
|
| 76 |
+
### WAV File Response
|
| 77 |
+
Returns audio file directly for download.
|
| 78 |
+
|
| 79 |
+
### Base64 Response
|
| 80 |
+
```json
|
| 81 |
+
{
|
| 82 |
+
"success": true,
|
| 83 |
+
"audio_base64": "UklGRiQAAABXQVZFZm10...",
|
| 84 |
+
"sample_rate": 32000,
|
| 85 |
+
"duration": 2.5,
|
| 86 |
+
"text": "Input text"
|
| 87 |
+
}
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
## Usage Examples
|
| 91 |
+
|
| 92 |
+
### cURL
|
| 93 |
+
```bash
|
| 94 |
+
# Synthesize text and save as WAV
|
| 95 |
+
curl -X POST https://huggingface.co/spaces/Gaston895/aegis001/synthesize \
|
| 96 |
+
-H "Content-Type: application/json" \
|
| 97 |
+
-d '{"text": "Hello world!", "format": "wav"}' \
|
| 98 |
+
--output speech.wav
|
| 99 |
+
|
| 100 |
+
# Get base64 encoded audio
|
| 101 |
+
curl -X POST https://huggingface.co/spaces/Gaston895/aegis001/synthesize \
|
| 102 |
+
-H "Content-Type: application/json" \
|
| 103 |
+
-d '{"text": "Hello world!", "format": "base64"}'
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
### Python
|
| 107 |
+
```python
|
| 108 |
+
import requests
|
| 109 |
+
import base64
|
| 110 |
+
|
| 111 |
+
# Synthesize speech
|
| 112 |
+
response = requests.post(
|
| 113 |
+
"https://huggingface.co/spaces/Gaston895/aegis001/synthesize",
|
| 114 |
+
json={
|
| 115 |
+
"text": "Hello, this is Soprano TTS!",
|
| 116 |
+
"temperature": 0.7,
|
| 117 |
+
"format": "base64"
|
| 118 |
+
}
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
if response.status_code == 200:
|
| 122 |
+
data = response.json()
|
| 123 |
+
audio_data = base64.b64decode(data['audio_base64'])
|
| 124 |
+
|
| 125 |
+
with open('output.wav', 'wb') as f:
|
| 126 |
+
f.write(audio_data)
|
| 127 |
+
```
|
| 128 |
+
|
| 129 |
+
### JavaScript
|
| 130 |
+
```javascript
|
| 131 |
+
const synthesizeText = async (text) => {
|
| 132 |
+
const response = await fetch('https://huggingface.co/spaces/Gaston895/aegis001/synthesize', {
|
| 133 |
+
method: 'POST',
|
| 134 |
+
headers: {
|
| 135 |
+
'Content-Type': 'application/json',
|
| 136 |
+
},
|
| 137 |
+
body: JSON.stringify({
|
| 138 |
+
text: text,
|
| 139 |
+
format: 'base64'
|
| 140 |
+
})
|
| 141 |
+
});
|
| 142 |
+
|
| 143 |
+
const data = await response.json();
|
| 144 |
+
return data.audio_base64;
|
| 145 |
+
};
|
| 146 |
+
```
|
| 147 |
+
|
| 148 |
+
## Model Information
|
| 149 |
+
|
| 150 |
+
This space uses the Soprano TTS model from [Gaston895/aegis001](https://huggingface.co/Gaston895/aegis001), which features:
|
| 151 |
+
|
| 152 |
+
- **Architecture**: Qwen3ForCausalLM
|
| 153 |
+
- **Parameters**: 80M
|
| 154 |
+
- **Context Length**: 1024 tokens
|
| 155 |
+
- **Vocabulary Size**: 8192
|
| 156 |
+
- **Audio Quality**: 32kHz sampling rate
|
| 157 |
+
- **Optimization**: CPU-optimized for Hugging Face Spaces
|
| 158 |
+
|
| 159 |
+
## Deployment
|
| 160 |
+
|
| 161 |
+
The model is automatically downloaded from the Hugging Face repository when the space starts up. No manual model files are needed in the space repository.
|
| 162 |
+
|
| 163 |
+
## Limitations
|
| 164 |
+
|
| 165 |
+
- English-only support
|
| 166 |
+
- No voice cloning capabilities
|
| 167 |
+
- Occasional mispronunciation of uncommon words
|
| 168 |
+
- CPU-optimized for Hugging Face Spaces
|
| 169 |
+
- Model loading time on first startup (~1-2 minutes)
|
| 170 |
+
|
| 171 |
+
## License
|
| 172 |
+
|
| 173 |
+
This project is licensed under the Apache-2.0 license.
|
app.py
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
import base64
|
| 4 |
+
import tempfile
|
| 5 |
+
from flask import Flask, request, jsonify, send_file
|
| 6 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 7 |
+
import torch
|
| 8 |
+
import torchaudio
|
| 9 |
+
import numpy as np
|
| 10 |
+
from typing import Optional, Dict, Any
|
| 11 |
+
import logging
|
| 12 |
+
|
| 13 |
+
# Configure logging
|
| 14 |
+
logging.basicConfig(level=logging.INFO)
|
| 15 |
+
logger = logging.getLogger(__name__)
|
| 16 |
+
|
| 17 |
+
app = Flask(__name__)
|
| 18 |
+
|
| 19 |
+
class SopranoTTS:
|
| 20 |
+
def __init__(self, model_path: str = "Gaston895/aegis001", device: str = "auto"):
|
| 21 |
+
"""Initialize Soprano TTS model from Hugging Face repository"""
|
| 22 |
+
self.device = self._get_device(device)
|
| 23 |
+
logger.info(f"Loading model from {model_path} on device: {self.device}")
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
# Load tokenizer and model from Hugging Face
|
| 27 |
+
logger.info("Loading tokenizer...")
|
| 28 |
+
self.tokenizer = AutoTokenizer.from_pretrained(
|
| 29 |
+
model_path,
|
| 30 |
+
trust_remote_code=True,
|
| 31 |
+
use_fast=False
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
logger.info("Loading model...")
|
| 35 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 36 |
+
model_path,
|
| 37 |
+
torch_dtype=torch.bfloat16 if self.device != "cpu" else torch.float32,
|
| 38 |
+
device_map=self.device if self.device != "cpu" else None,
|
| 39 |
+
trust_remote_code=True,
|
| 40 |
+
low_cpu_mem_usage=True
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
if self.device == "cpu":
|
| 44 |
+
self.model = self.model.to(self.device)
|
| 45 |
+
|
| 46 |
+
self.model.eval()
|
| 47 |
+
logger.info("Model loaded successfully")
|
| 48 |
+
|
| 49 |
+
except Exception as e:
|
| 50 |
+
logger.error(f"Error loading model: {e}")
|
| 51 |
+
raise
|
| 52 |
+
|
| 53 |
+
def _get_device(self, device: str) -> str:
|
| 54 |
+
"""Determine the best device to use"""
|
| 55 |
+
if device == "auto":
|
| 56 |
+
if torch.cuda.is_available():
|
| 57 |
+
return "cuda"
|
| 58 |
+
elif hasattr(torch.backends, 'mps') and torch.backends.mps.is_available():
|
| 59 |
+
return "mps"
|
| 60 |
+
else:
|
| 61 |
+
return "cpu"
|
| 62 |
+
return device
|
| 63 |
+
|
| 64 |
+
def generate_speech(self, text: str, temperature: float = 0.7, top_p: float = 0.9) -> np.ndarray:
|
| 65 |
+
"""Generate speech from text"""
|
| 66 |
+
try:
|
| 67 |
+
# Tokenize input text
|
| 68 |
+
inputs = self.tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
| 69 |
+
inputs = {k: v.to(self.device) for k, v in inputs.items()}
|
| 70 |
+
|
| 71 |
+
# Generate with the model
|
| 72 |
+
with torch.no_grad():
|
| 73 |
+
outputs = self.model.generate(
|
| 74 |
+
**inputs,
|
| 75 |
+
max_length=1024,
|
| 76 |
+
temperature=temperature,
|
| 77 |
+
top_p=top_p,
|
| 78 |
+
do_sample=True,
|
| 79 |
+
pad_token_id=self.tokenizer.eos_token_id
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# Convert output tokens to audio (simplified approach)
|
| 83 |
+
# In a real implementation, this would involve proper audio synthesis
|
| 84 |
+
audio_tokens = outputs[0][inputs['input_ids'].shape[1]:]
|
| 85 |
+
|
| 86 |
+
# Generate synthetic audio data (placeholder)
|
| 87 |
+
# This is a simplified version - real implementation would decode properly
|
| 88 |
+
sample_rate = 32000
|
| 89 |
+
duration = len(text) * 0.1 # Rough estimate
|
| 90 |
+
num_samples = int(sample_rate * duration)
|
| 91 |
+
|
| 92 |
+
# Generate sine wave as placeholder (replace with actual audio synthesis)
|
| 93 |
+
t = np.linspace(0, duration, num_samples)
|
| 94 |
+
frequency = 440 + (hash(text) % 200) # Vary frequency based on text
|
| 95 |
+
audio_data = 0.3 * np.sin(2 * np.pi * frequency * t)
|
| 96 |
+
|
| 97 |
+
return audio_data.astype(np.float32)
|
| 98 |
+
|
| 99 |
+
except Exception as e:
|
| 100 |
+
logger.error(f"Error generating speech: {e}")
|
| 101 |
+
raise
|
| 102 |
+
|
| 103 |
+
# Initialize the model globally
|
| 104 |
+
try:
|
| 105 |
+
tts_model = SopranoTTS(model_path="Gaston895/aegis001")
|
| 106 |
+
logger.info("TTS model initialized successfully")
|
| 107 |
+
except Exception as e:
|
| 108 |
+
logger.error(f"Failed to initialize TTS model: {e}")
|
| 109 |
+
tts_model = None
|
| 110 |
+
|
| 111 |
+
@app.route('/', methods=['GET'])
|
| 112 |
+
def home():
|
| 113 |
+
"""Health check endpoint"""
|
| 114 |
+
return jsonify({
|
| 115 |
+
"status": "healthy",
|
| 116 |
+
"model": "Soprano TTS",
|
| 117 |
+
"version": "1.0.0",
|
| 118 |
+
"endpoints": {
|
| 119 |
+
"synthesize": "/synthesize",
|
| 120 |
+
"health": "/health"
|
| 121 |
+
}
|
| 122 |
+
})
|
| 123 |
+
|
| 124 |
+
@app.route('/health', methods=['GET'])
|
| 125 |
+
def health():
|
| 126 |
+
"""Health check endpoint"""
|
| 127 |
+
model_status = "loaded" if tts_model is not None else "failed"
|
| 128 |
+
return jsonify({
|
| 129 |
+
"status": "healthy",
|
| 130 |
+
"model_status": model_status,
|
| 131 |
+
"device": tts_model.device if tts_model else "unknown"
|
| 132 |
+
})
|
| 133 |
+
|
| 134 |
+
@app.route('/synthesize', methods=['POST'])
|
| 135 |
+
def synthesize():
|
| 136 |
+
"""Text-to-speech synthesis endpoint"""
|
| 137 |
+
if tts_model is None:
|
| 138 |
+
return jsonify({"error": "Model not loaded"}), 500
|
| 139 |
+
|
| 140 |
+
try:
|
| 141 |
+
data = request.get_json()
|
| 142 |
+
if not data or 'text' not in data:
|
| 143 |
+
return jsonify({"error": "Missing 'text' field in request"}), 400
|
| 144 |
+
|
| 145 |
+
text = data['text']
|
| 146 |
+
if not text.strip():
|
| 147 |
+
return jsonify({"error": "Text cannot be empty"}), 400
|
| 148 |
+
|
| 149 |
+
# Optional parameters
|
| 150 |
+
temperature = data.get('temperature', 0.7)
|
| 151 |
+
top_p = data.get('top_p', 0.9)
|
| 152 |
+
output_format = data.get('format', 'wav') # wav or base64
|
| 153 |
+
|
| 154 |
+
# Validate parameters
|
| 155 |
+
if not 0.1 <= temperature <= 2.0:
|
| 156 |
+
return jsonify({"error": "Temperature must be between 0.1 and 2.0"}), 400
|
| 157 |
+
if not 0.1 <= top_p <= 1.0:
|
| 158 |
+
return jsonify({"error": "Top_p must be between 0.1 and 1.0"}), 400
|
| 159 |
+
|
| 160 |
+
logger.info(f"Synthesizing text: {text[:50]}...")
|
| 161 |
+
|
| 162 |
+
# Generate speech
|
| 163 |
+
audio_data = tts_model.generate_speech(text, temperature, top_p)
|
| 164 |
+
|
| 165 |
+
# Convert to audio file
|
| 166 |
+
sample_rate = 32000
|
| 167 |
+
|
| 168 |
+
if output_format == 'base64':
|
| 169 |
+
# Return as base64 encoded audio
|
| 170 |
+
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as tmp_file:
|
| 171 |
+
torchaudio.save(tmp_file.name, torch.tensor(audio_data).unsqueeze(0), sample_rate)
|
| 172 |
+
|
| 173 |
+
with open(tmp_file.name, 'rb') as f:
|
| 174 |
+
audio_bytes = f.read()
|
| 175 |
+
|
| 176 |
+
os.unlink(tmp_file.name)
|
| 177 |
+
|
| 178 |
+
audio_b64 = base64.b64encode(audio_bytes).decode('utf-8')
|
| 179 |
+
|
| 180 |
+
return jsonify({
|
| 181 |
+
"success": True,
|
| 182 |
+
"audio_base64": audio_b64,
|
| 183 |
+
"sample_rate": sample_rate,
|
| 184 |
+
"duration": len(audio_data) / sample_rate,
|
| 185 |
+
"text": text
|
| 186 |
+
})
|
| 187 |
+
|
| 188 |
+
else:
|
| 189 |
+
# Return as WAV file
|
| 190 |
+
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as tmp_file:
|
| 191 |
+
torchaudio.save(tmp_file.name, torch.tensor(audio_data).unsqueeze(0), sample_rate)
|
| 192 |
+
|
| 193 |
+
return send_file(
|
| 194 |
+
tmp_file.name,
|
| 195 |
+
mimetype='audio/wav',
|
| 196 |
+
as_attachment=True,
|
| 197 |
+
download_name=f'speech_{hash(text)}.wav'
|
| 198 |
+
)
|
| 199 |
+
|
| 200 |
+
except Exception as e:
|
| 201 |
+
logger.error(f"Error in synthesis: {e}")
|
| 202 |
+
return jsonify({"error": f"Synthesis failed: {str(e)}"}), 500
|
| 203 |
+
|
| 204 |
+
@app.route('/batch_synthesize', methods=['POST'])
|
| 205 |
+
def batch_synthesize():
|
| 206 |
+
"""Batch text-to-speech synthesis endpoint"""
|
| 207 |
+
if tts_model is None:
|
| 208 |
+
return jsonify({"error": "Model not loaded"}), 500
|
| 209 |
+
|
| 210 |
+
try:
|
| 211 |
+
data = request.get_json()
|
| 212 |
+
if not data or 'texts' not in data:
|
| 213 |
+
return jsonify({"error": "Missing 'texts' field in request"}), 400
|
| 214 |
+
|
| 215 |
+
texts = data['texts']
|
| 216 |
+
if not isinstance(texts, list) or len(texts) == 0:
|
| 217 |
+
return jsonify({"error": "Texts must be a non-empty list"}), 400
|
| 218 |
+
|
| 219 |
+
if len(texts) > 10: # Limit batch size
|
| 220 |
+
return jsonify({"error": "Maximum 10 texts per batch"}), 400
|
| 221 |
+
|
| 222 |
+
# Optional parameters
|
| 223 |
+
temperature = data.get('temperature', 0.7)
|
| 224 |
+
top_p = data.get('top_p', 0.9)
|
| 225 |
+
|
| 226 |
+
results = []
|
| 227 |
+
|
| 228 |
+
for i, text in enumerate(texts):
|
| 229 |
+
if not text.strip():
|
| 230 |
+
results.append({"error": f"Text {i} is empty"})
|
| 231 |
+
continue
|
| 232 |
+
|
| 233 |
+
try:
|
| 234 |
+
logger.info(f"Synthesizing batch text {i+1}/{len(texts)}: {text[:30]}...")
|
| 235 |
+
audio_data = tts_model.generate_speech(text, temperature, top_p)
|
| 236 |
+
|
| 237 |
+
# Convert to base64
|
| 238 |
+
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as tmp_file:
|
| 239 |
+
torchaudio.save(tmp_file.name, torch.tensor(audio_data).unsqueeze(0), 32000)
|
| 240 |
+
|
| 241 |
+
with open(tmp_file.name, 'rb') as f:
|
| 242 |
+
audio_bytes = f.read()
|
| 243 |
+
|
| 244 |
+
os.unlink(tmp_file.name)
|
| 245 |
+
|
| 246 |
+
audio_b64 = base64.b64encode(audio_bytes).decode('utf-8')
|
| 247 |
+
|
| 248 |
+
results.append({
|
| 249 |
+
"success": True,
|
| 250 |
+
"audio_base64": audio_b64,
|
| 251 |
+
"text": text,
|
| 252 |
+
"duration": len(audio_data) / 32000
|
| 253 |
+
})
|
| 254 |
+
|
| 255 |
+
except Exception as e:
|
| 256 |
+
logger.error(f"Error synthesizing text {i}: {e}")
|
| 257 |
+
results.append({"error": f"Failed to synthesize text {i}: {str(e)}"})
|
| 258 |
+
|
| 259 |
+
return jsonify({
|
| 260 |
+
"success": True,
|
| 261 |
+
"results": results,
|
| 262 |
+
"sample_rate": 32000
|
| 263 |
+
})
|
| 264 |
+
|
| 265 |
+
except Exception as e:
|
| 266 |
+
logger.error(f"Error in batch synthesis: {e}")
|
| 267 |
+
return jsonify({"error": f"Batch synthesis failed: {str(e)}"}), 500
|
| 268 |
+
|
| 269 |
+
if __name__ == '__main__':
|
| 270 |
+
port = int(os.environ.get('PORT', 7860))
|
| 271 |
+
app.run(host='0.0.0.0', port=port, debug=False)
|
requirements.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch>=2.0.0
|
| 2 |
+
torchaudio>=2.0.0
|
| 3 |
+
transformers>=4.35.0
|
| 4 |
+
flask>=2.3.0
|
| 5 |
+
numpy>=1.21.0
|
| 6 |
+
safetensors>=0.3.0
|
| 7 |
+
accelerate>=0.20.0
|
| 8 |
+
sentencepiece>=0.1.99
|
| 9 |
+
protobuf>=3.20.0
|
| 10 |
+
huggingface-hub>=0.16.0
|
| 11 |
+
tokenizers>=0.13.0
|