|
import gradio as gr |
|
import os |
|
from pytubefix import YouTube |
|
from moviepy.editor import VideoFileClip |
|
from transformers import pipeline |
|
|
|
|
|
asr = pipeline("automatic-speech-recognition", model="distil-whisper/distil-small.en") |
|
|
|
|
|
summarizer = pipeline("summarization", model="facebook/bart-large-cnn") |
|
|
|
def process_youtube_link(youtube_url): |
|
try: |
|
|
|
yt = YouTube(youtube_url) |
|
title = yt.title |
|
print(f"Downloading: {title}") |
|
|
|
video_stream = yt.streams.get_highest_resolution() |
|
if not video_stream: |
|
return "Error: No available video stream", "" |
|
|
|
video_path = f"{title}.mp4" |
|
video_stream.download(filename=video_path) |
|
|
|
|
|
audio_path = f"{title}.wav" |
|
video = VideoFileClip(video_path) |
|
video.audio.write_audiofile(audio_path, codec="pcm_s16le") |
|
|
|
|
|
transcription = asr(audio_path, return_timestamps=True) |
|
transcribed_text = transcription["text"] |
|
|
|
|
|
summary = summarizer(transcribed_text, max_length=150, min_length=50, do_sample=False)[0]["summary_text"] |
|
|
|
|
|
os.remove(video_path) |
|
os.remove(audio_path) |
|
|
|
return transcribed_text, summary |
|
|
|
except Exception as e: |
|
return f"Error: {str(e)}", "" |
|
|
|
|
|
iface = gr.Interface( |
|
fn=process_youtube_link, |
|
inputs=gr.Textbox(label="Enter YouTube URL"), |
|
outputs=[gr.Textbox(label="Transcription"), gr.Textbox(label="Summary")], |
|
title="YouTube Video Transcriber & Summarizer", |
|
description="Enter a YouTube link, and this app will transcribe and summarize the audio.", |
|
) |
|
|
|
iface.launch() |
|
|