antfraia commited on
Commit
db33eee
·
1 Parent(s): f0afc12

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -26
app.py CHANGED
@@ -1,41 +1,32 @@
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",
 
1
  import gradio as gr
2
+ from elevenlabs import generate, stream, set_api_key
 
 
3
 
4
  # Set up ElevenLabs API Key
5
  set_api_key("73bb17b223e2b0f90a403eaeaa3b4d35")
6
 
7
+ def generate_streamed_audio(text):
8
+ audio_stream = generate(
9
+ text=text,
10
+ stream=True
11
+ )
12
+ # This is where we'd integrate the streaming into Gradio.
13
+ # However, Gradio's native components don't support audio streaming directly.
14
+ # As a workaround, we might need to save the streamed audio to a file and return that.
15
+ # But this is not true real-time streaming.
16
 
17
+ audio_filename = "temp_audio.mp3"
18
+ with open(audio_filename, "wb") as f:
19
+ for chunk in audio_stream:
20
+ f.write(chunk)
 
 
 
 
 
 
 
21
 
22
+ return audio_filename
 
 
 
 
23
 
24
  # Set up Gradio components and interface
25
  input_text = gr.Textbox(label="Input Text", lines=2)
26
+ output_audio = gr.Audio(label="Generated Voice", type="file")
27
 
28
  iface = gr.Interface(
29
+ fn=generate_streamed_audio,
30
  inputs=input_text,
31
  outputs=output_audio,
32
  theme="Monochrome",