Spaces:
Runtime error
Runtime error
Code enhancement
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ from process_media import MediaProcessor
|
|
3 |
import streamlit as st
|
4 |
from summarizer import BARTSummarizer
|
5 |
from Utils import fetch_article_text, get_text_from_youtube_url
|
|
|
6 |
|
7 |
st.markdown(
|
8 |
"""
|
@@ -26,7 +27,89 @@ with st.sidebar:
|
|
26 |
input_type = st.radio("", ["Text", "Media"], label_visibility = "hidden")
|
27 |
|
28 |
|
29 |
-
text_to_summarize =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
if input_type == "Text":
|
32 |
|
@@ -37,16 +120,12 @@ if input_type == "Text":
|
|
37 |
if text_type == "Raw Text":
|
38 |
text = st.text_area("Enter raw text here", height=240, max_chars=10000, placeholder="Enter a paragraph to summarize")
|
39 |
if text:
|
40 |
-
|
41 |
|
42 |
elif text_type == "URL":
|
43 |
url = st.text_input("Enter URL here", placeholder="Enter URL to an article, blog post, etc.")
|
44 |
if url:
|
45 |
-
|
46 |
-
if article_text:
|
47 |
-
st.markdown("#### Text from url:")
|
48 |
-
st.write(article_text)
|
49 |
-
text_to_summarize = article_text
|
50 |
else:
|
51 |
## TODO: Add file upload option
|
52 |
pass
|
@@ -60,53 +139,30 @@ elif input_type == "Media":
|
|
60 |
if media_type == "Audio file":
|
61 |
audio_file = st.file_uploader("Upload an audio file", type=["mp3", "wav"], label_visibility="visible")
|
62 |
if audio_file is not None:
|
63 |
-
|
64 |
-
|
65 |
-
wav_bytes = None
|
66 |
-
media_processor = MediaProcessor()
|
67 |
-
if audio_file.type == "audio/mpeg":
|
68 |
-
wav_bytes = media_processor.get_wav_from_audio(audio_file.read())
|
69 |
-
else:
|
70 |
-
wav_bytes = audio_file.read()
|
71 |
-
text = media_processor.process_audio(wav_bytes)
|
72 |
-
st.markdown("#### Text from audio:")
|
73 |
-
st.write(text)
|
74 |
elif media_type == "Video file":
|
75 |
video_file = st.file_uploader("Upload a video file", type=["mp4"], label_visibility="visible")
|
76 |
if video_file is not None:
|
77 |
-
|
78 |
-
|
79 |
-
text = media_processor.process_video(video_file.read())
|
80 |
-
st.markdown("#### Text from video:")
|
81 |
-
st.write(text)
|
82 |
else:
|
83 |
youtube_url = st.text_input("Enter YouTube URL here", placeholder="Enter URL to an YouTube video", label_visibility="visible")
|
84 |
if youtube_url:
|
85 |
-
|
86 |
try:
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
90 |
except:
|
91 |
st.error("Unable to fetch text from video. Please try a different video.")
|
92 |
text_to_summarize = None
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
with st.spinner("Summarizing..."):
|
99 |
-
# time.sleep(2)
|
100 |
-
# st.write(text_to_summarize)
|
101 |
-
summarizer = BARTSummarizer()
|
102 |
-
summary = summarizer.chunk_summarize(text_to_summarize)
|
103 |
-
st.markdown("#### Summary:")
|
104 |
-
st.write(summary)
|
105 |
-
elif auto_chapters_summary:
|
106 |
-
with st.spinner("Summarizing..."):
|
107 |
-
# time.sleep(2)
|
108 |
-
# st.write(text_to_summarize)
|
109 |
-
summarizer = BARTSummarizer()
|
110 |
-
summary = summarizer.auto_chapters_summarize(text_to_summarize)
|
111 |
-
st.markdown("#### Summary:")
|
112 |
-
st.write(summary)
|
|
|
3 |
import streamlit as st
|
4 |
from summarizer import BARTSummarizer
|
5 |
from Utils import fetch_article_text, get_text_from_youtube_url
|
6 |
+
from pytube import YouTube
|
7 |
|
8 |
st.markdown(
|
9 |
"""
|
|
|
27 |
input_type = st.radio("", ["Text", "Media"], label_visibility = "hidden")
|
28 |
|
29 |
|
30 |
+
def generate_summary(overall_summary, auto_chapters_summarize, text_to_summarize, show_text = False):
|
31 |
+
if overall_summary:
|
32 |
+
with st.spinner("Generating overall summary..."):
|
33 |
+
summarizer = BARTSummarizer()
|
34 |
+
summary = summarizer.chunk_summarize(text_to_summarize)
|
35 |
+
elif auto_chapters_summarize:
|
36 |
+
with st.spinner("Generating auto chapters summary..."):
|
37 |
+
summarizer = BARTSummarizer()
|
38 |
+
summary = summarizer.auto_chapters_summarize(text_to_summarize)
|
39 |
+
if show_text:
|
40 |
+
st.markdown("#### Text before summarization:")
|
41 |
+
st.markdown('<div style="height: 500px; overflow: auto; margin-bottom: 20px;">' + text_to_summarize + '</div>', unsafe_allow_html=True)
|
42 |
+
st.markdown("#### Summary:")
|
43 |
+
st.write(summary)
|
44 |
+
|
45 |
+
def show_buttons(type, data = None):
|
46 |
+
text_to_summarize = None
|
47 |
+
overall_summary = st.button("Overall summary")
|
48 |
+
auto_chapters_summary = st.button("Auto Chapters summary...")
|
49 |
+
|
50 |
+
if type == "raw_text" or type == "url" and data:
|
51 |
+
if overall_summary or auto_chapters_summary:
|
52 |
+
if type == "raw_text":
|
53 |
+
text_to_summarize = data
|
54 |
+
generate_summary(overall_summary, auto_chapters_summary, text_to_summarize)
|
55 |
+
elif type == "url":
|
56 |
+
text_to_summarize = fetch_article_text(data)
|
57 |
+
generate_summary(overall_summary, auto_chapters_summary, text_to_summarize, show_text = True)
|
58 |
+
|
59 |
+
elif type == "audio_file" and data:
|
60 |
+
if overall_summary or auto_chapters_summary:
|
61 |
+
with st.spinner("Fetching text from audio..."):
|
62 |
+
text_to_summarize = process_audio_file(data)
|
63 |
+
generate_summary(overall_summary, auto_chapters_summary, text_to_summarize, show_text = True)
|
64 |
+
|
65 |
+
elif type == "video_file" and data:
|
66 |
+
if overall_summary or auto_chapters_summary:
|
67 |
+
with st.spinner("Fetching text from video..."):
|
68 |
+
text_to_summarize = process_video_file(data)
|
69 |
+
generate_summary(overall_summary, auto_chapters_summary, text_to_summarize, show_text = True)
|
70 |
+
|
71 |
+
elif type == "youtube_url" and data:
|
72 |
+
if overall_summary or auto_chapters_summary:
|
73 |
+
try:
|
74 |
+
try:
|
75 |
+
with st.spinner("Fetching text from video..."):
|
76 |
+
text_to_summarize = get_text_from_youtube_url(data)
|
77 |
+
except:
|
78 |
+
with st.spinner("Captions not available. Downloading video..."):
|
79 |
+
text_to_summarize = get_yt_video(data)
|
80 |
+
except:
|
81 |
+
st.error("Unable to fetch text from video. Please try a different video.")
|
82 |
+
text_to_summarize = None
|
83 |
+
if text_to_summarize:
|
84 |
+
generate_summary(overall_summary, auto_chapters_summarize, text_to_summarize, show_text = True)
|
85 |
+
elif type == 'document':
|
86 |
+
# TODO: Add document summary
|
87 |
+
pass
|
88 |
+
|
89 |
+
|
90 |
+
def process_video_file(video_file):
|
91 |
+
media_processor = MediaProcessor()
|
92 |
+
text = media_processor.process_video(video_file.read())
|
93 |
+
return text
|
94 |
+
|
95 |
+
def process_audio_file(audio_file):
|
96 |
+
media_processor = MediaProcessor()
|
97 |
+
if audio_file.type == "audio/mpeg":
|
98 |
+
wav_bytes = media_processor.get_wav_from_audio(audio_file.read())
|
99 |
+
else:
|
100 |
+
wav_bytes = audio_file.read()
|
101 |
+
text = media_processor.process_audio(wav_bytes)
|
102 |
+
return text
|
103 |
+
|
104 |
+
def get_yt_video(youtube_url):
|
105 |
+
yt = YouTube(youtube_url)
|
106 |
+
video_stream = yt.streams.first()
|
107 |
+
video_buffer = io.BytesIO()
|
108 |
+
video_stream.stream_to_buffer(video_buffer)
|
109 |
+
media_processor = MediaProcessor()
|
110 |
+
text = media_processor.process_video(video_buffer.getvalue())
|
111 |
+
return text
|
112 |
+
|
113 |
|
114 |
if input_type == "Text":
|
115 |
|
|
|
120 |
if text_type == "Raw Text":
|
121 |
text = st.text_area("Enter raw text here", height=240, max_chars=10000, placeholder="Enter a paragraph to summarize")
|
122 |
if text:
|
123 |
+
show_buttons("raw_text", text)
|
124 |
|
125 |
elif text_type == "URL":
|
126 |
url = st.text_input("Enter URL here", placeholder="Enter URL to an article, blog post, etc.")
|
127 |
if url:
|
128 |
+
show_buttons("url", url)
|
|
|
|
|
|
|
|
|
129 |
else:
|
130 |
## TODO: Add file upload option
|
131 |
pass
|
|
|
139 |
if media_type == "Audio file":
|
140 |
audio_file = st.file_uploader("Upload an audio file", type=["mp3", "wav"], label_visibility="visible")
|
141 |
if audio_file is not None:
|
142 |
+
show_buttons("audio_file", audio_file)
|
143 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
elif media_type == "Video file":
|
145 |
video_file = st.file_uploader("Upload a video file", type=["mp4"], label_visibility="visible")
|
146 |
if video_file is not None:
|
147 |
+
show_buttons("video_file", video_file)
|
148 |
+
|
|
|
|
|
|
|
149 |
else:
|
150 |
youtube_url = st.text_input("Enter YouTube URL here", placeholder="Enter URL to an YouTube video", label_visibility="visible")
|
151 |
if youtube_url:
|
152 |
+
if not get_text(url):
|
153 |
try:
|
154 |
+
try:
|
155 |
+
with st.spinner("Fetching text from video..."):
|
156 |
+
text_to_summarize = get_text_from_youtube_url(youtube_url)
|
157 |
+
except:
|
158 |
+
with st.spinner("Captions not available. Downloading video..."):
|
159 |
+
get_yt_video(youtube_url)
|
160 |
+
text_to_summarize = get_text(url)
|
161 |
except:
|
162 |
st.error("Unable to fetch text from video. Please try a different video.")
|
163 |
text_to_summarize = None
|
164 |
+
else:
|
165 |
+
text_to_summarize = get_text(url)
|
166 |
+
st.markdown("#### Text from video:")
|
167 |
+
|
168 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|