developer28 commited on
Commit
dc946ef
·
verified ·
1 Parent(s): f25e87e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -693
app.py CHANGED
@@ -1,703 +1,36 @@
1
  import gradio as gr
2
- import yt_dlp
 
3
  import os
4
- import tempfile
5
- import shutil
6
- from pathlib import Path
7
- import re
8
  import uuid
9
- import json
10
- from datetime import datetime
11
- import google.generativeai as genai
12
 
13
- class YouTubeDownloader:
14
- def __init__(self):
15
- self.download_dir = tempfile.mkdtemp()
16
- # Use temp directory for Gradio compatibility
17
- self.temp_downloads = tempfile.mkdtemp(prefix="youtube_downloads_")
18
- # Also create user downloads folder for copying
19
- self.downloads_folder = os.path.join(os.path.expanduser("~"), "Downloads", "YouTube_Downloads")
20
- os.makedirs(self.downloads_folder, exist_ok=True)
21
- self.gemini_model = None
22
-
23
- def configure_gemini(self, api_key):
24
- """Configure Gemini API with the provided key"""
25
- try:
26
- genai.configure(api_key=api_key)
27
- self.gemini_model = genai.GenerativeModel(model_name="gemini-1.5-flash-latest")
28
- return True, "✅ Gemini API configured successfully!"
29
- except Exception as e:
30
- return False, f"❌ Failed to configure Gemini API: {str(e)}"
31
-
32
- def cleanup(self):
33
- """Clean up temporary directories and files"""
34
- try:
35
- if hasattr(self, 'download_dir') and os.path.exists(self.download_dir):
36
- shutil.rmtree(self.download_dir)
37
- print(f"✅ Cleaned up temporary directory: {self.download_dir}")
38
- if hasattr(self, 'temp_downloads') and os.path.exists(self.temp_downloads):
39
- shutil.rmtree(self.temp_downloads)
40
- print(f"✅ Cleaned up temp downloads directory: {self.temp_downloads}")
41
- except Exception as e:
42
- print(f"⚠️ Warning: Could not clean up temporary directory: {e}")
43
 
44
- def is_valid_youtube_url(self, url):
45
- youtube_regex = re.compile(
46
- r'(https?://)?(www\.)?(youtube|youtu|youtube-nocookie)\.(com|be)/'
47
- r'(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})'
48
- )
49
- return youtube_regex.match(url) is not None
50
 
