File size: 7,808 Bytes
d3319a1
 
 
 
 
abc4408
d3319a1
 
 
abc4408
1cd73aa
abc4408
 
 
 
 
 
43c84e4
abc4408
d3319a1
 
abc4408
 
 
 
 
91b29f9
440327e
91b29f9
d3319a1
abc4408
d3319a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abc4408
d3319a1
 
 
 
 
 
 
abc4408
d3319a1
 
 
 
 
 
 
abc4408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3319a1
abc4408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3319a1
 
 
 
 
 
 
 
abc4408
d3319a1
abc4408
d3319a1
 
abc4408
 
d3319a1
abc4408
d3319a1
 
 
 
 
abc4408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import streamlit as st
import validators
import os
from dotenv import load_dotenv
from langchain_groq import ChatGroq
from langchain_core.prompts import PromptTemplate,ChatPromptTemplate,MessagesPlaceholder
from langchain.chains.summarize import load_summarize_chain
from langchain_community.document_loaders import YoutubeLoader,UnstructuredURLLoader
from PIL import Image
from langchain_core.output_parsers import StrOutputParser
from pytube.innertube import InnerTube
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_community.utilities import DuckDuckGoSearchAPIWrapper
from langchain.agents import create_openai_tools_agent,AgentExecutor


load_dotenv()

# api keys
groq_api_key = os.getenv('GROQ_API_KEY')

# tool
api_wrapper = DuckDuckGoSearchAPIWrapper(max_results=3,safesearch='none')
tool = DuckDuckGoSearchRun(api_wrapper=api_wrapper)

# app 
img1 = Image.open('youtubeLogo.jpg')

img2 = Image.open('web.jpeg')

st.set_page_config(page_title='Summarizer')

col1,col2,col3 = st.columns([1,1,3])

with col1:
    st.image(img1,use_column_width=True)

with col2:
    st.image(img2,use_column_width=True)

with col3:
    st.title('Summarize Youtube Videos / Web Page')


llm = ChatGroq(model='Gemma2-9b-It',groq_api_key=groq_api_key)


system_prompt = '''

Summarize the content from given {text} in best possible way.

'''

prompt = PromptTemplate(
    input_variables=['text'],
    template=system_prompt
)

system_prompt_n = '''

You are an advanced AI summarization tool .Summarize the {text} in the best possible way.

Analyze the content, focusing on the following criteria:

1.Main Ideas: Identify the primary arguments or messages conveyed in the content.

2.Supporting Details: Highlight important examples, statistics, or pieces of evidence that reinforce the main ideas.

3.Structure: Maintain the original flow of the content, summarizing it in a coherent manner that mirrors the order of presented information.

4.Length: Produce a summary that is concise.

5.Clarity and Readability: Ensure the summary is written in clear, simple language that is easy to understand, avoiding difficult words unless necessary.

6.Contextual Relevance: Tailor the summary based on the intended audience, making sure it is appropriate for their level of understanding and interest.



'''

final_prompt = PromptTemplate(
    input_variables=['text'],
    template=system_prompt_n
)

lyrics_prompt = '''

You're a skilled data retrieval assistant with expertise in extracting and processing information from various online sources, 

especially youtube videos. Your focus is on accurately identifying and presenting song lyrics as well as the 

artist of the song,singer of the song ,the lable company and the song writer from {text}, 

ensuring clarity and precision in the output.Your task is to fetch the lyrics and singer,artist,label and song writer from the {text} 

generated from a YouTube link of a music video.Don't provide lyrics in paragraph form, provide line by line with proper spacing.



Keep in mind that you should provide the lyrics in the {language} without any additional commentary or unrelated information.

'''

lyrics_template = PromptTemplate(
    input_variables=['text','language'],
    template=lyrics_prompt
)

tool_sys_prompt = '''

you are helpful assistant capable of providing full lyrics in poem format as well as singer,artist,label 

and writer of the {song} in {language}.

'''

# Tools and agent
tool_template = ChatPromptTemplate([
    ('system',tool_sys_prompt),
    MessagesPlaceholder('chat_history',optional=True),
    ('user','{song}'),
    ('user','{language}'),
    MessagesPlaceholder('agent_scratchpad')
])

agent = create_openai_tools_agent(llm=llm,tools=[tool],prompt=tool_template)

executor = AgentExecutor(agent=agent,tools=[tool],verbose=False)

# url loader and text extractor function
def loadAndExtract():
    if 'youtube.com' in URL or 'youtu.be' in URL:
        loader = YoutubeLoader.from_youtube_url(URL,add_video_info=True,language=['en-IN','hi','en'],)
    else:
        loader = UnstructuredURLLoader([URL],ssl_verify=False,mode='single',
                                    headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 BingBot/2.0'})

    txt = loader.load()

    return txt

# main app content

URL = st.text_input(label=':blue[**Paste URL here**]',placeholder='https://www.youtube.com/')

option = [':orange[**Select**]',':orange[**English**]',':orange[**Hindi**]']

if "button_pressed" not in st.session_state:
    st.session_state.button_pressed = None

if st.sidebar.button('Previous'):
    st.session_state.button_pressed = None                   # Reset state when pressing 'Previous'
    st.rerun()

if st.session_state.button_pressed != 2:
    if st.button('Summarize the Video/Web-page'):
        st.session_state.button_pressed = 1

if st.session_state.button_pressed != 1:
    if st.button('Provide Lyrics of the song'):
        st.session_state.button_pressed = 2
    selected_option = st.radio(label=':blue[**Select any one language for lyrics**]',options=option)

if st.session_state.button_pressed == 1:    
    if not URL:
        st.info('Please provide the url')
    
    elif not validators.url(URL):
        st.warning('Please provide the correct url')

    else:
        try:
            with st.spinner(':pink[summarizing]'):

                txt = loadAndExtract()

                chain = load_summarize_chain(llm,chain_type='map_reduce',
                                            map_prompt=prompt,
                                            combine_prompt=final_prompt)
                
                response = chain.run(txt)

                st.success(response)

        except Exception as e:
            st.exception(f'Exception : {e}')


elif st.session_state.button_pressed == 2:
    
    if not URL:
        st.info('Please provide the url')
    
    elif not validators.url(URL):
        st.warning('Please provide the correct url')

    else:
        try:
            if 'youtube.com' in URL or 'youtu.be' in URL:
                loader = YoutubeLoader.from_youtube_url(URL,add_video_info=True,language=['en-IN','hi','en'],)
            
                txt = loader.load()

                if txt == []:
                    user_input = st.text_input(label='Plz provide name of the song')
                    if user_input:
                        with st.spinner(':pink[lyrics on the way]'):
                            if option.index(selected_option) == 1:
                                language = 'english'
                            elif option.index(selected_option) == 2:
                                language = 'hindi'
                        
                        res = executor.invoke({'song':user_input,'language':language})

                        st.success(res)

                else:
                    chain = lyrics_template|llm|StrOutputParser()

                    with st.spinner(':pink[lyrics on the way]'):
                        if option.index(selected_option) == 1:
                            language = 'english'
                            res = chain.invoke({'text':txt,'language':language})
                            st.success(res)
                        elif option.index(selected_option) == 2:
                            language = 'hindi'
                            res = chain.invoke({'text':txt,'language':language})
                            st.success(res)
        
        except Exception as e:
            st.exception(f'Exception {e}')