File size: 3,341 Bytes
81000a9
2224634
f017bfe
2224634
 
 
81000a9
0b410bd
2224634
81000a9
2224634
 
 
 
 
 
81000a9
11a3ad8
2224634
f017bfe
 
2224634
 
f017bfe
 
2224634
 
11a3ad8
0b410bd
 
 
 
 
 
2224634
0b410bd
 
 
 
 
 
 
81000a9
2224634
 
 
 
11a3ad8
2224634
11a3ad8
2224634
 
81000a9
2224634
0b410bd
11a3ad8
0b410bd
81000a9
2224634
 
 
 
 
81000a9
0b410bd
2224634
 
 
11a3ad8
 
2224634
 
11a3ad8
2224634
 
 
 
 
 
 
 
 
0b410bd
 
 
 
 
 
 
 
 
 
 
 
81000a9
0b410bd
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
import os
import gradio as gr
import yt_dlp
import whisper
from transformers import pipeline, MarianMTModel, MarianTokenizer
import torch

# Load Whisper model
whisper_model = whisper.load_model("small")

# Load summarizer
summarizer = pipeline("summarization", model="Falconsai/text_summarization")

# Load translation model (multilingual to English)
translation_tokenizer = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-mul-en")
translation_model = MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-mul-en")

def download_audio(youtube_url):
    output_file = "audio.webm"
    ydl_opts = {
        'format': 'bestaudio/best',
        'outtmpl': output_file,
        'quiet': True,
    }
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        ydl.download([youtube_url])
    return output_file

def get_thumbnail(youtube_url):
    ydl_opts = {'quiet': True}
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        info = ydl.extract_info(youtube_url, download=False)
        return info.get("thumbnail", "")

def translate_to_english(text):
    chunks = [text[i:i+500] for i in range(0, len(text), 500)]
    translated = []
    for chunk in chunks:
        inputs = translation_tokenizer(chunk, return_tensors="pt", truncation=True, max_length=512)
        output = translation_model.generate(**inputs, max_length=512)
        translated.append(translation_tokenizer.decode(output[0], skip_special_tokens=True))
    return " ".join(translated)

def process_video(url):
    audio_path = download_audio(url)
    result = whisper_model.transcribe(audio_path)
    transcription = result["text"]

    translated_text = translate_to_english(transcription)

    # Summarize
    summary = summarizer(translated_text, max_length=130, min_length=30, do_sample=False)[0]["summary_text"]

    # Get thumbnail
    thumbnail_url = get_thumbnail(url)

    return transcription, translated_text, summary, thumbnail_url

def download_summary(text):
    filename = "summary.txt"
    with open(filename, "w", encoding="utf-8") as f:
        f.write(text)
    return filename

# UI
with gr.Blocks(theme=gr.themes.Soft(), title="πŸŽ₯ YouTube Video Summarizer with LLaMA") as demo:
    gr.Markdown("## 🧠 Multilingual YouTube Summarizer")
    gr.Markdown("Upload a video link and get the transcript, English translation, and summary.")

    with gr.Row():
        youtube_input = gr.Text(label="YouTube Video URL", placeholder="https://www.youtube.com/watch?v=...")
        submit_btn = gr.Button("Transcribe & Summarize")

    with gr.Row():
        with gr.Column():
            transcript_output = gr.Textbox(label="πŸ”Š Original Transcript", lines=10)
            translation_output = gr.Textbox(label="🌍 Translated to English", lines=10)
            summary_output = gr.Textbox(label="🧾 Summary", lines=10)
            download_btn = gr.Button("πŸ“₯ Download Summary")
            download_file = gr.File(label="Download Link")
        video_thumb = gr.Image(label="🎞️ Video Thumbnail", width=256)

    # Button actions
    submit_btn.click(
        fn=process_video,
        inputs=[youtube_input],
        outputs=[transcript_output, translation_output, summary_output, video_thumb]
    )

    download_btn.click(
        fn=download_summary,
        inputs=[summary_output],
        outputs=[download_file]
    )

demo.launch(share=True)