Spaces:
Runtime error
Runtime error
yixuan1999
commited on
Commit
•
ad7485b
1
Parent(s):
97c6bb8
Add application file
Browse files- LICENSE +21 -0
- README.md +19 -14
- app.py +73 -0
- env.yaml +9 -0
- interface.png +0 -0
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2024 Yixuan Wang
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
README.md
CHANGED
@@ -1,14 +1,19 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# OpenAI TTS Gradio Interface
|
2 |
+
|
3 |
+
This is a simple Gradio Interface to use OpenAI Text-to-Speech API. The interface looks like this:
|
4 |
+
|
5 |
+
![Interface](interface.png)
|
6 |
+
|
7 |
+
## Usage
|
8 |
+
|
9 |
+
Get your OpenAI API key following the tutorial [here](https://platform.openai.com/docs/quickstart). I use [Miniforge](https://github.com/conda-forge/miniforge) to set up the environment, but you can use Anaconda or any other package manager.
|
10 |
+
|
11 |
+
```bash
|
12 |
+
mamba env create -f env.yaml # Create the environment
|
13 |
+
conda activate openai_tts # Activate the environment
|
14 |
+
python app.py # Run the app
|
15 |
+
```
|
16 |
+
|
17 |
+
## Acknowledgement
|
18 |
+
|
19 |
+
This repo is adapted from [https://github.com/leokwsw/OpenAI-TTS-Gradio](https://github.com/leokwsw/OpenAI-TTS-Gradio)
|
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
from typing import Union, Literal
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
import tempfile
|
7 |
+
|
8 |
+
from openai import OpenAI
|
9 |
+
|
10 |
+
server_name = os.getenv("SERVER_NAME", "127.0.0.1")
|
11 |
+
|
12 |
+
def tts(
|
13 |
+
text: str,
|
14 |
+
model: Union[str, Literal["tts-1", "tts-1-hd"]],
|
15 |
+
voice: Literal["alloy", "echo", "fable", "onyx", "nova", "shimmer"],
|
16 |
+
output_file_format: Literal["mp3", "opus", "aac", "flac"] = "mp3",
|
17 |
+
speed: float = 1.0,
|
18 |
+
openai_key: str = ""
|
19 |
+
):
|
20 |
+
if len(text) > 0:
|
21 |
+
try:
|
22 |
+
client = OpenAI(api_key=openai_key)
|
23 |
+
|
24 |
+
response = client.audio.speech.create(
|
25 |
+
model=model,
|
26 |
+
voice=voice,
|
27 |
+
input=text,
|
28 |
+
response_format=output_file_format,
|
29 |
+
speed=speed
|
30 |
+
)
|
31 |
+
|
32 |
+
except Exception as error:
|
33 |
+
print(str(error))
|
34 |
+
raise gr.Error(
|
35 |
+
"An error occurred while generating speech. Please check your API key and come back try again.")
|
36 |
+
|
37 |
+
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
|
38 |
+
temp_file.write(response.content)
|
39 |
+
|
40 |
+
temp_file_path = temp_file.name
|
41 |
+
|
42 |
+
return temp_file_path
|
43 |
+
else:
|
44 |
+
return "1-second-of-silence.mp3"
|
45 |
+
|
46 |
+
|
47 |
+
with gr.Blocks() as demo:
|
48 |
+
gr.Markdown("# <center> OpenAI Text-To-Speech API with Gradio </center>")
|
49 |
+
with gr.Row(variant="panel"):
|
50 |
+
model = gr.Dropdown(choices=["tts-1", "tts-1-hd"], label="Model", value="tts-1")
|
51 |
+
voice = gr.Dropdown(choices=["alloy", "echo", "fable", "onyx", "nova", "shimmer"], label="Voice Options",
|
52 |
+
value="alloy")
|
53 |
+
output_file_format = gr.Dropdown(choices=["mp3", "opus", "aac", "flac"], label="Output Options", value="mp3")
|
54 |
+
speed = gr.Slider(minimum=0.25, maximum=4.0, value=1.0, step=0.01, label="Speed")
|
55 |
+
|
56 |
+
openai_key = gr.Textbox(label="OpenAI API Key", placeholder="Enter your OpenAI API key")
|
57 |
+
text = gr.Textbox(label="Input text",
|
58 |
+
placeholder="Enter your text and then click on the \"Text-To-Speech\" button, "
|
59 |
+
"or simply press the Enter key.")
|
60 |
+
btn = gr.Button("Text-To-Speech")
|
61 |
+
output_audio = gr.Audio(label="Speech Output")
|
62 |
+
|
63 |
+
text.submit(fn=tts, inputs=[text, model, voice, output_file_format, speed, openai_key], outputs=output_audio, api_name="tts")
|
64 |
+
btn.click(fn=tts, inputs=[text, model, voice, output_file_format, speed, openai_key], outputs=output_audio, api_name=False)
|
65 |
+
|
66 |
+
demo.launch(server_name=server_name)
|
67 |
+
|
68 |
+
# curl --location "http://127.0.0.1:7860/api/tts" \
|
69 |
+
# --header "Content-Type: application/json" \
|
70 |
+
# --data "{"data":["Hello", "tts-1", "alloy"]}"
|
71 |
+
|
72 |
+
# curl --location "http://127.0.0.1:7860/file=/private/var/folders/5v/
|
73 |
+
# 0b0l_t1x3752h3t1bt_rtnjr0000gn/T/gradio/e91d8bf36ee07f536bed6b5a5bf9522b1fc86436/tmpkem8ubvz.mp3"
|
env.yaml
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
name: openai_tts
|
2 |
+
channels:
|
3 |
+
- conda-forge
|
4 |
+
dependencies:
|
5 |
+
- python=3.9
|
6 |
+
- pip=22.2.2
|
7 |
+
- pip:
|
8 |
+
- gradio==4.12.0
|
9 |
+
- openai==1.2.3
|
interface.png
ADDED