Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,57 @@
|
|
| 1 |
import os
|
| 2 |
import openai
|
| 3 |
import streamlit as st
|
| 4 |
-
from youtube_transcript_api import YouTubeTranscriptApi
|
| 5 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
st.set_page_config(
|
| 11 |
-
page_title="YouTube Video Summarizer",
|
| 12 |
-
page_icon="🎥",
|
| 13 |
-
layout="centered",
|
| 14 |
-
initial_sidebar_state="expanded",
|
| 15 |
-
)
|
| 16 |
-
|
| 17 |
-
# Custom CSS for styling
|
| 18 |
-
st.markdown(
|
| 19 |
-
"""
|
| 20 |
-
<style>
|
| 21 |
-
body {
|
| 22 |
-
background-color: #f4f4f4;
|
| 23 |
-
font-family: Arial, sans-serif;
|
| 24 |
-
}
|
| 25 |
-
.stButton>button {
|
| 26 |
-
background-color: #ff6347;
|
| 27 |
-
color: white;
|
| 28 |
-
border-radius: 10px;
|
| 29 |
-
font-weight: bold;
|
| 30 |
-
}
|
| 31 |
-
.stSlider {
|
| 32 |
-
color: #ff6347;
|
| 33 |
-
}
|
| 34 |
-
</style>
|
| 35 |
-
""",
|
| 36 |
-
unsafe_allow_html=True
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
# Function to get the transcript from YouTube
|
| 40 |
def get_transcript(youtube_url):
|
| 41 |
-
video_id = youtube_url.split("v=")[-1]
|
| 42 |
-
transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
|
| 43 |
-
|
| 44 |
try:
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
| 48 |
try:
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=0)
|
| 61 |
texts = text_splitter.split_text(transcript)
|
| 62 |
text_to_summarize = " ".join(texts[:4]) # Adjust this as needed
|
| 63 |
|
|
|
|
| 64 |
system_prompt = 'I want you to act as a Life Coach that can create good summaries!'
|
| 65 |
prompt = f'''Summarize the following text in {language_code}.
|
| 66 |
Text: {text_to_summarize}
|
| 67 |
-
|
| 68 |
Add a title to the summary in {language_code}.
|
| 69 |
-
Include an INTRODUCTION, BULLET POINTS, and a CONCLUSION in {language_code}.'''
|
| 70 |
|
|
|
|
| 71 |
response = openai.ChatCompletion.create(
|
| 72 |
model=model_name,
|
| 73 |
messages=[
|
|
@@ -76,63 +60,52 @@ def summarize_with_openai(transcript, language_code, model_name='gpt-3.5-turbo')
|
|
| 76 |
],
|
| 77 |
temperature=1
|
| 78 |
)
|
| 79 |
-
|
| 80 |
return response['choices'][0]['message']['content']
|
| 81 |
|
| 82 |
-
# Function to create a PDF
|
| 83 |
-
def create_pdf(summary):
|
| 84 |
-
pdf = FPDF()
|
| 85 |
-
pdf.add_page()
|
| 86 |
-
pdf.set_font("Arial", size=12)
|
| 87 |
-
pdf.multi_cell(200, 10, summary)
|
| 88 |
-
pdf_output = "summary.pdf"
|
| 89 |
-
pdf.output(pdf_output)
|
| 90 |
-
return pdf_output
|
| 91 |
-
|
| 92 |
-
# Main function for the app
|
| 93 |
def main():
|
| 94 |
-
st.title('
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
st.
|
| 98 |
-
api_key = st.sidebar.text_input("Enter OpenAI API Key:", type="password")
|
| 99 |
-
openai.api_key = api_key
|
| 100 |
|
| 101 |
-
#
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
if
|
| 108 |
try:
|
| 109 |
progress = st.progress(0)
|
| 110 |
-
st.
|
| 111 |
|
| 112 |
-
|
| 113 |
-
progress.progress(
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
progress.progress(80)
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
st.write(summary)
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
with open(pdf_file, "rb") as file:
|
| 126 |
-
st.download_button(
|
| 127 |
-
label="Download Summary as PDF 📄",
|
| 128 |
-
data=file,
|
| 129 |
-
file_name="summary.pdf",
|
| 130 |
-
mime="application/pdf"
|
| 131 |
-
)
|
| 132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
progress.progress(100)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
except Exception as e:
|
| 135 |
-
st.error(f"
|
|
|
|
|
|
|
| 136 |
|
| 137 |
if __name__ == "__main__":
|
| 138 |
main()
|
|
|
|
| 1 |
import os
|
| 2 |
import openai
|
| 3 |
import streamlit as st
|
| 4 |
+
from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound, VideoUnavailable
|
| 5 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
+
|
| 7 |
+
# OpenAI API Key Input
|
| 8 |
+
openai.api_key = st.sidebar.text_input('Enter your OpenAI API Key', type='password')
|
| 9 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def get_transcript(youtube_url):
|
|
|
|
|
|
|
|
|
|
| 11 |
try:
|
| 12 |
+
video_id = youtube_url.split("v=")[-1]
|
| 13 |
+
transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
|
| 14 |
+
|
| 15 |
+
# Try fetching the manual transcript
|
| 16 |
try:
|
| 17 |
+
transcript = transcript_list.find_manually_created_transcript()
|
| 18 |
+
language_code = transcript.language_code # Save the detected language
|
| 19 |
+
except NoTranscriptFound:
|
| 20 |
+
# If no manual transcript is found, try fetching an auto-generated transcript in a supported language
|
| 21 |
+
try:
|
| 22 |
+
generated_transcripts = [trans for trans in transcript_list if trans.is_generated]
|
| 23 |
+
transcript = generated_transcripts[0]
|
| 24 |
+
language_code = transcript.language_code # Save the detected language
|
| 25 |
+
except NoTranscriptFound:
|
| 26 |
+
raise Exception("No suitable transcript found.")
|
| 27 |
+
|
| 28 |
+
full_transcript = " ".join([part['text'] for part in transcript.fetch()])
|
| 29 |
+
return full_transcript, language_code # Return both the transcript and detected language
|
| 30 |
+
|
| 31 |
+
except TranscriptsDisabled:
|
| 32 |
+
st.error("Subtitles are disabled for this video. Cannot retrieve a transcript.")
|
| 33 |
+
return None, None
|
| 34 |
+
except VideoUnavailable:
|
| 35 |
+
st.error("The video is unavailable. Please check the link.")
|
| 36 |
+
return None, None
|
| 37 |
+
except Exception as e:
|
| 38 |
+
st.error(f"Error retrieving transcript: {str(e)}")
|
| 39 |
+
return None, None
|
| 40 |
+
|
| 41 |
+
def summarize_with_langchain_and_openai(transcript, language_code, model_name='gpt-3.5-turbo'):
|
| 42 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=0)
|
| 43 |
texts = text_splitter.split_text(transcript)
|
| 44 |
text_to_summarize = " ".join(texts[:4]) # Adjust this as needed
|
| 45 |
|
| 46 |
+
# Prepare the prompt for summarization
|
| 47 |
system_prompt = 'I want you to act as a Life Coach that can create good summaries!'
|
| 48 |
prompt = f'''Summarize the following text in {language_code}.
|
| 49 |
Text: {text_to_summarize}
|
| 50 |
+
|
| 51 |
Add a title to the summary in {language_code}.
|
| 52 |
+
Include an INTRODUCTION, BULLET POINTS if possible, and a CONCLUSION in {language_code}.'''
|
| 53 |
|
| 54 |
+
# Start summarizing using OpenAI
|
| 55 |
response = openai.ChatCompletion.create(
|
| 56 |
model=model_name,
|
| 57 |
messages=[
|
|
|
|
| 60 |
],
|
| 61 |
temperature=1
|
| 62 |
)
|
| 63 |
+
|
| 64 |
return response['choices'][0]['message']['content']
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
def main():
|
| 67 |
+
st.title('YouTube Video Summarizer')
|
| 68 |
|
| 69 |
+
# YouTube video input
|
| 70 |
+
link = st.text_input('Enter the link of the YouTube video you want to summarize:')
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
# Error handling if OpenAI API key is not provided
|
| 73 |
+
if not openai.api_key:
|
| 74 |
+
st.error("Please enter your OpenAI API key to proceed.")
|
| 75 |
+
return
|
| 76 |
+
|
| 77 |
+
if st.button('Start'):
|
| 78 |
+
if link:
|
| 79 |
try:
|
| 80 |
progress = st.progress(0)
|
| 81 |
+
status_text = st.empty()
|
| 82 |
|
| 83 |
+
status_text.text('Loading the transcript...')
|
| 84 |
+
progress.progress(25)
|
| 85 |
|
| 86 |
+
# Getting both the transcript and language_code
|
| 87 |
+
transcript, language_code = get_transcript(link)
|
|
|
|
| 88 |
|
| 89 |
+
if transcript is None:
|
| 90 |
+
return # Exit early if no transcript is available
|
|
|
|
| 91 |
|
| 92 |
+
status_text.text(f'Creating summary...')
|
| 93 |
+
progress.progress(75)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
model_name = 'gpt-3.5-turbo'
|
| 96 |
+
summary = summarize_with_langchain_and_openai(transcript, language_code, model_name)
|
| 97 |
+
|
| 98 |
+
status_text.text('Summary:')
|
| 99 |
+
st.markdown(summary)
|
| 100 |
progress.progress(100)
|
| 101 |
+
|
| 102 |
+
# Option to download summary as PDF
|
| 103 |
+
st.download_button('Download Summary as PDF', summary, file_name='summary.pdf')
|
| 104 |
+
|
| 105 |
except Exception as e:
|
| 106 |
+
st.error(f"An error occurred: {str(e)}")
|
| 107 |
+
else:
|
| 108 |
+
st.error('Please enter a valid YouTube link.')
|
| 109 |
|
| 110 |
if __name__ == "__main__":
|
| 111 |
main()
|