-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
82 lines (71 loc) · 2.76 KB
/
app.py
File metadata and controls
82 lines (71 loc) · 2.76 KB
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
import streamlit as st
from dotenv import load_dotenv
import os
import google.generativeai as genai
from youtube_transcript_api import YouTubeTranscriptApi
# Load environment variables from .env file
load_dotenv()
# Configure Google Generative AI API
api_key = os.getenv("GOOGLE_API_KEY")
if not api_key:
st.error("Google API Key is missing. Please set it in a .env file as GOOGLE_API_KEY.")
st.stop()
genai.configure(api_key=api_key)
model = genai.GenerativeModel('models/text-bison-001')
# Prompt template
prompt = """You are a YouTube video summarizer. Summarize the given transcript text
into concise bullet points or short paragraphs, all within 250 words."""
# Extract video ID from YouTube URL
def extract_video_id(youtube_url):
try:
if "v=" in youtube_url:
return youtube_url.split("v=")[1].split("&")[0]
elif "youtu.be/" in youtube_url:
return youtube_url.split("youtu.be/")[1].split("?")[0]
else:
raise ValueError("Invalid YouTube URL format.")
except Exception as e:
st.error(f"Error extracting video ID: {e}")
return None
# Fetch transcript text from video
def extract_transcript_details(youtube_url):
try:
video_id = extract_video_id(youtube_url)
if not video_id:
return None
transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
transcript_text = " ".join([entry["text"] for entry in transcript_list])
return transcript_text
except Exception as e:
st.error(f"Transcript not available: {e}")
return None
# Generate summary from transcript
def generate_summary(transcript_text):
try:
response = model.generate_content(prompt + "\n\n" + transcript_text)
return response.text
except Exception as e:
st.error(f"Error generating summary: {e}")
return None
# Streamlit app layout
st.set_page_config(page_title="YouTube Video Summarizer", layout="centered")
st.title("YouTube Transcript Summarizer")
youtube_link = st.text_input("Paste YouTube Video URL:")
if youtube_link:
video_id = extract_video_id(youtube_link)
if video_id:
st.image(f"https://img.youtube.com/vi/{video_id}/0.jpg", use_column_width=True)
if st.button("Summarize Video"):
if youtube_link:
with st.spinner("Fetching transcript..."):
transcript = extract_transcript_details(youtube_link)
if transcript:
with st.spinner("Generating summary..."):
summary = generate_summary(transcript)
if summary:
st.markdown("## Summary:")
st.write(summary)
else:
st.error("Failed to generate summary.")
else:
st.warning("Could not retrieve transcript for this video.")