Skip to content

Instantly share code, notes, and snippets.

@umiyosh
Last active March 19, 2024 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umiyosh/c65a8adab76390cccb20b1ca41519d4b to your computer and use it in GitHub Desktop.
Save umiyosh/c65a8adab76390cccb20b1ca41519d4b to your computer and use it in GitHub Desktop.
Script for text to speech with text split by token size.
import logging
import os
from pathlib import Path
import sys
import openai
import pygame
from semantic_text_splitter import TextSplitter
# Usage :
# % open https://platform.openai.com/api-keys
# % export OPENAI_API_KEY="sk-xxxxxx"
# % pbpaste | python tss_by_token.py
openai.api_key = os.getenv("OPENAI_API_KEY")
SPEECH_FILE_PATH = Path(__file__).parent / "output.mp3"
chunk_size = 100
read_speed = 1
def text_to_speech(text: str):
"""Converting text to speech"""
client = openai.Client()
# Convert text to speech and get results
response = client.audio.speech.create(
model="tts-1",
voice="alloy",
speed=read_speed,
input=text,
)
response.stream_to_file(SPEECH_FILE_PATH)
def play_mp3(file_path: Path):
"""Play mp3 files"""
pygame.mixer.init()
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
def text_splitter(text: str, token_size: int) -> list:
"""Split text by the specified number of token size"""
splitter = TextSplitter.from_tiktoken_model("gpt-3.5-turbo")
chunks = splitter.chunks(text, token_size)
return chunks
if __name__ == "__main__":
text = sys.stdin.read()
logging.info(f"Input text: len={len(text)}")
chunks = text_splitter(text, chunk_size)
for chunk in chunks:
logging.info(f"chunk: len={len(chunk)}, chunklen={len(chunks)}")
text_to_speech(chunk)
play_mp3(SPEECH_FILE_PATH)
@umiyosh
Copy link
Author

umiyosh commented Mar 13, 2024

You need to install the following dependent libraries before using this script.

[tool.poetry.dependencies]
openai = "^1.13.3"
pygame = "^2.5.2"
semantic-text-splitter = "^0.7.0"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment