Nymbo commited on
Commit
a1ac836
·
1 Parent(s): 2674364

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -81
app.py CHANGED
@@ -1,83 +1,50 @@
1
- import random
2
- import gradio as gr
3
- import numpy as np
4
- from elevenlabs import voices, generate, set_api_key, UnauthenticatedRateLimitError
5
-
6
- def pad_buffer(audio):
7
- # Pad buffer to multiple of 2 bytes
8
- buffer_size = len(audio)
9
- element_size = np.dtype(np.int16).itemsize
10
- if buffer_size % element_size != 0:
11
- audio = audio + b'\0' * (element_size - (buffer_size % element_size))
12
- return audio
13
-
14
- def generate_voice(text, voice_name, api_key):
15
- set_api_key(api_key) #set API key
16
- try:
17
- audio = generate(
18
- text[:4000], # Limit to 4000 characters
19
- voice=voice_name,
20
- model="eleven_multilingual_v2"
21
- )
22
- return (44100, np.frombuffer(pad_buffer(audio), dtype=np.int16))
23
- except UnauthenticatedRateLimitError as e:
24
- raise gr.Error("Thanks for trying out ElevenLabs TTS! You've reached the free tier limit. Please provide an API key to continue.")
25
- except Exception as e:
26
- raise gr.Error(e)
27
-
28
- description = """
29
- Eleven Multilingual V2 is the world's best Text-to-Speech model. Features 38 voices and supports 28 languages. Sign up on [ElevenLabs](https://elevenlabs.io/?from=partnerpierce7156) to get an API Key.
30
- """
31
-
32
- with gr.Blocks() as block:
33
- #gr.Markdown('[ ![ElevenLabs](https://user-images.githubusercontent.com/12028621/262629275-4f85c9cf-85b6-435e-ab50-5b8c7c4e9dd2.png) ](https://elevenlabs.io)')
34
- gr.Markdown("# <center> ElevenLabs </center>")
35
- gr.Markdown(description)
36
-
37
- with gr.Row(variant='panel'):
38
-
39
- input_api_key = gr.Textbox(
40
- type='password',
41
- label='Elevenlabs API Key',
42
- placeholder='Enter your API key',
43
- elem_id="input_api_key"
44
- )
45
-
46
- all_voices = voices()
47
- input_voice = gr.Dropdown(
48
- [ voice.name for voice in all_voices ],
49
- value="Rachel",
50
- label="Voice",
51
- elem_id="input_voice"
52
- )
53
-
54
- input_text = gr.Textbox(
55
- label="Input Text (4000 characters max)",
56
- lines=1,
57
- value="Hello! 你好! Hola! नमस्ते! Bonjour! こんにちは! مرحبا! 안녕하세요! Ciao! Cześć! Привіт! Γειά σας! Здравей! வணக்கம்!",
58
- elem_id="input_text"
59
- )
60
-
61
- run_button = gr.Button(
62
- text="Generate Voice",
63
- type="button"
64
- )
65
-
66
- out_audio = gr.Audio(
67
- label="Generated Voice",
68
- type="numpy",
69
- elem_id="out_audio",
70
- format="mp3"
71
- )
72
-
73
- inputs = [input_text, input_voice, input_api_key]
74
- outputs = [out_audio]
75
 
76
- run_button.click(
77
- fn=generate_voice,
78
- inputs=inputs,
79
- outputs=outputs,
80
- queue=True
81
- )
82
 
83
- block.queue(concurrency_count=5).launch(debug=True)
 
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> Alyxsissy TTS Preview </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')
40
+ model = gr.Dropdown(choices=['tts-1','tts-1-hd'], label='Model', value='tts-1-hd')
41
+ voice = gr.Dropdown(choices=['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'], label='Voice Options', value='nova')
42
+
43
+ text = gr.Textbox(label="Input text", placeholder="Enter your text and then click on the button below.")
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()