Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,80 +1,93 @@
|
|
| 1 |
-
import
|
| 2 |
import openai
|
|
|
|
| 3 |
from youtube_transcript_api import YouTubeTranscriptApi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
def get_transcript(video_id):
|
| 7 |
-
"""Fetches the transcript for a given YouTube video ID."""
|
| 8 |
try:
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
return None
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
return None
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
response = openai.ChatCompletion.create(
|
| 39 |
-
model=
|
| 40 |
messages=[
|
| 41 |
-
{
|
| 42 |
-
{
|
| 43 |
-
]
|
|
|
|
| 44 |
)
|
| 45 |
-
|
| 46 |
-
return
|
| 47 |
-
|
| 48 |
-
# Streamlit UI
|
| 49 |
-
st.title("YouTube Video to Shorts Script Converter")
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
-
if st.button(
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
shorts_script = generate_shorts_script(summary)
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
st.write("Generated YouTube Shorts Script:")
|
| 74 |
-
st.write(shorts_script)
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
| 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()
|