Last active
March 19, 2024 06:41
-
-
Save umiyosh/c65a8adab76390cccb20b1ca41519d4b to your computer and use it in GitHub Desktop.
Script for text to speech with text split by token size.
This file contains 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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to install the following dependent libraries before using this script.