HomeArtificial IntelligenceA Coding Implementation to Construct an Interactive Transcript and PDF Evaluation with...

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


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 via dynamic interactions. Very best for researchers, educators, and content material creators, Lyzr accelerates the method of deriving significant insights, producing summaries, and formulating inventive questions immediately from multimedia assets.

!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 surroundings 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 recordsdata.

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 surroundings variables (or immediately set it by way of os.environ). This setup is important 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

Try the total 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 parts, and IPython.show for rendering Markdown content material in notebooks. The re module can also 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 (handbook 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 a 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.cut up('n'):
            if not paragraph.strip():
                proceed


            phrases = paragraph.cut 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.lengthen(chunks)
                else:
                    processed_words.append(phrase)


            processed_lines.append(' '.be a 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

Try the total Pocket book right here

This perform, 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 perform 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="Sort a query…", structure=Format(width="80%", peak="80px")
    )
    send_button = Button(description="Ship", button_style="success")
    output_area = Output(structure=Format(
        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("

Try the total Pocket book right here

This perform, create_interactive_chat, creates a easy and interactive chat interface inside Colab. Utilizing ipywidgets offers 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 important():
    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(
            "Examine 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)

Try the total Pocket book right here

Our important() perform serves because the core driver for the whole tutorial pipeline. It processes a listing of YouTube video IDs, changing out there transcripts into PDF recordsdata utilizing the transcript_to_pdf perform. 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 perform 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__":
    important()

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

In conclusion, by integrating Lyzr into our workflow as demonstrated on this tutorial, we will effortlessly rework YouTube movies into insightful, actionable data. Lyzr’s clever PDF-chat functionality simplifies extracting core themes and producing complete summaries, and likewise allows partaking, interactive exploration of content material via 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 educational analysis, academic functions, or inventive content material evaluation.


Try the Pocket book right here. All credit score for this analysis goes to the researchers of this mission. Additionally, be at liberty to observe us on Twitter and don’t neglect to affix our 95k+ ML SubReddit and Subscribe to our E-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 recognition amongst audiences.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments