Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions api.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
12 changes: 12 additions & 0 deletions examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions gte.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion phi_3_vision_mlx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down