Spaces:
Runtime error
Runtime error
| # 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) |