import os import gradio as gr import yt_dlp import whisper from pydub import AudioSegment from transformers import pipeline from youtube_transcript_api import YouTubeTranscriptApi from urllib.parse import urlparse, parse_qs import openai import json import tempfile import re import torch from googleapiclient.discovery import build # Add the import for Google API client # Function to extract YouTube video ID def extract_video_id(url): try: parsed_url = urlparse(url) if "youtube.com" in parsed_url.netloc: query_params = parse_qs(parsed_url.query) return query_params.get('v', [None])[0] elif "youtu.be" in parsed_url.netloc: return parsed_url.path.strip("/") return None except Exception: return None # Function to get video duration def get_video_duration(video_id, api_key): try: youtube = build("youtube", "v3", developerKey=api_key) request = youtube.videos().list(part="contentDetails", id=video_id) response = request.execute() if response["items"]: duration = response["items"][0]["contentDetails"]["duration"] match = re.match(r'PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?', duration) hours = int(match.group(1)) if match.group(1) else 0 minutes = int(match.group(2)) if match.group(2) else 0 seconds = int(match.group(3)) if match.group(3) else 0 return hours * 60 + minutes + seconds / 60 return None except Exception: return None # Download and transcribe with Whisper def download_and_transcribe_with_whisper(youtube_url): try: with tempfile.TemporaryDirectory() as temp_dir: temp_audio_file = os.path.join(temp_dir, "audio.mp3") ydl_opts = { 'format': 'bestaudio/best', 'outtmpl': temp_audio_file, 'extractaudio': True, } with yt_dlp.YoutubeDL(ydl_opts) as ydl: ydl.download([youtube_url]) audio = AudioSegment.from_file(temp_audio_file) wav_file = os.path.join(temp_dir, "audio.wav") audio.export(wav_file, format="wav") model = whisper.load_model("large") result = model.transcribe(wav_file) return result['text'] except Exception: return None # Function to summarize using Hugging Face def summarize_text_huggingface(text): summarizer = pipeline("summarization", model="facebook/bart-large-cnn", device=0 if torch.cuda.is_available() else -1) max_input_length = 1024 chunk_overlap = 100 text_chunks = [ text[i:i + max_input_length] for i in range(0, len(text), max_input_length - chunk_overlap) ] summaries = [ summarizer(chunk, max_length=100, min_length=50, do_sample=False)[0]['summary_text'] for chunk in text_chunks ] return " ".join(summaries) # Function to generate optimized content with OpenAI def generate_optimized_content(api_key, summarized_transcript): openai.api_key = api_key prompt = f""" Analyze the following summarized YouTube video transcript and: 1. Extract the top 10 keywords. 2. Generate an optimized title (less than 65 characters). 3. Create an engaging description. 4. Generate related tags for the video. Summarized Transcript: {summarized_transcript} Provide the results in the following JSON format: {{ "keywords": ["keyword1", "keyword2", ..., "keyword10"], "title": "Generated Title", "description": "Generated Description", "tags": ["tag1", "tag2", ..., "tag10"] }} """ try: response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are an SEO expert."}, {"role": "user", "content": prompt} ] ) response_content = response['choices'][0]['message']['content'] return json.loads(response_content) except Exception: return None # Main Gradio function def process_video(youtube_url, youtube_api_key, openai_api_key): video_id = extract_video_id(youtube_url) if not video_id: return "Invalid YouTube URL.", "", "" video_length = get_video_duration(video_id, youtube_api_key) if not video_length: return "Error fetching video duration.", "", "" transcript = download_and_transcribe_with_whisper(youtube_url) if not transcript: return "Error fetching transcript.", "", "" summary = summarize_text_huggingface(transcript) optimized_content = generate_optimized_content(openai_api_key, summary) return summary, json.dumps(optimized_content, indent=4), transcript # Gradio Interface youtube_api_key = os.getenv("YOUTUBE_API_KEY") openai_api_key = os.getenv("OPENAI_API_KEY") gr.Interface( fn=lambda youtube_url: process_video(youtube_url, youtube_api_key, openai_api_key), inputs="text", outputs=["text", "text", "text"], title="YouTube Transcript Summarizer", description="Enter a YouTube URL to extract, summarize, and optimize content.", ).launch()