milind1989 commited on
Commit
559f52d
·
verified ·
1 Parent(s): 2388aeb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import whisper
3
+ from selenium import webdriver
4
+ from selenium.webdriver.common.by import By
5
+ from selenium.webdriver.support.ui import WebDriverWait
6
+ from selenium.webdriver.support import expected_conditions as EC
7
+ from youtube_transcript_api import YouTubeTranscriptApi
8
+ import yt_dlp
9
+ import tempfile
10
+ import os
11
+ import re
12
+ import logging
13
+
14
+ # Setup logging
15
+ logging.basicConfig(level=logging.INFO)
16
+ logger = logging.getLogger(__name__)
17
+
18
+ # Initialize Whisper model
19
+ model = whisper.load_model("tiny")
20
+
21
+ def extract_video_id(url):
22
+ """Extract video ID from YouTube URL"""
23
+ patterns = [
24
+ r'(?:youtube\.com\/watch\?v=|youtu.be\/)([\w-]+)',
25
+ r'(?:youtube\.com\/embed\/)([\w-]+)',
26
+ r'(?:youtube\.com\/v\/)([\w-]+)',
27
+ ]
28
+
29
+ for pattern in patterns:
30
+ match = re.search(pattern, url)
31
+ if match:
32
+ return match.group(1)
33
+ return None
34
+
35
+ def try_transcript_api(url):
36
+ """Method 1: Try using YouTube Transcript API"""
37
+ logger.info("Trying YouTube Transcript API...")
38
+ video_id = extract_video_id(url)
39
+ if not video_id:
40
+ raise ValueError("Could not extract video ID")
41
+
42
+ transcript = YouTubeTranscriptApi.get_transcript(video_id)
43
+ return " ".join([entry['text'] for entry in transcript])
44
+
45
+ def setup_chrome_options():
46
+ """Setup Chrome options for Docker environment"""
47
+ chrome_options = webdriver.ChromeOptions()
48
+ chrome_options.add_argument('--headless=new') # Updated headless mode
49
+ chrome_options.add_argument('--no-sandbox')
50
+ chrome_options.add_argument('--disable-dev-shm-usage')
51
+ chrome_options.add_argument('--disable-gpu')
52
+ chrome_options.add_argument('--disable-software-rasterizer')
53
+ chrome_options.add_argument('--remote-debugging-port=9222')
54
+ chrome_options.add_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36')
55
+ return chrome_options
56
+
57
+ def try_selenium(url):
58
+ """Method 2: Try using Selenium"""
59
+ logger.info("Trying Selenium method...")
60
+ try:
61
+ options = setup_chrome_options()
62
+ driver = webdriver.Chrome(options=options)
63
+
64
+ logger.info("Navigating to URL...")
65
+ driver.get(url)
66
+
67
+ # Wait for video player to load
68
+ logger.info("Waiting for video player...")
69
+ WebDriverWait(driver, 20).until(
70
+ EC.presence_of_element_located((By.ID, "movie_player"))
71
+ )
72
+
73
+ # Try to enable captions if they're not already enabled
74
+ try:
75
+ captions_button = WebDriverWait(driver, 5).until(
76
+ EC.presence_of_element_located((By.CLASS_NAME, "ytp-subtitles-button"))
77
+ )
78
+ if "ytp-subtitles-button-active" not in captions_button.get_attribute("class"):
79
+ captions_button.click()
80
+ except Exception as e:
81
+ logger.warning(f"Could not enable captions: {str(e)}")
82
+
83
+ # Wait for captions