Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,27 +12,36 @@ VOICES = {
|
|
12 |
}
|
13 |
|
14 |
def parse_segments(text):
|
15 |
-
"""Parse input text for speaker segments
|
16 |
-
pattern = re.compile(r'$$(?P<speaker>[^$$]
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def generate_podcast(input_text):
|
21 |
"""Convert text to podcast with multiple voices"""
|
22 |
try:
|
23 |
-
segments = parse_segments(input_text)
|
24 |
|
25 |
if not segments:
|
26 |
-
return (22050, np.zeros(0)), "No valid speaker segments found"
|
27 |
-
|
28 |
all_audio = []
|
29 |
current_pipe = None
|
30 |
current_model = ""
|
31 |
|
32 |
for speaker, text in segments:
|
33 |
-
if speaker not in VOICES:
|
34 |
-
return (22050, np.zeros(0)), f"Invalid speaker: {speaker}"
|
35 |
-
|
36 |
model_name = VOICES[speaker]
|
37 |
|
38 |
# Load model only when needed
|
@@ -48,7 +57,11 @@ def generate_podcast(input_text):
|
|
48 |
# Combine all audio segments with short pauses
|
49 |
final_audio = np.concatenate([np.concatenate((audio, np.zeros(5000))) for audio in all_audio])
|
50 |
|
51 |
-
|
|
|
|
|
|
|
|
|
52 |
|
53 |
except Exception as e:
|
54 |
return (22050, np.zeros(0)), f"Error: {str(e)}"
|
@@ -64,8 +77,8 @@ demo = gr.Interface(
|
|
64 |
label="Input Text with Speaker Tags",
|
65 |
lines=12,
|
66 |
placeholder="""Example format:
|
67 |
-
[Amy (Female)]
|
68 |
-
[Joe (Male)]
|
69 |
),
|
70 |
outputs=[
|
71 |
gr.Audio(label="Generated Podcast", type="numpy"),
|
@@ -73,7 +86,8 @@ demo = gr.Interface(
|
|
73 |
],
|
74 |
examples=[
|
75 |
["""[Amy (Female)]Welcome to our podcast![/Amy (Female)]
|
76 |
-
[Joe (Male)]Today we're discussing AI innovations.[/Joe (Male)]
|
|
|
77 |
],
|
78 |
title="🎙️ Multi-Voice Podcast Generator",
|
79 |
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.",
|
|
|
12 |
}
|
13 |
|
14 |
def parse_segments(text):
|
15 |
+
"""Parse input text for speaker segments with improved validation"""
|
16 |
+
pattern = re.compile(r'$$(?P<speaker>[^$$]+?)$$(?P<text>.*?)$$/(?P=speaker)$$', re.DOTALL)
|
17 |
+
matches = list(pattern.finditer(text))
|
18 |
+
|
19 |
+
# Validate speaker names and collect results
|
20 |
+
valid_segments = []
|
21 |
+
for match in matches:
|
22 |
+
speaker = match.group('speaker')
|
23 |
+
if speaker in VOICES:
|
24 |
+
valid_segments.append((speaker, match.group('text').strip()))
|
25 |
+
|
26 |
+
# Find any invalid segments
|
27 |
+
if len(matches) < len(text.strip()):
|
28 |
+
return valid_segments, f"Warning: Found {len(matches)} valid segments, but text contains untagged content or invalid speaker names"
|
29 |
+
|
30 |
+
return valid_segments, None
|
31 |
|
32 |
def generate_podcast(input_text):
|
33 |
"""Convert text to podcast with multiple voices"""
|
34 |
try:
|
35 |
+
segments, warning = parse_segments(input_text)
|
36 |
|
37 |
if not segments:
|
38 |
+
return (22050, np.zeros(0)), "No valid speaker segments found. Please use the format: [Speaker Name]text[/Speaker Name]"
|
39 |
+
|
40 |
all_audio = []
|
41 |
current_pipe = None
|
42 |
current_model = ""
|
43 |
|
44 |
for speaker, text in segments:
|
|
|
|
|
|
|
45 |
model_name = VOICES[speaker]
|
46 |
|
47 |
# Load model only when needed
|
|
|
57 |
# Combine all audio segments with short pauses
|
58 |
final_audio = np.concatenate([np.concatenate((audio, np.zeros(5000))) for audio in all_audio])
|
59 |
|
60 |
+
status = "Podcast generated successfully!"
|
61 |
+
if warning:
|
62 |
+
status += " " + warning
|
63 |
+
|
64 |
+
return (output["sampling_rate"], final_audio), status
|
65 |
|
66 |
except Exception as e:
|
67 |
return (22050, np.zeros(0)), f"Error: {str(e)}"
|
|
|
77 |
label="Input Text with Speaker Tags",
|
78 |
lines=12,
|
79 |
placeholder="""Example format:
|
80 |
+
[Amy (Female)]Welcome to our podcast![/Amy (Female)]
|
81 |
+
[Joe (Male)]Today we're discussing AI innovations.[/Joe (Male)]"""
|
82 |
),
|
83 |
outputs=[
|
84 |
gr.Audio(label="Generated Podcast", type="numpy"),
|
|
|
86 |
],
|
87 |
examples=[
|
88 |
["""[Amy (Female)]Welcome to our podcast![/Amy (Female)]
|
89 |
+
[Joe (Male)]Today we're discussing AI innovations.[/Joe (Male)]
|
90 |
+
[Clara (Female)]This is Clara speaking![/Clara (Female)]"""]
|
91 |
],
|
92 |
title="🎙️ Multi-Voice Podcast Generator",
|
93 |
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.",
|