windszzlang commited on
Commit
8c4e5b4
·
1 Parent(s): 0c06d38

Add application file

Browse files
Files changed (3) hide show
  1. .env +1 -0
  2. app.py +88 -0
  3. requirements.txt +3 -0
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ OPENAI_KEY=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
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
16
+ if openai_key == "<YOUR_OPENAI_KEY>":
17
+ openai_key = ""
18
+
19
+ if openai_key == "":
20
+ sys.exit("Please Provide Your OpenAI API Key")
21
+
22
+
23
+ def tts(
24
+ text: str,
25
+ model: Union[str, Literal["tts-1", "tts-1-hd"]],
26
+ voice: Literal["alloy", "echo", "fable", "onyx", "nova", "shimmer"],
27
+ output_file_format: Literal["mp3", "opus", "aac", "flac"] = "mp3",
28
+ speed: float = 1.0
29
+ ):
30
+ if len(text) > 0:
31
+ try:
32
+ client = OpenAI(api_key=openai_key)
33
+
34
+ response = client.audio.speech.create(
35
+ model=model,
36
+ voice=voice,
37
+ input=text,
38
+ response_format=output_file_format,
39
+ speed=speed
40
+ )
41
+
42
+ except Exception as error:
43
+ print(str(error))
44
+ raise gr.Error(
45
+ "An error occurred while generating speech. Please check your API key and come back try again.")
46
+
47
+ with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
48
+ temp_file.write(response.content)
49
+
50
+ temp_file_path = temp_file.name
51
+
52
+ return temp_file_path
53
+ else:
54
+ return "1-second-of-silence.mp3"
55
+
56
+
57
+ with gr.Blocks() as demo:
58
+ gr.Markdown("# <center> OpenAI Text-To-Speech API with Gradio </center>")
59
+ with gr.Row(variant="panel"):
60
+ model = gr.Dropdown(choices=["tts-1", "tts-1-hd"], label="Model", value="tts-1")
61
+ voice = gr.Dropdown(choices=["alloy", "echo", "fable", "onyx", "nova", "shimmer"], label="Voice Options",
62
+ value="alloy")
63
+ output_file_format = gr.Dropdown(choices=["mp3", "opus", "aac", "flac"], label="Output Options", value="mp3")
64
+ speed = gr.Slider(minimum=0.25, maximum=4.0, value=1.0, step=0.01, label="Speed")
65
+
66
+ text = gr.Textbox(label="Input text",
67
+ placeholder="Enter your text and then click on the \"Text-To-Speech\" button, "
68
+ "or simply press the Enter key.")
69
+ btn = gr.Button("Text-To-Speech")
70
+ output_audio = gr.Audio(label="Speech Output")
71
+
72
+ text.submit(fn=tts, inputs=[text, model, voice, output_file_format, speed], outputs=output_audio, api_name="tts")
73
+ btn.click(fn=tts, inputs=[text, model, voice, output_file_format, speed], outputs=output_audio, api_name=False)
74
+
75
+ demo.launch(
76
+ share=True,
77
+ debug=True,
78
+ # server_name="127.0.0.1",
79
+ server_name="0.0.0.0",
80
+ server_port=8088
81
+ )
82
+
83
+ # curl --location "http://127.0.0.1:7860/api/tts" \
84
+ # --header "Content-Type: application/json" \
85
+ # --data "{"data":["Hello", "tts-1", "alloy"]}"
86
+
87
+ # curl --location "http://127.0.0.1:7860/file=/private/var/folders/5v/
88
+ # 0b0l_t1x3752h3t1bt_rtnjr0000gn/T/gradio/e91d8bf36ee07f536bed6b5a5bf9522b1fc86436/tmpkem8ubvz.mp3"
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio~=4.12.0
2
+ openai~=1.2.3
3
+ python-dotenv~=1.0.0