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()