|
import streamlit as st |
|
import ssl |
|
ssl._create_default_https_context = ssl._create_unverified_context |
|
import glob |
|
import os |
|
|
|
def vid_to_audio(url=None): |
|
|
|
from pytube import YouTube |
|
import os |
|
|
|
|
|
yt = YouTube(url) |
|
|
|
|
|
video = yt.streams.filter(only_audio=True).first() |
|
|
|
|
|
destination = '.' |
|
|
|
|
|
out_file = video.download(output_path=destination) |
|
|
|
|
|
base, ext = os.path.splitext(out_file) |
|
new_file = base + '.mp3' |
|
os.rename(out_file, new_file) |
|
|
|
|
|
print(yt.title + " has been successfully downloaded.") |
|
|
|
return "OK" |
|
|
|
|
|
|
|
def audio_to_text(): |
|
import torch |
|
|
|
|
|
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline |
|
|
|
|
|
|
|
device = "cuda:0" if torch.cuda.is_available() else "cpu" |
|
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32 |
|
|
|
model_id = "openai/whisper-tiny" |
|
|
|
model = AutoModelForSpeechSeq2Seq.from_pretrained( |
|
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True |
|
) |
|
model.to(device) |
|
|
|
processor = AutoProcessor.from_pretrained(model_id) |
|
|
|
pipe = pipeline( |
|
"automatic-speech-recognition", |
|
model=model, |
|
tokenizer=processor.tokenizer, |
|
feature_extractor=processor.feature_extractor, |
|
max_new_tokens=128, |
|
chunk_length_s=30, |
|
batch_size=16, |
|
|
|
torch_dtype=torch_dtype, |
|
device=device, |
|
) |
|
|
|
|
|
files = os.listdir() |
|
|
|
files = os.listdir() |
|
|
|
|
|
|
|
results = [] |
|
|
|
|
|
for i in files: |
|
if ".mp3" in i: |
|
|
|
file_path = os.path.join(os.getcwd(), i) |
|
|
|
|
|
st.write("Current Directory:", os.getcwd()) |
|
st.write("File Path:", file_path) |
|
|
|
|
|
result = pipe(file_path) |
|
|
|
return result['text'] |
|
|
|
|
|
|
|
def summarize(): |
|
transcript = audio_to_text() |
|
len_trans = len(transcript) |
|
|
|
chunks = int(len_trans/512) |
|
from transformers import pipeline |
|
|
|
summarizer = pipeline("summarization", model="snrspeaks/t5-one-line-summary") |
|
|
|
|
|
|
|
cutoff = 512 |
|
final_output = '' |
|
""" |
|
for i in range(chunks): |
|
print(i) |
|
if i == 0: |
|
tran_text = transcript[:512] |
|
inter_output = summarizer(tran_text, do_sample=False)[0]['summary_text'] |
|
final_output += inter_output |
|
final_output += ' ' |
|
|
|
else: |
|
#end_slice = cutoff + cutoff |
|
tran_text = transcript[cutoff:cutoff + 2] |
|
inter_output = summarizer(tran_text, do_sample=False)[0]['summary_text'] |
|
final_output += inter_output |
|
final_output += ' ' |
|
cutoff += cutoff |
|
""" |
|
final_output = summarizer(tran_text, do_sample=False) |
|
return final_output |
|
|
|
yt_link = st.text_input("Enter the YouTube URL: ") |
|
|
|
if st.button("Start Summarization"): |
|
|
|
with st.status("Downloading the video..."): |
|
vid_to_audio(url=yt_link) |
|
with st.status("Summarizing..."): |
|
s = summarize() |
|
st.write(s) |
|
|