File size: 2,090 Bytes
95aa78d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from langchain_groq import ChatGroq
from youtube_transcript_api import YouTubeTranscriptApi
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
import os

def get_transcript(video_id):
    def transcript_to_string(transcript):
        transcript_text = '\n'.join([item['text'] for item in transcript])
        return transcript_text

    try:
        transcripts = YouTubeTranscriptApi.list_transcripts(video_id)

        for transcript in transcripts:
            if transcript.is_translatable:
                transcript = transcript.fetch()
                return transcript_to_string(transcript)

        transcript = transcripts.find_manually_created_transcript(['en', 'tr']).fetch()
        return transcript_to_string(transcript)

    except Exception as e:
        return "Sorry, the video cannot be transcribed."


def summarize_video(video_url, language):
    video_id = video_url.split('v=')[-1].split('&')[0]
    transcript = get_transcript(video_id)

    model = ChatGroq(
        model="llama-3.1-70b-versatile",
        temperature=0,
        max_tokens=None,
        timeout=None,
        max_retries=2,
    )

    system_template = """Below you will see a text. Read the text. First provide the major points that they discuss and then provide summaries for each major point. Your response should be in the language specified below. 
    Transcript: {transcript}
    Language: {language}"""

    prompt_template = ChatPromptTemplate.from_messages(
        [("system", system_template)]
    )

    parser = StrOutputParser()

    chain = prompt_template | model | parser

    response = chain.invoke({"transcript": transcript, "language": language})
    return response


iface = gr.Interface(
    fn=summarize_video,
    inputs=[
        gr.Textbox(label="YouTube Video URL"),
        gr.Textbox(label="Language for Summary")
    ],
    outputs="text",
    title="YouTube Video Summarizer",
    description="Enter a YouTube video URL and the desired language for the summary."
)

iface.launch()