seawolf2357 commited on
Commit
bc18a34
ยท
1 Parent(s): 0d2047f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -48
app.py CHANGED
@@ -1,50 +1,40 @@
1
  import gradio as gr
2
- import os
3
- import tempfile
4
- from openai import OpenAI
5
-
6
-
7
- def tts(text, model, voice, api_key):
8
- if api_key == '':
9
- raise gr.Error('Please enter your OpenAI API Key')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  else:
11
- try:
12
- client = OpenAI(api_key=api_key)
13
-
14
- response = client.audio.speech.create(
15
- model=model, # "tts-1","tts-1-hd"
16
- voice=voice, # 'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'
17
- input=text,
18
- )
19
-
20
- except Exception as error:
21
- # Handle any exception that occurs
22
- raise gr.Error("An error occurred while generating speech. Please check your API key and try again.")
23
- print(str(error))
24
-
25
- # Create a temp file to save the audio
26
- with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
27
- temp_file.write(response.content)
28
-
29
- # Get the file path of the temp file
30
- temp_file_path = temp_file.name
31
-
32
- return temp_file_path
33
-
34
-
35
- with gr.Blocks() as demo:
36
- gr.Markdown("# <center> OpenAI Text-To-Speech API with Gradio </center>")
37
- #gr.HTML("You can also access the Streaming demo for OpenAI TTS by clicking this <a href='https://huggingface.co/spaces/ysharma/OpenAI_TTS_Streaming'>Gradio demo link</a>")
38
- with gr.Row(variant='panel'):
39
- api_key = gr.Textbox(type='password', label='OpenAI API Key', placeholder='Enter your API key to access the TTS demo')
40
- model = gr.Dropdown(choices=['tts-1','tts-1-hd'], label='Model', value='tts-1')
41
- voice = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Voice Options', value='alloy')
42
-
43
- text = gr.Textbox(label="Input text", placeholder="Enter your text and then click on the 'Text-To-Speech' button, or simply press the Enter key.")
44
- btn = gr.Button("Text-To-Speech")
45
- output_audio = gr.Audio(label="Speech Output")
46
-
47
- text.submit(fn=tts, inputs=[text, model, voice, api_key], outputs=output_audio, api_name="tts_enter_key", concurrency_limit=None)
48
- btn.click(fn=tts, inputs=[text, model, voice, api_key], outputs=output_audio, api_name="tts_button", concurrency_limit=None)
49
-
50
- demo.launch()
 
1
  import gradio as gr
2
+ import requests
3
+
4
+ def generate_audio(text):
5
+ voice_id = 'SKwm0HLYsVDCM2ruvw2p'
6
+ url = "https://api.elevenlabs.io/v1/text-to-speech/" + voice_id
7
+ model_id = "eleven_multilingual_v2"
8
+ payload = {
9
+ "model_id": model_id,
10
+ "text": text,
11
+ "voice_settings": {
12
+ "similarity_boost": 0.75,
13
+ "stability": 0.5,
14
+ "style": 0,
15
+ "use_speaker_boost": True
16
+ }
17
+ }
18
+ headers = {
19
+ "Content-Type": "application/json",
20
+ "xi-api-key": "c5fb99a2b25402f104d246379bcf819a"
21
+ }
22
+
23
+ response = requests.post(url, json=payload, headers=headers, stream=True)
24
+
25
+ if response.status_code == 200:
26
+ return response.content
27
  else:
28
+ raise Exception(f"์˜ค๋ฅ˜ ๋ฐœ์ƒ. ์ƒํƒœ ์ฝ”๋“œ: {response.status_code}")
29
+
30
+ # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
31
+ iface = gr.Interface(
32
+ fn=generate_audio,
33
+ inputs=gr.inputs.Textbox(label="๋ณ€ํ™˜ํ•  ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”."),
34
+ outputs=gr.outputs.Audio(label="์ƒ์„ฑ๋œ ์˜ค๋””์˜ค"),
35
+ title="ํ…์ŠคํŠธ๋ฅผ ์Œ์„ฑ์œผ๋กœ ๋ณ€ํ™˜",
36
+ description="Eleven Labs API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ๋ฅผ ์Œ์„ฑ์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค."
37
+ )
38
+
39
+ if __name__ == "__main__":
40
+ iface.launch()