-
Notifications
You must be signed in to change notification settings - Fork 4
/
yt_summary.py
85 lines (69 loc) · 2.9 KB
/
yt_summary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import streamlit as st
from pytube import YouTube
from haystack.nodes import PromptNode, PromptModel
from haystack.nodes.audio import WhisperTranscriber
from haystack.pipelines import Pipeline
from model_add import LlamaCPPInvocationLayer
import time
st.set_page_config(
layout="wide"
)
def download_video(url):
yt = YouTube(url)
video = yt.streams.filter(abr='160kbps').last()
return video.download()
def initialize_model(full_path):
return PromptModel(
model_name_or_path=full_path,
invocation_layer_class=LlamaCPPInvocationLayer,
use_gpu=False,
max_length=512
)
def initialize_prompt_node(model):
summary_prompt = "deepset/summarization"
return PromptNode(model_name_or_path=model, default_prompt_template=summary_prompt, use_gpu=False)
def transcribe_audio(file_path, prompt_node):
whisper = WhisperTranscriber()
pipeline = Pipeline()
pipeline.add_node(component=whisper, name="whisper", inputs=["File"])
pipeline.add_node(component=prompt_node, name="prompt", inputs=["whisper"])
output = pipeline.run(file_paths=[file_path])
return output
def main():
# Set the title and background color
st.title("YouTube Video Summarizer 🎥")
st.markdown('<style>h1{color: orange; text-align: center;}</style>', unsafe_allow_html=True)
st.subheader('Built with the Llama 2 🦙, Haystack, Streamlit and ❤️')
st.markdown('<style>h3{color: pink; text-align: center;}</style>', unsafe_allow_html=True)
# Expander for app details
with st.expander("About the App"):
st.write("This app allows you to summarize while watching a YouTube video.")
st.write("Enter a YouTube URL in the input box below and click 'Submit' to start.")
# Input box for YouTube URL
youtube_url = st.text_input("Enter YouTube URL")
# Submit button
if st.button("Submit") and youtube_url:
start_time = time.time() # Start the timer
# Download video
file_path = download_video(youtube_url)
# Initialize model
full_path = "llama-2-7b-32k-instruct.Q4_K_S.gguf"
model = initialize_model(full_path)
prompt_node = prompt_node = initialize_prompt_node(model)
# Transcribe audio
output = transcribe_audio(file_path, prompt_node)
end_time = time.time() # End the timer
elapsed_time = end_time - start_time
# Display layout with 2 columns
col1, col2 = st.columns([1,1])
# Column 1: Video view
with col1:
st.video(youtube_url)
# Column 2: Summary View
with col2:
st.header("Summarization of YouTube Video")
st.write(output)
st.success(output["results"][0].split("\n\n[INST]")[0])
st.write(f"Time taken: {elapsed_time:.2f} seconds")
if __name__ == "__main__":
main()