Spaces:
Sleeping
Sleeping
Commit ·
7f6a0d4
1
Parent(s): 7e130d1
Added updated files
Browse filesFiles changed (3)
- model_inference.pyAdded+66
- nettalk_model.ptAdded
- requirements.txtAdded+12
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566# model_inference.pyimport torchimport numpy as np
class NetTALKWrapper: def __init__(self, model_path="nettalk_model.pt", device=None): # pick device automatically if device is None: device = torch.device("cuda" if torch.cuda.is_available() else "cpu") self.device = device
# If you saved state_dict, load accordingly: try: self.model = torch.load(model_path, map_location=self.device) except Exception as e: # fallback: user may have saved state_dict print("torch.load failed; try loading state_dict. Error:", e) # Example placeholder architecture - REPLACE with your actual model class from torch import nn class DummyModel(nn.Module): def __init__(self): super().__init__() self.dummy = nn.Linear(10, 10) def forward(self, x): return torch.randn(1, 20) # placeholder m = DummyModel() sd = torch.load(model_path, map_location="cpu") try: m.load_state_dict(sd) self.model = m.to(self.device) except Exception: raise RuntimeError("Could not load model. Please update model_inference.py to use your architecture.")
self.model.eval()
# ---- Replace these helper methods with your real preprocess/decoder ---- def preprocess(self, word: str): """ Convert `word` (string) to input tensor expected by your NetTALK model. Example NetTALK uses character windowing / one-hot encoding — replace below. """ # PLACEHOLDER: map characters to indices, pad/truncate to length L, then to tensor # *Replace with your actual preprocessing code* max_len = 32 arr = np.zeros((1, max_len), dtype=np.int64) for i, c in enumerate(word.lower()[:max_len]): arr[0, i] = ord(c) # placeholder mapping return torch.from_numpy(arr).to(self.device).float()
def decode_to_arpabet(self, model_output): """ Convert model raw output to an ARPAbet string (e.g., "HH AH0 L OW1"). Replace this with your decoder logic (argmax, beam search, label mapping, etc). """ # PLACEHOLDER: just return dummy tokens return "AH0 N T EH1 R P AH0 B EH1 T"
def predict(self, word: str): # basic sanitization word = word.strip() if not word: return "" x = self.preprocess(word) with torch.no_grad(): y = self.model(x) return self.decode_to_arpabet(y)123456789101112torchgradio>=3.0numpyscipysoundfile# Optional TTS backends (pick one):# For a fast fallback TTS:gTTS# For a more advanced phoneme-aware TTS (may require GPU & larger install):TTS# Helpful: phonemizer if you want alternative phoneme utilitiesphonemizermodel_inference.pyAdded+66
nettalk_model.ptAdded
requirements.txtAdded+12