|
import requests |
|
import gradio as gr |
|
import os |
|
import time |
|
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 get_po_token(): |
|
try: |
|
response = requests.get("http://localhost:3001/get-token") |
|
response.raise_for_status() |
|
return response.json().get("poToken") |
|
except Exception as e: |
|
print(f"Error fetching PO token: {e}") |
|
return None |
|
|
|
def process_youtube_link(youtube_url): |
|
try: |
|
print(f"Received YouTube URL: {youtube_url}") |
|
|
|
po_token = get_po_token() |
|
if not po_token: |
|
return "Error: Unable to fetch PO token.", "" |
|
|
|
|
|
yt = YouTube(youtube_url, po_token=po_token) |
|
title = yt.title |
|
print(f"Downloading: {title}") |
|
|
|
ys = yt.streams.get_highest_resolution() |
|
video_path = f"{title}.mp4" |
|
ys.download(filename=video_path) |
|
|
|
|
|
if os.path.exists(video_path): |
|
print(f"Download successful: {video_path}") |
|
print("Files in current directory:", os.listdir(".")) |
|
else: |
|
print("Download failed!") |
|
|
|
|
|
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"] |
|
|
|
return transcribed_text, summary |
|
|
|
except Exception as e: |
|
print(f"Error: {str(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 Summarizer", |
|
description="Enter a YouTube link, and this app will summarize the content of the video.", |
|
) |
|
|
|
iface.launch() |
|
|