Spaces:
Configuration error
Configuration error
Create myanmar_tts.py
Browse files- myanmar_tts.py +95 -0
myanmar_tts.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
This is a simplified wrapper for myanmar-tts to handle import issues.
|
| 3 |
+
It's intended to make the HuggingFace Space deployment easier.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
import sys
|
| 8 |
+
import importlib.util
|
| 9 |
+
|
| 10 |
+
# Add the repository directory to Python path if needed
|
| 11 |
+
REPO_DIR = "myanmar-tts"
|
| 12 |
+
if os.path.exists(REPO_DIR) and REPO_DIR not in sys.path:
|
| 13 |
+
sys.path.append(os.path.abspath(REPO_DIR))
|
| 14 |
+
|
| 15 |
+
# Try to import directly, or from the repository
|
| 16 |
+
try:
|
| 17 |
+
# First attempt: direct imports
|
| 18 |
+
from text import text_to_sequence
|
| 19 |
+
from utils.hparams import create_hparams
|
| 20 |
+
from train import load_model
|
| 21 |
+
from synthesis import generate_speech
|
| 22 |
+
except ImportError:
|
| 23 |
+
try:
|
| 24 |
+
# Second attempt: repository imports
|
| 25 |
+
from myanmar_tts.text import text_to_sequence
|
| 26 |
+
from myanmar_tts.utils.hparams import create_hparams
|
| 27 |
+
from myanmar_tts.train import load_model
|
| 28 |
+
from myanmar_tts.synthesis import generate_speech
|
| 29 |
+
except ImportError:
|
| 30 |
+
# If still failing, try to load modules dynamically
|
| 31 |
+
def load_module(module_name, file_path):
|
| 32 |
+
if not os.path.exists(file_path):
|
| 33 |
+
raise ImportError(f"Module file not found: {file_path}")
|
| 34 |
+
|
| 35 |
+
spec = importlib.util.spec_from_file_location(module_name, file_path)
|
| 36 |
+
module = importlib.util.module_from_spec(spec)
|
| 37 |
+
spec.loader.exec_module(module)
|
| 38 |
+
return module
|
| 39 |
+
|
| 40 |
+
# Try to load critical modules
|
| 41 |
+
try:
|
| 42 |
+
text_module = load_module("text", os.path.join(REPO_DIR, "text", "__init__.py"))
|
| 43 |
+
text_to_sequence = text_module.text_to_sequence
|
| 44 |
+
|
| 45 |
+
hparams_module = load_module("hparams", os.path.join(REPO_DIR, "utils", "hparams.py"))
|
| 46 |
+
create_hparams = hparams_module.create_hparams
|
| 47 |
+
|
| 48 |
+
train_module = load_module("train", os.path.join(REPO_DIR, "train.py"))
|
| 49 |
+
load_model = train_module.load_model
|
| 50 |
+
|
| 51 |
+
synthesis_module = load_module("synthesis", os.path.join(REPO_DIR, "synthesis.py"))
|
| 52 |
+
generate_speech = synthesis_module.generate_speech
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"Failed to import myanmar-tts modules: {str(e)}")
|
| 55 |
+
raise
|
| 56 |
+
|
| 57 |
+
# Define a simple synthesis function
|
| 58 |
+
def synthesize(text, model_dir="trained_model"):
|
| 59 |
+
"""
|
| 60 |
+
Synthesize speech from the given text using the Myanmar TTS model.
|
| 61 |
+
|
| 62 |
+
Args:
|
| 63 |
+
text (str): The Burmese text to synthesize
|
| 64 |
+
model_dir (str): Directory containing the model files
|
| 65 |
+
|
| 66 |
+
Returns:
|
| 67 |
+
tuple: (waveform, sample_rate)
|
| 68 |
+
"""
|
| 69 |
+
import torch
|
| 70 |
+
import numpy as np
|
| 71 |
+
|
| 72 |
+
checkpoint_path = os.path.join(model_dir, "checkpoint_latest.pth.tar")
|
| 73 |
+
config_path = os.path.join(model_dir, "hparams.yml")
|
| 74 |
+
|
| 75 |
+
if not os.path.exists(checkpoint_path) or not os.path.exists(config_path):
|
| 76 |
+
raise FileNotFoundError(f"Model files not found in {model_dir}")
|
| 77 |
+
|
| 78 |
+
# Load the model
|
| 79 |
+
hparams = create_hparams(config_path)
|
| 80 |
+
model = load_model(hparams)
|
| 81 |
+
model.load_state_dict(torch.load(checkpoint_path, map_location=torch.device('cpu'))['state_dict'])
|
| 82 |
+
model.eval()
|
| 83 |
+
|
| 84 |
+
# Process text
|
| 85 |
+
sequence = np.array(text_to_sequence(text, ['burmese_cleaners']))[None, :]
|
| 86 |
+
sequence = torch.autograd.Variable(torch.from_numpy(sequence)).cpu().long()
|
| 87 |
+
|
| 88 |
+
# Generate mel spectrograms
|
| 89 |
+
mel_outputs, mel_outputs_postnet, _, alignments = model.inference(sequence)
|
| 90 |
+
|
| 91 |
+
# Generate waveform
|
| 92 |
+
with torch.no_grad():
|
| 93 |
+
waveform = generate_speech(mel_outputs_postnet, hparams)
|
| 94 |
+
|
| 95 |
+
return waveform, hparams.sampling_rate
|