File size: 5,109 Bytes
b91ecf4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from dotenv import load_dotenv
import os
import google.generativeai as genai
from textblob import TextBlob

from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api.formatters import JSONFormatter

load_dotenv()  # Load all the environment variables

genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))

prompt = """**You are a YouTube video summarizer.** You will be taking the transcript text
and summarizing the entire video and providing the important summary in points
within 250 words. Please provide the summary of the text given here:  """


# Function to extract transcript data from YouTube videos
def extract_transcript_details(youtube_video_url):
    try:
        video_id = youtube_video_url.split("=")[1]

        transcript_text = YouTubeTranscriptApi.get_transcript(video_id)

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

        return transcript

    except Exception as e:
        raise e


# Function to generate summary based on prompt using Google Gemini Pro
def generate_gemini_content(transcript_text, prompt, max_length):
    model = genai.GenerativeModel("gemini-pro")
    response = model.generate_content(prompt + transcript_text)
    summary = response.text

    # Split the summary into words and limit the length
    words = summary.split()
    if len(words) > max_length:
        summary = ' '.join(words[:max_length])

    return summary


# Function to analyze sentiment of text
def analyze_sentiment(text):
    blob = TextBlob(text)
    sentiment_score = blob.sentiment.polarity
    if sentiment_score > 0:
        return "Positive"
    elif sentiment_score == 0:
        return "Neutral"
    else:
        return "Negative"


# Streamlit app
st.set_page_config(page_title="YouTube  Summarizer and Sentiment Analyzer", page_icon=":clapper:", layout="wide")

st.title("πŸŽ₯ YouTube Sentiment Analyzer")

st.sidebar.header("Options")
youtube_link = st.sidebar.text_input("Enter YouTube Video Link:")
action = st.sidebar.selectbox("Select Action:", ["Choose", "Get Detailed Summary", "Analyze Sentiment"])

if action == "Get Detailed Summary":
    max_length = st.sidebar.slider("Select Maximum Summary Length (words)", min_value=50, max_value=500, value=250)

if st.sidebar.button("πŸš€ Perform Action"):
    if action == "Get Detailed Summary":
        transcript_text = extract_transcript_details(youtube_link)
        if transcript_text:
            summary = generate_gemini_content(transcript_text, prompt, max_length)
            st.markdown("## Detailed Summary:")
            st.write(summary)

    elif action == "Analyze Sentiment":
        transcript_text = extract_transcript_details(youtube_link)
        if transcript_text:
            sentiment = analyze_sentiment(transcript_text)
            st.markdown("## Sentiment Analysis:")
            st.write(f"The sentiment of the video is: {sentiment}")

# Show YouTube video thumbnail if link provided
if youtube_link:
    video_id = youtube_link.split("=")[1]
    st.image(f"http://img.youtube.com/vi/{video_id}/0.jpg", use_column_width=True)
else:
    # Stretched GIF
    st.markdown('<div style="font-family:Arial; text-align:center;"><iframe allow="fullscreen" frameBorder="0" height="400" src="https://giphy.com/embed/SNHd3FpcOrPHoBHtLD/video" width="800"></iframe></div>', unsafe_allow_html=True)

# Footer
footer_with_image_light_blue = """
<style>
.footer {
    background-color: #E0F2F1;
    padding: 20px;
    border-radius: 10px;
    text-align: center;
    animation: fadeIn 1s;
}

.footer img {
    max-width: 100%;
    border-radius: 10px;
    margin-top: 10px;
}

.footer .line {
    height: 1px;
    background-color: #B2DFDB;
    margin: 10px 0;
}

.footer .connect-text {
    color: #004D40;
    font-weight: bold;
    margin-bottom: 10px;
}

.footer a {
    margin: 0 10px;
}

.footer .powered-by {
    color: #004D40;
    font-size: 14px;
    margin-top: 10px;
}

.bright-text {
    color: #004D40;
}

/* Add Animation */
@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.chat-message {
    animation: fadeIn 0.5s ease-out;
}
</style>
<div class="footer">
    <div class="line"></div>
    <div class="connect-text">Connect with me at</div>
    <a href="https://github.com/FasilHameed" target="_blank"><img src="https://img.icons8.com/plasticine/30/000000/github.png" alt="GitHub"></a>
    <a href="https://www.linkedin.com/in/faisal--hameed/" target="_blank"><img src="https://img.icons8.com/plasticine/30/000000/linkedin.png" alt="LinkedIn"></a>
    <a href="tel:+917006862681"><img src="https://img.icons8.com/plasticine/30/000000/phone.png" alt="Phone"></a>
    <a href="mailto:faisalhameed763@gmail.com"><img src="https://img.icons8.com/plasticine/30/000000/gmail.png" alt="Gmail"></a>
    <div class="line"></div>
    <div class="powered-by">Powered By <img src="https://img.icons8.com/clouds/30/000000/gemini.png" alt="Gemini"> Gemini πŸ’« and Streamlit πŸš€</div>
</div>
"""

# Render Footer
st.markdown(footer_with_image_light_blue, unsafe_allow_html=True)