File size: 13,734 Bytes
45857ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
931d313
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45857ba
931d313
45857ba
 
 
 
 
 
 
 
 
931d313
45857ba
 
931d313
45857ba
 
 
 
931d313
 
 
 
45857ba
931d313
45857ba
 
 
 
 
 
 
 
 
 
 
 
931d313
45857ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
931d313
 
 
 
 
 
 
 
 
 
45857ba
931d313
 
 
45857ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
931d313
45857ba
 
 
 
 
 
 
 
 
931d313
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45857ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
931d313
 
 
 
45857ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import gradio as gr
import tempfile
import os
import yt_dlp
import re
from pathlib import Path
import google.generativeai as genai
from google.generativeai import upload_file, get_file
import time

# Configure Google AI
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
if GOOGLE_API_KEY:
    genai.configure(api_key=GOOGLE_API_KEY)
else:
    raise ValueError("GOOGLE_API_KEY environment variable not set")

def is_valid_url(url):
    """Check if URL is from supported platforms"""
    patterns = [
        r'(https?://)?(www\.)?(youtube\.com|youtu\.be)',
        r'(https?://)?(www\.)?(instagram\.com|instagr\.am)',
        r'(https?://)?(www\.)?(tiktok\.com)',
        r'(https?://)?(www\.)?(twitter\.com|x\.com)',
    ]
    
    for pattern in patterns:
        if re.search(pattern, url, re.IGNORECASE):
            return True
    return False

def get_video_info(url):
    """Get video information without downloading"""
    try:
        # Extract video ID for YouTube
        if 'youtube.com' in url or 'youtu.be' in url:
            video_id = None
            if 'youtu.be/' in url:
                video_id = url.split('youtu.be/')[-1].split('?')[0]
            elif 'watch?v=' in url:
                video_id = url.split('watch?v=')[-1].split('&')[0]
            
            if video_id:
                # Use YouTube oEmbed API (no key required, often works better)
                import requests
                oembed_url = f"https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v={video_id}&format=json"
                response = requests.get(oembed_url, timeout=10)
                if response.status_code == 200:
                    data = response.json()
                    return {
                        'title': data.get('title', 'Unknown Video'),
                        'author': data.get('author_name', 'Unknown Author'),
                        'thumbnail': data.get('thumbnail_url', ''),
                    }
    except Exception as e:
        print(f"Could not get video info: {e}")
    
    return None

def analyze_video_from_url(url, query):
    """Analyze video based on URL and metadata instead of downloading"""
    # Get video information
    video_info = get_video_info(url)
    
    # Create context from URL and available info
    context = f"Video URL: {url}\n"
    if video_info:
        context += f"Title: {video_info['title']}\n"
        context += f"Author: {video_info['author']}\n"
    
    # Generate AI response based on URL and context
    try:
        model = genai.GenerativeModel('gemini-2.0-flash-exp')
        
        prompt = f"""
        I have a video with the following information:
        {context}
        
        User question: {query}
        
        Based on the video URL and available metadata, please provide helpful analysis and insights. 
        Since I cannot directly access the video content, please:
        
        1. Analyze what type of content this might be based on the URL and title
        2. Provide general guidance about analyzing this type of video
        3. Suggest what insights could typically be extracted
        4. Give relevant advice based on the platform (YouTube, Instagram, etc.)
        
        Be helpful and informative while acknowledging the limitations of not having direct video access.
        If this appears to be a specific type of content (tutorial, entertainment, news, etc.), 
        provide relevant analysis frameworks and questions that would be useful.
        
        Be conversational and engaging.
        """
        
        response = model.generate_content(prompt)
        return response.text
        
    except Exception as e:
        raise gr.Error(f"AI analysis failed: {str(e)}")

