File size: 3,343 Bytes
2e56c11
b13d50e
 
 
06bb06f
 
b13d50e
 
06bb06f
b13d50e
 
 
 
 
2e56c11
b13d50e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd2d49f
2e56c11
 
 
 
 
b13d50e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from youtube_transcript_api import YouTubeTranscriptApi
from urllib.parse import urlparse, parse_qs
from pytube import YouTube
from huggingface_hub import InferenceClient
import gradio as gr

model_name = "mistralai/Mixtral-8x7B-Instruct-v0.1"
client = InferenceClient(model=model_name)


def transcribe_video(url):
    video_id = parse_youtube_url(url)
    if video_id:
        video_metadata = get_video_metadata(video_id)
        transcript_content = get_transcript_content(video_id)
        transcript_summary = summarise_transcript(transcript_content)
        return (
            f"Title: {video_metadata['title']}\nAuthor: {video_metadata['author']}",
            transcript_content,
            transcript_summary,
        )
    else:
        return None


def parse_youtube_url(url):
    parsed_url = urlparse(url)
    video_id = parse_qs(parsed_url.query).get("v")
    if video_id:
        return video_id[0]
    return None


def get_video_metadata(video_id):
    yt = YouTube(f"https://www.youtube.com/watch?v={video_id}")
    title = yt.title or "Unknown"
    author = yt.author or "Unknown"

    metadata = {"title": title, "author": author}

    return metadata


def get_transcript_content(video_id):
    try:
        transcript = YouTubeTranscriptApi.get_transcript(video_id)
        transcript_content = parse_transcript(transcript)
        return transcript_content
    except Exception as e:
        raise e


def parse_transcript(transcript):
    content = " ".join(
        map(
            lambda transcript_piece: transcript_piece["text"].strip(" "),
            transcript,
        )
    )
    return content


def summarise_transcript(transcript_content):
    prompt = f"""Provide a summary of the following video transcription in 150-350 words, focusing on the key points and core ideas discussed: {transcript_content}"""

    message = [{"role": "user", "content": prompt}]

    result = client.chat_completion(
        messages=message,
        max_tokens=2048,
        temperature=0.1,
    )

    return result.choices[0].message["content"].strip()


with gr.Blocks(theme=gr.themes.Base()) as demo:
    gr.Markdown("<H1>YoutTube Transcriber</H1>")
    gr.Markdown(
        "<H3>Provide a link to a YouTube video and get a transcription and summary</H3>"
    )
    gr.Markdown(
        "<H6>This project uses the youtube_transcript_api to fetch a transcript from a YouTube link, pytube to get video metadata, and Mistral 7B to generate a summary.</H6>"
    )

    with gr.Row():
        with gr.Column(scale=1):
            video_link = gr.Textbox(
                label="Link to video",
                value="https://www.youtube.com/watch?v=ZIyB9e_7a4c",
            )
            transcribe_btn = gr.Button(
                value="Transcribe & Summarise ⚡️", variant="primary"
            )

        with gr.Column(scale=5):
            video_info = gr.Textbox(label="Video Info")
            transcription = gr.TextArea(
                label="Transcription", scale=1, lines=12, max_lines=12
            )
            transcription_summary = gr.TextArea(
                label="Summary", scale=1, lines=12, max_lines=12
            )

    transcribe_btn.click(
        fn=transcribe_video,
        inputs=video_link,
        outputs=[video_info, transcription, transcription_summary],
    )

demo.launch()