diff --git a/app.py b/app.py index 72113c6..1ff14a5 100644 --- a/app.py +++ b/app.py @@ -1,8 +1,10 @@ import os +import re + +import google.generativeai as genai import streamlit as st from dotenv import load_dotenv from youtube_transcript_api import YouTubeTranscriptApi -import google.generativeai as genai # Load environment variables load_dotenv() @@ -14,22 +16,31 @@ prompt = """Welcome, Video Summarizer! Your task is to distill the essence of a given YouTube video transcript into a concise summary. Your summary should capture the key points and essential information, presented in bullet points, within a 250-word limit. Let's dive into the provided transcript and extract the vital details for our audience.""" +def get_video_id(youtube_video_url): + match = re.match( + r"^(http|https)://www\.youtube\.com/(v=|watch\?v=)([a-zA-Z0-9_-]+)", youtube_video_url) + if match: + return match.group(3) + else: + raise ValueError("Invalid YouTube URL format.") + # Function to extract transcript details from a YouTube video URL + + def extract_transcript_details(youtube_video_url): try: - video_id = youtube_video_url.split("=")[1] + video_id = get_video_id(youtube_video_url) transcript_text = YouTubeTranscriptApi.get_transcript(video_id) - - transcript = "" - for i in transcript_text: - transcript += " " + i["text"] - + transcript = " ".join([item["text"] for item in transcript_text]) return transcript + except ConnectionError: + raise ConnectionError("Network error occurred.") except Exception as e: raise e - # Function to generate summary using Google Gemini Pro + + def generate_gemini_content(transcript_text, prompt): model = genai.GenerativeModel("gemini-pro") response = model.generate_content(prompt + transcript_text) @@ -43,8 +54,9 @@ def generate_gemini_content(transcript_text, prompt): youtube_link = st.text_input("Enter YouTube Video Link:") 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) + video_id = get_video_id(youtube_link) + st.image( + f"http://img.youtube.com/vi/{video_id}/0.jpg", use_column_width=True) # Button to trigger summary generation if st.button("Get Detailed Notes"):