AI

A Coding Implementation to Build an Interactive Transcript and PDF Analysis with Lyzr Chatbot Framework

In this tutorial, we present a simplified approach to extracting texts on YouTube, processing and analyzing them LyzrAn advanced framework that works artificial intelligence designed to simplify interaction with text data. Take advantage of the intuitive Chatbot interface in Lyzr along with YouTube-TRARANSCRIPT-API and FPDF, users can transform video content without hassting into organized PDF documents and perform insight through dynamic reactions. Ideal for researchers, teachers and content creators, lyzr accelerates the process of extracting meaningful visions, generating summaries, and formulating creative questions directly from multimedia resources.

!pip install lyzr youtube-transcript-api fpdf2 ipywidgets
!apt-get update -qq && apt-get install -y fonts-dejavu-core

We created the necessary environment for the educational program. The first command installs the basic Python libraries, including Lyzr chat that works with male intelligence, YouTube-TRARANSCRIPT-API to extract text, FPDF2 to generate PDF, and iPywidgets to create interactive chat facades. The second matter guarantees the installation of the Dejavu Sans line on the system to support the full Unicode Text display within the created PDF files.

import os
import openai


openai.api_key = os.getenv("OPENAI_API_KEY")
os.environ['OPENAI_API_KEY'] = "YOUR_OPENAI_API_KEY_HERE"

We configure the access of the API Openai key for the educational program. We import the operating system units and Openai, then we return the API key from the environmental variables (or appoint it directly via OS.NVIRON). This setting is necessary to take advantage of the strong Openai models in the lyzr frame.

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, Layout
from IPython.display import display, Markdown
import re

Check the full notebook here

We import the basic libraries required for the educational program. JSON includes data processing, Chatbot from Lyzr chat capabilities that AI and Youtubeetranscriptapi are to extract text from YouTube videos. Also, it brings FPDF to generate PDF, iPywidgets for the interactive user interface components, and iPython.display to provide Markdown content in notebooks. The RE unit is also imported for regular expression operations in text processing tasks.

def transcript_to_pdf(video_id: str, output_pdf_path: str) -> bool:
    """
    Download YouTube transcript (manual or auto) and write it into a PDF
    using the system-installed DejaVuSans.ttf for full Unicode support.
    Fixed to handle long words and text formatting issues.
    """
    try:
        entries = YouTubeTranscriptApi.get_transcript(video_id)
    except (TranscriptsDisabled, NoTranscriptFound, CouldNotRetrieveTranscript):
        try:
            entries = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])
        except Exception:
            print(f"[!] No transcript for {video_id}")
            return False
    except Exception as e:
        print(f"[!] Error fetching transcript for {video_id}: {e}")
        return False


    text = "\n".join(e['text'] for e in entries).strip()
    if not text:
        print(f"[!] Empty transcript for {video_id}")
        return False


    pdf = FPDF()
    pdf.add_page()


    font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
    try:
        if os.path.exists(font_path):
            pdf.add_font("DejaVu", "", font_path)
            pdf.set_font("DejaVu", size=10)
        else:
            pdf.set_font("Arial", size=10)
    except Exception:
        pdf.set_font("Arial", size=10)


    pdf.set_margins(20, 20, 20)
    pdf.set_auto_page_break(auto=True, margin=25)


    def process_text_for_pdf(text):
        text = re.sub(r'\s+', ' ', text)
        text = text.replace('\n\n', '\n')


        processed_lines = []
        for paragraph in text.split('\n'):
            if not paragraph.strip():
                continue


            words = paragraph.split()
            processed_words = []
            for word in words:
                if len(word) > 50:
                    chunks = [word[i:i+50] for i in range(0, len(word), 50)]
                    processed_words.extend(chunks)
                else:
                    processed_words.append(word)


            processed_lines.append(' '.join(processed_words))


        return processed_lines


    processed_lines = process_text_for_pdf(text)


    for line in processed_lines:
        if line.strip():
            try:
                pdf.multi_cell(0, 8, line.encode('utf-8', 'replace').decode('utf-8'), align='L')
                pdf.ln(2)
            except Exception as e:
                print(f"[!] Warning: Skipped problematic line: {str(e)[:100]}...")
                continue


    try:
        pdf.output(output_pdf_path)
        print(f"[+] PDF saved: {output_pdf_path}")
        return True
    except Exception as e:
        print(f"[!] Error saving PDF: {e}")
        return False

Check the full notebook here

