{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "3bedf0dc-8d8e-4ede-a9e6-b8f35136aa00", "metadata": {}, "outputs": [], "source": [ "#|default_exp app" ] }, { "cell_type": "code", "execution_count": null, "id": "667802a7-0f36-4136-a381-e66210b20462", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "OPENAI_API_KEY var not found. Trying import tts_openai_secrets\n", "import tts_openai_secrets succeeded\n" ] } ], "source": [ "#| export\n", "#tts_openai_secrets.py content:\n", "#import os\n", "#os.environ['OPENAI_API_KEY'] = 'sk-XXXXXXXXXXXXXXXXXXXXXX'\n", "try:\n", " import os\n", " _ = os.environ['OPENAI_API_KEY']\n", " print('OPENAI_API_KEY var was found.')\n", "except:\n", " print('OPENAI_API_KEY var not found. Trying import tts_openai_secrets')\n", " try:\n", " import tts_openai_secrets\n", " print('import tts_openai_secrets succeeded')\n", " except:\n", " print('secret import from tts_openai_secrets.py failed - env var must be previously set')" ] }, { "cell_type": "code", "execution_count": null, "id": "4d9863fc-969e-409b-8e20-b9c3cd2cc3e7", "metadata": {}, "outputs": [], "source": [ "#| hide\n", "try:\n", " import nbdev\n", "except:\n", " print('to convert this notebook to app.py you need to pip install nbdev')" ] }, { "cell_type": "code", "execution_count": null, "id": "4f486d3a", "metadata": {}, "outputs": [], "source": [ "#| export\n", "import gradio as gr\n", "import openai" ] }, { "cell_type": "code", "execution_count": null, "id": "0ffd33b4-cb9b-4c01-bff6-4c3102854ab6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "successfully got tts model list: ['tts-1-hd', 'tts-1-hd-1106', 'canary-tts', 'tts-1', 'tts-1-1106']\n" ] } ], "source": [ "#| export\n", "try:\n", " tts_models = [o.id for o in openai.models.list().data if 'tts' in o.id]\n", " print('successfully got tts model list:', tts_models)\n", "except:\n", " tts_models = ['tts-1']" ] }, { "cell_type": "code", "execution_count": null, "id": "2ddbca5d-4b04-43ab-afaf-430802980e78", "metadata": {}, "outputs": [], "source": [ "#| export\n", "tts_voices = ['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer']" ] }, { "cell_type": "code", "execution_count": null, "id": "5388e860", "metadata": {}, "outputs": [], "source": [ "#| export\n", "def create_speech(input_text, model='tts-1', voice='alloy'):\n", " client = openai.OpenAI()\n", " response = client.audio.speech.create(\n", " model=model,\n", " voice=voice,\n", " input=input_text,\n", " speed=1.0\n", " )\n", " client.close()\n", " return response.content" ] }, { "cell_type": "code", "execution_count": null, "id": "236dd8d3-4364-4731-af93-7dcdec6f18a1", "metadata": {}, "outputs": [], "source": [ "#| export\n", "def get_input_text_len(input_text):\n", " return len(input_text)" ] }, { "cell_type": "code", "execution_count": null, "id": "e4fb3159-579b-4271-bc96-4cd1e2816eca", "metadata": {}, "outputs": [], "source": [ "#| export\n", "with gr.Blocks(title='OpenAI TTS', head='OpenAI TTS') as app:\n", " gr.Markdown(\"# OpenAI TTS\")\n", " gr.Markdown(\"Start typing below and then click **Go** to create the speech from your text. The current limit is 4,000 characters.\")\n", " with gr.Row():\n", " input_text = gr.Textbox(max_lines=100, label=\"Enter text here\")\n", " with gr.Row():\n", " tts_model_dropdown = gr.Dropdown(value='tts-1',choices=tts_models, label='Model')\n", " tts_voice_dropdown = gr.Dropdown(value='alloy',choices=tts_voices,label='Voice')\n", " input_text_length = gr.Label(label=\"Number of characters\")\n", " output_audio = gr.Audio()\n", " input_text.input(fn=get_input_text_len, inputs=input_text, outputs=input_text_length)\n", " go_btn = gr.Button(\"Go\")\n", " go_btn.click(fn=create_speech, inputs=[input_text, tts_model_dropdown, tts_voice_dropdown], outputs=[output_audio])\n", " clear_btn = gr.Button('Clear')\n", " clear_btn.click(fn=lambda: '', outputs=input_text)\n", " " ] }, { "cell_type": "code", "execution_count": null, "id": "cb886d45", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://0.0.0.0:7861\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "