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
7 |
+
https://colab.research.google.com/drive/1rbZ98r1Z_IM0Z3VDuNQObxpuZf5KUgmL
|
8 |
+
|
9 |
+
### Initialization
|
10 |
+
"""
|
11 |
+
|
12 |
+
import os
|
13 |
+
save_dir= os.path.join('./','docs')
|
14 |
+
if not os.path.exists(save_dir):
|
15 |
+
os.mkdir(save_dir)
|
16 |
+
|
17 |
+
transcription_model = "openai/whisper-base"
|
18 |
+
llm_model = "gmurro/bart-large-finetuned-filtered-spotify-podcast-summ"
|
19 |
+
|
20 |
+
!pip install -U -q pytube transformers
|
21 |
+
|
22 |
+
!pip -q install gradio==3.45.0
|
23 |
+
|
24 |
+
import pandas as pd
|
25 |
+
import numpy as np
|
26 |
+
import pytube
|
27 |
+
from pytube import YouTube
|
28 |
+
import transformers
|
29 |
+
from transformers import pipeline
|
30 |
+
import torch
|
31 |
+
|
32 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
33 |
+
|
34 |
+
"""### Define how to get transcript of the YT video"""
|
35 |
+
|
36 |
+
def get_transcript(url):
|
37 |
+
yt_video = YouTube(str(url))
|
38 |
+
yt_audio = yt_video.streams.filter(only_audio=True, file_extension='mp4').first() # get 1st available audio stream
|
39 |
+
out_file = yt_audio.download(filename="audio.mp4", output_path = save_dir)
|
40 |
+
|
41 |
+
asr = pipeline("automatic-speech-recognition", model=transcription_model, device=device)
|
42 |
+
|
43 |
+
import librosa
|
44 |
+
speech_array, sampling_rate = librosa.load(out_file, sr=16000) # getting audio file array
|
45 |
+
|
46 |
+
audio_text = asr(
|
47 |
+
speech_array,
|
48 |
+
max_new_tokens=256,
|
49 |
+
generate_kwargs={"task": "transcribe"},
|
50 |
+
chunk_length_s=30,
|
51 |
+
batch_size=8) # calling whisper model
|
52 |
+
|
53 |
+
del(asr)
|
54 |
+
torch.cuda.empty_cache() #deleting cache
|
55 |
+
|
56 |
+
return audio_text['text']
|
57 |
+
|
58 |
+
"""### Define functions to generate summary"""
|
59 |
+
|
60 |
+
def clean_sent(sent_list):
|
61 |
+
new_sent_list = [sent_list[0]]
|
62 |
+
for i in range(len(sent_list)):
|
63 |
+
if sent_list[i] != new_sent_list[-1]: new_sent_list.append(sent_list[i])
|
64 |
+
return new_sent_list
|
65 |
+
|
66 |
+
import nltk
|
67 |
+
nltk.download('punkt')
|
68 |
+
|
69 |
+
def get_chunks (audio_text, sent_overlap, max_token, tokenizer):
|
70 |
+
# pre-processing text
|
71 |
+
sentences = nltk.tokenize.sent_tokenize(audio_text)
|
72 |
+
sentences = clean_sent(sentences)
|
73 |
+
|
74 |
+
first_sentence = 0
|
75 |
+
last_sentence = 0
|
76 |
+
chunks=[]
|
77 |
+
while last_sentence <= len(sentences) - 1:
|
78 |
+
last_sentence = first_sentence
|
79 |
+
chunk_parts = []
|
80 |
+
chunk_size = 0
|
81 |
+
for sentence in sentences[first_sentence:]:
|
82 |
+
sentence_sz = len(tokenizer.tokenize(sentence))
|
83 |
+
if chunk_size + sentence_sz > max_token:
|
84 |
+
break
|
85 |
+
|
86 |
+
chunk_parts.append(sentence)
|
87 |
+
chunk_size += sentence_sz
|
88 |
+
last_sentence += 1
|
89 |
+
|
90 |
+
chunks.append(" ".join(chunk_parts))
|
91 |
+
first_sentence = last_sentence - sent_overlap
|
92 |
+
return chunks
|
93 |
+
|
94 |
+
"""### Define how to get summary of the transcript"""
|
95 |
+
|
96 |
+
def get_summary(audio_text):
|
97 |
+
import re
|
98 |
+
audio_text = re.sub(r'\b(\w+) \1\b', r'\1', audio_text, flags=re.IGNORECASE) # cleaning text
|
99 |
+
|
100 |
+
from transformers import AutoTokenizer
|
101 |
+
tokenizer = AutoTokenizer.from_pretrained(llm_model) # set tockenizer
|
102 |
+
|
103 |
+
from transformers import pipeline
|
104 |
+
summarizer = pipeline("summarization", model=llm_model) # set summarizer
|
105 |
+
|
106 |
+
model_max_tokens = tokenizer.model_max_length # get max tockens model can process
|
107 |
+
text_tokens = len(tokenizer.tokenize(audio_text)) # get number of tockens in audio text
|
108 |
+
|
109 |
+
def get_map_summary(chunk_text, summarizer):
|
110 |
+
max_token = model_max_tokens - 2 #protect for "" before and after the text
|
111 |
+
sent_overlap = 3 #overlapping sentences between 2 chunks
|
112 |
+
sent_chunks = get_chunks(audio_text = chunk_text,sent_overlap = sent_overlap,max_token = max_token, tokenizer = tokenizer) # get chunks
|
113 |
+
chunk_summary_list = summarizer(sent_chunks,min_length=50, max_length=200, batch_size=8) # get summary per chunk
|
114 |
+
|
115 |
+
grouped_summary = ""
|
116 |
+
for c in chunk_summary_list: grouped_summary += c['summary_text'] + " "
|
117 |
+
|
118 |
+
|
119 |
+
return grouped_summary
|
120 |
+
|
121 |
+
# check text requires map-reduce stategy
|
122 |
+
|
123 |
+
map_text = audio_text
|
124 |
+
long_summary = ""
|
125 |
+
|
126 |
+
while text_tokens > model_max_tokens:
|
127 |
+
map_summary = get_map_summary(chunk_text=map_text, summarizer=summarizer)
|
128 |
+
text_tokens = len(tokenizer.tokenize(map_summary))
|
129 |
+
long_summary = map_summary
|
130 |
+
map_text = map_summary
|
131 |
+
|
132 |
+
# else deploy reduce method
|
133 |
+
else:
|
134 |
+
max_token = round(text_tokens*0.3) # 1/3rd reduction
|
135 |
+
final_summary = summarizer(map_text,min_length=35, max_length=max_token)
|
136 |
+
final_summary = final_summary[0]["summary_text"]
|
137 |
+
|
138 |
+
if long_summary == "": long_summary = "The video is too short to produce a descriptive summary"
|
139 |
+
|
140 |
+
del(tokenizer, summarizer)
|
141 |
+
torch.cuda.empty_cache() #deleting cache
|
142 |
+
|
143 |
+
return final_summary, long_summary
|
144 |
+
|
145 |
+
|
146 |
+
"""### Defining Gradio App"""
|
147 |
+
|
148 |
+
import gradio as gr
|
149 |
+
|
150 |
+
import pytube
|
151 |
+
from pytube import YouTube
|
152 |
+
|
153 |
+
def get_youtube_title(url):
|
154 |
+
yt = YouTube(str(url))
|
155 |
+
return yt.title
|
156 |
+
|
157 |
+
def get_video(url):
|
158 |
+
vid_id = pytube.extract.video_id(url)
|
159 |
+
embed_html = '<iframe width="100%" height="315" src="https://www.youtube.com/embed/{}" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>'.format(vid_id)
|
160 |
+
return embed_html
|
161 |
+
|
162 |
+
def summarize_youtube_video(url):
|
163 |
+
print("URL:",url)
|
164 |
+
text = get_transcript(url)
|
165 |
+
print("Transcript:",text[:500])
|
166 |
+
short_summary, long_summary = get_summary(text)
|
167 |
+
print("Short Summary:",short_summary)
|
168 |
+
print("Long Summary:",long_summary)
|
169 |
+
return text, short_summary, long_summary
|
170 |
+
|
171 |
+
html = '<iframe width="100%" height="315" src="https://www.youtube.com/embed/" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>'
|
172 |
+
|
173 |
+
# Defining the structure of the UI
|
174 |
+
with gr.Blocks() as demo:
|
175 |
+
with gr.Row():
|
176 |
+
gr.Markdown("# Summarize a Long YouTube Video")
|
177 |
+
|
178 |
+
with gr.Row():
|
179 |
+
with gr.Column(scale=4):
|
180 |
+
url = gr.Textbox(label="Enter YouTube video link here:",placeholder="https://www.youtube.com/watch?v=")
|
181 |
+
with gr.Column(scale=1):
|
182 |
+
sum_btn = gr.Button("Summarize!")
|
183 |
+
|
184 |
+
gr.Markdown("# Results")
|
185 |
+
|
186 |
+
title = gr.Textbox(label="Video Title",placeholder="title...")
|
187 |
+
|
188 |
+
with gr.Row():
|
189 |
+
with gr.Column(scale=4):
|
190 |
+
video = gr.HTML(html,scale=1)
|
191 |
+
with gr.Column():
|
192 |
+
with gr.Row():
|
193 |
+
short_summary = gr.Textbox(label="Gist",placeholder="short summary...",scale=1)
|
194 |
+
with gr.Row():
|
195 |
+
long_summary = gr.Textbox(label="Summary",placeholder="long summary...",scale=2)
|
196 |
+
|
197 |
+
|
198 |
+
with gr.Row():
|
199 |
+
with gr.Group():
|
200 |
+
text = gr.Textbox(label="Full Transcript",placeholder="transcript...",show_label=True)
|
201 |
+
|
202 |
+
with gr.Accordion("Credits and Notes",open=False):
|
203 |
+
gr.Markdown("""
|
204 |
+
1. Transcipt is generated by openai/whisper-base model by downloading YouTube video.\n
|
205 |
+
2. Summary is generated by gmurro/bart-large-finetuned-filtered-spotify-podcast-summ.\n
|
206 |
+
3. The model is possible because of Hugging Face transformers.\n
|
207 |
+
""")
|
208 |
+
|
209 |
+
# Defining the functions to call on clicking the button
|
210 |
+
sum_btn.click(fn=get_youtube_title, inputs=url, outputs=title, api_name="get_youtube_title", queue=False)
|
211 |
+
sum_btn.click(fn=summarize_youtube_video, inputs=url, outputs=[text, short_summary, long_summary], api_name="summarize_youtube_video", queue=True)
|
212 |
+
sum_btn.click(fn=get_video, inputs=url, outputs=video, api_name="get_youtube_video", queue=False)
|
213 |
+
|
214 |
+
demo.queue()
|
215 |
+
demo.launch()
|