File size: 2,490 Bytes
3c10179 356bab3 5d6bc67 1a77d2d 949b582 93d849e 949b582 00318c5 949b582 79348af 00318c5 a952e20 3c10179 949b582 356bab3 3c10179 d9c9d47 d52f724 d9c9d47 d52f724 6421aad d52f724 949b582 d9c9d47 949b582 d9c9d47 949b582 d9c9d47 949b582 356bab3 d52f724 356bab3 00318c5 356bab3 949b582 00318c5 356bab3 949b582 |
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 |
import requests
import gradio as gr
import os
import time
from pytubefix import YouTube
from moviepy.editor import VideoFileClip
from transformers import pipeline
# Whisper model
asr = pipeline("automatic-speech-recognition", model="distil-whisper/distil-small.en")
# Summarization model
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
# Function to fetch PO Token from the Node.js server
def get_po_token():
try:
response = requests.get("http://localhost:3001/get-token") # Call Node.js API
response.raise_for_status()
return response.json().get("poToken") # Extract token
except Exception as e:
print(f"Error fetching PO token: {e}")
return None # Return None if the request fails
def process_youtube_link(youtube_url):
try:
print(f"Received YouTube URL: {youtube_url}") # Debugging
po_token = get_po_token()
if not po_token:
return "Error: Unable to fetch PO token.", ""
# Use the token with pytubefix
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)
# Log download success
if os.path.exists(video_path):
print(f"Download successful: {video_path}")
print("Files in current directory:", os.listdir("."))
else:
print("Download failed!")
# Extract Audio
audio_path = f"{title}.wav"
video = VideoFileClip(video_path)
video.audio.write_audiofile(audio_path, codec="pcm_s16le")
# Transcribe Audio
transcription = asr(audio_path, return_timestamps=True)
transcribed_text = transcription["text"]
# Summarize Transcription
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)}") # Log errors
return f"Error: {str(e)}", ""
# Gradio Interface
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()
|