import os import openai import streamlit as st from youtube_transcript_api import YouTubeTranscriptApi from langchain.text_splitter import RecursiveCharacterTextSplitter from dotenv import load_dotenv from fpdf import FPDF # Set Streamlit page configuration st.set_page_config( page_title="YouTube Video Summarizer", page_icon="🎥", layout="centered", initial_sidebar_state="expanded", ) # Custom CSS for styling st.markdown( """ """, unsafe_allow_html=True ) # Function to get the transcript from YouTube def get_transcript(youtube_url): video_id = youtube_url.split("v=")[-1] transcript_list = YouTubeTranscriptApi.list_transcripts(video_id) try: transcript = transcript_list.find_manually_created_transcript() language_code = transcript.language_code except: try: generated_transcripts = [trans for trans in transcript_list if trans.is_generated] transcript = generated_transcripts[0] language_code = transcript.language_code except: raise Exception("No suitable transcript found.") full_transcript = " ".join([part['text'] for part in transcript.fetch()]) return full_transcript, language_code # Function to summarize with OpenAI using LangChain def summarize_with_openai(transcript, language_code, model_name='gpt-3.5-turbo'): text_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=0) texts = text_splitter.split_text(transcript) text_to_summarize = " ".join(texts[:4]) # Adjust this as needed system_prompt = 'I want you to act as a Life Coach that can create good summaries!' prompt = f'''Summarize the following text in {language_code}. Text: {text_to_summarize} Add a title to the summary in {language_code}. Include an INTRODUCTION, BULLET POINTS, and a CONCLUSION in {language_code}.''' response = openai.ChatCompletion.create( model=model_name, messages=[ {'role': 'system', 'content': system_prompt}, {'role': 'user', 'content': prompt} ], temperature=1 ) return response['choices'][0]['message']['content'] # Function to create a PDF def create_pdf(summary): pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size=12) pdf.multi_cell(200, 10, summary) pdf_output = "summary.pdf" pdf.output(pdf_output) return pdf_output # Main function for the app def main(): st.title('🎥 YouTube Video Summarizer') # Slider input for OpenAI API key st.sidebar.title("API Configuration") api_key = st.sidebar.text_input("Enter OpenAI API Key:", type="password") openai.api_key = api_key # Input for YouTube video link st.sidebar.title("Video Settings") youtube_link = st.text_input("Enter the YouTube video link:") if youtube_link and api_key: # Start button if st.button("Start Summarizing 🚀"): try: progress = st.progress(0) st.write("Fetching transcript...") transcript, language_code = get_transcript(youtube_link) progress.progress(40) st.write("Summarizing transcript...") summary = summarize_with_openai(transcript, language_code) progress.progress(80) # Display summary st.markdown("### Summary:") st.write(summary) # Generate PDF and provide download link pdf_file = create_pdf(summary) with open(pdf_file, "rb") as file: st.download_button( label="Download Summary as PDF 📄", data=file, file_name="summary.pdf", mime="application/pdf" ) progress.progress(100) except Exception as e: st.error(f"Error: {str(e)}") if __name__ == "__main__": main()