51
- def generate_scene_breakdown_gemini(self, video_info):
52
- """Generate AI-powered scene breakdown using Gemini"""
53
- if not self.gemini_model:
54
- return self.generate_scene_breakdown_fallback(video_info)
55
-
56
- try:
57
- duration = video_info.get('duration', 0)
58
- title = video_info.get('title', '')
59
- description = video_info.get('description', '')[:1500] # Increased limit for better context
60
-
61
- if not duration:
62
- return ["**[Duration Unknown]**: Unable to generate timestamped breakdown - video duration not available"]
63
-
64
- # Create enhanced prompt for Gemini
65
- prompt = f"""
66
- Analyze this YouTube video and create a highly detailed, scene-by-scene breakdown with precise timestamps and specific descriptions:
67
-
68
- Title: {title}
69
- Duration: {duration} seconds
70
- Description: {description}
71
-
72
- IMPORTANT INSTRUCTIONS:
73
- 1. Create detailed scene descriptions that include:
74
- - Physical appearance of people (age, gender, clothing, hair, etc.)
75
- - Exact actions being performed
76
- - Dialogue or speech (include actual lines if audible, or infer probable spoken lines based on actions and setting; format them as "Character: line...")
77
- - Setting and environment details
78
- - Props, objects, or products being shown
79
- - Visual effects, text overlays, or graphics
80
- - Mood, tone, and atmosphere
81
- - Camera movements or angles (if apparent)
82
- 2. Dialogue Emphasis:
83
- - Include short dialogue lines in **every scene** wherever plausible.
84
- - Write lines like: Character: "Actual or inferred line..."
85
- - If dialogue is not available, intelligently infer probable phrases (e.g., "Welcome!", "Try this now!", "It feels amazing!").
86
- - Do NOT skip dialogue unless it’s clearly impossible.
87
-
88
- 3. Timestamp Guidelines:
89
- - For videos under 1 minute: 2-3 second segments
90
- - For videos 1-5 minutes: 3-5 second segments
91
- - For videos 5-15 minutes: 5-10 second segments
92
- - For videos over 15 minutes: 10-15 second segments
93
- - Maximum 20 scenes total for longer videos
94
-
95
- 4. Format each scene EXACTLY like this:
96
- **[MM:SS-MM:SS]**: Detailed description including who is visible, what they're wearing, what they're doing, what they're saying (if applicable), setting details, objects shown, and any visual elements.
97
-
98
-
99
- 5. Write descriptions as if you're watching the video in real-time, noting everything visible and audible.
100
-
101
- Based on the title and description, intelligently infer what would likely happen in each time segment. Consider the video type and create contextually appropriate, detailed descriptions.
102
- """
103
-
104
- response = self.gemini_model.generate_content(prompt)
105
-
106
- # Parse the response into individual scenes
107
- if response and response.text:
108
- scenes = []
109
- lines = response.text.split('\n')
110
- current_scene = ""
111
-
112
- for line in lines:
113
- line = line.strip()
114
- if line.strip().startswith("**[") and "]**:" in line:
115
- # This is a new scene timestamp line
116
- if current_scene:
117
- scenes.append(current_scene.strip())
118
- current_scene = line.strip()
119
- elif current_scene:
120
- # This is continuation of the current scene description
121
- current_scene += "\n" + line.strip()
122
 
123
- # Add the last scene if exists
124
- if current_scene:
125
- scenes.append(current_scene.strip())
126
-
127
- return scenes if scenes else self.generate_scene_breakdown_fallback(video_info)
128
- else:
129
- return self.generate_scene_breakdown_fallback(video_info)
130
-
131
- except Exception as e:
132
- print(f"Gemini API error: {e}")
133
- return self.generate_scene_breakdown_fallback(video_info)
134
 
