siddhartharya
commited on
Commit
•
3e62a46
1
Parent(s):
e81cad8
Update app.py
Browse files
app.py
CHANGED
@@ -4,33 +4,8 @@ from prompts import SYSTEM_PROMPT
|
|
4 |
from pydub import AudioSegment
|
5 |
import pypdf
|
6 |
import os
|
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'):
|
@@ -51,21 +26,15 @@ def generate_podcast(file, tone, length):
|
|
51 |
|
52 |
# Generate script
|
53 |
try:
|
54 |
-
script = generate_script(SYSTEM_PROMPT, truncated_text, tone)
|
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
|
69 |
audio_file = generate_audio(item.text, item.speaker)
|
70 |
audio_segment = AudioSegment.from_mp3(audio_file)
|
71 |
audio_segments.append(audio_segment)
|
@@ -77,11 +46,6 @@ def generate_podcast(file, tone, length):
|
|
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:
|
87 |
combined_audio.export(temp_audio.name, format="mp3")
|
@@ -89,7 +53,7 @@ def generate_podcast(file, tone, length):
|
|
89 |
|
90 |
return temp_audio_path, transcript
|
91 |
|
92 |
-
# Gradio interface setup
|
93 |
instructions = """
|
94 |
# Podcast Generator
|
95 |
|
|
|
4 |
from pydub import AudioSegment
|
5 |
import pypdf
|
6 |
import os
|
|
|
7 |
import tempfile
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
def generate_podcast(file, tone, length):
|
10 |
# Extract text from PDF
|
11 |
if not file.name.lower().endswith('.pdf'):
|
|
|
26 |
|
27 |
# Generate script
|
28 |
try:
|
29 |
+
script = generate_script(SYSTEM_PROMPT, truncated_text, tone, length)
|
30 |
except Exception as e:
|
31 |
raise gr.Error(f"Error generating script: {str(e)}")
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
# Generate audio for each dialogue item
|
34 |
audio_segments = []
|
35 |
transcript = ""
|
36 |
try:
|
37 |
+
for item in script.dialogue:
|
38 |
audio_file = generate_audio(item.text, item.speaker)
|
39 |
audio_segment = AudioSegment.from_mp3(audio_file)
|
40 |
audio_segments.append(audio_segment)
|
|
|
46 |
# Combine audio segments
|
47 |
combined_audio = sum(audio_segments)
|
48 |
|
|
|
|
|
|
|
|
|
|
|
49 |
# Save combined audio to a temporary file
|
50 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
|
51 |
combined_audio.export(temp_audio.name, format="mp3")
|
|
|
53 |
|
54 |
return temp_audio_path, transcript
|
55 |
|
56 |
+
# Gradio interface setup
|
57 |
instructions = """
|
58 |
# Podcast Generator
|
59 |
|