Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,45 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
-
from elevenlabs import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
try:
|
15 |
-
audio
|
16 |
-
|
17 |
-
voice=voice_name,
|
18 |
-
model=model_name
|
19 |
-
)
|
20 |
-
return (44100, np.frombuffer(pad_buffer(audio), dtype=np.int16))
|
21 |
-
except UnauthenticatedRateLimitError as e:
|
22 |
-
raise gr.Error("Thanks for trying out ElevenLabs TTS! You've reached the free tier limit. Please provide an API key to continue.")
|
23 |
-
except Exception as e:
|
24 |
-
raise gr.Error(str(e))
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
29 |
|
|
|
30 |
input_text = gr.Textbox(label="Input Text", lines=2)
|
31 |
-
|
32 |
-
out_audio = gr.Audio(label="Generated Voice", type="numpy")
|
33 |
|
34 |
iface = gr.Interface(
|
35 |
-
fn=
|
36 |
-
inputs=
|
37 |
-
outputs=
|
38 |
theme="Monochrome",
|
39 |
-
live=True
|
40 |
)
|
41 |
|
|
|
42 |
iface.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from elevenlabs import clone, generate, set_api_key
|
4 |
+
from pydub import AudioSegment
|
5 |
+
|
6 |
+
# Set up ElevenLabs API Key
|
7 |
+
set_api_key("73bb17b223e2b0f90a403eaeaa3b4d35")
|
8 |
+
|
9 |
+
# Function to convert MP3 to NumPy array
|
10 |
+
def mp3_to_numpy(file_path):
|
11 |
+
audio = AudioSegment.from_mp3(file_path)
|
12 |
+
samples = np.array(audio.get_array_of_samples())
|
13 |
+
return audio.frame_rate, samples
|
14 |
+
|
15 |
+
# Load and clone voice
|
16 |
+
voice = clone(
|
17 |
+
name="Voice Name",
|
18 |
+
description="An old American male voice with a slight hoarseness in his throat. Perfect for news.",
|
19 |
+
files=["./sample1.mp3", "./sample2.mp3"],
|
20 |
+
)
|
21 |
+
|
22 |
+
def generate_voice_output(text):
|
23 |
try:
|
24 |
+
# Generate audio for the provided text
|
25 |
+
audio = generate(text=text, voice=voice)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
# Convert audio for Gradio
|
28 |
+
audio_numpy = (44100, np.frombuffer(audio, dtype=np.int16))
|
29 |
+
return audio_numpy
|
30 |
+
except Exception as e:
|
31 |
+
return str(e)
|
32 |
|
33 |
+
# Set up Gradio components and interface
|
34 |
input_text = gr.Textbox(label="Input Text", lines=2)
|
35 |
+
output_audio = gr.Audio(label="Generated Voice", type="numpy")
|
|
|
36 |
|
37 |
iface = gr.Interface(
|
38 |
+
fn=generate_voice_output,
|
39 |
+
inputs=input_text,
|
40 |
+
outputs=output_audio,
|
41 |
theme="Monochrome",
|
|
|
42 |
)
|
43 |
|
44 |
+
# Launch Gradio app
|
45 |
iface.launch()
|