siddhartharya
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,43 @@
|
|
1 |
import gradio as gr
|
2 |
-
from utils import generate_script, generate_audio, truncate_text
|
3 |
from prompts import SYSTEM_PROMPT
|
4 |
from pydub import AudioSegment
|
5 |
import pypdf
|
6 |
import os
|
7 |
import tempfile
|
8 |
|
9 |
-
def generate_podcast(file, tone, length):
|
10 |
-
|
11 |
-
|
12 |
-
raise gr.Error("Please upload a PDF file.")
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
# Truncate text to 2048 tokens
|
23 |
truncated_text = truncate_text(text)
|
24 |
if len(truncated_text) < len(text):
|
25 |
print("Warning: The input text was truncated to fit within 2048 tokens.")
|
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:
|
@@ -43,30 +50,27 @@ def generate_podcast(file, tone, length):
|
|
43 |
except Exception as e:
|
44 |
raise gr.Error(f"Error generating audio: {str(e)}")
|
45 |
|
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")
|
52 |
temp_audio_path = temp_audio.name
|
53 |
|
54 |
return temp_audio_path, transcript
|
55 |
|
56 |
-
# Gradio interface setup
|
57 |
instructions = """
|
58 |
# Podcast Generator
|
59 |
|
60 |
Welcome to the Podcast Generator project! This tool allows you to create custom podcast episodes using AI-generated content.
|
61 |
|
62 |
## Features
|
63 |
-
* Generate podcast scripts from PDF content
|
64 |
* Convert text to speech for a natural listening experience
|
65 |
* Choose the tone of your podcast
|
66 |
* Export episodes as MP3 files
|
67 |
|
68 |
## How to Use
|
69 |
-
1. Upload a PDF file (content will be truncated to 2048 tokens if longer)
|
70 |
2. Select the desired tone (humorous, casual, formal)
|
71 |
3. Choose the podcast length
|
72 |
4. Click "Generate" to create your podcast
|
@@ -78,7 +82,8 @@ Note: This tool uses the LLaMa 3.1 70B model for script generation and gTTS for
|
|
78 |
iface = gr.Interface(
|
79 |
fn=generate_podcast,
|
80 |
inputs=[
|
81 |
-
gr.File(label="Upload PDF file", file_types=[".pdf"]),
|
|
|
82 |
gr.Radio(["humorous", "casual", "formal"], label="Select podcast tone", value="casual"),
|
83 |
gr.Radio(["Short (1-2 min)", "Medium (3-5 min)"], label="Podcast length", value="Medium (3-5 min)")
|
84 |
],
|
|
|
1 |
import gradio as gr
|
2 |
+
from utils import generate_script, generate_audio, truncate_text, extract_text_from_url
|
3 |
from prompts import SYSTEM_PROMPT
|
4 |
from pydub import AudioSegment
|
5 |
import pypdf
|
6 |
import os
|
7 |
import tempfile
|
8 |
|
9 |
+
def generate_podcast(file, url, tone, length):
|
10 |
+
if file and url:
|
11 |
+
raise gr.Error("Please provide either a PDF file or a URL, not both.")
|
|
|
12 |
|
13 |
+
if file:
|
14 |
+
if not file.name.lower().endswith('.pdf'):
|
15 |
+
raise gr.Error("Please upload a PDF file.")
|
16 |
+
|
17 |
+
try:
|
18 |
+
pdf_reader = pypdf.PdfReader(file.name)
|
19 |
+
text = ""
|
20 |
+
for page in pdf_reader.pages:
|
21 |
+
text += page.extract_text()
|
22 |
+
except Exception as e:
|
23 |
+
raise gr.Error(f"Error reading the PDF file: {str(e)}")
|
24 |
+
elif url:
|
25 |
+
try:
|
26 |
+
text = extract_text_from_url(url)
|
27 |
+
except Exception as e:
|
28 |
+
raise gr.Error(f"Error extracting text from URL: {str(e)}")
|
29 |
+
else:
|
30 |
+
raise gr.Error("Please provide either a PDF file or a URL.")
|
31 |
|
|
|
32 |
truncated_text = truncate_text(text)
|
33 |
if len(truncated_text) < len(text):
|
34 |
print("Warning: The input text was truncated to fit within 2048 tokens.")
|
35 |
|
|
|
36 |
try:
|
37 |
script = generate_script(SYSTEM_PROMPT, truncated_text, tone, length)
|
38 |
except Exception as e:
|
39 |
raise gr.Error(f"Error generating script: {str(e)}")
|
40 |
|
|
|
41 |
audio_segments = []
|
42 |
transcript = ""
|
43 |
try:
|
|
|
50 |
except Exception as e:
|
51 |
raise gr.Error(f"Error generating audio: {str(e)}")
|
52 |
|
|
|
53 |
combined_audio = sum(audio_segments)
|
54 |
|
|
|
55 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
|
56 |
combined_audio.export(temp_audio.name, format="mp3")
|
57 |
temp_audio_path = temp_audio.name
|
58 |
|
59 |
return temp_audio_path, transcript
|
60 |
|
|
|
61 |
instructions = """
|
62 |
# Podcast Generator
|
63 |
|
64 |
Welcome to the Podcast Generator project! This tool allows you to create custom podcast episodes using AI-generated content.
|
65 |
|
66 |
## Features
|
67 |
+
* Generate podcast scripts from PDF content or web pages
|
68 |
* Convert text to speech for a natural listening experience
|
69 |
* Choose the tone of your podcast
|
70 |
* Export episodes as MP3 files
|
71 |
|
72 |
## How to Use
|
73 |
+
1. Upload a PDF file OR enter a URL (content will be truncated to 2048 tokens if longer)
|
74 |
2. Select the desired tone (humorous, casual, formal)
|
75 |
3. Choose the podcast length
|
76 |
4. Click "Generate" to create your podcast
|
|
|
82 |
iface = gr.Interface(
|
83 |
fn=generate_podcast,
|
84 |
inputs=[
|
85 |
+
gr.File(label="Upload PDF file (optional)", file_types=[".pdf"]),
|
86 |
+
gr.Textbox(label="OR Enter URL"),
|
87 |
gr.Radio(["humorous", "casual", "formal"], label="Select podcast tone", value="casual"),
|
88 |
gr.Radio(["Short (1-2 min)", "Medium (3-5 min)"], label="Podcast length", value="Medium (3-5 min)")
|
89 |
],
|