File size: 3,864 Bytes
283ba3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from dotenv import load_dotenv

load_dotenv() ##load all the nevironment variables
import os
import google.generativeai as genai

from youtube_transcript_api import YouTubeTranscriptApi

genai.configure(api_key="AIzaSyBjEeTXoxrkk3eFc_o_CAfHMIfgWglPrlU")

prompt="""You are Yotube video summarizer. You will be taking the transcript text

and summarizing the entire video and providing the important summary

within 200 words. Please provide the summary of the text given here:  """

## getting the summary based on Prompt from Google Gemini Pro
def generate_gemini_content(transcript_text,prompt):

    model=genai.GenerativeModel("gemini-pro")
    response=model.generate_content(prompt+transcript_text)
    return response.text

st.title("YouTube Video Summarizer")
youtube_link = st.text_input("Enter YouTube Video Link:")

if youtube_link:
    try:
        video_id = youtube_link.split("=")[1]
        # print(video_id)
        st.image(f"http://img.youtube.com/vi/{video_id}/0.jpg", use_container_width=True)

        # Fetch subtitles
        transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
        data = ""
        for transcript in transcript_list:
            if transcript.is_translatable:
                subtitles = transcript.translate('en').fetch()
                data += " ".join([entry['text'] for entry in subtitles])

            if data:
                st.subheader("Extracted Subtitles")
                st.write(data)

                if st.button("Summarize"):
                    with st.spinner("Summarizing..."):
                        summary = generate_gemini_content(data, prompt)
                        st.subheader("Summary")
                        st.write(summary)                  
        
        # transcript_text=YouTubeTranscriptApi.get_transcript(video_id)

        # transcript = ""
        # for i in transcript_text:
        #     transcript += " " + i["text"]

        # return transcript

    except Exception as e:
        raise e
    
# if st.button("Summarize"):

#     if transcript_text:
#         summary=generate_gemini_content(transcript_text,prompt)
#         st.markdown("## Summary:")
#         st.write(summary)


# # App title
# st.title("YouTube Video Summarizer")

# # Input: YouTube video URL
# video_url = st.text_input("Enter YouTube Video URL:")

# if video_url:
#     try:
#         # Extract video ID from the URL
#         if "youtube.com" in video_url:
#             video_id = video_url.split("v=")[-1]
#         elif "youtu.be" in video_url:
#             video_id = video_url.split("/")[-1]
#         else:
#             st.error("Invalid YouTube URL!")
#             video_id = None

#         if video_id:
#             # Fetch subtitles
#             transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
#             data = ""
#             for transcript in transcript_list:
#                 if transcript.is_translatable:
#                     subtitles = transcript.translate('en').fetch()
#                     data += " ".join([entry['text'] for entry in subtitles])
#                     break

#             # Display subtitles
#             if data:
#                 st.subheader("Extracted Subtitles")
#                 st.write(data)

#                 # Summarize subtitles
#                 if st.button("Summarize"):
#                     with st.spinner("Summarizing..."):
#                         summary = generate_gemini_content(data, prompt)
#                         st.subheader("Summary")
#                         st.write(summary)
#             else:
#                 st.warning("No subtitles found for this video.")

#     except Exception as e:
#         st.error(f"An error occurred: {e}")