135
- def generate_scene_breakdown_fallback(self, video_info):
136
- """Enhanced fallback scene generation when Gemini is not available"""
137
- duration = video_info.get('duration', 0)
138
- title = video_info.get('title', '').lower()
139
- description = video_info.get('description', '').lower()
140
- uploader = video_info.get('uploader', 'Content creator')
141
-
142
- if not duration:
143
- return ["**[Duration Unknown]**: Unable to generate timestamped breakdown"]
144
-
145
- # Determine segment length based on duration
146
- if duration <= 60:
147
- segment_length = 3
148
- elif duration <= 300:
149
- segment_length = 5
150
- elif duration <= 900:
151
- segment_length = 10
152
- else:
153
- segment_length = 15
154
-
155
- scenes = []
156
- num_segments = min(duration // segment_length + 1, 20)
157
-
158
- # Detect video type for better descriptions
159
- video_type = self.detect_video_type_detailed(title, description)
160
-
161
- for i in range(num_segments):
162
- start_time = i * segment_length
163
- end_time = min(start_time + segment_length - 1, duration)
164
-
165
- start_formatted = f"{start_time//60}:{start_time%60:02d}"
166
- end_formatted = f"{end_time//60}:{end_time%60:02d}"
167
-
168
- # Generate contextual descriptions based on video type and timing
169
- desc = self.generate_contextual_description(i, num_segments, video_type, uploader, title)
170
-
171
- scenes.append(f"**[{start_formatted}-{end_formatted}]**: {desc}")
172
-
173
- return scenes
174
 
175
- def detect_video_type_detailed(self, title, description):
176
- """Detect video type with more detail for better fallback descriptions"""
177
- text = (title + " " + description).lower()
178
-
179
- if any(word in text for word in ['tutorial', 'how to', 'guide', 'learn', 'diy', 'step by step']):
180
- return 'tutorial'
181
- elif any(word in text for word in ['review', 'unboxing', 'test', 'comparison', 'vs']):
182
- return 'review'
183
- elif any(word in text for word in ['vlog', 'daily', 'routine', 'day in', 'morning', 'skincare']):
184
- return 'vlog'
185
- elif any(word in text for word in ['music', 'song', 'cover', 'lyrics', 'dance']):
186
- return 'music'
187
- elif any(word in text for word in ['comedy', 'funny', 'prank', 'challenge', 'reaction']):
188
- return 'entertainment'
189
- elif any(word in text for word in ['news', 'breaking', 'update', 'report']):
190
- return 'news'
191
- elif any(word in text for word in ['cooking', 'recipe', 'food', 'kitchen']):
192
- return 'cooking'
193
- elif any(word in text for word in ['workout', 'fitness', 'exercise', 'yoga']):
194
- return 'fitness'
195
- else:
196
- return 'general'
197
 
198
- def generate_contextual_description(self, scene_index, total_scenes, video_type, uploader, title):
199
- """Generate contextual descriptions based on video type and scene position"""
200
-
201
- # Common elements
202
- presenter_desc = f"The content creator"
203
- if 'woman' in title.lower() or 'girl' in title.lower():
204
- presenter_desc = "A woman"
205
- elif 'man' in title.lower() or 'guy' in title.lower():
206
- presenter_desc = "A man"
207
-
208
- # Position-based descriptions
209
- if scene_index == 0:
210
- # Opening scene
211
- if video_type == 'tutorial':
212
- return f"{presenter_desc} appears on screen, likely introducing themselves and the topic. They may be in a well-lit indoor setting, wearing casual clothing, and addressing the camera directly with a welcoming gesture."
213
- elif video_type == 'vlog':
214
- return f"{presenter_desc} greets the camera with a smile, possibly waving. They appear to be in their usual filming location, wearing their typical style, and beginning their introduction to today's content."
215
- elif video_type == 'review':
216
- return f"{presenter_desc} introduces the product or topic they'll be reviewing, likely holding or displaying the item. The setting appears organized, possibly with the product prominently featured."
217
- else:
218
- return f"{presenter_desc} appears on screen to begin the video, introducing the topic with engaging body language and clear speech directed at the audience."
219
-
220
- elif scene_index == total_scenes - 1:
221
- # Closing scene
222
- if video_type == 'tutorial':
223
- return f"{presenter_desc} concludes the tutorial, possibly showing the final result. They may be thanking viewers, asking for engagement (likes/comments), and suggesting related content."
224
- elif video_type == 'vlog':
225
- return f"{presenter_desc} wraps up their vlog, possibly reflecting on the day's events. They appear relaxed and are likely saying goodbye to viewers with a friendly gesture."
226
- else:
227
- return f"{presenter_desc} concludes the video with final thoughts, thanking viewers for watching, and encouraging engagement through likes, comments, and subscriptions."
228
-
229
- else:
230
- # Middle scenes - content-specific
231
- if video_type == 'tutorial':
232
- step_num = scene_index
233
- return f"{presenter_desc} demonstrates step {step_num} of the process, showing specific techniques and explaining the procedure. They may be using tools or materials, with close-up shots of their hands working."
234
-
235
- elif video_type == 'review':
236
- return f"{presenter_desc} examines different aspects of the product, pointing out features and sharing their opinions. They may be holding, using, or demonstrating the item while speaking to the camera."
237
-
238
- elif video_type == 'vlog':
239
- return f"{presenter_desc} continues sharing their experience, possibly showing different locations or activities. The scene captures candid moments with natural lighting and casual interactions."
240
-
241
- elif video_type == 'cooking':
242
- return f"{presenter_desc} works in the kitchen, preparing ingredients or cooking. They demonstrate techniques while explaining each step, with kitchen tools and ingredients visible on the counter."
243
-
244
- elif video_type == 'fitness':
245
- return f"{presenter_desc} demonstrates exercise movements, likely in workout attire in a gym or home setting. They show proper form while providing instruction and motivation."
246
-
247
- else:
248
- return f"{presenter_desc} continues with the main content, engaging with the audience through clear explanations and demonstrations. The setting remains consistent with good lighting and clear audio."
249
-
250
- def detect_video_type(self, title, description):
251
- """Detect video type based on title and description"""
252
- text = (title + " " + description).lower()
253
-
254
- if any(word in text for word in ['music', 'song', 'album', 'artist', 'band', 'lyrics']):
255
- return "🎵 Music Video"
256
- elif any(word in text for word in ['tutorial', 'how to', 'guide', 'learn', 'teaching']):
257
- return "📚 Tutorial/Educational"
258
- elif any(word in text for word in ['funny', 'comedy', 'entertainment', 'vlog', 'challenge']):
259
- return "🎭 Entertainment/Comedy"
260
- elif any(word in text for word in ['news', 'breaking', 'report', 'update']):
261
- return "📰 News/Information"
262
- elif any(word in text for word in ['review', 'unboxing', 'test', 'comparison']):
263
- return "⭐ Review/Unboxing"
264
- elif any(word in text for word in ['commercial', 'ad', 'brand', 'product']):
265
- return "📺 Commercial/Advertisement"
266
- else:
267
- return "🎬 General Content"
268
-
269
- def detect_background_music(self, video_info):
270
- """Detect background music style"""
271
- title = video_info.get('title', '').lower()
272
- description = video_info.get('description', '').lower()
273
-
274
- if any(word in title for word in ['music', 'song', 'soundtrack']):
275
- return "🎵 Original Music/Soundtrack - Primary audio content"
276
- elif any(word in title for word in ['commercial', 'ad', 'brand']):
277
- return "🎶 Upbeat Commercial Music - Designed to enhance brand appeal"
278
- elif any(word in title for word in ['tutorial', 'how to', 'guide']):
279
- return "🔇 Minimal/No Background Music - Focus on instruction"
280
- elif any(word in title for word in ['vlog', 'daily', 'life']):
281
- return "🎼 Ambient Background Music - Complementary to narration"
282
- else:
283
- return "🎵 Background Music - Complementing video mood and pacing"
284
-
285
- def detect_influencer_status(self, video_info):
286
- """Detect influencer status"""
287
- subscriber_count = video_info.get('channel_followers', 0)
288
- view_count = video_info.get('view_count', 0)
289
-
290
- if subscriber_count > 10000000:
291
- return "🌟 Mega Influencer (10M+ subscribers)"
292
- elif subscriber_count > 1000000:
293
- return "⭐ Major Influencer (1M+ subscribers)"
294
- elif subscriber_count > 100000:
295
- return "🎯 Mid-tier Influencer (100K+ subscribers)"
296
- elif subscriber_count > 10000:
297
- return "📈 Micro Influencer (10K+ subscribers)"
298
- elif view_count > 100000:
299
- return "🔥 Viral Content Creator"
300
- else:
301
- return "👤 Regular Content Creator"
302
-
303
- def format_number(self, num):
304
- if num is None or num == 0:
305
- return "0"
306
- if num >= 1_000_000_000:
307
- return f"{num/1_000_000_000:.1f}B"
308
- elif num >= 1_000_000:
309
- return f"{num/1_000_000:.1f}M"
310
- elif num >= 1_000:
311
- return f"{num/1_000:.1f}K"
312
- return str(num)
313
-
314
- def format_video_info(self, video_info):
315
- """Streamlined video information formatting"""
316
- if not video_info:
317
- return "❌ No video information available."
318
-
319
- # Basic information
320
- title = video_info.get("title", "Unknown")
321
- uploader = video_info.get("uploader", "Unknown")
322
- duration = video_info.get("duration", 0)
323
- duration_str = f"{duration//60}:{duration%60:02d}" if duration else "Unknown"
324
- view_count = video_info.get("view_count", 0)
325
- like_count = video_info.get("like_count", 0)
326
- comment_count = video_info.get("comment_count", 0)
327
- upload_date = video_info.get("upload_date", "Unknown")
328
-
329
- # Format upload date
330
- if len(upload_date) == 8:
331
- formatted_date = f"{upload_date[:4]}-{upload_date[4:6]}-{upload_date[6:8]}"
332
- else:
333
- formatted_date = upload_date
334
-
335
- # Generate enhanced analysis
336
- scene_descriptions = self.generate_scene_breakdown_gemini(video_info)
337
- video_type = self.detect_video_type(title, video_info.get('description', ''))
338
- background_music = self.detect_background_music(video_info)
339
- influencer_status = self.detect_influencer_status(video_info)
340
-
341
- # Calculate engagement metrics
342
- engagement_rate = (like_count / view_count) * 100 if view_count > 0 else 0
343
-
344
- # Generate streamlined report
345
- report = f"""
346
- 🎬 YOUTUBE VIDEO ANALYSIS REPORT
347
- {'='*50}
348
- 📋 BASIC INFORMATION
349
- {'─'*25}
350
- 📹 **Title:** {title}
351
- 👤 **Uploader:** {uploader}
352
- 📅 **Upload Date:** {formatted_date}
353
- ⏱️ **Duration:** {duration_str}
354
- 🆔 **Video ID:** {video_info.get('id', 'Unknown')}
355
- 📊 PERFORMANCE METRICS
356
- {'─'*25}
357
- 👀 **Views:** {self.format_number(view_count)} ({view_count:,})
358
- 👍 **Likes:** {self.format_number(like_count)} ({like_count:,})
359
- 💬 **Comments:** {self.format_number(comment_count)} ({comment_count:,})
360
- 📈 **Engagement Rate:** {engagement_rate:.2f}%
361
- 🎯 CONTENT ANALYSIS
362
- {'─'*25}
363
- 📂 **Video Type:** {video_type}
364
- 🎵 **Background Music:** {background_music}
365
- 👑 **Creator Status:** {influencer_status}
366
- 🎬 DETAILED SCENE BREAKDOWN
367
- {'─'*30}
368
- {chr(10).join(scene_descriptions)}
369
- 📝 DESCRIPTION PREVIEW
370
- {'─'*25}
371
- {video_info.get('description', 'No description available')[:500]}
372
- {'...(truncated)' if len(video_info.get('description', '')) > 500 else ''}
373
- {'='*50}
374
- 📊 **Analysis completed:** {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
375
- 🤖 **AI Enhancement:** {'Gemini AI' if self.gemini_model else 'Standard Analysis'}
376
- """
377
-
378
- return report.strip()
379
-
380
- def get_video_info(self, url, progress=gr.Progress(), cookiefile=None):
381
- """Extract video information"""
382
- if not url or not url.strip():
383
- return None, "❌ Please enter a YouTube URL"
384
-
385
- if not self.is_valid_youtube_url(url):
386
- return None, "❌ Invalid YouTube URL format"
387
-
388
- try:
389
- progress(0.1, desc="Initializing YouTube extractor...")
390
-
391
- ydl_opts = {
392
- 'noplaylist': True,
393
- 'extract_flat': False,
394
- }
395
-
396
- if cookiefile and os.path.exists(cookiefile):
397
- ydl_opts['cookiefile'] = cookiefile
398
-
399
- progress(0.5, desc="Extracting video metadata...")
400
-
401
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
402
- info = ydl.extract_info(url, download=False)
403
-
404
- progress(1.0, desc="✅ Analysis complete!")
405
-
406
- return info, "✅ Video information extracted successfully"
407
-
408
- except Exception as e:
409
- return None, f"❌ Error: {str(e)}"
410
-
411
- def download_video(self, url, quality="best", audio_only=False, progress=gr.Progress(), cookiefile=None):
412
- """Download video with progress tracking"""
413
- if not url or not url.strip():
414
- return None, "❌ Please enter a YouTube URL"
415
-
416
- if not self.is_valid_youtube_url(url):
417
- return None, "❌ Invalid YouTube URL format"
418
-
419
- try:
420
- progress(0.1, desc="Preparing download...")
421
-
422
- # Create unique filename
423
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
424
-
425
- # Download to temp directory first (Gradio compatible)
426
- ydl_opts = {
427
- 'outtmpl': os.path.join(self.temp_downloads, f'%(title)s_{timestamp}.%(ext)s'),
428
- 'noplaylist': True,
429
- }
430
-
431
- if audio_only:
432
- ydl_opts['format'] = 'bestaudio/best'
433
- ydl_opts['postprocessors'] = [{
434
- 'key': 'FFmpegExtractAudio',
435
- 'preferredcodec': 'mp3',
436
- 'preferredquality': '192',
437
- }]
438
- else:
439
- if quality == "best":
440
- ydl_opts['format'] = 'best[height<=1080]'
441
- elif quality == "720p":
442
- ydl_opts['format'] = 'best[height<=720]'
443
- elif quality == "480p":
444
- ydl_opts['format'] = 'best[height<=480]'
445
- else:
446
- ydl_opts['format'] = 'best'
447
-
448
- if cookiefile and os.path.exists(cookiefile):
449
- ydl_opts['cookiefile'] = cookiefile
450
-
451
- # Progress hook
452
- def progress_hook(d):
453
- if d['status'] == 'downloading':
454
- if 'total_bytes' in d:
455
- percent = (d['downloaded_bytes'] / d['total_bytes']) * 100
456
- progress(0.1 + (percent / 100) * 0.7, desc=f"Downloading... {percent:.1f}%")
457
- else:
458
- progress(0.5, desc="Downloading...")
459
- elif d['status'] == 'finished':
460
- progress(0.8, desc="Processing download...")
461
-
462
- ydl_opts['progress_hooks'] = [progress_hook]
463
-
464
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
465
- info = ydl.extract_info(url, download=True)
466
-
467
- progress(0.9, desc="Copying to Downloads folder...")
468
-
469
- # Find the downloaded file in temp directory
470
- downloaded_file_temp = None
471
-
472
- for file in os.listdir(self.temp_downloads):
473
- if timestamp in file:
474
- downloaded_file_temp = os.path.join(self.temp_downloads, file)
475
- break
476
-
477
- if not downloaded_file_temp:
478
- return None, "❌ Downloaded file not found in temp directory"
479
-
480
- # Copy to user's Downloads folder
481
- final_filename = os.path.basename(downloaded_file_temp)
482
- final_path = os.path.join(self.downloads_folder, final_filename)
483
-
484
- try:
485
- shutil.copy2(downloaded_file_temp, final_path)
486
- copy_success = True
487
- except Exception as e:
488
- print(f"Warning: Could not copy to Downloads folder: {e}")
489
- copy_success = False
490
- final_path = "File downloaded to temp location only"
491
-
492
- progress(1.0, desc="✅ Download complete!")
493
-
494
- success_msg = f"""✅ Download successful!
495
- 📁 Temp file (for download): {os.path.basename(downloaded_file_temp)}
496
- 📁 Permanent location: {final_path if copy_success else 'Copy failed'}
497
- 🎯 File size: {os.path.getsize(downloaded_file_temp) / (1024*1024):.1f} MB"""
498
-
499
- return downloaded_file_temp, success_msg
500
-
501
- except Exception as e:
502
- return None, f"❌ Download failed: {str(e)}"
503
-
504
- # Initialize global downloader
505
- downloader = YouTubeDownloader()
506
-
507
- def configure_api_key(api_key):
508
- """Configure Gemini API key"""
509
- if not api_key or not api_key.strip():
510
- return "❌ Please enter a valid Google API key", gr.update(visible=False)
511
-
512
- success, message = downloader.configure_gemini(api_key.strip())
513
-
514
- if success:
515
- return message, gr.update(visible=True)
516
- else:
517
- return message, gr.update(visible=False)
518
-
519
- def analyze_with_cookies(url, cookies_file, progress=gr.Progress()):
520
- """Main analysis function"""
521
- try:
522
- progress(0.05, desc="Starting analysis...")
523
-
524
- cookiefile = None
525
- if cookies_file and os.path.exists(cookies_file):
526
- cookiefile = cookies_file
527
-
528
- info, msg = downloader.get_video_info(url, progress=progress, cookiefile=cookiefile)
529
-
530
- if info:
531
- progress(0.95, desc="Generating comprehensive report...")
532
- formatted_info = downloader.format_video_info(info)
533
- progress(1.0, desc="✅ Complete!")
534
- return formatted_info
535
- else:
536
- return f"❌ Analysis Failed: {msg}"
537
-
538
- except Exception as e:
539
- return f"❌ System Error: {str(e)}"
540
-
541
- def download_with_cookies(url, quality, audio_only, cookies_file, progress=gr.Progress()):
542
- """Main download function"""
543
- try:
544
- progress(0.05, desc="Preparing download...")
545
-
546
- cookiefile = None
547
- if cookies_file and os.path.exists(cookies_file):
548
- cookiefile = cookies_file
549
-
550
- file_path, msg = downloader.download_video(url, quality, audio_only, progress=progress, cookiefile=cookiefile)
551
-
552
- if file_path:
553
- return file_path, msg
554
- else:
555
- return None, msg
556
-
557
- except Exception as e:
558
- return None, f"❌ System Error: {str(e)}"
559
-
560
- def create_interface():
561
- """Create and configure the Gradio interface"""
562
- with gr.Blocks(theme=gr.themes.Soft(), title="🎥 YouTube Video Analyzer & Downloader Pro") as interface:
563
-
564
- gr.HTML("<h1>🎥 YouTube Video Analyzer & Downloader Pro</h1>")
565
-
566
- # API Key Configuration Section
567
- with gr.Group():
568
- gr.HTML("<h3>🔑 Google Gemini API Configuration</h3>")
569
- with gr.Row():
570
- api_key_input = gr.Textbox(
571
- label="🔑 Google API Key",
572
- placeholder="Enter your Google API Key for enhanced AI analysis...",
573
- type="password",
574
- value=""
575
- )
576
- configure_btn = gr.Button("🔧 Configure API", variant="secondary")
577
-
578
- api_status = gr.Textbox(
579
- label="API Status",
580
- value="❌ Gemini API not configured - Using fallback analysis",
581
- interactive=False,
582
- lines=1
583
- )
584
-
585
- # Main Interface (initially hidden until API is configured)
586
- main_interface = gr.Group(visible=False)
587
-
588
- with main_interface:
589
- with gr.Row():
590
- url_input = gr.Textbox(
591
- label="🔗 YouTube URL",
592
- placeholder="Paste your YouTube video URL here...",
593
- value=""
594
- )
595
-
596
- cookies_input = gr.File(
597
- label="🍪 Upload cookies.txt (Optional)",
598
- file_types=[".txt"],
599
- type="filepath"
600
- )
601
-
602
- with gr.Tabs():
603
- with gr.TabItem("📊 Video Analysis"):
604
- analyze_btn = gr.Button("🔍 Analyze Video", variant="primary")
605
-
606
- analysis_output = gr.Textbox(
607
- label="📊 Analysis Report",
608
- lines=25,
609
- show_copy_button=True
610
- )
611
-
612
- analyze_btn.click(
613
- fn=analyze_with_cookies,
614
- inputs=[url_input, cookies_input],
615
- outputs=analysis_output,
616
- show_progress=True
617
- )
618
-
619
- with gr.TabItem("⬇️ Video Download"):
620
- with gr.Row():
621
- quality_dropdown = gr.Dropdown(
622
- choices=["best", "720p", "480p"],
623
- value="best",
624
- label="📺 Video Quality"
625
- )
626
-
627
- audio_only_checkbox = gr.Checkbox(
628
- label="🎵 Audio Only (MP3)",
629
- value=False
630
- )
631
-
632
- download_btn = gr.Button("⬇️ Download Video", variant="primary")
633
-
634
- download_status = gr.Textbox(
635
- label="📥 Download Status",
636
- lines=5,
637
- show_copy_button=True
638
- )
639
-
640
- download_file = gr.File(
641
- label="📁 Downloaded File",
642
- visible=False
643
- )
644
-
645
- def download_and_update(url, quality, audio_only, cookies_file, progress=gr.Progress()):
646
- file_path, status = download_with_cookies(url, quality, audio_only, cookies_file, progress)
647
- if file_path and os.path.exists(file_path):
648
- return status, gr.update(value=file_path, visible=True)
649
- else:
650
- return status, gr.update(visible=False)
651
-
652
- download_btn.click(
653
- fn=download_and_update,
654
- inputs=[url_input, quality_dropdown, audio_only_checkbox, cookies_input],
655
- outputs=[download_status, download_file],
656
- show_progress=True
657
- )
658
-
659
- # Configure API key button action
660
- configure_btn.click(
661
- fn=configure_api_key,
662
- inputs=[api_key_input],
663
- outputs=[api_status, main_interface]
664
- )
665
- # Always show interface option (for fallback mode)
666
- with gr.Row():
667
- show_interface_btn = gr.Button("🚀 Use Without Gemini API (Fallback Mode)", variant="secondary")
668
-
669
- def show_fallback_interface():
670
- return "⚠️ Using fallback analysis mode", gr.update(visible=True)
671
-
672
- show_interface_btn.click(
673
- fn=show_fallback_interface,
674
- outputs=[api_status, main_interface]
675
- )
676
-
677
- gr.HTML("""
678
- <div style="margin-top: 20px; padding: 15px; background-color: #f0f8ff; border-radius: 10px; border-left: 5px solid #4285f4;">
679
- <h3>🔑 How to Get Google API Key:</h3>
680
- <ol>
681
- <li>Go to <a href="https://console.cloud.google.com/" target="_blank">Google Cloud Console</a></li>
682
- <li>Create a new project or select an existing one</li>
683
- <li>Enable the "Generative Language API"</li>
684
- <li>Go to "Credentials" and create an API key</li>
685
- <li>Copy the API key and paste it above</li>
686
- </ol>
687
- <p><strong>✨ Benefits of using Gemini API:</strong></p>
688
- <ul>
689
- <li>🤖 AI-powered scene descriptions with contextual understanding</li>
690
- <li>🎯 More accurate content type detection</li>
691
- <li>📊 Enhanced analysis based on video content</li>
692
- <li>⏰ Intelligent timestamp segmentation</li>
693
- </ul>
694
- </div>
695
- """)
696
-
697
- return interface
698
-
699
- if __name__ == "__main__":
700
- demo = create_interface()
701
- import atexit
702
- atexit.register(downloader.cleanup)
703
- demo.launch(debug=True, show_error=True)
 
1
  import gradio as gr
2
+ from xhtml2pdf import pisa
3
+ from io import BytesIO
4
  import os
 
 
 
 
5
  import uuid
 
 
 
6
 
7
+ def generate_dummy_pdf():
8
+ html = """
9
+ <html>
10
+ <body>
11
+ <h1>Hello, PDF!</h1>
12
+ <p>This is a test PDF file.</p>
13
+ </body>
14
+ </html>
15
+ """
16
+ result = BytesIO()
17
+ pisa_status = pisa.CreatePDF(html, dest=result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ if pisa_status.err:
20
+ print("PDF generation failed")
21
+ return None
 
 
 
22
 
23
+ pdf_path = os.path.join(os.path.expanduser("~"), "Downloads", f"test_pdf_{uuid.uuid4().hex}.pdf")
24
+ with open(pdf_path, "wb") as f:
25
+ f.write(result.getvalue())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ print("✅ PDF saved at:", pdf_path)
28
+ return pdf_path
 
 
 
 
 
 
 
 
 
29
 
30
+ with gr.Blocks() as demo:
31
+ btn = gr.Button("📄 Generate PDF")
32
+ output = gr.File(label="📥 Download PDF")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ btn.click(fn=generate_dummy_pdf, inputs=[], outputs=output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ demo.launch()