def download_video(url, progress=gr.Progress()):
    """Download video from URL using yt-dlp with better error handling"""
    if not is_valid_url(url):
        raise gr.Error("Please enter a valid YouTube, Instagram, TikTok, or X video URL")
    
    progress(0.1, desc="Starting download...")
    
    # Create temp file
    temp_video = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
    temp_video.close()

    # yt-dlp options optimized for HF Spaces with better network handling
    ydl_opts = {
        'outtmpl': temp_video.name,
        'format': 'best[filesize<50M]/worst[filesize<50M]/best',  # Smaller files for HF
        'quiet': True,
        'no_warnings': True,
        'nooverwrites': False,
        'user_agent': 'Mozilla/5.0 (compatible; GradioApp/1.0)',
        'retries': 1,  # Reduced for HF Spaces
        'fragment_retries': 1,
        'extractor_retries': 1,
        'socket_timeout': 15,  # Shorter timeout
        'nocheckcertificate': True,
        'prefer_insecure': True,  # Sometimes helps with network issues
    }

    progress(0.3, desc="Downloading video...")
    
    try:
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            ydl.download([url])

        # Verify download
        if os.path.exists(temp_video.name) and os.path.getsize(temp_video.name) > 0:
            file_size_mb = os.path.getsize(temp_video.name) / (1024 * 1024)
            
            if file_size_mb > 50:  # Smaller limit for HF
                os.unlink(temp_video.name)
                raise gr.Error(f"Video too large ({file_size_mb:.1f}MB). Please use a smaller video.")
            
            progress(0.7, desc="Processing video...")
            return temp_video.name
        else:
            raise gr.Error("Downloaded file is empty or doesn't exist")

    except Exception as e:
        # Clean up on error
        if os.path.exists(temp_video.name):
            try:
                os.unlink(temp_video.name)
            except:
                pass
        
        error_msg = str(e)
        
        # Handle specific network errors common in HF Spaces
        if "Failed to resolve" in error_msg or "No address associated with hostname" in error_msg:
            raise gr.Error("🌐 Network connectivity issue. Hugging Face Spaces may have restricted access to this platform. Try a different video or platform.")
        elif "403" in error_msg or "Forbidden" in error_msg:
            raise gr.Error("🚫 Video download was blocked by the platform. Try a different video.")
        elif "404" in error_msg or "not found" in error_msg.lower():
            raise gr.Error("πŸ“Ή Video not found. Please check the URL.")
        elif "timeout" in error_msg.lower():
            raise gr.Error("⏱️ Download timed out. Try a shorter video.")
        else:
            # For network errors, fall back to URL-based analysis
            print(f"Download failed, falling back to URL analysis: {error_msg}")
            raise Exception("download_failed")  # Special exception for fallback

def analyze_video_with_ai(video_path, query, progress=gr.Progress()):
    """Analyze video using Google Gemini AI"""
    if not video_path or not os.path.exists(video_path):
        raise gr.Error("No video file found")
    
    progress(0.1, desc="Uploading video to AI...")
    
    try:
        # Upload video to Google AI
        processed_video = upload_file(video_path)
        
        progress(0.5, desc="Processing video...")
        
        # Wait for processing
        while processed_video.state.name == "PROCESSING":
            time.sleep(2)
            processed_video = get_file(processed_video.name)
        
        if processed_video.state.name == "FAILED":
            raise gr.Error("Video processing failed")
        
        progress(0.8, desc="Generating response...")
        
        # Generate AI response
        model = genai.GenerativeModel('gemini-2.0-flash-exp')
        
        prompt = f"""
        Analyze this video and respond to the user's question: {query}
        
        Provide a comprehensive, insightful response that includes:
        1. Direct analysis of the video content
        2. Key insights and observations
        3. Specific details from what you can see/hear
        4. Actionable takeaways if relevant
        
        Be conversational and engaging while being thorough and accurate.
        """
        
        response = model.generate_content([processed_video, prompt])
        
        progress(1.0, desc="Complete!")
        
        return response.text
        
    except Exception as e:
        raise gr.Error(f"AI analysis failed: {str(e)}")
    
    finally:
        # Clean up video file
        try:
            if video_path and os.path.exists(video_path):
                os.unlink(video_path)
        except:
            pass

