On this tutorial, we introduce a streamlined strategy for extracting, processing, and analyzing YouTube video transcripts utilizing Lyzr, a sophisticated AI-powered framework designed to simplify interplay with textual information. Leveraging Lyzr’s intuitive ChatBot interface alongside the youtube-transcript-api and FPDF, customers can effortlessly convert video content material into structured PDF paperwork and conduct insightful analyses by way of dynamic interactions. Ultimate for researchers, educators, and content material creators, Lyzr accelerates the method of deriving significant insights, producing summaries, and formulating inventive questions instantly from multimedia sources.
!pip set up lyzr youtube-transcript-api fpdf2 ipywidgets
!apt-get replace -qq && apt-get set up -y fonts-dejavu-core
We arrange the mandatory atmosphere for the tutorial. The primary command installs important Python libraries, together with lyzr for AI-powered chat, youtube-transcript-api for transcript extraction, fpdf2 for PDF technology, and ipywidgets for creating interactive chat interfaces. The second command ensures the DejaVu Sans font is put in on the system to assist full Unicode textual content rendering inside the generated PDF information.
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
os.environ['OPENAI_API_KEY'] = "YOUR_OPENAI_API_KEY_HERE"
We configure OpenAI API key entry for the tutorial. We import the os and openai modules, then retrieve the API key from atmosphere variables (or instantly set it through os.environ). This setup is crucial for leveraging OpenAI’s highly effective fashions inside the Lyzr framework.
import json
from lyzr import ChatBot
from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound, CouldNotRetrieveTranscript
from fpdf import FPDF
from ipywidgets import Textarea, Button, Output, Format
from IPython.show import show, Markdown
import re
Take a look at the complete Pocket book right here
We import important libraries required for the tutorial. It consists of json for information dealing with, Lyzr’s ChatBot for AI-driven chat capabilities, and YouTubeTranscriptApi for extracting transcripts from YouTube movies. Additionally, it brings in FPDF for PDF technology, ipywidgets for interactive UI elements, and IPython.show for rendering Markdown content material in notebooks. The re module can be imported for normal expression operations in textual content processing duties.
def transcript_to_pdf(video_id: str, output_pdf_path: str) -> bool:
"""
Obtain YouTube transcript (guide or auto) and write it right into a PDF
utilizing the system-installed DejaVuSans.ttf for full Unicode assist.
Fastened to deal with lengthy phrases and textual content formatting points.
"""
strive:
entries = YouTubeTranscriptApi.get_transcript(video_id)
besides (TranscriptsDisabled, NoTranscriptFound, CouldNotRetrieveTranscript):
strive:
entries = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])
besides Exception:
print(f"[!] No transcript for {video_id}")
return False
besides Exception as e:
print(f"[!] Error fetching transcript for {video_id}: {e}")
return False
textual content = "n".be part of(e['text'] for e in entries).strip()
if not textual content:
print(f"[!] Empty transcript for {video_id}")
return False
pdf = FPDF()
pdf.add_page()
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
strive:
if os.path.exists(font_path):
pdf.add_font("DejaVu", "", font_path)
pdf.set_font("DejaVu", measurement=10)
else:
pdf.set_font("Arial", measurement=10)
besides Exception:
pdf.set_font("Arial", measurement=10)
pdf.set_margins(20, 20, 20)
pdf.set_auto_page_break(auto=True, margin=25)
def process_text_for_pdf(textual content):
textual content = re.sub(r's+', ' ', textual content)
textual content = textual content.exchange('nn', 'n')
processed_lines = []
for paragraph in textual content.break up('n'):
if not paragraph.strip():
proceed
phrases = paragraph.break up()
processed_words = []
for phrase in phrases:
if len(phrase) > 50:
chunks = [word[i:i+50] for i in vary(0, len(phrase), 50)]
processed_words.prolong(chunks)
else:
processed_words.append(phrase)
processed_lines.append(' '.be part of(processed_words))
return processed_lines
processed_lines = process_text_for_pdf(textual content)
for line in processed_lines:
if line.strip():
strive:
pdf.multi_cell(0, 8, line.encode('utf-8', 'exchange').decode('utf-8'), align='L')
pdf.ln(2)
besides Exception as e:
print(f"[!] Warning: Skipped problematic line: {str(e)[:100]}...")
proceed
strive:
pdf.output(output_pdf_path)
print(f"[+] PDF saved: {output_pdf_path}")
return True
besides Exception as e:
print(f"[!] Error saving PDF: {e}")
return False
Take a look at the complete Pocket book right here
This operate, transcript_to_pdf, automates changing YouTube video transcripts into clear, readable PDF paperwork. It retrieves the transcript utilizing the YouTubeTranscriptApi, gracefully handles exceptions similar to unavailable transcripts, and codecs the textual content to keep away from points like lengthy phrases breaking the PDF structure. The operate additionally ensures correct Unicode assist by utilizing the DejaVuSans font (if accessible) and optimizes textual content for PDF rendering by splitting overly lengthy phrases and sustaining constant margins. It returns True if the PDF is generated efficiently or False if errors happen.
def create_interactive_chat(agent):
input_area = Textarea(
placeholder="Kind a query…", structure=Format(width="80%", top="80px")
)
send_button = Button(description="Ship", button_style="success")
output_area = Output(structure=Format(
border="1px stable grey", width="80%", top="200px", overflow='auto'
))
def on_send(btn):
query = input_area.worth.strip()
if not query:
return
with output_area:
print(f">> You: {query}")
strive:
print("<< Bot:", agent.chat(query), "n")
besides Exception as e:
print(f"[!] Error: {e}n")
send_button.on_click(on_send)
show(input_area, send_button, output_area)
Take a look at the complete Pocket book right here
This operate, create_interactive_chat, creates a easy and interactive chat interface inside Colab. Utilizing ipywidgets supplies a textual content enter space (Textarea) for customers to kind questions, a ship button (Button) to set off the chat, and an output space (Output) to show the dialog. When the consumer clicks ship, the entered query is handed to the Lyzr ChatBot agent, which generates and shows a response. This permits customers to interact in dynamic Q&A periods based mostly on the transcript evaluation, making the interplay like a stay dialog with the AI mannequin.
def principal():
video_ids = ["dQw4w9WgXcQ", "jNQXAC9IVRw"]
processed = []
for vid in video_ids:
pdf_path = f"{vid}.pdf"
if transcript_to_pdf(vid, pdf_path):
processed.append((vid, pdf_path))
else:
print(f"[!] Skipping {vid} — no transcript accessible.")
if not processed:
print("[!] No PDFs generated. Please strive different video IDs.")
return
first_vid, first_pdf = processed[0]
print(f"[+] Initializing PDF-chat agent for video {first_vid}…")
bot = ChatBot.pdf_chat(
input_files=[first_pdf]
)
questions = [
"Summarize the transcript in 2–3 sentences.",
"What are the top 5 insights and why?",
"List any recommendations or action items mentioned.",
"Write 3 quiz questions to test comprehension.",
"Suggest 5 creative prompts to explore further."
]
responses = {}
for q in questions:
print(f"[?] {q}")
strive:
resp = bot.chat(q)
besides Exception as e:
resp = f"[!] Agent error: {e}"
responses[q] = resp
print(f"[/] {resp}n" + "-"*60 + "n")
with open('responses.json','w',encoding='utf-8') as f:
json.dump(responses,f,indent=2)
md = "# Transcript Evaluation Reportnn"
for q,a in responses.gadgets():
md += f"## Q: {q}n{a}nn"
with open('report.md','w',encoding='utf-8') as f:
f.write(md)
show(Markdown(md))
if len(processed) > 1:
print("[+] Producing comparability…")
_, pdf1 = processed[0]
_, pdf2 = processed[1]
compare_bot = ChatBot.pdf_chat(
input_files=[pdf1, pdf2]
)
comparability = compare_bot.chat(
"Evaluate the primary themes of those two movies and spotlight key variations."
)
print("[+] Comparability Consequence:n", comparability)
print("n=== Interactive Chat (Video 1) ===")
create_interactive_chat(bot)
Take a look at the complete Pocket book right here
Our principal() operate serves because the core driver for the whole tutorial pipeline. It processes an inventory of YouTube video IDs, changing accessible transcripts into PDF information utilizing the transcript_to_pdf operate. As soon as PDFs are generated, a Lyzr PDF-chat agent is initialized on the primary PDF, permitting the mannequin to reply predefined questions similar to summarizing the content material, figuring out insights, and producing quiz questions. The solutions are saved in a responses.json file and formatted right into a Markdown report (report.md). If a number of PDFs are created, the operate compares them utilizing the Lyzr agent to spotlight key variations between the movies. Lastly, it launches an interactive chat interface with the consumer, enabling dynamic conversations based mostly on the transcript content material, showcasing the ability of Lyzr for seamless PDF evaluation and AI-driven interactions.
if __name__ == "__main__":
principal()
We make sure that the primary() operate runs solely when the script is executed instantly, not when it’s imported as a module. It’s a finest observe in Python scripts to regulate execution circulate.
In conclusion, by integrating Lyzr into our workflow as demonstrated on this tutorial, we will effortlessly remodel YouTube movies into insightful, actionable information. Lyzr’s clever PDF-chat functionality simplifies extracting core themes and producing complete summaries, and likewise permits partaking, interactive exploration of content material by way of an intuitive conversational interface. Adopting Lyzr empowers customers to unlock deeper insights and considerably enhances productiveness when working with video transcripts, whether or not for tutorial analysis, academic functions, or inventive content material evaluation.
Take a look at the Pocket book right here. All credit score for this analysis goes to the researchers of this undertaking. Additionally, be happy to comply with us on Twitter and don’t overlook to affix our 95k+ ML SubReddit and Subscribe to our Publication.
Asif Razzaq is the CEO of Marktechpost Media Inc.. As a visionary entrepreneur and engineer, Asif is dedicated to harnessing the potential of Synthetic Intelligence for social good. His most up-to-date endeavor is the launch of an Synthetic Intelligence Media Platform, Marktechpost, which stands out for its in-depth protection of machine studying and deep studying information that’s each technically sound and simply comprehensible by a large viewers. The platform boasts of over 2 million month-to-month views, illustrating its reputation amongst audiences.