Upgrade to Kokoro TTS - 10x faster with emotional voices
Browse files- .gitignore +32 -0
- README.md +122 -0
- app.py +290 -67
- packages.txt +1 -0
- requirements.txt +6 -4
- supabase_schema.sql +47 -0
.gitignore
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.py[cod]
|
| 3 |
+
*$py.class
|
| 4 |
+
*.so
|
| 5 |
+
.Python
|
| 6 |
+
build/
|
| 7 |
+
develop-eggs/
|
| 8 |
+
dist/
|
| 9 |
+
downloads/
|
| 10 |
+
eggs/
|
| 11 |
+
.eggs/
|
| 12 |
+
lib/
|
| 13 |
+
lib64/
|
| 14 |
+
parts/
|
| 15 |
+
sdist/
|
| 16 |
+
var/
|
| 17 |
+
wheels/
|
| 18 |
+
*.egg-info/
|
| 19 |
+
.installed.cfg
|
| 20 |
+
*.egg
|
| 21 |
+
.env
|
| 22 |
+
.venv
|
| 23 |
+
env/
|
| 24 |
+
venv/
|
| 25 |
+
ENV/
|
| 26 |
+
env.bak/
|
| 27 |
+
venv.bak/
|
| 28 |
+
*.wav
|
| 29 |
+
*.mp3
|
| 30 |
+
*.onnx
|
| 31 |
+
voices/
|
| 32 |
+
.DS_Store
|
README.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Kokoro TTS Service - Professional & Lightning Fast β‘
|
| 2 |
+
|
| 3 |
+
**10x faster than XTTS** | Emotional voices for storytelling | CPU-optimized
|
| 4 |
+
|
| 5 |
+
## π Why Kokoro?
|
| 6 |
+
|
| 7 |
+
- β‘ **Lightning Fast**: Generates audio in real-time on CPU
|
| 8 |
+
- π **Emotional Expression**: Perfect for audiobooks & storytelling
|
| 9 |
+
- πΎ **Lightweight**: Only 82M parameters (vs XTTS 400M+)
|
| 10 |
+
- π **Secure**: Supabase authentication with user management
|
| 11 |
+
- π **High Quota**: 50 generations/day (vs 3 with XTTS)
|
| 12 |
+
|
| 13 |
+
## π Admin Credentials
|
| 14 |
+
```
|
| 15 |
+
Username: madhab
|
| 16 |
+
Password: Madhab@Studify2024!
|
| 17 |
+
```
|
| 18 |
+
|
| 19 |
+
## π¦ Setup
|
| 20 |
+
|
| 21 |
+
### 1. Supabase Database
|
| 22 |
+
Run `supabase_schema.sql` in your Supabase SQL Editor
|
| 23 |
+
|
| 24 |
+
### 2. Install & Run
|
| 25 |
+
```bash
|
| 26 |
+
pip install -r requirements.txt
|
| 27 |
+
python app.py
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
## π€ Available Voices
|
| 31 |
+
|
| 32 |
+
| Voice | Description | Best For |
|
| 33 |
+
|-------|-------------|----------|
|
| 34 |
+
| `af_heart` | American Female (warm) | General narration |
|
| 35 |
+
| `af_bella` | American Female (professional) | Educational content |
|
| 36 |
+
| `am_adam` | American Male (confident) | Business/Tech |
|
| 37 |
+
| `am_michael` | American Male (friendly) | Casual/Conversational |
|
| 38 |
+
| `bf_emma` | British Female (elegant) | Formal content |
|
| 39 |
+
| `bf_isabella` | British Female (storytelling) | **Audiobooks/Stories** β |
|
| 40 |
+
|
| 41 |
+
## π‘ API Usage
|
| 42 |
+
|
| 43 |
+
### Generate Speech (Fast!)
|
| 44 |
+
```bash
|
| 45 |
+
curl -X POST http://localhost:7860/api/generate \
|
| 46 |
+
-F "username=madhab" \
|
| 47 |
+
-F "password=Madhab@Studify2024!" \
|
| 48 |
+
-F "text=The concept of artificial intelligence has evolved significantly over the past few decades." \
|
| 49 |
+
-F "voice=bf_isabella" \
|
| 50 |
+
-F "speed=1.0" \
|
| 51 |
+
--output output.wav
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
### Check Quota
|
| 55 |
+
```bash
|
| 56 |
+
curl -X POST http://localhost:7860/api/quota \
|
| 57 |
+
-F "username=madhab" \
|
| 58 |
+
-F "password=Madhab@Studify2024!"
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
### Create New User (Admin)
|
| 62 |
+
```bash
|
| 63 |
+
curl -X POST http://localhost:7860/api/admin/create-user \
|
| 64 |
+
-F "admin_username=madhab" \
|
| 65 |
+
-F "admin_password=Madhab@Studify2024!" \
|
| 66 |
+
-F "new_username=student" \
|
| 67 |
+
-F "new_password=SecurePass123!" \
|
| 68 |
+
-F "role=user" \
|
| 69 |
+
-F "daily_limit=50"
|
| 70 |
+
```
|
| 71 |
+
|
| 72 |
+
## β‘ Performance Comparison
|
| 73 |
+
|
| 74 |
+
| Model | Speed | Quality | CPU Usage | Max Chars |
|
| 75 |
+
|-------|-------|---------|-----------|-----------|
|
| 76 |
+
| **Kokoro** | **10x** | High | Low | 5000 |
|
| 77 |
+
| XTTS v2 | 1x | Very High | High | 3000 |
|
| 78 |
+
| Bark | 0.5x | High | Very High | 1000 |
|
| 79 |
+
|
| 80 |
+
## π― Perfect For
|
| 81 |
+
|
| 82 |
+
- β
Long-form educational content
|
| 83 |
+
- β
Audiobook narration
|
| 84 |
+
- β
Real-time reader mode in apps
|
| 85 |
+
- β
Storytelling with emotion
|
| 86 |
+
- β
Multi-voice projects
|
| 87 |
+
|
| 88 |
+
## π§ Integration Example (Flutter)
|
| 89 |
+
|
| 90 |
+
Update your `AIService.dart`:
|
| 91 |
+
```dart
|
| 92 |
+
final response = await http.post(
|
| 93 |
+
Uri.parse('https://your-space.hf.space/api/generate'),
|
| 94 |
+
headers: {'Content-Type': 'multipart/form-data'},
|
| 95 |
+
body: {
|
| 96 |
+
'username': 'your_username',
|
| 97 |
+
'password': 'your_password',
|
| 98 |
+
'text': text,
|
| 99 |
+
'voice': 'bf_isabella', // Storytelling voice
|
| 100 |
+
'speed': '1.0'
|
| 101 |
+
}
|
| 102 |
+
);
|
| 103 |
+
```
|
| 104 |
+
|
| 105 |
+
## π Quota System
|
| 106 |
+
|
| 107 |
+
| Role | Daily Limit | Speed Limit |
|
| 108 |
+
|------|-------------|-------------|
|
| 109 |
+
| User | 50 generations | Normal |
|
| 110 |
+
| Premium | Custom | Priority |
|
| 111 |
+
| Admin | Unlimited | Highest |
|
| 112 |
+
|
| 113 |
+
## π Deploy to Hugging Face
|
| 114 |
+
|
| 115 |
+
1. Push to GitHub
|
| 116 |
+
2. Create new Space on Hugging Face
|
| 117 |
+
3. Set secrets: `SUPABASE_URL`, `SUPABASE_KEY`
|
| 118 |
+
4. Deploy!
|
| 119 |
+
|
| 120 |
+
---
|
| 121 |
+
|
| 122 |
+
**Built for [Studify](https://github.com/your-repo)** - Making education accessible through AI
|
app.py
CHANGED
|
@@ -1,97 +1,320 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException,
|
| 2 |
-
from fastapi.responses import FileResponse
|
| 3 |
import gradio as gr
|
| 4 |
-
from
|
| 5 |
import tempfile
|
| 6 |
import os
|
| 7 |
-
import
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2")
|
| 15 |
-
print("β
TTS model loaded!")
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
raise HTTPException(status_code=401, detail="Invalid API key")
|
| 24 |
-
return True
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
def health():
|
| 28 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
@app.post("/generate")
|
| 31 |
async def generate_tts(
|
|
|
|
|
|
|
|
|
|
| 32 |
text: str = Form(...),
|
| 33 |
-
|
| 34 |
-
|
| 35 |
):
|
| 36 |
-
"""
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
try:
|
| 40 |
-
|
| 41 |
-
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as tmp:
|
| 42 |
-
tts.tts_to_file(
|
| 43 |
-
text=text,
|
| 44 |
-
language=language,
|
| 45 |
-
file_path=tmp.name
|
| 46 |
-
)
|
| 47 |
-
temp_path = tmp.name
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
except Exception as e:
|
| 56 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 57 |
|
| 58 |
-
|
| 59 |
-
def
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
try:
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
-
|
| 67 |
|
| 68 |
gradio_app = gr.Interface(
|
| 69 |
fn=generate_tts_gradio,
|
| 70 |
inputs=[
|
| 71 |
-
gr.Textbox(label="
|
| 72 |
-
gr.
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
],
|
| 75 |
-
outputs=gr.Audio(label="Generated Speech"),
|
| 76 |
-
title="
|
| 77 |
-
description="
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
### π API Key
|
| 90 |
-
Set your API key in Space settings as `API_KEY` environment variable.
|
| 91 |
-
"""
|
| 92 |
)
|
| 93 |
|
| 94 |
-
# Mount Gradio to FastAPI
|
| 95 |
app = gr.mount_gradio_app(app, gradio_app, path="/")
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Form, BackgroundTasks
|
| 2 |
+
from fastapi.responses import FileResponse
|
| 3 |
import gradio as gr
|
| 4 |
+
from kokoro_onnx import Kokoro
|
| 5 |
import tempfile
|
| 6 |
import os
|
| 7 |
+
import bcrypt
|
| 8 |
+
from datetime import datetime, timedelta
|
| 9 |
+
from supabase import create_client, Client
|
| 10 |
+
import soundfile as sf
|
| 11 |
|
| 12 |
+
# ============== CONFIG ==============
|
| 13 |
+
SUPABASE_URL = os.getenv("SUPABASE_URL")
|
| 14 |
+
SUPABASE_KEY = os.getenv("SUPABASE_KEY")
|
| 15 |
|
| 16 |
+
if not SUPABASE_URL or not SUPABASE_KEY:
|
| 17 |
+
raise ValueError("SUPABASE_URL and SUPABASE_KEY environment variables must be set")
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
DAILY_QUOTA = 50 # Increased since Kokoro is much faster
|
| 20 |
+
MAX_CHARS = 4500 # ~5 minutes of audio (speaking rate: ~900 chars/min)
|
| 21 |
+
MIN_CHARS = 5
|
| 22 |
+
MAX_AUDIO_DURATION = 300 # 5 minutes of audio
|
| 23 |
|
| 24 |
+
# Admin credentials
|
| 25 |
+
ADMIN_USERNAME = "madhab"
|
| 26 |
+
ADMIN_PASSWORD = "Madhab@Studify2024!"
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
# ============== SUPABASE ==============
|
| 29 |
+
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 30 |
+
|
| 31 |
+
def init_admin():
|
| 32 |
+
"""Create admin user if not exists"""
|
| 33 |
+
try:
|
| 34 |
+
result = supabase.table("tts_users").select("username").eq("username", ADMIN_USERNAME).execute()
|
| 35 |
+
|
| 36 |
+
if not result.data:
|
| 37 |
+
password_hash = bcrypt.hashpw(ADMIN_PASSWORD.encode(), bcrypt.gensalt()).decode()
|
| 38 |
+
supabase.table("tts_users").insert({
|
| 39 |
+
"username": ADMIN_USERNAME,
|
| 40 |
+
"password_hash": password_hash,
|
| 41 |
+
"role": "admin",
|
| 42 |
+
"daily_limit": -1,
|
| 43 |
+
"is_active": True
|
| 44 |
+
}).execute()
|
| 45 |
+
print(f"β
Admin user created: {ADMIN_USERNAME}")
|
| 46 |
+
else:
|
| 47 |
+
print(f"β
Admin user exists: {ADMIN_USERNAME}")
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"β οΈ Error creating admin: {e}")
|
| 50 |
+
|
| 51 |
+
# ============== KOKORO TTS MODEL ==============
|
| 52 |
+
print("π€ Loading Kokoro TTS model...")
|
| 53 |
+
try:
|
| 54 |
+
kokoro = Kokoro("kokoro-v0_19.onnx", "voices")
|
| 55 |
+
print("β
Kokoro TTS loaded successfully!")
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"β οΈ Kokoro not found locally. Will download on first use.")
|
| 58 |
+
kokoro = None
|
| 59 |
+
|
| 60 |
+
app = FastAPI(title="Kokoro TTS API - Professional & Fast")
|
| 61 |
+
|
| 62 |
+
@app.on_event("startup")
|
| 63 |
+
def startup():
|
| 64 |
+
global kokoro
|
| 65 |
+
if kokoro is None:
|
| 66 |
+
print("π₯ Downloading Kokoro TTS model...")
|
| 67 |
+
kokoro = Kokoro("kokoro-v0_19.onnx", "voices")
|
| 68 |
+
print("β
Kokoro TTS loaded!")
|
| 69 |
+
init_admin()
|
| 70 |
+
|
| 71 |
+
# ============== AUTH ==============
|
| 72 |
+
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
| 73 |
+
try:
|
| 74 |
+
return bcrypt.checkpw(plain_password.encode(), hashed_password.encode())
|
| 75 |
+
except:
|
| 76 |
+
return False
|
| 77 |
+
|
| 78 |
+
def authenticate_user(username: str, password: str) -> dict:
|
| 79 |
+
result = supabase.table("tts_users").select("*").eq("username", username).execute()
|
| 80 |
+
|
| 81 |
+
if not result.data or len(result.data) == 0:
|
| 82 |
+
raise HTTPException(status_code=401, detail="Access denied. User not found in database.")
|
| 83 |
+
|
| 84 |
+
user = result.data[0]
|
| 85 |
+
|
| 86 |
+
if not user.get('is_active', True):
|
| 87 |
+
raise HTTPException(status_code=403, detail="Account is disabled. Contact admin.")
|
| 88 |
+
|
| 89 |
+
if not verify_password(password, user['password_hash']):
|
| 90 |
+
raise HTTPException(status_code=401, detail="Invalid credentials.")
|
| 91 |
+
|
| 92 |
+
return user
|
| 93 |
+
|
| 94 |
+
def check_quota(username: str, daily_limit: int, role: str) -> dict:
|
| 95 |
+
if role == 'admin' or daily_limit == -1:
|
| 96 |
+
return {"used": 0, "remaining": -1, "is_unlimited": True}
|
| 97 |
+
|
| 98 |
+
since = (datetime.utcnow() - timedelta(hours=24)).isoformat()
|
| 99 |
+
result = supabase.table("tts_usage_logs").select("id", count="exact").eq("username", username).gte("created_at", since).execute()
|
| 100 |
+
|
| 101 |
+
used = result.count or 0
|
| 102 |
+
remaining = daily_limit - used
|
| 103 |
+
|
| 104 |
+
if remaining <= 0:
|
| 105 |
+
raise HTTPException(status_code=429, detail=f"Daily quota exceeded. Used {used}/{daily_limit}. Resets in 24h.")
|
| 106 |
+
|
| 107 |
+
return {"used": used, "remaining": remaining, "is_unlimited": False}
|
| 108 |
+
|
| 109 |
+
def log_usage(username: str, text_length: int, language: str):
|
| 110 |
+
supabase.table("tts_usage_logs").insert({
|
| 111 |
+
"username": username,
|
| 112 |
+
"text_length": text_length,
|
| 113 |
+
"language": language,
|
| 114 |
+
"created_at": datetime.utcnow().isoformat()
|
| 115 |
+
}).execute()
|
| 116 |
+
|
| 117 |
+
# ============== HELPERS ==============
|
| 118 |
+
def cleanup_file(path: str):
|
| 119 |
+
try:
|
| 120 |
+
if os.path.exists(path):
|
| 121 |
+
os.unlink(path)
|
| 122 |
+
except:
|
| 123 |
+
pass
|
| 124 |
+
|
| 125 |
+
def generate_speech(text: str, voice: str = "af_heart", speed: float = 1.0) -> str:
|
| 126 |
+
"""
|
| 127 |
+
Generate speech using Kokoro TTS
|
| 128 |
+
Available voices: af (American Female), am (American Male), bf (British Female), etc.
|
| 129 |
+
"""
|
| 130 |
+
if len(text) < MIN_CHARS:
|
| 131 |
+
raise ValueError(f"Text too short. Minimum {MIN_CHARS} characters.")
|
| 132 |
+
if len(text) > MAX_CHARS:
|
| 133 |
+
raise ValueError(f"Text too long. Maximum {MAX_CHARS} characters.")
|
| 134 |
+
|
| 135 |
+
# Generate audio samples
|
| 136 |
+
samples, sample_rate = kokoro.create(
|
| 137 |
+
text=text,
|
| 138 |
+
voice=voice,
|
| 139 |
+
speed=speed,
|
| 140 |
+
lang="en-us" # Kokoro supports: en-us, en-gb, ja, etc.
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
# Save to temporary file
|
| 144 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
| 145 |
+
sf.write(tmp.name, samples, sample_rate)
|
| 146 |
+
return tmp.name
|
| 147 |
+
|
| 148 |
+
# ============== API ENDPOINTS ==============
|
| 149 |
+
@app.get("/health")
|
| 150 |
def health():
|
| 151 |
+
return {
|
| 152 |
+
"status": "healthy",
|
| 153 |
+
"model": "Kokoro TTS 82M",
|
| 154 |
+
"speed": "10x faster than XTTS",
|
| 155 |
+
"authentication": "required",
|
| 156 |
+
"default_quota": DAILY_QUOTA
|
| 157 |
+
}
|
| 158 |
|
| 159 |
+
@app.post("/api/generate")
|
| 160 |
async def generate_tts(
|
| 161 |
+
background_tasks: BackgroundTasks,
|
| 162 |
+
username: str = Form(...),
|
| 163 |
+
password: str = Form(...),
|
| 164 |
text: str = Form(...),
|
| 165 |
+
voice: str = Form("af_heart"), # American Female - Heart
|
| 166 |
+
speed: float = Form(1.0)
|
| 167 |
):
|
| 168 |
+
"""
|
| 169 |
+
Generate TTS with Kokoro (Fast & Emotional)
|
| 170 |
+
|
| 171 |
+
Performance:
|
| 172 |
+
- Max audio length: 5 minutes
|
| 173 |
+
- Speaking rate: ~900 chars/minute
|
| 174 |
+
- Max chars: 4500 (~5 min audio)
|
| 175 |
+
- Generation time: ~20-30 seconds on CPU
|
| 176 |
+
|
| 177 |
+
Available voices:
|
| 178 |
+
- af_heart: American Female (warm)
|
| 179 |
+
- af_bella: American Female (professional)
|
| 180 |
+
- am_adam: American Male (confident)
|
| 181 |
+
- am_michael: American Male (friendly)
|
| 182 |
+
- bf_emma: British Female (elegant)
|
| 183 |
+
- bf_isabella: British Female (storytelling) β
|
| 184 |
+
|
| 185 |
+
Usage:
|
| 186 |
+
curl -X POST https://your-service.hf.space/api/generate \
|
| 187 |
+
-F "username=madhab" \
|
| 188 |
+
-F "password=Madhab@Studify2024!" \
|
| 189 |
+
-F "text=Hello world. This is much faster!" \
|
| 190 |
+
-F "voice=bf_isabella" \
|
| 191 |
+
-F "speed=1.0" \
|
| 192 |
+
--output output.wav
|
| 193 |
+
"""
|
| 194 |
+
user = authenticate_user(username, password)
|
| 195 |
+
quota = check_quota(user['username'], user['daily_limit'], user['role'])
|
| 196 |
|
| 197 |
try:
|
| 198 |
+
output_path = generate_speech(text.strip(), voice, speed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
+
if not quota['is_unlimited']:
|
| 201 |
+
log_usage(user['username'], len(text), "en")
|
| 202 |
+
|
| 203 |
+
background_tasks.add_task(cleanup_file, output_path)
|
| 204 |
+
|
| 205 |
+
response = FileResponse(output_path, media_type="audio/wav", filename="kokoro_tts.wav")
|
| 206 |
+
response.headers["X-Quota-Used"] = str(quota["used"] + (0 if quota["is_unlimited"] else 1))
|
| 207 |
+
response.headers["X-Quota-Remaining"] = "unlimited" if quota["is_unlimited"] else str(quota["remaining"] - 1)
|
| 208 |
+
response.headers["X-Model"] = "Kokoro-82M"
|
| 209 |
+
return response
|
| 210 |
+
|
| 211 |
+
except ValueError as e:
|
| 212 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 213 |
except Exception as e:
|
| 214 |
+
raise HTTPException(status_code=500, detail=f"TTS generation failed: {str(e)}")
|
| 215 |
|
| 216 |
+
@app.post("/api/quota")
|
| 217 |
+
async def check_user_quota(username: str = Form(...), password: str = Form(...)):
|
| 218 |
+
user = authenticate_user(username, password)
|
| 219 |
+
quota = check_quota(user['username'], user['daily_limit'], user['role'])
|
| 220 |
+
|
| 221 |
+
return {
|
| 222 |
+
"username": user['username'],
|
| 223 |
+
"role": user['role'],
|
| 224 |
+
"used_today": quota["used"],
|
| 225 |
+
"remaining": "unlimited" if quota["is_unlimited"] else quota["remaining"],
|
| 226 |
+
"daily_limit": "unlimited" if quota["is_unlimited"] else user['daily_limit']
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
@app.post("/api/admin/create-user")
|
| 230 |
+
async def create_user(
|
| 231 |
+
admin_username: str = Form(...),
|
| 232 |
+
admin_password: str = Form(...),
|
| 233 |
+
new_username: str = Form(...),
|
| 234 |
+
new_password: str = Form(...),
|
| 235 |
+
role: str = Form("user"),
|
| 236 |
+
daily_limit: int = Form(50)
|
| 237 |
+
):
|
| 238 |
+
admin = authenticate_user(admin_username, admin_password)
|
| 239 |
+
if admin['role'] != 'admin':
|
| 240 |
+
raise HTTPException(status_code=403, detail="Admin access required")
|
| 241 |
+
|
| 242 |
+
existing = supabase.table("tts_users").select("username").eq("username", new_username).execute()
|
| 243 |
+
if existing.data:
|
| 244 |
+
raise HTTPException(status_code=400, detail="Username already exists")
|
| 245 |
+
|
| 246 |
+
password_hash = bcrypt.hashpw(new_password.encode(), bcrypt.gensalt()).decode()
|
| 247 |
+
|
| 248 |
+
supabase.table("tts_users").insert({
|
| 249 |
+
"username": new_username,
|
| 250 |
+
"password_hash": password_hash,
|
| 251 |
+
"role": role,
|
| 252 |
+
"daily_limit": daily_limit,
|
| 253 |
+
"is_active": True
|
| 254 |
+
}).execute()
|
| 255 |
+
|
| 256 |
+
return {"success": True, "username": new_username, "role": role, "daily_limit": daily_limit}
|
| 257 |
+
|
| 258 |
+
@app.post("/api/admin/list-users")
|
| 259 |
+
async def list_users(admin_username: str = Form(...), admin_password: str = Form(...)):
|
| 260 |
+
admin = authenticate_user(admin_username, admin_password)
|
| 261 |
+
if admin['role'] != 'admin':
|
| 262 |
+
raise HTTPException(status_code=403, detail="Admin access required")
|
| 263 |
+
|
| 264 |
+
result = supabase.table("tts_users").select("username, role, daily_limit, is_active, created_at").execute()
|
| 265 |
+
return {"users": result.data}
|
| 266 |
+
|
| 267 |
+
# ============== GRADIO UI ==============
|
| 268 |
+
def generate_tts_gradio(username, password, text, voice="af_heart", speed=1.0):
|
| 269 |
+
if not username or not password:
|
| 270 |
+
raise gr.Error("Username and password required")
|
| 271 |
+
if not text or len(text.strip()) < MIN_CHARS:
|
| 272 |
+
raise gr.Error(f"Text must be at least {MIN_CHARS} characters")
|
| 273 |
+
|
| 274 |
try:
|
| 275 |
+
user = authenticate_user(username, password)
|
| 276 |
+
quota = check_quota(user['username'], user['daily_limit'], user['role'])
|
| 277 |
+
|
| 278 |
+
output_path = generate_speech(text.strip(), voice, speed)
|
| 279 |
+
|
| 280 |
+
if not quota['is_unlimited']:
|
| 281 |
+
log_usage(user['username'], len(text), "en")
|
| 282 |
+
|
| 283 |
+
return output_path
|
| 284 |
+
except HTTPException as e:
|
| 285 |
+
raise gr.Error(e.detail)
|
| 286 |
except Exception as e:
|
| 287 |
+
raise gr.Error(str(e))
|
| 288 |
|
| 289 |
gradio_app = gr.Interface(
|
| 290 |
fn=generate_tts_gradio,
|
| 291 |
inputs=[
|
| 292 |
+
gr.Textbox(label="Username", placeholder="Enter your username"),
|
| 293 |
+
gr.Textbox(label="Password", type="password", placeholder="Enter your password"),
|
| 294 |
+
gr.Textbox(label="Text", placeholder=f"Enter text ({MIN_CHARS}-{MAX_CHARS} chars)", lines=8),
|
| 295 |
+
gr.Dropdown(
|
| 296 |
+
choices=["af_heart", "af_bella", "am_adam", "am_michael", "bf_emma", "bf_isabella"],
|
| 297 |
+
value="af_heart",
|
| 298 |
+
label="Voice (Emotional & Expressive)"
|
| 299 |
+
),
|
| 300 |
+
gr.Slider(0.5, 2.0, value=1.0, step=0.1, label="Speed")
|
| 301 |
],
|
| 302 |
+
outputs=gr.Audio(label="Generated Speech (Kokoro TTS)", type="filepath"),
|
| 303 |
+
title="π Kokoro TTS - Professional & Lightning Fast",
|
| 304 |
+
description=f"""
|
| 305 |
+
**High-Speed Text-to-Speech with Emotional Expression**
|
| 306 |
+
|
| 307 |
+
- β‘ Lightning fast generation (~20-30 sec)
|
| 308 |
+
- π Emotional & expressive voices
|
| 309 |
+
- π Secure authentication required
|
| 310 |
+
- π Quota: {DAILY_QUOTA} generations/day
|
| 311 |
+
- π΅ Max audio: 5 minutes (4500 chars)
|
| 312 |
+
- πΎ Runs smoothly on CPU
|
| 313 |
+
|
| 314 |
+
Perfect for audiobooks, educational content, and storytelling!
|
| 315 |
+
""",
|
|
|
|
|
|
|
|
|
|
| 316 |
)
|
| 317 |
|
|
|
|
| 318 |
app = gr.mount_gradio_app(app, gradio_app, path="/")
|
| 319 |
|
| 320 |
if __name__ == "__main__":
|
packages.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
libsndfile1
|
requirements.txt
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
-
TTS>=0.22.0
|
| 2 |
fastapi
|
| 3 |
-
uvicorn
|
|
|
|
|
|
|
|
|
|
| 4 |
python-multipart
|
|
|
|
|
|
|
| 5 |
numpy
|
| 6 |
-
scipy
|
| 7 |
-
pydub
|
|
|
|
|
|
|
| 1 |
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
gradio
|
| 4 |
+
kokoro-onnx
|
| 5 |
+
soundfile
|
| 6 |
python-multipart
|
| 7 |
+
bcrypt
|
| 8 |
+
supabase
|
| 9 |
numpy
|
|
|
|
|
|
supabase_schema.sql
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-- TTS Service Supabase Schema
|
| 2 |
+
|
| 3 |
+
-- Users table
|
| 4 |
+
CREATE TABLE IF NOT EXISTS tts_users (
|
| 5 |
+
id BIGSERIAL PRIMARY KEY,
|
| 6 |
+
username VARCHAR(50) UNIQUE NOT NULL,
|
| 7 |
+
password_hash VARCHAR(255) NOT NULL,
|
| 8 |
+
role VARCHAR(20) DEFAULT 'user' CHECK (role IN ('user', 'admin', 'premium')),
|
| 9 |
+
daily_limit INTEGER DEFAULT 3, -- -1 for unlimited
|
| 10 |
+
is_active BOOLEAN DEFAULT TRUE,
|
| 11 |
+
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
| 12 |
+
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
| 13 |
+
);
|
| 14 |
+
|
| 15 |
+
-- Usage logs
|
| 16 |
+
CREATE TABLE IF NOT EXISTS tts_usage_logs (
|
| 17 |
+
id BIGSERIAL PRIMARY KEY,
|
| 18 |
+
username VARCHAR(50) NOT NULL,
|
| 19 |
+
text_length INTEGER NOT NULL,
|
| 20 |
+
language VARCHAR(10) DEFAULT 'en',
|
| 21 |
+
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
| 22 |
+
FOREIGN KEY (username) REFERENCES tts_users(username) ON DELETE CASCADE
|
| 23 |
+
);
|
| 24 |
+
|
| 25 |
+
-- Create indexes
|
| 26 |
+
CREATE INDEX IF NOT EXISTS idx_tts_users_username ON tts_users(username);
|
| 27 |
+
CREATE INDEX IF NOT EXISTS idx_tts_usage_logs_username ON tts_usage_logs(username);
|
| 28 |
+
CREATE INDEX IF NOT EXISTS idx_tts_usage_logs_created_at ON tts_usage_logs(created_at);
|
| 29 |
+
|
| 30 |
+
-- Enable Row Level Security
|
| 31 |
+
ALTER TABLE tts_users ENABLE ROW LEVEL SECURITY;
|
| 32 |
+
ALTER TABLE tts_usage_logs ENABLE ROW LEVEL SECURITY;
|
| 33 |
+
|
| 34 |
+
-- Allow service role to do everything
|
| 35 |
+
CREATE POLICY "Service role can do everything on tts_users"
|
| 36 |
+
ON tts_users
|
| 37 |
+
FOR ALL
|
| 38 |
+
TO service_role
|
| 39 |
+
USING (true);
|
| 40 |
+
|
| 41 |
+
CREATE POLICY "Service role can do everything on tts_usage_logs"
|
| 42 |
+
ON tts_usage_logs
|
| 43 |
+
FOR ALL
|
| 44 |
+
TO service_role
|
| 45 |
+
USING (true);
|
| 46 |
+
|
| 47 |
+
-- Admin user will be created via Python code with bcrypt hash
|