siddhartharya
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,30 @@ import os
|
|
7 |
import io
|
8 |
import tempfile
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def generate_podcast(file, tone, length):
|
11 |
# Extract text from PDF
|
12 |
if not file.name.lower().endswith('.pdf'):
|
@@ -31,11 +55,17 @@ def generate_podcast(file, tone, length):
|
|
31 |
except Exception as e:
|
32 |
raise gr.Error(f"Error generating script: {str(e)}")
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
# Generate audio for each dialogue item
|
35 |
audio_segments = []
|
36 |
transcript = ""
|
37 |
try:
|
38 |
-
for item in
|
39 |
audio_file = generate_audio(item.text, item.speaker)
|
40 |
audio_segment = AudioSegment.from_mp3(audio_file)
|
41 |
audio_segments.append(audio_segment)
|
@@ -47,11 +77,10 @@ def generate_podcast(file, tone, length):
|
|
47 |
# Combine audio segments
|
48 |
combined_audio = sum(audio_segments)
|
49 |
|
50 |
-
#
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
combined_audio = combined_audio[:300000] # 5 minutes max
|
55 |
|
56 |
# Save combined audio to a temporary file
|
57 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
|
@@ -60,6 +89,7 @@ def generate_podcast(file, tone, length):
|
|
60 |
|
61 |
return temp_audio_path, transcript
|
62 |
|
|
|
63 |
instructions = """
|
64 |
# Podcast Generator
|
65 |
|
|
|
7 |
import io
|
8 |
import tempfile
|
9 |
|
10 |
+
def estimate_audio_length(text):
|
11 |
+
# Estimate 150 words per minute
|
12 |
+
word_count = len(text.split())
|
13 |
+
return word_count / 150 # Returns length in minutes
|
14 |
+
|
15 |
+
def trim_dialogue(dialogue, target_length_minutes):
|
16 |
+
trimmed_dialogue = []
|
17 |
+
current_length = 0
|
18 |
+
target_length_seconds = target_length_minutes * 60
|
19 |
+
|
20 |
+
for item in dialogue.dialogue:
|
21 |
+
item_length = estimate_audio_length(item.text)
|
22 |
+
if current_length + item_length > target_length_minutes:
|
23 |
+
# Trim this item to fit
|
24 |
+
words = item.text.split()
|
25 |
+
words_to_keep = int((target_length_minutes - current_length) * 150)
|
26 |
+
item.text = " ".join(words[:words_to_keep]) + "..."
|
27 |
+
trimmed_dialogue.append(item)
|
28 |
+
break
|
29 |
+
trimmed_dialogue.append(item)
|
30 |
+
current_length += item_length
|
31 |
+
|
32 |
+
return trimmed_dialogue
|
33 |
+
|
34 |
def generate_podcast(file, tone, length):
|
35 |
# Extract text from PDF
|
36 |
if not file.name.lower().endswith('.pdf'):
|
|
|
55 |
except Exception as e:
|
56 |
raise gr.Error(f"Error generating script: {str(e)}")
|
57 |
|
58 |
+
# Determine target length in minutes
|
59 |
+
target_length = 2 if length == "Short (1-2 min)" else 5
|
60 |
+
|
61 |
+
# Trim dialogue to fit target length
|
62 |
+
trimmed_dialogue = trim_dialogue(script, target_length)
|
63 |
+
|
64 |
# Generate audio for each dialogue item
|
65 |
audio_segments = []
|
66 |
transcript = ""
|
67 |
try:
|
68 |
+
for item in trimmed_dialogue:
|
69 |
audio_file = generate_audio(item.text, item.speaker)
|
70 |
audio_segment = AudioSegment.from_mp3(audio_file)
|
71 |
audio_segments.append(audio_segment)
|
|
|
77 |
# Combine audio segments
|
78 |
combined_audio = sum(audio_segments)
|
79 |
|
80 |
+
# Ensure audio doesn't exceed target length
|
81 |
+
target_length_ms = target_length * 60 * 1000
|
82 |
+
if len(combined_audio) > target_length_ms:
|
83 |
+
combined_audio = combined_audio[:target_length_ms]
|
|
|
84 |
|
85 |
# Save combined audio to a temporary file
|
86 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
|
|
|
89 |
|
90 |
return temp_audio_path, transcript
|
91 |
|
92 |
+
# Gradio interface setup remains the same
|
93 |
instructions = """
|
94 |
# Podcast Generator
|
95 |
|