A Coding Implementation to Construct an Interactive Transcript and PDF Evaluation with Lyzr Chatbot Framework


On this tutorial, we introduce a streamlined method 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. Ideally suited for researchers, educators, and content material creators, Lyzr accelerates the method of deriving significant insights, producing summaries, and formulating artistic questions immediately 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 required 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 era, 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 immediately 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, Structure
from IPython.show import show, Markdown
import re

Take a look at the complete Notebook 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 era, ipywidgets for interactive UI parts, 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.
    Mounted 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", dimension=10)
        else:
            pdf.set_font("Arial", dimension=10)
    besides Exception:
        pdf.set_font("Arial", dimension=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.change('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', 'change').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 Notebook 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 resembling 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 out there) 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=Structure(width="80%", peak="80px")
    )
    send_button = Button(description="Ship", button_style="success")
    output_area = Output(structure=Structure(
        border="1px strong grey", width="80%", peak="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 Notebook 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 person clicks ship, the entered query is handed to the Lyzr ChatBot agent, which generates and shows a response. This allows customers to interact in dynamic Q&A periods based mostly on the transcript evaluation, making the interplay like a dwell dialog with the AI mannequin.

def primary():
    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 out there.")


    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 principle 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 Notebook here

Our primary() operate serves because the core driver for the whole tutorial pipeline. It processes an inventory of YouTube video IDs, changing out there 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 resembling 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 focus on key variations between the movies. Lastly, it launches an interactive chat interface with the person, 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__":
    primary()

We be certain that the principle() operate runs solely when the script is executed immediately, not when it’s imported as a module. It’s a finest observe in Python scripts to manage execution circulate.

In conclusion, by integrating Lyzr into our workflow as demonstrated on this tutorial, we will effortlessly remodel YouTube movies into insightful, actionable data. Lyzr’s clever PDF-chat functionality simplifies extracting core themes and producing complete summaries, and in addition 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 artistic content material evaluation.


Take a look at the Notebook here. All credit score for this analysis goes to the researchers of this challenge. Additionally, be at liberty to observe us on Twitter and don’t overlook to affix our 95k+ ML SubReddit and Subscribe to our Newsletter.


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.

Leave a Reply

Your email address will not be published. Required fields are marked *