| 160 | return cls._instance |
| 161 | |
| 162 | def initialize(self, model_path: str, target_sample_rate: int = 16000): |
| 163 | if self._initialized: |
| 164 | log("info", "initialized, skipping ...") |
| 165 | return |
| 166 | |
| 167 | self.device = "cuda" if torch.cuda.is_available() else "cpu" |
| 168 | self.target_sample_rate = target_sample_rate |
| 169 | |
| 170 | log("info", f"loading s2s model to {self.device}...") |
| 171 | |
| 172 | config = AutoConfig.from_pretrained(model_path) |
| 173 | text_config = getattr(config, "text_config", None) |
| 174 | if text_config and getattr(text_config, "model_type", None) in ["qwen3_moe", ]: |
| 175 | setattr(text_config, "output_router_logits", False) |
| 176 | |
| 177 | self.processor = AutoProcessor.from_pretrained(model_path) |
| 178 | self.model = AutoModelForSeq2SeqLM.from_pretrained( |
| 179 | model_path, |
| 180 | config=config, |
| 181 | torch_dtype=torch.bfloat16 |
| 182 | ).to(self.device) |
| 183 | |
| 184 | # set gen args |
| 185 | self.gen_kwargs = DEFAULT_S2M_GEN_KWARGS.copy() |
| 186 | if 'bad_words_ids' not in self.gen_kwargs or self.gen_kwargs['bad_words_ids'] is None: |
| 187 | self.gen_kwargs['bad_words_ids'] = [[self.processor.tokenizer.convert_tokens_to_ids('<|audio_bos|>'), |
| 188 | self.processor.tokenizer.convert_tokens_to_ids('<|sil|>')]] |
| 189 | |
| 190 | self.model.sp_gen_kwargs = DEFAULT_SP_GEN_KWARGS.copy() |
| 191 | |
| 192 | log("info", f"s2s model loaded (: {self.device})") |
| 193 | |
| 194 | self._initialized = True |
| 195 | log("info", f"waiting for tts model loading ... ") |
| 196 | |
| 197 | |
| 198 | class ServerState: |