File size: 1,607 Bytes
74245b5
1e7b36b
38f82cf
1e7b36b
 
851995d
1e7b36b
 
74245b5
1e7b36b
38f82cf
 
1e7b36b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74245b5
a727789
1e7b36b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import gradio as gr
from gtts import gTTS
import io
import os
from pydub import AudioSegment

def text_to_speech(text):
    lines = text.split('\n')
    audio_segments = []

    for i, line in enumerate(lines):
        if line.strip():  # Skip empty lines
            tts = gTTS(text=line, lang='en', slow=False)
            
            # Save to a temporary file
            temp_filename = f"temp_{i}.mp3"
            tts.save(temp_filename)
            
            # Load the audio segment
            audio_segment = AudioSegment.from_mp3(temp_filename)
            
            # Alternate between two voices
            if i % 2 == 0:
                audio_segment = audio_segment.set_frame_rate(44100)  # Higher pitch for even lines (speaker 1)
            else:
                audio_segment = audio_segment.set_frame_rate(22050)  # Lower pitch for odd lines (speaker 2)
            
            audio_segments.append(audio_segment)
            
            # Remove the temporary file
            os.remove(temp_filename)

    # Concatenate all audio segments
    final_audio = sum(audio_segments)

    # Export the final audio to a byte stream
    buffer = io.BytesIO()
    final_audio.export(buffer, format="mp3")
    buffer.seek(0)

    return buffer

iface = gr.Interface(
    fn=text_to_speech,
    inputs=gr.Textbox(lines=10, placeholder="Enter your text here..."),
    outputs=gr.Audio(type="binary"),
    title="Two-Speaker Text-to-Speech Converter",
    description="Convert text to speech with alternating voices for even and odd lines."
)

if __name__ == "__main__":
    iface.launch()