|
|
|
import os |
|
import requests |
|
import streamlit as st |
|
from youtube_transcript_api import YouTubeTranscriptApi |
|
from langchain.text_splitter import RecursiveCharacterTextSplitter |
|
from dotenv import load_dotenv |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
API_URL = os.getenv('HUGGING_FACE_API_URL') |
|
API_KEY = os.getenv('HUGGING_FACE_API_KEY') |
|
|
|
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 |
|
|
|
def summarize_with_hugging_face(transcript, language_code, model_name='meta-llama/Meta-Llama-3-8B'): |
|
|
|
text_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=0) |
|
texts = text_splitter.split_text(transcript) |
|
text_to_summarize = " ".join(texts[:4]) |
|
|
|
|
|
payload = { |
|
"inputs": { |
|
"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 if possible, and a CONCLUSION in {language_code}.''' |
|
} |
|
} |
|
|
|
|
|
headers = {"Authorization": f"Bearer {API_KEY}"} |
|
response = requests.post(API_URL, headers=headers, json=payload) |
|
|
|
if response.status_code == 200: |
|
return response.json()["generated_text"] |
|
else: |
|
raise Exception("Summarization failed.") |
|
|
|
def main(): |
|
st.title('YouTube Video Summarizer') |
|
link = st.text_input('Enter the link of the YouTube video you want to summarize:') |
|
|
|
if st.button('Start'): |
|
if link: |
|
try: |
|
progress = st.progress(0) |
|
status_text = st.empty() |
|
|
|
status_text.text('Loading the transcript...') |
|
progress.progress(25) |
|
|
|
|
|
transcript, language_code = get_transcript(link) |
|
|
|
status_text.text(f'Creating summary...') |
|
progress.progress(75) |
|
|
|
summary = summarize_with_hugging_face(transcript, language_code) |
|
|
|
status_text.text('Summary:') |
|
st.markdown(summary) |
|
progress.progress(100) |
|
except Exception as e: |
|
st.write(str(e)) |
|
else: |
|
st.write('Please enter a valid YouTube link.') |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|