Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +68 -45
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,60 +1,83 @@
|
|
1 |
import streamlit as st
|
2 |
import google.generativeai as genai
|
3 |
from youtube_transcript_api import YouTubeTranscriptApi
|
|
|
|
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
and summarizing the entire video and providing the important summary in points
|
8 |
-
within 1500 words. Please provide the summary of the text given here: """
|
9 |
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
def extract_transcript_details(youtube_video_url):
|
12 |
try:
|
13 |
video_id = youtube_video_url.split("=")[1]
|
14 |
-
|
15 |
-
|
16 |
-
for i in transcript_text:
|
17 |
-
transcript += " " + i["text"]
|
18 |
-
return transcript
|
19 |
except Exception as e:
|
20 |
-
|
|
|
21 |
|
22 |
-
## getting the summary based on Prompt from Google Gemini Pro
|
23 |
def generate_gemini_content(transcript_text, prompt, api_key):
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
|
|
29 |
st.title("YouTube Video Summarizer")
|
30 |
|
31 |
-
# Display
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
)
|
36 |
|
37 |
-
#
|
38 |
-
google_api_key
|
39 |
-
|
40 |
-
if
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
st.error("Failed to extract transcript.")
|
59 |
-
except Exception as e:
|
60 |
-
st.error(f"An error occurred: {e}")
|
|
|
1 |
import streamlit as st
|
2 |
import google.generativeai as genai
|
3 |
from youtube_transcript_api import YouTubeTranscriptApi
|
4 |
+
from reportlab.lib.pagesizes import letter
|
5 |
+
from reportlab.pdfgen import canvas
|
6 |
+
import io
|
7 |
|
8 |
+
# Set Streamlit page configuration
|
9 |
+
st.set_page_config(page_title="YouTube Video Summarizer", layout="wide")
|
|
|
|
|
10 |
|
11 |
+
# Sidebar for user inputs
|
12 |
+
google_api_key = st.sidebar.text_input("Enter your Google API Key:", type="password")
|
13 |
+
youtube_link = st.sidebar.text_input("Enter YouTube Video Link:")
|
14 |
+
|
15 |
+
# Summary length customization
|
16 |
+
summary_length = st.sidebar.select_slider(
|
17 |
+
"Select Summary Length:", options=['Short', 'Medium', 'Long'], value='Medium'
|
18 |
+
)
|
19 |
+
|
20 |
+
# Define functions
|
21 |
def extract_transcript_details(youtube_video_url):
|
22 |
try:
|
23 |
video_id = youtube_video_url.split("=")[1]
|
24 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
25 |
+
return " ".join(segment["text"] for segment in transcript)
|
|
|
|
|
|
|
26 |
except Exception as e:
|
27 |
+
st.sidebar.error(f"An error occurred: {e}")
|
28 |
+
return None
|
29 |
|
|
|
30 |
def generate_gemini_content(transcript_text, prompt, api_key):
|
31 |
+
try:
|
32 |
+
genai.configure(api_key=api_key)
|
33 |
+
model = genai.GenerativeModel("gemini-pro")
|
34 |
+
response = model.generate_content(prompt + transcript_text)
|
35 |
+
return response.text
|
36 |
+
except Exception as e:
|
37 |
+
st.error(f"An error occurred: {e}")
|
38 |
+
return None
|
39 |
+
|
40 |
+
def create_pdf(summary_text):
|
41 |
+
buffer = io.BytesIO()
|
42 |
+
c = canvas.Canvas(buffer, pagesize=letter)
|
43 |
+
c.drawString(72, 800, "Summary")
|
44 |
+
text = c.beginText(40, 780)
|
45 |
+
text.setFont("Helvetica", 12)
|
46 |
+
for line in summary_text.split('\n'):
|
47 |
+
text.textLine(line)
|
48 |
+
c.drawText(text)
|
49 |
+
c.showPage()
|
50 |
+
c.save()
|
51 |
+
buffer.seek(0)
|
52 |
+
return buffer.getvalue()
|
53 |
|
54 |
+
# UI elements
|
55 |
st.title("YouTube Video Summarizer")
|
56 |
|
57 |
+
# Display video thumbnail
|
58 |
+
if youtube_link:
|
59 |
+
video_id = youtube_link.split("=")[1]
|
60 |
+
video_thumbnail = f"http://img.youtube.com/vi/{video_id}/0.jpg"
|
61 |
+
st.image(video_thumbnail, caption="Video Thumbnail", use_column_width=True)
|
62 |
|
63 |
+
# Process and display summary
|
64 |
+
if google_api_key and youtube_link and st.button("Get Detailed Notes"):
|
65 |
+
transcript_text = extract_transcript_details(youtube_link)
|
66 |
+
if transcript_text:
|
67 |
+
prompt = """You are a YouTube video summarizer. Summarize the video content into key points within 1500 words."""
|
68 |
+
customized_prompt = f"{prompt} Please generate a {summary_length.lower()} summary."
|
69 |
+
summary = generate_gemini_content(transcript_text, customized_prompt, google_api_key)
|
70 |
+
if summary:
|
71 |
+
st.success("Transcript extracted and summary generated successfully!")
|
72 |
+
st.subheader("Detailed Notes:")
|
73 |
+
st.write(summary)
|
74 |
+
# PDF download
|
75 |
+
pdf_bytes = create_pdf(summary)
|
76 |
+
st.download_button(label="Download Summary as PDF",
|
77 |
+
data=pdf_bytes,
|
78 |
+
file_name="YouTube_Summary.pdf",
|
79 |
+
mime="application/pdf")
|
80 |
+
else:
|
81 |
+
st.error("Failed to generate summary.")
|
82 |
+
else:
|
83 |
+
st.error("Failed to extract transcript.")
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -2,4 +2,5 @@ youtube_transcript_api
|
|
2 |
streamlit
|
3 |
google-generativeai
|
4 |
python-dotenv
|
5 |
-
pathlib
|
|
|
|
2 |
streamlit
|
3 |
google-generativeai
|
4 |
python-dotenv
|
5 |
+
pathlib
|
6 |
+
reportlab
|