From 61faad97e3a101b5aeb03756a7222d226bb9e826 Mon Sep 17 00:00:00 2001 From: octo-patch <266937838+octo-patch@users.noreply.github.com> Date: Fri, 31 Jul 2026 04:19:05 +0000 Subject: [PATCH] Add MiniMax text-to-speech API tool for global and CN endpoints --- api.py | 66 +++++++++++++++++++++++++++++++++++++++++++++ examples.py | 12 +++++++++ gte.py | 21 +++++++++++++++ phi_3_vision_mlx.py | 2 +- 4 files changed, 100 insertions(+), 1 deletion(-) diff --git a/api.py b/api.py index 10fc39d..675c1ed 100644 --- a/api.py +++ b/api.py @@ -1,8 +1,74 @@ import os from pathlib import Path +import requests from huggingface_hub import InferenceClient +MINIMAX_TTS_DEFAULT_MODEL = "speech-2.8-hd" +MINIMAX_TTS_MODELS = ( + "speech-2.8-hd", + "speech-2.8-turbo", + "speech-2.6-hd", + "speech-2.6-turbo", + "speech-02-hd", + "speech-02-turbo", + "speech-01-hd", + "speech-01-turbo", +) +MINIMAX_TTS_AUDIO_FORMATS = ("mp3", "wav", "flac", "pcm") +MINIMAX_TTS_BASE_URLS = { + "global_en": "https://api.minimax.io/v1/t2a_v2", + "cn_zh": "https://api.minimaxi.com/v1/t2a_v2", +} + +def minimax_tts_api(prompt, model=MINIMAX_TTS_DEFAULT_MODEL, region="global_en", + voice_setting=None, output_format="mp3", verbose=True, + return_dict=True): + """ + Synthesize speech from text through the MiniMax text-to-audio endpoint. + + Both the global (api.minimax.io) and China (api.minimaxi.com) T2A endpoints + are supported via the ``region`` argument. The non-streaming response carries + the audio payload as a hex string in ``data.audio``; ``base_resp.status_code`` + is ``0`` on success. + + Example: + -------- + agent = Agent(toolchain = "responses = minimax_tts_api(prompt)") + agent('People say nothing is impossible, but I do nothing every day.') + """ + base_url = MINIMAX_TTS_BASE_URLS.get(region, MINIMAX_TTS_BASE_URLS["global_en"]) + extension = output_format if output_format in MINIMAX_TTS_AUDIO_FORMATS else "mp3" + body = dict( + model=model, + text=prompt, + stream=False, + output_format=extension, + ) + if voice_setting is not None: + body["voice_setting"] = voice_setting + response = requests.post( + f"{base_url}", + headers={ + "Authorization": f"Bearer {os.environ.get('MINIMAX_API_KEY', '')}", + "Content-Type": "application/json", + }, + json=body, + ) + response.raise_for_status() + payload = response.json() + status_code = payload.get("base_resp", {}).get("status_code", -1) + if status_code != 0: + raise RuntimeError(f"MiniMax T2A request failed: {payload}") + audio_hex = payload.get("data", {}).get("audio") + audio_bytes = bytes.fromhex(audio_hex) + Path(f"minimax_tts.{extension}").write_bytes(audio_bytes) + if verbose: + print(f'### Prompt ###\n{prompt}\n### Saved ###\nminimax_tts.{extension}') + if return_dict: + return {'responses': prompt} + return prompt + def mistral_api(prompt, history, verbose=True, return_dict=True, api_model="mistralai/Mistral-Nemo-Instruct-2407"): """ Example: diff --git a/examples.py b/examples.py index c362375..91bf67c 100644 --- a/examples.py +++ b/examples.py @@ -63,6 +63,18 @@ agent('Speak "People say nothing is impossible, but I do nothing every day."') agent.end() +# Text-to-Speech + +## MiniMax TTS (speech-2.8-hd is the default; other MiniMax TTS models are also available) +agent = pv.Agent(toolchain = "responses = minimax_tts_api(prompt)") +agent('People say nothing is impossible, but I do nothing every day.') +agent.end() + +### Selecting the China endpoint and a different MiniMax TTS model +agent = pv.Agent(toolchain = 'responses = minimax_tts_api(prompt, region="cn_zh", model="speech-2.8-turbo")') +agent('People say nothing is impossible, but I do nothing every day.') +agent.end() + # Toolchain ## LLM Backend Hotswap diff --git a/gte.py b/gte.py index 19560e7..163e4fc 100644 --- a/gte.py +++ b/gte.py @@ -173,6 +173,27 @@ def __call__(self, input_text: List[str]) -> mx.array: print('<|api_output|>'+result) ``` """, +"""Text to speech +```python +import os +import requests +endpoint = "https://api.minimax.io/v1/t2a_v2" +response = requests.post( + endpoint, + headers={ + "Authorization": "Bearer " + os.environ["MINIMAX_API_KEY"], + "Content-Type": "application/json", + }, + json={ + "model": "speech-2.8-hd", + "text": "{prompt}", + "stream": False, + "output_format": "mp3", + }, +) +print('<|api_output|>'+response.json()["data"]["audio"]) +``` +""", """Transcribe youtube video ```python from gradio_client import Client diff --git a/phi_3_vision_mlx.py b/phi_3_vision_mlx.py index a8c9af7..b38beaa 100644 --- a/phi_3_vision_mlx.py +++ b/phi_3_vision_mlx.py @@ -26,7 +26,7 @@ from mlx.utils import tree_flatten, tree_unflatten from PIL import Image -from api import bark_api, mistral_api +from api import bark_api, minimax_tts_api, mistral_api from gte import VDB, GteModel from phi import (LoRALinear, Phi3ForCausalLM, Phi3FProcessor, Phi3VForCausalLM, Phi3VProcessor, Tic, TrainingCallback)