Abu1998 commited on
Commit
014002f
·
verified ·
1 Parent(s): 73d8276

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -23
app.py CHANGED
@@ -1,19 +1,9 @@
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
- #https://huggingface.co/spaces/Abu1998/YouTubeTranscriptApi1/resolve/main/.env
10
- env_path = 'YouTubeTranscriptApi1/resolve/main/.env' # Change the Path
11
- # Load the OpenAI API key from the .env file
12
- load_dotenv(env_path)
13
- openai.api_key = os.getenv('OPENAI_API_KEY')
14
-
15
- def get_transcript(youtube_url):
16
- video_id = youtube_url.split("v=")[-1]
17
  transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
18
 
19
  # Try fetching the manual transcript
@@ -33,12 +23,14 @@ def get_transcript(youtube_url):
33
  full_transcript = " ".join([part['text'] for part in transcript.fetch()])
34
  return full_transcript, language_code # Return both the transcript and detected language
35
 
 
 
 
36
 
37
- def summarize_with_langchain_and_openai(transcript, language_code, model_name='gpt-3.5-turbo'):
38
  # Split the document if it's too long
39
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=0)
40
  texts = text_splitter.split_text(transcript)
41
- text_to_summarize = " ".join(texts[:4]) # Adjust this as needed
42
 
43
  # Prepare the prompt for summarization
44
  system_prompt = 'I want you to act as a Life Coach that can create good summaries!'
@@ -57,15 +49,17 @@ def summarize_with_langchain_and_openai(transcript, language_code, model_name='g
57
  ],
58
  temperature=1
59
  )
60
-
61
  return response['choices'][0]['message']['content']
62
 
63
  def main():
64
- st.title('YouTube video summarizer')
65
- link = st.text_input('Enter the link of the YouTube video you want to summarize:')
66
-
 
 
67
  if st.button('Start'):
68
- if link:
69
  try:
70
  progress = st.progress(0)
71
  status_text = st.empty()
@@ -74,13 +68,12 @@ def main():
74
  progress.progress(25)
75
 
76
  # Getting both the transcript and language_code
77
- transcript, language_code = get_transcript(link)
78
 
79
  status_text.text(f'Creating summary...')
80
  progress.progress(75)
81
 
82
- model_name = 'gpt-3.5-turbo'
83
- summary = summarize_with_langchain_and_openai(transcript, language_code, model_name)
84
 
85
  status_text.text('Summary:')
86
  st.markdown(summary)
@@ -88,7 +81,7 @@ def main():
88
  except Exception as e:
89
  st.write(str(e))
90
  else:
91
- st.write('Please enter a valid YouTube link.')
92
 
93
  if __name__ == "__main__":
94
- main()
 
 
1
  import openai
2
  import streamlit as st
3
  from youtube_transcript_api import YouTubeTranscriptApi
4
  from langchain.text_splitter import RecursiveCharacterTextSplitter
 
5
 
6
+ def get_transcript(video_id):
 
 
 
 
 
 
 
 
7
  transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
8
 
9
  # Try fetching the manual transcript
 
23
  full_transcript = " ".join([part['text'] for part in transcript.fetch()])
24
  return full_transcript, language_code # Return both the transcript and detected language
25
 
26
+ def summarize_with_langchain_and_openai(transcript, language_code, openai_api_key, model_name='gpt-3.5-turbo'):
27
+ # Set the OpenAI API key
28
+ openai.api_key = openai_api_key
29
 
 
30
  # Split the document if it's too long
31
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=0)
32
  texts = text_splitter.split_text(transcript)
33
+ text_to_summarize = " ".join(texts[:4]) # Adjust this as needed
34
 
35
  # Prepare the prompt for summarization
36
  system_prompt = 'I want you to act as a Life Coach that can create good summaries!'
 
49
  ],
50
  temperature=1
51
  )
52
+
53
  return response['choices'][0]['message']['content']
54
 
55
  def main():
56
+ st.title('YouTube Video Summarizer')
57
+
58
+ openai_api_key = st.text_input('Enter your OpenAI API Key:', type='password')
59
+ video_id = st.text_input('Enter the YouTube Video ID:')
60
+
61
  if st.button('Start'):
62
+ if openai_api_key and video_id:
63
  try:
64
  progress = st.progress(0)
65
  status_text = st.empty()
 
68
  progress.progress(25)
69
 
70
  # Getting both the transcript and language_code
71
+ transcript, language_code = get_transcript(video_id)
72
 
73
  status_text.text(f'Creating summary...')
74
  progress.progress(75)
75
 
76
+ summary = summarize_with_langchain_and_openai(transcript, language_code, openai_api_key)
 
77
 
78
  status_text.text('Summary:')
79
  st.markdown(summary)
 
81
  except Exception as e:
82
  st.write(str(e))
83
  else:
84
+ st.write('Please enter both your OpenAI API Key and YouTube Video ID.')
85
 
86
  if __name__ == "__main__":
87
+ main()