Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,12 @@
|
|
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 |
try:
|
11 |
if file and url:
|
@@ -31,13 +32,11 @@ def generate_podcast(file, url, tone, length):
|
|
31 |
script = generate_script(SYSTEM_PROMPT, truncated_text, tone, length)
|
32 |
|
33 |
audio_segments = []
|
34 |
-
transcript = ""
|
35 |
try:
|
36 |
for item in script.dialogue:
|
37 |
audio_file = generate_audio(item.text, item.speaker)
|
38 |
audio_segment = AudioSegment.from_mp3(audio_file)
|
39 |
audio_segments.append(audio_segment)
|
40 |
-
transcript += f"**{item.speaker}**: {item.text}\n\n"
|
41 |
os.remove(audio_file) # Clean up temporary audio file
|
42 |
except Exception as e:
|
43 |
raise gr.Error(f"Error generating audio: {str(e)}")
|
@@ -48,32 +47,70 @@ def generate_podcast(file, url, tone, length):
|
|
48 |
combined_audio.export(temp_audio.name, format="mp3")
|
49 |
temp_audio_path = temp_audio.name
|
50 |
|
51 |
-
return temp_audio_path
|
52 |
|
53 |
except Exception as e:
|
54 |
return None, f"An error occurred: {str(e)}"
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
instructions = """
|
57 |
# Podify Studio
|
58 |
-
Welcome to the Podcast Generator
|
|
|
59 |
"""
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
gr.
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
],
|
73 |
-
title="
|
74 |
-
description=instructions
|
75 |
-
allow_flagging="never",
|
76 |
-
theme=gr.themes.Soft()
|
77 |
)
|
78 |
|
79 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
2 |
+
from utils import generate_script, generate_audio, truncate_text, extract_text_from_url, summarize_text
|
3 |
from prompts import SYSTEM_PROMPT
|
4 |
from pydub import AudioSegment
|
5 |
import pypdf
|
6 |
import os
|
7 |
import tempfile
|
8 |
|
9 |
+
# Podcast generation function
|
10 |
def generate_podcast(file, url, tone, length):
|
11 |
try:
|
12 |
if file and url:
|
|
|
32 |
script = generate_script(SYSTEM_PROMPT, truncated_text, tone, length)
|
33 |
|
34 |
audio_segments = []
|
|
|
35 |
try:
|
36 |
for item in script.dialogue:
|
37 |
audio_file = generate_audio(item.text, item.speaker)
|
38 |
audio_segment = AudioSegment.from_mp3(audio_file)
|
39 |
audio_segments.append(audio_segment)
|
|
|
40 |
os.remove(audio_file) # Clean up temporary audio file
|
41 |
except Exception as e:
|
42 |
raise gr.Error(f"Error generating audio: {str(e)}")
|
|
|
47 |
combined_audio.export(temp_audio.name, format="mp3")
|
48 |
temp_audio_path = temp_audio.name
|
49 |
|
50 |
+
return temp_audio_path
|
51 |
|
52 |
except Exception as e:
|
53 |
return None, f"An error occurred: {str(e)}"
|
54 |
|
55 |
+
# Briefing document summarization function
|
56 |
+
def summarize_document(file, url):
|
57 |
+
try:
|
58 |
+
if file and url:
|
59 |
+
return "Please provide either a PDF file or a URL, not both."
|
60 |
+
|
61 |
+
if file:
|
62 |
+
if not file.name.lower().endswith('.pdf'):
|
63 |
+
return "Please upload a PDF file."
|
64 |
+
|
65 |
+
pdf_reader = pypdf.PdfReader(file.name)
|
66 |
+
text = ""
|
67 |
+
for page in pdf_reader.pages:
|
68 |
+
text += page.extract_text()
|
69 |
+
elif url:
|
70 |
+
text = extract_text_from_url(url)
|
71 |
+
else:
|
72 |
+
return "Please provide either a PDF file or a URL."
|
73 |
+
|
74 |
+
summary = summarize_text(text) # Summarize the text using your utility function
|
75 |
+
return summary
|
76 |
+
|
77 |
+
except Exception as e:
|
78 |
+
return f"An error occurred: {str(e)}"
|
79 |
+
|
80 |
instructions = """
|
81 |
# Podify Studio
|
82 |
+
Welcome to the Podcast Generator and Briefing Document Summarizer!
|
83 |
+
Use this tool to generate custom podcast episodes or summarize documents.
|
84 |
"""
|
85 |
|
86 |
+
# Gradio interface
|
87 |
+
iface = gr.TabbedInterface(
|
88 |
+
[
|
89 |
+
gr.Interface(
|
90 |
+
fn=generate_podcast,
|
91 |
+
inputs=[
|
92 |
+
gr.File(label="Upload PDF file (optional)", file_types=[".pdf"]),
|
93 |
+
gr.Textbox(label="OR Enter URL"),
|
94 |
+
gr.Radio(["humorous", "casual", "formal"], label="Select podcast tone", value="casual"),
|
95 |
+
gr.Radio(["Short (1-2 min)", "Medium (3-5 min)"], label="Podcast length", value="Medium (3-5 min)")
|
96 |
+
],
|
97 |
+
outputs=gr.Audio(label="Generated Podcast"),
|
98 |
+
title="🎙️ Amuthvani: AI Podcast",
|
99 |
+
description="Generate podcasts using PDF documents or URLs.",
|
100 |
+
),
|
101 |
+
gr.Interface(
|
102 |
+
fn=summarize_document,
|
103 |
+
inputs=[
|
104 |
+
gr.File(label="Upload PDF file (optional)", file_types=[".pdf"]),
|
105 |
+
gr.Textbox(label="OR Enter URL")
|
106 |
+
],
|
107 |
+
outputs=gr.Textbox(label="Summarized Document"),
|
108 |
+
title="📄 Briefing Document Summarizer",
|
109 |
+
description="Summarize documents using AI. Provide a PDF or URL to get a brief summary.",
|
110 |
+
)
|
111 |
],
|
112 |
+
title="Amuthvani: AI Studio",
|
113 |
+
description=instructions
|
|
|
|
|
114 |
)
|
115 |
|
116 |
if __name__ == "__main__":
|