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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -25
app.py CHANGED
@@ -391,38 +391,43 @@ def process_youtube_video(url="", keywords=""):
391
  if not video_id:
392
  return thumbnail, "Invalid YouTube URL", sentiment_label, recommendations
393
 
394
- video_id = extract_video_id(url)
395
- if not video_id:
396
- return thumbnail, "Invalid YouTube URL", sentiment_label, recommendations
397
-
398
- # Set thumbnail
399
  thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
400
 
401
  try:
402
- # Get transcript
403
- transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
404
- transcript = None
405
-
406
- # Try different transcript options
407
- for lang in ['en', 'en-US', 'a.en']:
408
  try:
409
- transcript = transcript_list.find_transcript([lang])
410
- break
 
 
 
 
 
 
 
 
 
 
 
 
 
411
  except:
412
- continue
 
 
 
413
 
414
- if not transcript:
415
- transcript = transcript_list.find_generated_transcript(['en'])
416
-
417
- # Get transcript text
418
- text = " ".join([t['text'] for t in transcript.fetch()])
419
-
420
- # Clean text
421
  cleaned_text = re.sub(r'[^\w\s.]', '', text)
422
  cleaned_text = ' '.join(cleaned_text.split())
423
 
424
  # Sentiment Analysis
425
- blob = TextBlob(cleaned_text[:2000]) # Analyze first 2000 chars
426
  polarity = blob.sentiment.polarity
427
  subjectivity = blob.sentiment.subjectivity
428
 
@@ -436,10 +441,9 @@ def process_youtube_video(url="", keywords=""):
436
  model = genai.GenerativeModel("gemini-pro")
437
  summary = model.generate_content(f"Summarize this content: {cleaned_text[:4000]}").text
438
 
439
- except (TranscriptsDisabled, NoTranscriptFound):
440
- return thumbnail, "⚠️ No English subtitles available", "N/A", recommendations
441
  except Exception as e:
442
- return thumbnail, f"⚠️ Error: {str(e)}", "N/A", recommendations
 
443
 
444
  # Get recommendations
445
  if keywords.strip():
@@ -448,6 +452,7 @@ def process_youtube_video(url="", keywords=""):
448
  return thumbnail, summary, sentiment_label, recommendations
449
 
450
  except Exception as e:
 
451
  return None, f"Error: {str(e)}", "N/A", ""
452
 
453
 
 
391
  if not video_id:
392
  return thumbnail, "Invalid YouTube URL", sentiment_label, recommendations
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
 
429
  # Sentiment Analysis
430
+ blob = TextBlob(cleaned_text[:2000])
431
  polarity = blob.sentiment.polarity
432
  subjectivity = blob.sentiment.subjectivity
433
 
 
441
  model = genai.GenerativeModel("gemini-pro")
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
  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