gen24AI/YT_Notes_Maker
0
1import streamlit as st2from dotenv import load_dotenv3import os4from langchain_google_genai import ChatGoogleGenerativeAI5from youtube_transcript_api import YouTubeTranscriptApi, NoTranscriptFound, VideoUnavailable, TranscriptsDisabled6 7# Load environment variables8load_dotenv()9 10# Set up Google API key11api_key = os.getenv("GOOGLE_API_KEY")12if not api_key:13 st.error("Google API key is missing. Please set the GOOGLE_API_KEY environment variable.")14 st.stop() # Stop execution if API key is missing15 16# Configure Google Generative AI17llm = ChatGoogleGenerativeAI(model="gemini-pro", api_key=api_key)18 19# Improved prompt for Google Generative AI20prompt = """21You are an expert YouTube video summarizer. Your task is to create a well-structured and engaging summary of the video transcript provided. Please follow these guidelines for the summary:221. **Main Heading**: Start with a clear and concise heading that reflects the main topic or theme of the video.232. **Subheadings**: Break down the summary into key sections or topics discussed in the video. Each subheading should represent a major point or segment of the video content.243. **Professional Bullet Points**: Under each subheading, list important points using professional bullet points (•). Ensure that the points are clear, concise, and effectively convey the essence of the discussion.254. **Details and Clarity**: For each point, provide enough detail to convey the essence of the discussion. Avoid overly technical jargon and use simple language. The goal is to make the summary easily understandable and engaging for a broad audience.265. **Conciseness**: Keep the entire summary concise and within 500 words. Focus on the most important information and avoid unnecessary details.27Make sure the summary is easy to read and enjoyable, with clear and structured points that capture the essence of the video content. Here’s the transcript text to summarize:28"""29 30# Function to extract transcript details31def extract_transcript_details(youtube_video_url):32 try:33 video_id = youtube_video_url.split("v=")[1]34 transcript_text = YouTubeTranscriptApi.get_transcript(video_id)35 36 transcript = ""37 for entry in transcript_text:38 transcript += " " + entry["text"]39 40 return transcript41 42 except NoTranscriptFound:43 return "Transcript not found for this video."44 except VideoUnavailable:45 return "This video is unavailable."46 except TranscriptsDisabled:47 return "Transcripts are disabled for this video."48 except Exception as e:49 return f"An error occurred: {str(e)}"50 51# Function to generate summary using Google Generative AI52def generate_summary(transcript_text, prompt):53 response = llm.invoke(prompt + transcript_text)54 return response.content55 56# Streamlit App UI57st.set_page_config(page_title="YouTube Summarizer", layout="wide")58 59# Custom CSS styling with Tailwind color palette60st.markdown("""61 <style>62 body {63 background-color: #f5efed; /* Isabelline for the overall background */64 color: #000000; /* Black text color for better readability */65 }66 .main {67 background-color: #ffffff; /* White for main content area */68 padding: 20px;69 border-radius: 8px;70 box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);71 }72 .stTextInput>div>div>input {73 background-color: #ffffff;74 color: #000000; /* Black text color for input field */75 border-radius: 5px;76 padding: 10px;77 border: 1px solid #cccccc; /* Light gray border */78 }79 .stTextInput>label {80 color: #000000; /* Black color for input field label */81 }82 .stButton>button {83 background-color: #2292a4; /* Blue (Munsell) for buttons */84 color: white;85 border: none;86 padding: 10px 20px;87 border-radius: 5px;88 font-size: 16px;89 cursor: pointer;90 transition: background-color 0.3s ease;91 }92 .stButton>button:hover {93 background-color: #1b7684; /* Darker Blue (Munsell) on hover */94 }95 .summary {96 font-size: 1em;97 line-height: 1.6;98 color: #000000; /* Black text color for summary */99 }100 h1, h2, h3, h4, h5, h6 {101 color: #000000; /* Black color for headings */102 }103 .stMarkdown ul {104 list-style-type: disc;105 padding-left: 20px;106 }107 .stMarkdown ul li {108 margin-bottom: 0.5em;109 }110 .emoji {111 font-size: 1.2em;112 margin-right: 0.5em;113 }114 .stImage>img {115 max-width: 40%; /* Smaller image size */116 height: auto;117 border-radius: 5px;118 border: 2px solid #d96c06; /* Cocoa Brown border for the image */119 }120 </style>121""", unsafe_allow_html=True)122 123# Sidebar for additional options124with st.sidebar:125 st.title("YouTube Summarizer")126 st.markdown("Convert YouTube videos into detailed summaries with ease.")127 128# Main content129st.title("YouTube Video Summary")130 131# Input field for YouTube link132youtube_link = st.text_input("YouTube Video Link")133 134if youtube_link:135 video_id = youtube_link.split("v=")[1]136 st.image(f"http://img.youtube.com/vi/{video_id}/0.jpg", use_column_width=True)137 138# Button to get detailed notes139if st.button("Generate Summary"):140 with st.spinner("Extracting transcript and generating summary..."):141 transcript_text = extract_transcript_details(youtube_link)142 143 if "error" in transcript_text.lower():144 st.error(transcript_text)145 else:146 summary = generate_summary(transcript_text, prompt)147 st.markdown("## Summary:")148 # Display the summary with headings, subheadings, and emojis149 st.markdown(f'<div class="summary">{summary}</div>', unsafe_allow_html=True)150 