Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -580,15 +580,11 @@ def get_recommendations(keywords, max_results=5):
|
|
| 580 |
return f"Error: {str(e)}"
|
| 581 |
|
| 582 |
def process_youtube_video(url):
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
from textblob import TextBlob
|
| 587 |
-
|
| 588 |
-
thumbnail = None
|
| 589 |
-
summary = "No transcript available"
|
| 590 |
-
sentiment_label = "N/A"
|
| 591 |
|
|
|
|
| 592 |
# Extract video ID
|
| 593 |
video_id = extract_video_id(url)
|
| 594 |
if not video_id:
|
|
@@ -597,8 +593,13 @@ def process_youtube_video(url):
|
|
| 597 |
# Generate thumbnail URL
|
| 598 |
thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
|
| 599 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 600 |
try:
|
| 601 |
# Fetch transcript
|
|
|
|
| 602 |
transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
|
| 603 |
transcript = None
|
| 604 |
try:
|
|
@@ -612,15 +613,18 @@ def process_youtube_video(url):
|
|
| 612 |
raise ValueError("Transcript is empty")
|
| 613 |
|
| 614 |
# Clean and analyze text
|
|
|
|
| 615 |
cleaned_text = clean_text_for_analysis(text)
|
| 616 |
sentiment = TextBlob(cleaned_text).sentiment
|
| 617 |
sentiment_label = f"{'Positive' if sentiment.polarity > 0 else 'Negative' if sentiment.polarity < 0 else 'Neutral'} ({sentiment.polarity:.2f})"
|
| 618 |
|
| 619 |
# Summarize text
|
| 620 |
summary = f"Summary: {cleaned_text[:400]}..."
|
|
|
|
| 621 |
|
| 622 |
except (TranscriptsDisabled, NoTranscriptFound):
|
| 623 |
# Fall back to metadata if no transcript
|
|
|
|
| 624 |
metadata = get_video_metadata(video_id)
|
| 625 |
summary = metadata.get("description", "No subtitles available")
|
| 626 |
sentiment_label = "N/A"
|
|
@@ -628,6 +632,7 @@ def process_youtube_video(url):
|
|
| 628 |
return thumbnail, summary, sentiment_label
|
| 629 |
|
| 630 |
except Exception as e:
|
|
|
|
| 631 |
return None, f"Error: {str(e)}", "N/A"
|
| 632 |
|
| 633 |
# Test the function
|
|
@@ -637,6 +642,7 @@ print(f"Thumbnail: {thumbnail}\n")
|
|
| 637 |
print(f"Summary:\n{summary}\n")
|
| 638 |
print(f"Sentiment: {sentiment}")
|
| 639 |
|
|
|
|
| 640 |
# Gradio Interface
|
| 641 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 642 |
# Login Page
|
|
|
|
| 580 |
return f"Error: {str(e)}"
|
| 581 |
|
| 582 |
def process_youtube_video(url):
|
| 583 |
+
import re
|
| 584 |
+
from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
|
| 585 |
+
from textblob import TextBlob
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 586 |
|
| 587 |
+
try:
|
| 588 |
# Extract video ID
|
| 589 |
video_id = extract_video_id(url)
|
| 590 |
if not video_id:
|
|
|
|
| 593 |
# Generate thumbnail URL
|
| 594 |
thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
|
| 595 |
|
| 596 |
+
# Initialize default values
|
| 597 |
+
summary = "No transcript available"
|
| 598 |
+
sentiment_label = "N/A"
|
| 599 |
+
|
| 600 |
try:
|
| 601 |
# Fetch transcript
|
| 602 |
+
print(f"Fetching transcript for video ID: {video_id}")
|
| 603 |
transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
|
| 604 |
transcript = None
|
| 605 |
try:
|
|
|
|
| 613 |
raise ValueError("Transcript is empty")
|
| 614 |
|
| 615 |
# Clean and analyze text
|
| 616 |
+
print(f"Transcript fetched successfully. Length: {len(text)} characters")
|
| 617 |
cleaned_text = clean_text_for_analysis(text)
|
| 618 |
sentiment = TextBlob(cleaned_text).sentiment
|
| 619 |
sentiment_label = f"{'Positive' if sentiment.polarity > 0 else 'Negative' if sentiment.polarity < 0 else 'Neutral'} ({sentiment.polarity:.2f})"
|
| 620 |
|
| 621 |
# Summarize text
|
| 622 |
summary = f"Summary: {cleaned_text[:400]}..."
|
| 623 |
+
print(f"Sentiment analysis completed: {sentiment_label}")
|
| 624 |
|
| 625 |
except (TranscriptsDisabled, NoTranscriptFound):
|
| 626 |
# Fall back to metadata if no transcript
|
| 627 |
+
print(f"No transcript found for video ID: {video_id}")
|
| 628 |
metadata = get_video_metadata(video_id)
|
| 629 |
summary = metadata.get("description", "No subtitles available")
|
| 630 |
sentiment_label = "N/A"
|
|
|
|
| 632 |
return thumbnail, summary, sentiment_label
|
| 633 |
|
| 634 |
except Exception as e:
|
| 635 |
+
print(f"Error processing video: {e}")
|
| 636 |
return None, f"Error: {str(e)}", "N/A"
|
| 637 |
|
| 638 |
# Test the function
|
|
|
|
| 642 |
print(f"Summary:\n{summary}\n")
|
| 643 |
print(f"Sentiment: {sentiment}")
|
| 644 |
|
| 645 |
+
|
| 646 |
# Gradio Interface
|
| 647 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 648 |
# Login Page
|