Last active
March 1, 2026 02:44
-
-
Save NoRaincheck/5d1b2c5df5582f10a77be60650f1f3fa to your computer and use it in GitHub Desktop.
maya1 GGUF example. To use this, first launch the maya1 gguf using llama-cpp or LM Studio. Then the example below should 'just work'.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # /// script | |
| # requires-python = ">=3.12" | |
| # dependencies = [ | |
| # "httpx>=0.28.1", | |
| # "snac>=1.2.1", | |
| # "torch>=2.10.0", | |
| # "transformers>=5.2.0", | |
| # ] | |
| # /// | |
| import httpx | |
| import torch | |
| from snac import SNAC | |
| from transformers import AutoTokenizer | |
| class Maya1Helpers: | |
| CODE_START_TOKEN_ID = 128257 | |
| CODE_END_TOKEN_ID = 128258 | |
| CODE_TOKEN_OFFSET = 128266 | |
| SNAC_MIN_ID = 128266 | |
| SNAC_MAX_ID = 156937 | |
| SNAC_TOKENS_PER_FRAME = 7 | |
| SNAC_OFFSET = 4096 | |
| SOH_ID = 128259 | |
| EOH_ID = 128260 | |
| SOA_ID = 128261 | |
| BOS_ID = 128000 | |
| TEXT_EOT_ID = 128009 | |
| def __init__(self): | |
| self.tokenizer = AutoTokenizer.from_pretrained( | |
| "maya-research/maya1", trust_remote_code=True | |
| ) | |
| self.snac_model = SNAC.from_pretrained("hubertsiuzdak/snac_24khz").eval() | |
| def build_prompt(self, text: str, description: str): | |
| """Build formatted prompt for Maya1.""" | |
| soh_token = self.tokenizer.decode([self.SOH_ID]) | |
| eoh_token = self.tokenizer.decode([self.EOH_ID]) | |
| soa_token = self.tokenizer.decode([self.SOA_ID]) | |
| sos_token = self.tokenizer.decode([self.CODE_START_TOKEN_ID]) | |
| eot_token = self.tokenizer.decode([self.TEXT_EOT_ID]) | |
| bos_token = self.tokenizer.bos_token | |
| formatted_text = f'<description="{description}"> {text}' | |
| prompt = ( | |
| soh_token + bos_token + formatted_text + eot_token + eoh_token + soa_token + sos_token | |
| ) | |
| return prompt | |
| def _extract_snac_codes(self, token_ids: list) -> list: | |
| """Extract SNAC codes from generated tokens.""" | |
| try: | |
| eos_idx = token_ids.index(self.CODE_END_TOKEN_ID) | |
| except ValueError: | |
| eos_idx = len(token_ids) | |
| snac_codes = [ | |
| token_id | |
| for token_id in token_ids[:eos_idx] | |
| if self.SNAC_MIN_ID <= token_id <= self.SNAC_MAX_ID | |
| ] | |
| return snac_codes | |
| def _unpack_snac(self, snac_tokens): | |
| # Decode SNAC tokens to audio frames | |
| frames = len(snac_tokens) // self.SNAC_TOKENS_PER_FRAME | |
| codes = [[], [], []] | |
| for i in range(frames): | |
| s = snac_tokens[i * self.SNAC_TOKENS_PER_FRAME : (i + 1) * self.SNAC_TOKENS_PER_FRAME] | |
| codes[0].append((s[0] - self.SNAC_MIN_ID) % self.SNAC_OFFSET) | |
| codes[1].extend( | |
| [ | |
| (s[1] - self.SNAC_MIN_ID) % self.SNAC_OFFSET, | |
| (s[4] - self.SNAC_MIN_ID) % self.SNAC_OFFSET, | |
| ] | |
| ) | |
| codes[2].extend( | |
| [ | |
| (s[2] - self.SNAC_MIN_ID) % self.SNAC_OFFSET, | |
| (s[3] - self.SNAC_MIN_ID) % self.SNAC_OFFSET, | |
| (s[5] - self.SNAC_MIN_ID) % self.SNAC_OFFSET, | |
| (s[6] - self.SNAC_MIN_ID) % self.SNAC_OFFSET, | |
| ] | |
| ) | |
| return codes | |
| def get_audio_from_prompt_output(self, output: str): | |
| levels = self._unpack_snac(self._extract_snac_codes(self.tokenizer.encode(output))) | |
| codes_tensor = [torch.tensor(level, dtype=torch.long).unsqueeze(0) for level in levels] | |
| with torch.inference_mode(): | |
| z_q = self.snac_model.quantizer.from_codes(codes_tensor) | |
| audio = self.snac_model.decoder(z_q)[0, 0].cpu().numpy() | |
| return audio | |
| API_URL = "http://127.0.0.1:1234/v1/completions" | |
| HEADERS = {"Content-Type": "application/json"} | |
| maya1 = Maya1Helpers() | |
| prompt = maya1.build_prompt(text, description) | |
| output = httpx.post( | |
| API_URL, | |
| headers=HEADERS, | |
| json={ | |
| "model": "maya1", | |
| "prompt": prompt, | |
| }, | |
| timeout=None, | |
| ) | |
| output = output.json()["choices"][0]["text"] | |
| audio = maya1.get_audio_from_prompt_output(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment