File size: 8,466 Bytes
ff027b3
 
 
 
 
85563e1
9246621
ff027b3
85563e1
 
 
 
 
 
 
 
 
 
 
bfc16ca
 
85563e1
 
 
 
 
 
ff027b3
85563e1
ff027b3
 
 
85563e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff027b3
 
85563e1
ff027b3
 
9246621
 
85563e1
 
9246621
 
 
 
d185723
85563e1
ff027b3
 
 
85563e1
d185723
 
 
 
85563e1
 
 
 
 
 
d185723
 
 
 
 
 
 
ff027b3
85563e1
 
ff027b3
85563e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9246621
ff027b3
85563e1
ff027b3
 
 
 
 
9246621
85563e1
ff027b3
85563e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff027b3
85563e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff027b3
85563e1
ff027b3
85563e1
 
 
 
 
bfc16ca
85563e1
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import gradio as gr
from transformers import pipeline
import yt_dlp
import whisper
import os
import logging
from urllib.parse import urlparse

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Initialize components at startup
def initialize_components():
    logger.info("Loading Whisper model...")
    whisper_model = whisper.load_model("base")
    logger.info("Loading classifier...")
    classifier = pipeline(
        "zero-shot-classification", 
        model="facebook/bart-large-mnli",
        device="cpu"  # Explicitly set to CPU for Hugging Face Spaces
    )
    return whisper_model, classifier

# Global initialization
whisper_model, classifier = initialize_components()

def clean_temp_files():
    """Remove temporary files"""
    temp_files = ["temp_video.mp4", "temp_audio.mp3"]
    for file in temp_files:
        if os.path.exists(file):
            try:
                os.remove(file)
                logger.info(f"Removed temporary file: {file}")
            except Exception as e:
                logger.warning(f"Could not remove {file}: {e}")

def is_valid_youtube_url(url):
    """Validate YouTube URL"""
    youtube_domains = ['youtube.com', 'www.youtube.com', 'youtu.be', 'www.youtu.be']
    try:
        parsed = urlparse(url)
        if not parsed.scheme in ('http', 'https'):
            return False
        if not any(domain in parsed.netloc for domain in youtube_domains):
            return False
        return True
    except Exception as e:
        logger.error(f"URL validation error: {e}")
        return False

def download_video(video_url):
    """Download YouTube video with enhanced error handling"""
    try:
        ydl_opts = {
            'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
            'outtmpl': 'temp_video.%(ext)s',
            'quiet': False,
            'no_warnings': False,
            'merge_output_format': 'mp4',
            'retries': 3,
            'socket_timeout': 30,
            'extract_flat': False,
            'ignoreerrors': True,
            'cookiefile': os.getenv('COOKIES_PATH') if os.getenv('COOKIES_PATH') and os.path.exists(os.getenv('COOKIES_PATH')) else None,
        }
        
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            # Check availability first
            try:
                info = ydl.extract_info(video_url, download=False)
                if info.get('availability') == 'unavailable':
                    return None, "Video is unavailable (private, deleted, or region-locked)"
                if info.get('age_limit', 0) > 0 and not ydl_opts['cookiefile']:
                    return None, "Age-restricted content detected (try adding cookies.txt)"
            except Exception as e:
                logger.warning(f"Video info check failed: {e}")

            # Download the video
            try:
                ydl.download([video_url])
                filename = 'temp_video.mp4' if os.path.exists('temp_video.mp4') else None
                return filename, None
            except yt_dlp.utils.DownloadError as e:
                return None, f"Download failed: {str(e)}"
                
    except Exception as e:
        logger.error(f"Download error: {e}")
        return None, f"Download system error: {str(e)}"

def extract_audio(video_path):
    """Extract audio from video file"""
    try:
        if not os.path.exists(video_path):
            return None
            
        audio_path = "temp_audio.mp3"
        cmd = f"ffmpeg -i \"{video_path}\" -vn -acodec libmp3lame -q:a 2 \"{audio_path}\" -y -loglevel error"
        os.system(cmd)
        return audio_path if os.path.exists(audio_path) else None
    except Exception as e:
        logger.error(f"Audio extraction error: {e}")
        return None

def transcribe_audio(audio_path):
    """Transcribe audio using Whisper"""
    try:
        if not os.path.exists(audio_path):
            return None
            
        result = whisper_model.transcribe(audio_path, fp16=False)
        return result['text']
    except Exception as e:
        logger.error(f"Transcription error: {e}")
        return None

def classify_content(text):
    """Classify content using zero-shot classification"""
    try:
        if not text or len(text.strip()) == 0:
            return None, None
            
        labels = [
            "educational", "entertainment", "news", "political",
            "religious", "technical", "advertisement", "social"
        ]
        
        result = classifier(
            text,
            candidate_labels=labels,
            hypothesis_template="This text is about {}."
        )
        
        return result['labels'][0], result['scores'][0]
    except Exception as e:
        logger.error(f"Classification error: {e}")
        return None, None

def process_video(video_url):
    """Main processing pipeline"""
    clean_temp_files()
    
    if not video_url or len(video_url.strip()) == 0:
        return "Please enter a valid YouTube URL", ""
    
    if not is_valid_youtube_url(video_url):
        return "Please enter a valid YouTube URL (should start with https://youtube.com or https://youtu.be)", ""
    
    try:
        # Download video
        video_path, download_error = download_video(video_url)
        if not video_path:
            clean_temp_files()
            error_msg = download_error or "Failed to download video"
            return error_msg, ""
        
        # Extract audio
        audio_path = extract_audio(video_path)
        if not audio_path:
            clean_temp_files()
            return "Failed to extract audio from video", ""
        
        # Transcribe
        transcription = transcribe_audio(audio_path)
        if not transcription:
            clean_temp_files()
            return "Failed to transcribe audio (may be no speech detected)", ""
        
        # Classify
        category, confidence = classify_content(transcription)
        if not category:
            clean_temp_files()
            return transcription, "Failed to classify content"
        
        # Clean up
        clean_temp_files()
        
        # Format results
        classification_result = f"{category} (confidence: {confidence:.2%})"
        return transcription, classification_result
        
    except Exception as e:
        logger.error(f"Processing error: {e}")
        clean_temp_files()
        return f"An error occurred: {str(e)}", ""

def create_app():
    """Create Gradio interface"""
    with gr.Blocks(title="YouTube Content Analyzer", css=".gradio-container {max-width: 800px !important}") as demo:
        gr.Markdown("""
        # ▶️ YouTube Content Analyzer
        Enter a YouTube video URL to get transcription and content classification
        """)
        
        with gr.Row():
            url_input = gr.Textbox(
                label="YouTube URL",
                placeholder="Enter YouTube video URL here...",
                max_lines=1
            )
        
        with gr.Row():
            submit_btn = gr.Button("Analyze Video", variant="primary")
            clear_btn = gr.Button("Clear")
        
        with gr.Row():
            with gr.Column():
                transcription_output = gr.Textbox(
                    label="Transcription",
                    interactive=True,
                    lines=10,
                    max_lines=20
                )
            
            with gr.Column():
                category_output = gr.Textbox(
                    label="Content Category",
                    interactive=False
                )
        
        # Examples
        gr.Examples(
            examples=[
                ["https://www.youtube.com/watch?v=dQw4w9WgXcQ"],  # Rick Astley
                ["https://youtu.be/J---aiyznGQ"]  # Keyboard Cat
            ],
            inputs=url_input,
            label="Try these examples:"
        )
        
        # Button actions
        submit_btn.click(
            fn=process_video,
            inputs=url_input,
            outputs=[transcription_output, category_output]
        )
        
        clear_btn.click(
            fn=lambda: ["", ""],
            inputs=None,
            outputs=[transcription_output, category_output]
        )
    
    return demo

if __name__ == "__main__":
    app = create_app()
    app.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False
    )