ericwangpq commited on
Commit
3aa6525
·
1 Parent(s): 5fd244d
Files changed (2) hide show
  1. app.py +67 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ from dotenv import load_dotenv
10
+
11
+ load_dotenv()
12
+
13
+ openai_key = os.getenv("OPENAI_KEY")
14
+
15
+ def tts(
16
+ text: str,
17
+ model: Union[str, Literal["tts-1", "tts-1-hd"]],
18
+ voice: Literal["alloy", "echo", "fable", "onyx", "nova", "shimmer"],
19
+ output_file_format: Literal["mp3", "opus", "aac", "flac"] = "mp3",
20
+ speed: float = 1.0
21
+ ):
22
+ if len(text) > 0:
23
+ try:
24
+ client = OpenAI(api_key=openai_key)
25
+
26
+ response = client.audio.speech.create(
27
+ model=model,
28
+ voice=voice,
29
+ input=text,
30
+ response_format=output_file_format,
31
+ speed=speed
32
+ )
33
+
34
+ except Exception as error:
35
+ print(str(error))
36
+ raise gr.Error(
37
+ "An error occurred while generating speech. Please check your API key and come back try again.")
38
+
39
+ with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
40
+ temp_file.write(response.content)
41
+
42
+ temp_file_path = temp_file.name
43
+
44
+ return temp_file_path
45
+ else:
46
+ return "1-second-of-silence.mp3"
47
+
48
+
49
+ with gr.Blocks() as demo:
50
+ gr.Markdown("# <center> OpenAI Text-To-Speech API with Gradio </center>")
51
+ with gr.Row(variant="panel"):
52
+ model = gr.Dropdown(choices=["tts-1", "tts-1-hd"], label="Model", value="tts-1")
53
+ voice = gr.Dropdown(choices=["alloy", "echo", "fable", "onyx", "nova", "shimmer"], label="Voice Options",
54
+ value="alloy")
55
+ output_file_format = gr.Dropdown(choices=["mp3", "opus", "aac", "flac"], label="Output Options", value="mp3")
56
+ speed = gr.Slider(minimum=0.25, maximum=4.0, value=1.0, step=0.01, label="Speed")
57
+
58
+ text = gr.Textbox(label="Input text",
59
+ placeholder="Enter your text and then click on the \"Text-To-Speech\" button, "
60
+ "or simply press the Enter key.")
61
+ btn = gr.Button("Text-To-Speech")
62
+ output_audio = gr.Audio(label="Speech Output")
63
+
64
+ text.submit(fn=tts, inputs=[text, model, voice, output_file_format, speed], outputs=output_audio, api_name="tts")
65
+ btn.click(fn=tts, inputs=[text, model, voice, output_file_format, speed], outputs=output_audio, api_name=False)
66
+
67
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio~=4.12.0
2
+ openai~=1.2.3
3
+ python-dotenv~=1.0.0