def process_video_and_chat(url, query):
    """Main function to download video and get AI response with fallback"""
    if not url.strip():
        raise gr.Error("Please enter a video URL")
    
    if not query.strip():
        raise gr.Error("Please enter a question about the video")
    
    progress = gr.Progress()
    progress(0.0, desc="Starting...")
    
    try:
        # Try to download video first
        video_path = download_video(url, progress)
        
        # Analyze with AI using actual video
        response = analyze_video_with_ai(video_path, query, progress)
        
        return response
        
    except Exception as e:
        # If download fails due to network issues, fall back to URL-based analysis
        if str(e) == "download_failed" or "Network connectivity" in str(e) or "Failed to resolve" in str(e):
            progress(0.5, desc="Switching to URL-based analysis...")
            
            # Add a notice about the fallback
            fallback_notice = "πŸ”„ **Note**: Direct video download failed due to network restrictions, so I'm providing analysis based on the video URL and metadata.\n\n"
            
            try:
                response = analyze_video_from_url(url, query)
                return fallback_notice + response
            except Exception as fallback_error:
                raise gr.Error(f"Both video download and URL analysis failed: {str(fallback_error)}")
        else:
            # Re-raise the original error if it's not a network issue
            raise e

# Create Gradio interface
def create_interface():
    with gr.Blocks(
        title="The Plug - AI Video Analyzer",
        theme=gr.themes.Soft(),
        css="""
        .gradio-container {
            max-width: 800px !important;
            margin: auto !important;
        }
        .header {
            text-align: center;
            margin-bottom: 2rem;
        }
        .footer {
            text-align: center;
            margin-top: 2rem;
            color: #666;
        }
        """
    ) as demo:
        
        # Header
        gr.HTML("""
        <div class="header">
            <h1>πŸŽ₯ The Plug - AI Video Analyzer</h1>
            <p>Analyze videos from YouTube, Instagram, TikTok, and X with AI</p>
            <div style="background: #f0f0f0; padding: 10px; border-radius: 8px; margin: 10px 0;">
                <small><strong>πŸ”„ Smart Fallback:</strong> If video download fails due to network restrictions, 
                the app will analyze based on video metadata and URL instead.</small>
            </div>
        </div>
        """)
        
        with gr.Row():
            with gr.Column():
                # Video URL input
                video_url = gr.Textbox(
                    label="Video URL",
                    placeholder="https://youtube.com/watch?v=... or Instagram/TikTok/X link",
                    lines=1,
                    info="Paste a video URL from YouTube, Instagram, TikTok, or X"
                )
                
                # Query input
                query = gr.Textbox(
                    label="Your Question",
                    placeholder="What is the main topic? Summarize the key points...",
                    lines=3,
                    info="Ask anything about the video content"
                )
                
                # Submit button
                submit_btn = gr.Button("πŸš€ Analyze Video", variant="primary", size="lg")
        
        with gr.Row():
            with gr.Column():
                # Response output
                response = gr.Textbox(
                    label="AI Analysis",
                    lines=15,
                    max_lines=25,
                    interactive=False,
                    show_copy_button=True
                )
        
        # Example queries
        gr.Examples(
            examples=[
                ["https://www.youtube.com/watch?v=dQw4w9WgXcQ", "What is this video about?"],
                ["", "Summarize the key points mentioned"],
                ["", "What are the main takeaways?"],
                ["", "Who is the target audience?"],
            ],
            inputs=[video_url, query],
            label="Example Questions"
        )
        
        # Footer
        gr.HTML("""
        <div class="footer">
            <p>Built with ❀️ using Gradio and Google Gemini AI</p>
            <p>Supports YouTube, Instagram, TikTok, and X video URLs</p>
        </div>
        """)
        
        # Connect the function
        submit_btn.click(
            fn=process_video_and_chat,
            inputs=[video_url, query],
            outputs=response,
            show_progress=True
        )
        
        # Also allow Enter key to submit
        query.submit(
            fn=process_video_and_chat,
            inputs=[video_url, query],
            outputs=response,
            show_progress=True
        )
    
    return demo

# Launch the app
if __name__ == "__main__":
    demo = create_interface()
    demo.launch(
        share=True,
        server_name="0.0.0.0",
        server_port=7860,
        show_error=True
    )