This function, transcript_to_Pdf, converts YouTube videos to clean and readable PDF documents. It recalls the text using Youtubetranscriptapi, and deals with exceptions such as the texts that are not available, and coordinate the text to avoid problems such as long words that break the PDF layout. The function also guarantees the appropriate Unicode support with the Dejavusans (if available) and improve the text to present PDF by dividing excessive words and keeping consistent margins. It returns correctly if the PDF is successfully created or wrong in the event of errors.

def create_interactive_chat(agent):
    input_area = Textarea(
        placeholder="Type a question…", layout=Layout(width="80%", height="80px")
    )
    send_button = Button(description="Send", button_style="success")
    output_area = Output(layout=Layout(
        border="1px solid gray", width="80%", height="200px", overflow='auto'
    ))


    def on_send(btn):
        question = input_area.value.strip()
        if not question:
            return
        with output_area:
            print(f">> You: {question}")
            try:
                print("<< Bot:", agent.chat(question), "\n")
            except Exception as e:
                print(f"[!] Error: {e}\n")


    send_button.on_click(on_send)
    display(input_area, send_button, output_area)

Check the full notebook here

This function is created, Create_interactive_Chat, and a simple and interactive chat interface inside the colum. The use of iPywidgets provides a text input area (Textarea) for users to write questions, send a (button) chat, and the output area (output) to display the conversation. When sending user clicks, the question that was inserted is passed to the lyzr chatbot agent, which creates and displays a response. This enables users to engage in a dynamic question and answer sessions based on text analysis, which makes the interaction like a direct conversation with the artificial intelligence model.

def main():
    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 available.")


    if not processed:
        print("[!] No PDFs generated. Please try other 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}")
        try:
            resp = bot.chat(q)
        except 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 Analysis Report\n\n"
    for q,a in responses.items():
        md += f"## Q: {q}\n{a}\n\n"
    with open('report.md','w',encoding='utf-8') as f:
        f.write(md)


    display(Markdown(md))


    if len(processed) > 1:
        print("[+] Generating comparison…")
        _, pdf1 = processed[0]
        _, pdf2 = processed[1]
        compare_bot = ChatBot.pdf_chat(
            input_files=[pdf1, pdf2]
        )
        comparison = compare_bot.chat(
            "Compare the main themes of these two videos and highlight key differences."
        )
        print("[+] Comparison Result:\n", comparison)


    print("\n=== Interactive Chat (Video 1) ===")
    create_interactive_chat(bot)

Check the full notebook here

Main function () works as a basic operating program for the entire tutorial pipeline. It processes a list of YouTube videos, and converted available texts into PDF files using the Transcript_to_PDF function. Once you create PDF files, the Kyzr PDF-CAT factor is prepared on the first PDF, allowing the model to answer predetermined questions such as summary of content, determining visions, and creating test questions. The answers are stored in the Responses.json file and its format in the Markdown (Report.MD) report. If multiple PDFS is created, the function is compared to using Lyzr agent to highlight the main differences between videos. Finally, it runs an interactive chat interface with the user, allowing dynamic conversations based on the text content, and shows the Lyzr power to analyze a smooth PDF and the AI’s reactions.

if __name__ == "__main__":
    main()

We guarantee the operation of the main function () only when implementing the text program directly, not when it is imported as a stereotype. It is the best practices in Python textual programs to control implementation flow.

In conclusion, by integrating Lyzr into the course of our work as shown in this tutorial, we can convert YouTube videos without trouble into insight and implemented. The lyzr smart ability to extract basic features and generate comprehensive summaries also allows interactive exploration of the content through an intuitive conversation interface. The adoption of lyzr enables users to open deeper visions and promote productivity significantly when working with video texts, whether for academic research, educational purposes or creative content analysis.


Check the notebook here. All the credit for this research goes to researchers in this project. Also, do not hesitate to follow us twitter And do not forget to join 95K+ ML Subreddit And subscribe to Our newsletter.


Asif Razzaq is the CEO of Marktechpost Media Inc .. As a pioneer and vision engineer, ASIF is committed to harnessing the potential of artificial intelligence for social goodness. His last endeavor is to launch the artificial intelligence platform, Marktechpost, which highlights its in -depth coverage of machine learning and deep learning news, which is technically sound and can be easily understood by a wide audience. The platform is proud of more than 2 million monthly views, which shows its popularity among the masses.

Don’t miss more hot News like this! Click here to discover the latest in AI news!

2025-05-28 04:51:00

Related Articles

Back to top button