API Reference

You can use neets.ai's Text-To-Speech (TTS) API for tasks that generate large amounts of audio, such as rendering an audiobook from raw text. In this example, we'll load text from a book, and run inference in parallel using the API to produce a result

Prerequisites:

  • Python installation: Ensure you have Python version 3.8 or newer installed on your computer to run the code effectively..
  • Book as a text file: Make sure to have your desired book as a text file. You can prepare your book as a text file by doing the following in shell:
wget -O alice.txt https://gutenberg.org/cache/epub/11/pg11.txt

Setting Up:

  1. Open your terminal: Launch your terminal and navigate to your desired directory where you want to set up the project.
  2. Install Python dependencies: You need to install two Python packages. Execute the following commands in your terminal:
pip install requests
pip install requests tqdm

Creating Your Script

  1. Code preparation: Open your desired code editor. Copy and paste the code provided from our API documentation below:
from concurrent.futures import ThreadPoolExecutor
import pathlib

import requests
from tqdm import tqdm

NEETS_API_KEY="your_api_key"

def chunk(parts: list[str], n: int):
  chunks = []

  chunk = ""
  for sentence in parts:
    chunk += sentence
    if len(chunk) > n:
      chunks.append(chunk + " ")
      chunk = ""

  if len(chunk) > 0:
    chunks.append(chunk)

  return chunks

def generate_audio(
  api_key: str,
  book_path: str,
  output_path: str,
  chunk_size: int = 500,
):
  output_p = pathlib.Path(output_path)
  output_p.mkdir(parents=True, exist_ok=True)

  book_text = open(book_path, "r")
  sentences = book_text.read().replace("\n", " ").replace("\r", "").split(".")
  trimmed_sentences = [sentence.strip() + "." for sentence in sentences]

  chunks = chunk(trimmed_sentences, chunk_size)

  def generate_chunk_audio(chunk: tuple[int, str]):
    index, text = chunk
    filename = f"{str(index).zfill(4)}.mp3"

    response = requests.request(
      method="POST",
      url="https://api.neets.ai/v1/tts",
      headers={
        "Content-Type": "application/json",
        "X-API-Key": api_key
      },
      json={
        "text": text,
        "voice_id": "us-female-2",
        "params": {
          "model": "style-diff-500"
        }
      }
    )

    # Check API status
    response.raise_for_status()

    (output_p / filename).write_bytes(response.content)

  with ThreadPoolExecutor(max_workers=5) as executor:
    list(
      tqdm(
        executor.map(generate_chunk_audio, enumerate(chunks)),
        desc="Generating audio",
        total=len(chunks)
      )
    )

generate_audio(
  api_key=NEETS_API_KEY,
  book_path="alice.txt",
  output_path="alice_audio",
  chunk_size=500
)

  1. Save your script: Ensure you save the file with a clear name. For example: audiobook.py

Running Your Script

  1. Execute the script: In your terminal, run the script using the command python audiobook.py
  2. Enter your API Key: When prompted, enter your NEETS_API_KEY. You can find this on your API Keys page here.

Now you're all set! Follow these steps for tasks requiring generation of large amounts of audio using neets.ai's easy-to-use APIs