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 1500 words.The Summary Should Be As detailed As Possible . 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 and provide explanation def analyze_sentiment_with_explanation(text): blob = TextBlob(text) sentiment_score = blob.sentiment.polarity if sentiment_score > 0: sentiment = "Positive" explanation = "The sentiment is positive because the text contains predominantly positive language." elif sentiment_score == 0: sentiment = "Neutral" explanation = "The sentiment is neutral as there is an equal balance of positive and negative language." else: sentiment = "Negative" explanation = "The sentiment is negative because the text contains predominantly negative language." return sentiment, explanation # 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=1500, value=400) 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, explanation = analyze_sentiment_with_explanation(transcript_text) st.markdown("## Sentiment Analysis:") st.markdown(f"The sentiment of the video is: **{sentiment}**") # Display explanation st.markdown("### Explanation:") st.write(explanation) # Adjust appearance based on sentiment if sentiment == "Positive": st.write(f"The sentiment of the video is: {sentiment}", unsafe_allow_html=True) st.write('

The sentiment of the video is positive!

', unsafe_allow_html=True) elif sentiment == "Negative": st.write(f"The sentiment of the video is: {sentiment}", unsafe_allow_html=True) st.write('

The sentiment of the video is negative!

', unsafe_allow_html=True) # Show YouTube video thumbnail if link provided and action is not sentiment analysis if youtube_link and action != "Analyze Sentiment": 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('
', unsafe_allow_html=True) # Footer footer_with_image_light_blue = """ """ # Render Footer st.markdown(footer_with_image_light_blue, unsafe_allow_html=True)