Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Standalone TTS Program untuk Bahasa Indonesia | |
| Berdasarkan app.py - TTS Indonesiaku Gratis | |
| Program ini menghasilkan file audio dari teks Bahasa Indonesia | |
| dengan menggunakan model TTS yang sudah dilatih khusus untuk bahasa Indonesia. | |
| Parameter: | |
| - text: Teks yang akan diubah menjadi suara | |
| - file_path: Path file output audio (.wav) | |
| """ | |
| import subprocess | |
| import html | |
| from pathlib import Path | |
| try: | |
| from g2pid import G2P | |
| # Inisialisasi G2P (Grapheme to Phoneme) | |
| g2p = G2P() | |
| G2P_AVAILABLE = True | |
| except ImportError: | |
| print("WARNING: G2P module tidak tersedia. Install dependencies dengan: pip install -r requirements.txt") | |
| G2P_AVAILABLE = False | |
| # Parameter default untuk konfigurasi | |
| params = { | |
| "model_path": "checkpoint_1260000-inference.pth", | |
| "config_path": "config.json", | |
| # "speaker": "ardi" # Default speaker | |
| "speaker": "gadis" # Default speaker | |
| } | |
| # params = { | |
| # "model_path": "kobov2.pth", | |
| # "config_path": "config.json", | |
| # # "speaker": "kobov2.index" # Default speaker | |
| # "speaker": "gadis", | |
| # } | |
| def generate_tts(text, file_path, speaker="ardi"): | |
| """ | |
| Generate TTS audio dari teks Bahasa Indonesia | |
| Args: | |
| text (str): Teks yang akan diubah menjadi suara | |
| file_path (str): Path file output audio (.wav) | |
| speaker (str): Nama speaker (default: "ardi") | |
| Returns: | |
| bool: True jika berhasil, False jika gagal | |
| """ | |
| try: | |
| print(f"Memproses teks: {text}") | |
| # Konversi teks ke format TTS menggunakan G2P jika tersedia | |
| if G2P_AVAILABLE: | |
| print("Mengonversi teks ke fonem...") | |
| text_to_tts = g2p(text) | |
| print(f"Teks setelah konversi G2P: {text_to_tts}") | |
| else: | |
| print("WARNING: Menggunakan teks asli tanpa konversi G2P") | |
| text_to_tts = text | |
| # Pastikan direktori output ada | |
| output_path = Path(file_path) | |
| output_path.parent.mkdir(parents=True, exist_ok=True) | |
| # Perintah untuk menjalankan TTS | |
| command = [ | |
| "tts", | |
| "--text", text_to_tts, | |
| "--model_path", params["model_path"], | |
| "--config_path", params["config_path"], | |
| "--speaker_idx", speaker, | |
| "--out_path", str(output_path) | |
| ] | |
| print("Menjalankan proses TTS...") | |
| print(f"Command: {' '.join(command)}") | |
| # Jalankan proses TTS | |
| result = subprocess.run(command, capture_output=True, text=True) | |
| if result.returncode != 0: | |
| print(f"Error: {result.stderr}") | |
| return False | |
| print(f"SUCCESS: Audio berhasil dibuat: {file_path}") | |
| return True | |
| except Exception as e: | |
| print(f"ERROR: {str(e)}") | |
| return False | |
| def main(): | |
| """ | |
| Fungsi utama dengan parameter yang diminta | |
| """ | |
| # Parameter sesuai permintaan | |
| text = "Selamat pagi, nama saya Aryo." | |
| file_path = "output_id_coqui.wav" | |
| print("=" * 60) | |
| print("TTS Standalone - Bahasa Indonesia") | |
| print("=" * 60) | |
| print(f"Teks: {text}") | |
| print(f"Output file: {file_path}") | |
| print(f"Speaker: {params['speaker']}") | |
| print("=" * 60) | |
| # Generate TTS | |
| success = generate_tts(text, file_path, params["speaker"]) | |
| if success: | |
| print("\nSUCCESS: Proses TTS selesai!") | |
| print(f"File audio tersimpan di: {file_path}") | |
| else: | |
| print("\nERROR: Proses TTS gagal!") | |
| print("Pastikan:") | |
| print("1. Model checkpoint_1260000-inference.pth tersedia") | |
| print("2. File config.json tersedia") | |
| print("3. TTS library terinstall (pip install TTS)") | |
| print("4. Speaker tersedia dalam model") | |
| print("5. Dependencies terinstall: pip install -r requirements.txt") | |
| if __name__ == "__main__": | |
| main() | |