Abu1998 commited on
Commit
ae477d3
·
verified ·
1 Parent(s): 048df7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -62
app.py CHANGED
@@ -1,80 +1,93 @@
1
- import streamlit as st
2
  import openai
 
3
  from youtube_transcript_api import YouTubeTranscriptApi
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- # Function to get YouTube transcript
6
- def get_transcript(video_id):
7
- """Fetches the transcript for a given YouTube video ID."""
8
  try:
9
- transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
10
- transcript = " ".join([entry['text'] for entry in transcript_list])
11
- return transcript
12
- except Exception as e:
13
- st.error(f"Error fetching transcript: {e}")
14
- return None
15
-
16
- # Function to summarize the transcript using OpenAI's GPT API
17
- def summarize_transcript(transcript):
18
- """Summarizes the given transcript using OpenAI's GPT API."""
19
- if transcript is None:
20
- return None
21
 
22
- response = openai.ChatCompletion.create(
23
- model="gpt-3.5-turbo",
24
- messages=[
25
- {"role": "system", "content": "You are a helpful AI assistant that summarizes YouTube videos."},
26
- {"role": "user", "content": f"Please provide a concise summary of the following YouTube video transcript:\n\n{transcript}"}
27
- ]
28
- )
29
- summary = response.choices[0].message["content"].strip()
30
- return summary
31
 
32
- # Function to generate a 1-minute YouTube Shorts script based on the summary
33
- def generate_shorts_script(summary):
34
- """Generates a 1-minute shorts script based on the summary."""
35
- if summary is None:
36
- return None
37
 
 
 
 
 
38
  response = openai.ChatCompletion.create(
39
- model="gpt-3.5-turbo",
40
  messages=[
41
- {"role": "system", "content": "You are a creative script writer for YouTube Shorts."},
42
- {"role": "user", "content": f"Write a captivating and engaging 1-minute script for a YouTube Short based on this summary: {summary}. Focus on impactful visuals and concise storytelling."}
43
- ]
 
44
  )
45
- script = response.choices[0].message["content"].strip()
46
- return script
47
-
48
- # Streamlit UI
49
- st.title("YouTube Video to Shorts Script Converter")
50
 
51
- # Input fields
52
- openai_api_key = st.text_input("OpenAI API Key", type="password")
53
- video_id = st.text_input("YouTube Video ID")
54
 
55
- if st.button("Generate Shorts Script"):
56
- if openai_api_key and video_id:
57
- try:
58
- # Set OpenAI API key
59
- openai.api_key = openai_api_key
60
 
61
- # Get transcript
62
- transcript = get_transcript(video_id)
63
 
64
- # Summarize the transcript
65
- summary = summarize_transcript(transcript)
66
 
67
- # Generate shorts script
68
- if summary:
69
- shorts_script = generate_shorts_script(summary)
70
 
71
- # Display the generated shorts script
72
- if shorts_script:
73
- st.write("Generated YouTube Shorts Script:")
74
- st.write(shorts_script)
75
 
76
- except Exception as e:
77
- st.error(f"An error occurred: {e}")
78
- else:
79
- st.error("Please fill in both the OpenAI API Key and YouTube Video ID.")
 
 
 
80
 
 
 
 
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, find_dotenv
7
+
8
+ # Specify the path to your .env file
9
+ env_path = '/home/USER/.env/openai_api' # Change the Path
10
+ # Load the OpenAI API key from the .env file
11
+ load_dotenv(env_path)
12
+ openai.api_key = os.getenv('OPENAI_API_KEY')
13
+
14
+ def get_transcript(youtube_url):
15
+ video_id = youtube_url.split("v=")[-1]
16
+ transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
17
 
18
+ # Try fetching the manual transcript
 
 
19
  try:
20
+ transcript = transcript_list.find_manually_created_transcript()
21
+ language_code = transcript.language_code # Save the detected language
22
+ except:
23
+ # If no manual transcript is found, try fetching an auto-generated transcript in a supported language
24
+ try:
25
+ generated_transcripts = [trans for trans in transcript_list if trans.is_generated]
26
+ transcript = generated_transcripts[0]
27
+ language_code = transcript.language_code # Save the detected language
28
+ except:
29
+ # If no auto-generated transcript is found, raise an exception
30
+ raise Exception("No suitable transcript found.")
 
31
 
32
+ full_transcript = " ".join([part['text'] for part in transcript.fetch()])
33
+ return full_transcript, language_code # Return both the transcript and detected language
34
+
35
+
36
+ def summarize_with_langchain_and_openai(transcript, language_code, model_name='gpt-3.5-turbo'):
37
+ # Split the document if it's too long
38
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=0)
39
+ texts = text_splitter.split_text(transcript)
40
+ text_to_summarize = " ".join(texts[:4]) # Adjust this as needed
41
 
42
+ # Prepare the prompt for summarization
43
+ system_prompt = 'I want you to act as a Life Coach that can create good summaries!'
44
+ prompt = f'''Summarize the following text in {language_code}.
45
+ Text: {text_to_summarize}
 
46
 
47
+ Add a title to the summary in {language_code}.
48
+ Include an INTRODUCTION, BULLET POINTS if possible, and a CONCLUSION in {language_code}.'''
49
+
50
+ # Start summarizing using OpenAI
51
  response = openai.ChatCompletion.create(
52
+ model=model_name,
53
  messages=[
54
+ {'role': 'system', 'content': system_prompt},
55
+ {'role': 'user', 'content': prompt}
56
+ ],
57
+ temperature=1
58
  )
59
+
60
+ return response['choices'][0]['message']['content']
 
 
 
61
 
62
+ def main():
63
+ st.title('YouTube video summarizer')
64
+ link = st.text_input('Enter the link of the YouTube video you want to summarize:')
65
 
66
+ if st.button('Start'):
67
+ if link:
68
+ try:
69
+ progress = st.progress(0)
70
+ status_text = st.empty()
71
 
72
+ status_text.text('Loading the transcript...')
73
+ progress.progress(25)
74
 
75
+ # Getting both the transcript and language_code
76
+ transcript, language_code = get_transcript(link)
77
 
78
+ status_text.text(f'Creating summary...')
79
+ progress.progress(75)
 
80
 
81
+ model_name = 'gpt-3.5-turbo'
82
+ summary = summarize_with_langchain_and_openai(transcript, language_code, model_name)
 
 
83
 
84
+ status_text.text('Summary:')
85
+ st.markdown(summary)
86
+ progress.progress(100)
87
+ except Exception as e:
88
+ st.write(str(e))
89
+ else:
90
+ st.write('Please enter a valid YouTube link.')
91
 
92
+ if __name__ == "__main__":
93
+ main()