usmanyousaf commited on
Commit
bf378bb
·
verified ·
1 Parent(s): c395248

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -0
app.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ from dotenv import load_dotenv
7
+ from fpdf import FPDF
8
+
9
+ # Set Streamlit page configuration
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
+ transcript = transcript_list.find_manually_created_transcript()
46
+ language_code = transcript.language_code
47
+ except:
48
+ try:
49
+ generated_transcripts = [trans for trans in transcript_list if trans.is_generated]
50
+ transcript = generated_transcripts[0]
51
+ language_code = transcript.language_code
52
+ except:
53
+ raise Exception("No suitable transcript found.")
54
+
55
+ full_transcript = " ".join([part['text'] for part in transcript.fetch()])
56
+ return full_transcript, language_code
57
+
58
+ # Function to summarize with OpenAI using LangChain
59
+ def summarize_with_openai(transcript, language_code, model_name='gpt-3.5-turbo'):
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=[
74
+ {'role': 'system', 'content': system_prompt},
75
+ {'role': 'user', 'content': prompt}
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('🎥 YouTube Video Summarizer')
95
+
96
+ # Slider input for OpenAI API key
97
+ st.sidebar.title("API Configuration")
98
+ api_key = st.sidebar.text_input("Enter OpenAI API Key:", type="password")
99
+ openai.api_key = api_key
100
+
101
+ # Input for YouTube video link
102
+ st.sidebar.title("Video Settings")
103
+ youtube_link = st.text_input("Enter the YouTube video link:")
104
+
105
+ if youtube_link and api_key:
106
+ # Start button
107
+ if st.button("Start Summarizing 🚀"):
108
+ try:
109
+ progress = st.progress(0)
110
+ st.write("Fetching transcript...")
111
+
112
+ transcript, language_code = get_transcript(youtube_link)
113
+ progress.progress(40)
114
+
115
+ st.write("Summarizing transcript...")
116
+ summary = summarize_with_openai(transcript, language_code)
117
+ progress.progress(80)
118
+
119
+ # Display summary
120
+ st.markdown("### Summary:")
121
+ st.write(summary)
122
+
123
+ # Generate PDF and provide download link
124
+ pdf_file = create_pdf(summary)
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"Error: {str(e)}")
136
+
137
+ if __name__ == "__main__":
138
+ main()