File size: 2,006 Bytes
4b73a30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Importing Libraries
from langchain_community.document_loaders import YoutubeLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_huggingface import HuggingFaceEmbeddings
from langchain.vectorstores import Chroma
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
import streamlit as st

st.title ("Video Summarizer")

# Loading Video
input_video = st.chat_input("Enter the video url: ")
if input_video:
  with st.chat_message("user"):
    st.write(input_video)
  loader = YoutubeLoader.from_youtube_url(
    input_video, add_video_info=True
  )
  docs = loader.load()

  # Splitting Video
  r_splitter = RecursiveCharacterTextSplitter(
    chunk_size=400,
    chunk_overlap= 20
  )
  splits = r_splitter.split_documents(docs)

  # Embedding Video
  embedding = HuggingFaceEmbeddings()

  # Vector Storing Video
  vectordb = Chroma.from_documents(
    documents=splits,
    embedding=embedding,
  )

  # Composing Chain
  llm = ChatGoogleGenerativeAI(model = "gemini-pro", temperature= 0, google_api_key="AIzaSyAxmFrjhr4NRY2eZWwFl3xNVt_TM1aBDrA")
  template_string = """



  You are an assistant for summarizing the content in the video.

  Use the pieces of retrieved context to summarize the video.

  Summarize the text in the form of notes under headings.



  Context: {context}



  Question: {question}



  """

  prompt_template = ChatPromptTemplate.from_template(template_string)

  # Chain for Video
  retriever = vectordb.as_retriever()

  rag_chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | prompt_template
    | llm
    | StrOutputParser()
  )

  # Input for Video
  result = rag_chain.invoke("Summarize the video in the form of notes.")
  with st.chat_message("assistant"):
    st.write(result)