Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +95 -0
- requirments.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import ssl
|
| 3 |
+
ssl._create_default_https_context = ssl._create_unverified_context
|
| 4 |
+
import glob
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
def vid_to_audio(url=None):
|
| 8 |
+
# importing packages
|
| 9 |
+
from pytube import YouTube
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
# url input from user
|
| 13 |
+
yt = YouTube(url)
|
| 14 |
+
|
| 15 |
+
# extract only audio
|
| 16 |
+
video = yt.streams.filter(only_audio=True).first()
|
| 17 |
+
|
| 18 |
+
# check for destination to save file
|
| 19 |
+
destination = '.'
|
| 20 |
+
|
| 21 |
+
# download the file
|
| 22 |
+
out_file = video.download(output_path=destination)
|
| 23 |
+
|
| 24 |
+
# save the file
|
| 25 |
+
base, ext = os.path.splitext(out_file)
|
| 26 |
+
new_file = base + '.mp3'
|
| 27 |
+
os.rename(out_file, new_file)
|
| 28 |
+
|
| 29 |
+
# result of success
|
| 30 |
+
print(yt.title + " has been successfully downloaded.")
|
| 31 |
+
|
| 32 |
+
return "OK"
|
| 33 |
+
|
| 34 |
+
#vid_to_text(url='https://youtu.be/FE5tva_o7ew?si=ztkKeO7qwcpC36AS')
|
| 35 |
+
|
| 36 |
+
def audio_to_text():
|
| 37 |
+
import torch
|
| 38 |
+
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 43 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 44 |
+
|
| 45 |
+
model_id = "openai/whisper-tiny"
|
| 46 |
+
|
| 47 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
| 48 |
+
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
|
| 49 |
+
)
|
| 50 |
+
model.to(device)
|
| 51 |
+
|
| 52 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
| 53 |
+
|
| 54 |
+
pipe = pipeline(
|
| 55 |
+
"automatic-speech-recognition",
|
| 56 |
+
model=model,
|
| 57 |
+
tokenizer=processor.tokenizer,
|
| 58 |
+
feature_extractor=processor.feature_extractor,
|
| 59 |
+
max_new_tokens=128,
|
| 60 |
+
chunk_length_s=30,
|
| 61 |
+
batch_size=16,
|
| 62 |
+
|
| 63 |
+
torch_dtype=torch_dtype,
|
| 64 |
+
device=device,
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
files = glob.glob('*.mp3')[0]
|
| 68 |
+
current_path = os.getcwd()
|
| 69 |
+
file_path = os.path.join(current_path,files)
|
| 70 |
+
result = pipe(file_path)
|
| 71 |
+
print(result["text"])
|
| 72 |
+
return result["text"]
|
| 73 |
+
audio_to_text()
|
| 74 |
+
|
| 75 |
+
def summarize():
|
| 76 |
+
transcript = audio_to_text()
|
| 77 |
+
from transformers import pipeline
|
| 78 |
+
|
| 79 |
+
summarizer = pipeline("summarization", model="philschmid/flan-t5-base-samsum")
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
#print(summarizer(transcript, do_sample=False))
|
| 83 |
+
|
| 84 |
+
return summarizer(transcript, do_sample=False)
|
| 85 |
+
|
| 86 |
+
yt_link = st.text_input("Enter the YouTube URL: ")
|
| 87 |
+
|
| 88 |
+
with st.button("Start Summarization"):
|
| 89 |
+
|
| 90 |
+
with st.status("Downloading the video..."):
|
| 91 |
+
vid_to_audio()
|
| 92 |
+
with st.status("Summarizing..."):
|
| 93 |
+
s = summarize()
|
| 94 |
+
st.write(s)
|
| 95 |
+
|
requirments.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pytube
|
| 2 |
+
streamlit
|
| 3 |
+
os_sys
|
| 4 |
+
transformers
|
| 5 |
+
ffmpeg
|
| 6 |
+
accelerate
|