File size: 3,596 Bytes
4cfc491
 
 
 
 
 
 
 
 
 
 
 
 
 
7eae664
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cfc491
 
 
 
7eae664
4cfc491
 
7eae664
 
4cfc491
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7eae664
 
 
 
 
4cfc491
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7eae664
 
4cfc491
 
 
 
 
 
 
7eae664
 
4cfc491
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import re
import numpy as np
from transformers import pipeline
import gradio as gr

# Available voices and their corresponding models
VOICES = {
    "Amy (Female)": "microsoft/vits-piper-en-us-amy",
    "Joe (Male)": "microsoft/vits-piper-en-us-joe",
    "Clara (Female)": "microsoft/vits-piper-en-us-clb",
    "Ryan (Male)": "microsoft/vits-piper-en-us-jvs"
}

def parse_segments(text):
    """Parse input text for speaker segments with improved validation"""
    pattern = re.compile(r'$$(?P<speaker>[^$$]+?)$$(?P<text>.*?)$$/(?P=speaker)$$', re.DOTALL)
    matches = list(pattern.finditer(text))
    
    # Validate speaker names and collect results
    valid_segments = []
    for match in matches:
        speaker = match.group('speaker')
        if speaker in VOICES:
            valid_segments.append((speaker, match.group('text').strip()))
    
    # Find any invalid segments
    if len(matches) < len(text.strip()):
        return valid_segments, f"Warning: Found {len(matches)} valid segments, but text contains untagged content or invalid speaker names"
    
    return valid_segments, None

def generate_podcast(input_text):
    """Convert text to podcast with multiple voices"""
    try:
        segments, warning = parse_segments(input_text)
        
        if not segments:
            return (22050, np.zeros(0)), "No valid speaker segments found. Please use the format: [Speaker Name]text[/Speaker Name]"
        
        all_audio = []
        current_pipe = None
        current_model = ""
        
        for speaker, text in segments:
            model_name = VOICES[speaker]
            
            # Load model only when needed
            if current_model != model_name:
                if current_pipe: del current_pipe
                current_pipe = pipeline("text-to-speech", model=model_name)
                current_model = model_name
                
            # Generate audio for this segment
            output = current_pipe(text)
            all_audio.append(output["audio"])
        
        # Combine all audio segments with short pauses
        final_audio = np.concatenate([np.concatenate((audio, np.zeros(5000))) for audio in all_audio])
        
        status = "Podcast generated successfully!"
        if warning:
            status += " " + warning
            
        return (output["sampling_rate"], final_audio), status

    except Exception as e:
        return (22050, np.zeros(0)), f"Error: {str(e)}"

# Create Gradio interface
def podcast_interface(text):
    (sr, audio), status = generate_podcast(text)
    return (sr, audio) if audio.size > 0 else gr.update(), status

demo = gr.Interface(
    fn=podcast_interface,
    inputs=gr.Textbox(
        label="Input Text with Speaker Tags",
        lines=12,
        placeholder="""Example format:
[Amy (Female)]Welcome to our podcast![/Amy (Female)]
[Joe (Male)]Today we're discussing AI innovations.[/Joe (Male)]"""
    ),
    outputs=[
        gr.Audio(label="Generated Podcast", type="numpy"),
        gr.Textbox(label="Status", value="Ready")
    ],
    examples=[
        ["""[Amy (Female)]Welcome to our podcast![/Amy (Female)]
[Joe (Male)]Today we're discussing AI innovations.[/Joe (Male)]
[Clara (Female)]This is Clara speaking![/Clara (Female)]"""]
    ],
    title="🎙️ Multi-Voice Podcast Generator",
    description="Generate podcasts with multiple free AI voices using Microsoft's Piper TTS models. Use [SpeakerName] tags to assign different voices to different text segments.",
    theme="soft",
    allow_flagging="never"
)

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