File size: 2,652 Bytes
2396200
f4b5326
 
a5ec086
c830615
 
97ad7fb
 
 
 
287e480
 
 
3e05df8
c830615
f4b5326
287e480
2396200
c5b4835
2396200
 
 
 
 
 
7e7d4b3
2396200
287e480
f4b5326
287e480
f4b5326
287e480
 
 
f4b5326
287e480
7e7d4b3
287e480
 
 
 
f4b5326
287e480
 
3e05df8
f4b5326
287e480
f4b5326
 
287e480
f4b5326
 
 
 
 
287e480
f4b5326
 
 
287e480
f4b5326
287e480
97ad7fb
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
import os
import validators
import streamlit as st

st.set_page_config(page_title="LangChain: Summarize Text From YT or Website", page_icon="🦜")

from langchain.prompts import PromptTemplate
from langchain_groq import ChatGroq
from langchain.chains.summarize import load_summarize_chain
from langchain_community.document_loaders import YoutubeLoader, UnstructuredURLLoader

st.title("Last-Minute??? No Worries! Summarize Text From YT or Website :)")
st.subheader('Summarize URL')
default_api_key = os.getenv("default_api_key", "")

# Sidebar Inputs
with st.sidebar:
    # Option to input default API key
    default_api_key_option = st.checkbox("Use Owner's API Key", value=False)
    
    if default_api_key_option:
        groq_api_key = default_api_key
    else:
        groq_api_key = st.text_input("Groq API Key", value="", type="password", help="Enter your API key")

# Additional user input text
st.sidebar.text("You can also input your own API key above.")

generic_url = st.text_input("URL", label_visibility="collapsed")

prompt_template = """
Provide a summary of the following content in 300 words:
Content:{text}
"""
prompt = PromptTemplate(template=prompt_template, input_variables=["text"])

# Summarization
if st.button("Summarize the Content from YT or Website"):
    if not groq_api_key.strip() or not generic_url.strip():
        st.error("Please provide the information to get started")
    elif not validators.url(generic_url):
        st.error("Please enter a valid Url. It can be a YouTube video URL or website URL")
    else:
        try:
            llm = ChatGroq(model="Gemma-7b-It", groq_api_key=groq_api_key)
            with st.spinner("Fetching content..."):
                if "youtube.com" in generic_url:
                    # Fetch video content
                    loader = YoutubeLoader.from_youtube_url(generic_url, add_video_info=True)
                else:
                    # Fetch content from a regular website
                    loader = UnstructuredURLLoader(urls=[generic_url], ssl_verify=False,
                                                   headers={"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 13_5_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"})
                
                docs = loader.load()  # Load content from the URL

                # Summarization chain setup
                chain = load_summarize_chain(llm, chain_type="stuff", prompt=prompt)
                output_summary = chain.run(docs)

                st.success(output_summary)  # Display summarized output
        except Exception as e:
            st.exception(f"Exception: {e}")