Sayiqa commited on
Commit
d271517
·
verified ·
1 Parent(s): 985d66e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -26
app.py CHANGED
@@ -383,6 +383,7 @@ def process_youtube_video(url="", keywords=""):
383
  summary = ""
384
  sentiment_label = "N/A"
385
  recommendations = ""
 
386
 
387
  if not url.strip():
388
  return thumbnail, "Please enter a YouTube URL", sentiment_label, recommendations
@@ -393,36 +394,49 @@ def process_youtube_video(url="", keywords=""):
393
 
394
  thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
395
 
 
396
  try:
397
- # Method 1: Direct transcript fetch
 
 
 
398
  try:
399
- transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])
400
- text = " ".join([t['text'] for t in transcript])
401
- except:
402
- # Method 2: Try list_transcripts
403
- try:
404
- transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
405
-
406
- # Try multiple language variants
407
- for lang_code in ['en', 'en-US', 'en-GB', 'a.en']:
408
- try:
409
- transcript = transcript_list.find_transcript([lang_code])
410
  text = " ".join([t['text'] for t in transcript.fetch()])
411
  break
412
- except:
413
- continue
414
-
415
- # If no English transcript found, try auto-generated
416
- if 'text' not in locals():
 
417
  transcript = transcript_list.find_generated_transcript(['en'])
418
  text = " ".join([t['text'] for t in transcript.fetch()])
419
- except:
420
- # Method 3: Try translation
421
- available_transcripts = transcript_list.find_manually_created_transcript()
422
- translated = available_transcripts.translate('en')
423
- text = " ".join([t['text'] for t in translated.fetch()])
 
 
 
 
 
 
424
 
425
- # Clean and process text
 
 
 
 
 
426
  cleaned_text = re.sub(r'[^\w\s.]', '', text)
427
  cleaned_text = ' '.join(cleaned_text.split())
428
 
@@ -442,8 +456,7 @@ def process_youtube_video(url="", keywords=""):
442
  summary = model.generate_content(f"Summarize this content: {cleaned_text[:4000]}").text
443
 
444
  except Exception as e:
445
- print(f"Debug - Transcript Error: {str(e)}") # Debug logging
446
- return thumbnail, f"⚠️ Unable to process video: {str(e)}", "N/A", recommendations
447
 
448
  # Get recommendations
449
  if keywords.strip():
@@ -452,7 +465,7 @@ def process_youtube_video(url="", keywords=""):
452
  return thumbnail, summary, sentiment_label, recommendations
453
 
454
  except Exception as e:
455
- print(f"Debug - Main Error: {str(e)}") # Debug logging
456
  return None, f"Error: {str(e)}", "N/A", ""
457
 
458
 
 
383
  summary = ""
384
  sentiment_label = "N/A"
385
  recommendations = ""
386
+ text = ""
387
 
388
  if not url.strip():
389
  return thumbnail, "Please enter a YouTube URL", sentiment_label, recommendations
 
394
 
395
  thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
396
 
397
+ # Method 1: Direct transcript fetch
398
  try:
399
+ transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])
400
+ text = " ".join([t['text'] for t in transcript])
401
+ except:
402
+ # Method 2: List available transcripts
403
  try:
404
+ transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
405
+ transcript = None
406
+
407
+ # Try English variants
408
+ for lang_code in ['en', 'en-US', 'en-GB', 'a.en']:
409
+ try:
410
+ transcript = transcript_list.find_transcript([lang_code])
411
+ if transcript:
 
 
 
412
  text = " ".join([t['text'] for t in transcript.fetch()])
413
  break
414
+ except:
415
+ continue
416
+
417
+ # Try auto-generated if no manual transcript
418
+ if not text:
419
+ try:
420
  transcript = transcript_list.find_generated_transcript(['en'])
421
  text = " ".join([t['text'] for t in transcript.fetch()])
422
+ except:
423
+ # Try translation as last resort
424
+ try:
425
+ manual_transcript = transcript_list.find_manually_created_transcript()
426
+ translated = manual_transcript.translate('en')
427
+ text = " ".join([t['text'] for t in translated.fetch()])
428
+ except:
429
+ raise Exception("No available transcripts found")
430
+
431
+ except Exception as e:
432
+ return thumbnail, f"⚠️ No transcripts available: {str(e)}", sentiment_label, recommendations
433
 
434
+ if not text:
435
+ return thumbnail, "⚠️ Could not extract transcript text", sentiment_label, recommendations
436
+
437
+ # Process valid transcript
438
+ try:
439
+ # Clean text
440
  cleaned_text = re.sub(r'[^\w\s.]', '', text)
441
  cleaned_text = ' '.join(cleaned_text.split())
442
 
 
456
  summary = model.generate_content(f"Summarize this content: {cleaned_text[:4000]}").text
457
 
458
  except Exception as e:
459
+ return thumbnail, f"⚠️ Error processing content: {str(e)}", sentiment_label, recommendations
 
460
 
461
  # Get recommendations
462
  if keywords.strip():
 
465
  return thumbnail, summary, sentiment_label, recommendations
466
 
467
  except Exception as e:
468
+ print(f"Debug - Main Error: {str(e)}")
469
  return None, f"Error: {str(e)}", "N/A", ""
470
 
471