Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,83 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
from
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 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 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 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('[  ](https://elevenlabs.io)')
|
| 34 |
+
gr.Markdown("# <center> ElevenLabs </center>")
|
| 35 |
+
gr.Markdown(description)
|
| 36 |
+
|
| 37 |
|
| 38 |
+
with gr.Row(variant='panel'):
|
| 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)
|