Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -524,9 +524,6 @@ courses_data = [
|
|
| 524 |
(5, "Mathematics", "Ms. Smith", "Intermediate")
|
| 525 |
]
|
| 526 |
|
| 527 |
-
# def extract_video_id(url):
|
| 528 |
-
# match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11})", url)
|
| 529 |
-
# return match.group(1) if match else None
|
| 530 |
def extract_video_id(url):
|
| 531 |
# Improved regex to handle various YouTube URL formats
|
| 532 |
match = re.search(r"(?:v=|\/|be\/|embed\/|watch\?v=)([0-9A-Za-z_-]{11})", url)
|
|
@@ -573,33 +570,23 @@ def get_recommendations(keywords, max_results=5):
|
|
| 573 |
title = item["snippet"]["title"]
|
| 574 |
channel = item["snippet"]["channelTitle"]
|
| 575 |
video_id = item["id"]["videoId"]
|
| 576 |
-
results.append(f"
|
| 577 |
|
| 578 |
return "\n".join(results) if results else "No recommendations found"
|
| 579 |
except Exception as e:
|
| 580 |
return f"Error: {str(e)}"
|
| 581 |
|
| 582 |
-
def
|
| 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:
|
| 591 |
-
return
|
| 592 |
-
|
| 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,41 +600,23 @@ def process_youtube_video(url):
|
|
| 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 |
-
|
| 622 |
-
summary = f"Summary: {cleaned_text[:400]}..."
|
| 623 |
-
print(f"Sentiment analysis completed: {sentiment_label}")
|
| 624 |
|
| 625 |
except (TranscriptsDisabled, NoTranscriptFound):
|
| 626 |
-
|
| 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"
|
| 631 |
-
|
| 632 |
-
return thumbnail, summary, sentiment_label
|
| 633 |
|
| 634 |
except Exception as e:
|
| 635 |
-
|
| 636 |
-
return None, f"Error: {str(e)}", "N/A"
|
| 637 |
-
|
| 638 |
-
# Test the function
|
| 639 |
-
url = "https://www.youtube.com/watch?v=q1XFm21I-VQ"
|
| 640 |
-
thumbnail, summary, sentiment = process_youtube_video(url)
|
| 641 |
-
print(f"Thumbnail: {thumbnail}\n")
|
| 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
|
| 649 |
with gr.Group() as login_page:
|
| 650 |
-
gr.Markdown("#
|
| 651 |
username = gr.Textbox(label="Username")
|
| 652 |
password = gr.Textbox(label="Password", type="password")
|
| 653 |
login_btn = gr.Button("Login", variant="primary")
|
|
@@ -657,57 +626,34 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 657 |
with gr.Group(visible=False) as main_page:
|
| 658 |
with gr.Row():
|
| 659 |
with gr.Column(scale=1):
|
| 660 |
-
gr.Markdown("###
|
| 661 |
-
nav_dashboard = gr.Button("
|
| 662 |
-
nav_students = gr.Button("
|
| 663 |
-
nav_teachers = gr.Button("
|
| 664 |
-
nav_courses = gr.Button("
|
| 665 |
-
nav_youtube = gr.Button("
|
| 666 |
-
logout_btn = gr.Button("
|
| 667 |
|
| 668 |
with gr.Column(scale=3):
|
| 669 |
# Dashboard Content
|
| 670 |
dashboard_page = gr.Group()
|
| 671 |
with dashboard_page:
|
| 672 |
-
gr.Markdown("##
|
| 673 |
-
gr.Markdown(f"""
|
| 674 |
-
### System Overview
|
| 675 |
-
- π₯ Total Students: {len(students_data)}
|
| 676 |
-
- π¨βπ« Total Teachers: {len(teachers_data)}
|
| 677 |
-
- π Total Courses: {len(courses_data)}
|
| 678 |
-
|
| 679 |
-
### Quick Actions
|
| 680 |
-
- View student performance
|
| 681 |
-
- Access course materials
|
| 682 |
-
- Generate learning insights
|
| 683 |
-
""")
|
| 684 |
|
| 685 |
# Students Content
|
| 686 |
students_page = gr.Group(visible=False)
|
| 687 |
with students_page:
|
| 688 |
-
gr.Markdown("##
|
| 689 |
-
gr.DataFrame(
|
| 690 |
-
value=students_data,
|
| 691 |
-
headers=["ID", "Name", "Grade", "Program"]
|
| 692 |
-
)
|
| 693 |
|
| 694 |
# Teachers Content
|
| 695 |
teachers_page = gr.Group(visible=False)
|
| 696 |
with teachers_page:
|
| 697 |
-
gr.Markdown("##
|
| 698 |
-
gr.DataFrame(
|
| 699 |
-
value=teachers_data,
|
| 700 |
-
headers=["ID", "Name", "Subject", "Qualification"]
|
| 701 |
-
)
|
| 702 |
|
| 703 |
# Courses Content
|
| 704 |
courses_page = gr.Group(visible=False)
|
| 705 |
with courses_page:
|
| 706 |
-
gr.Markdown("##
|
| 707 |
-
gr.DataFrame(
|
| 708 |
-
value=courses_data,
|
| 709 |
-
headers=["ID", "Name", "Instructor", "Level"]
|
| 710 |
-
)
|
| 711 |
|
| 712 |
# YouTube Tool Content
|
| 713 |
youtube_page = gr.Group(visible=False)
|
|
@@ -723,18 +669,19 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 723 |
label="Keywords for Recommendations",
|
| 724 |
placeholder="e.g., python programming, machine learning"
|
| 725 |
)
|
| 726 |
-
analyze_btn = gr.Button("
|
| 727 |
-
recommend_btn = gr.Button("
|
|
|
|
| 728 |
|
| 729 |
with gr.Column(scale=1):
|
| 730 |
video_thumbnail = gr.Image(label="Video Preview")
|
| 731 |
|
| 732 |
with gr.Row():
|
| 733 |
with gr.Column():
|
| 734 |
-
summary = gr.Textbox(label="
|
| 735 |
-
sentiment = gr.Textbox(label="
|
| 736 |
with gr.Column():
|
| 737 |
-
recommendations = gr.Textbox(label="
|
| 738 |
|
| 739 |
def login_check(user, pwd):
|
| 740 |
if USER_CREDENTIALS.get(user) == pwd:
|
|
@@ -746,7 +693,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 746 |
return {
|
| 747 |
login_page: gr.update(visible=True),
|
| 748 |
main_page: gr.update(visible=False),
|
| 749 |
-
login_msg: "
|
| 750 |
}
|
| 751 |
|
| 752 |
def show_page(page_name):
|
|
@@ -776,7 +723,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 776 |
analyze_btn.click(
|
| 777 |
process_youtube_video,
|
| 778 |
inputs=[video_url],
|
| 779 |
-
outputs=[video_thumbnail, summary
|
| 780 |
)
|
| 781 |
|
| 782 |
recommend_btn.click(
|
|
@@ -785,6 +732,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 785 |
outputs=[recommendations]
|
| 786 |
)
|
| 787 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 788 |
logout_btn.click(
|
| 789 |
lambda: {
|
| 790 |
login_page: gr.update(visible=True),
|
|
@@ -795,3 +748,274 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 795 |
|
| 796 |
if __name__ == "__main__":
|
| 797 |
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 524 |
(5, "Mathematics", "Ms. Smith", "Intermediate")
|
| 525 |
]
|
| 526 |
|
|
|
|
|
|
|
|
|
|
| 527 |
def extract_video_id(url):
|
| 528 |
# Improved regex to handle various YouTube URL formats
|
| 529 |
match = re.search(r"(?:v=|\/|be\/|embed\/|watch\?v=)([0-9A-Za-z_-]{11})", url)
|
|
|
|
| 570 |
title = item["snippet"]["title"]
|
| 571 |
channel = item["snippet"]["channelTitle"]
|
| 572 |
video_id = item["id"]["videoId"]
|
| 573 |
+
results.append(f"\ud83d\udcfa {title}\n\ud83d\udc64 {channel}\n\ud83d\udd17 https://youtube.com/watch?v={video_id}\n")
|
| 574 |
|
| 575 |
return "\n".join(results) if results else "No recommendations found"
|
| 576 |
except Exception as e:
|
| 577 |
return f"Error: {str(e)}"
|
| 578 |
|
| 579 |
+
def analyze_sentiment(url):
|
|
|
|
| 580 |
from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
|
| 581 |
from textblob import TextBlob
|
| 582 |
|
| 583 |
try:
|
|
|
|
| 584 |
video_id = extract_video_id(url)
|
| 585 |
if not video_id:
|
| 586 |
+
return "Invalid YouTube URL", "N/A"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 587 |
|
| 588 |
try:
|
| 589 |
# Fetch transcript
|
|
|
|
| 590 |
transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
|
| 591 |
transcript = None
|
| 592 |
try:
|
|
|
|
| 600 |
raise ValueError("Transcript is empty")
|
| 601 |
|
| 602 |
# Clean and analyze text
|
|
|
|
| 603 |
cleaned_text = clean_text_for_analysis(text)
|
| 604 |
sentiment = TextBlob(cleaned_text).sentiment
|
| 605 |
sentiment_label = f"{'Positive' if sentiment.polarity > 0 else 'Negative' if sentiment.polarity < 0 else 'Neutral'} ({sentiment.polarity:.2f})"
|
| 606 |
|
| 607 |
+
return "Sentiment Analysis Completed", sentiment_label
|
|
|
|
|
|
|
| 608 |
|
| 609 |
except (TranscriptsDisabled, NoTranscriptFound):
|
| 610 |
+
return "No transcript available", "N/A"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 611 |
|
| 612 |
except Exception as e:
|
| 613 |
+
return f"Error: {str(e)}", "N/A"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 614 |
|
| 615 |
# Gradio Interface
|
| 616 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 617 |
# Login Page
|
| 618 |
with gr.Group() as login_page:
|
| 619 |
+
gr.Markdown("# \ud83c\udf93 Educational Learning Management System")
|
| 620 |
username = gr.Textbox(label="Username")
|
| 621 |
password = gr.Textbox(label="Password", type="password")
|
| 622 |
login_btn = gr.Button("Login", variant="primary")
|
|
|
|
| 626 |
with gr.Group(visible=False) as main_page:
|
| 627 |
with gr.Row():
|
| 628 |
with gr.Column(scale=1):
|
| 629 |
+
gr.Markdown("### \ud83d\uddcb Navigation")
|
| 630 |
+
nav_dashboard = gr.Button("\ud83d\udcca Dashboard", variant="primary")
|
| 631 |
+
nav_students = gr.Button("\ud83d\udc65 Students")
|
| 632 |
+
nav_teachers = gr.Button("\ud83d\udc68\u200d\ud83c\udf93 Teachers")
|
| 633 |
+
nav_courses = gr.Button("\ud83d\udcda Courses")
|
| 634 |
+
nav_youtube = gr.Button("\ud83c\udfa5 YouTube Tool")
|
| 635 |
+
logout_btn = gr.Button("\ud83d\udeaa Logout", variant="stop")
|
| 636 |
|
| 637 |
with gr.Column(scale=3):
|
| 638 |
# Dashboard Content
|
| 639 |
dashboard_page = gr.Group()
|
| 640 |
with dashboard_page:
|
| 641 |
+
gr.Markdown("## \ud83d\udcca Dashboard")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 642 |
|
| 643 |
# Students Content
|
| 644 |
students_page = gr.Group(visible=False)
|
| 645 |
with students_page:
|
| 646 |
+
gr.Markdown("## \ud83d\udc65 Students")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 647 |
|
| 648 |
# Teachers Content
|
| 649 |
teachers_page = gr.Group(visible=False)
|
| 650 |
with teachers_page:
|
| 651 |
+
gr.Markdown("## \ud83d\udc68\u200d\ud83c\udf93 Teachers")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 652 |
|
| 653 |
# Courses Content
|
| 654 |
courses_page = gr.Group(visible=False)
|
| 655 |
with courses_page:
|
| 656 |
+
gr.Markdown("## \ud83d\udcda Courses")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 657 |
|
| 658 |
# YouTube Tool Content
|
| 659 |
youtube_page = gr.Group(visible=False)
|
|
|
|
| 669 |
label="Keywords for Recommendations",
|
| 670 |
placeholder="e.g., python programming, machine learning"
|
| 671 |
)
|
| 672 |
+
analyze_btn = gr.Button("\ud83d\udd0d Analyze Video", variant="primary")
|
| 673 |
+
recommend_btn = gr.Button("\ud83d\udd0e Get Recommendations", variant="primary")
|
| 674 |
+
sentiment_btn = gr.Button("\ud83d\ude0a Analyze Sentiment", variant="primary")
|
| 675 |
|
| 676 |
with gr.Column(scale=1):
|
| 677 |
video_thumbnail = gr.Image(label="Video Preview")
|
| 678 |
|
| 679 |
with gr.Row():
|
| 680 |
with gr.Column():
|
| 681 |
+
summary = gr.Textbox(label="\ud83d\udd8b\ufe0f Summary", lines=8)
|
| 682 |
+
sentiment = gr.Textbox(label="\ud83d\ude0a Content Sentiment")
|
| 683 |
with gr.Column():
|
| 684 |
+
recommendations = gr.Textbox(label="\ud83c\udfaf Related Videos", lines=10)
|
| 685 |
|
| 686 |
def login_check(user, pwd):
|
| 687 |
if USER_CREDENTIALS.get(user) == pwd:
|
|
|
|
| 693 |
return {
|
| 694 |
login_page: gr.update(visible=True),
|
| 695 |
main_page: gr.update(visible=False),
|
| 696 |
+
login_msg: "\u274c Invalid credentials"
|
| 697 |
}
|
| 698 |
|
| 699 |
def show_page(page_name):
|
|
|
|
| 723 |
analyze_btn.click(
|
| 724 |
process_youtube_video,
|
| 725 |
inputs=[video_url],
|
| 726 |
+
outputs=[video_thumbnail, summary]
|
| 727 |
)
|
| 728 |
|
| 729 |
recommend_btn.click(
|
|
|
|
| 732 |
outputs=[recommendations]
|
| 733 |
)
|
| 734 |
|
| 735 |
+
sentiment_btn.click(
|
| 736 |
+
analyze_sentiment,
|
| 737 |
+
inputs=[video_url],
|
| 738 |
+
outputs=[summary, sentiment]
|
| 739 |
+
)
|
| 740 |
+
|
| 741 |
logout_btn.click(
|
| 742 |
lambda: {
|
| 743 |
login_page: gr.update(visible=True),
|
|
|
|
| 748 |
|
| 749 |
if __name__ == "__main__":
|
| 750 |
app.launch()
|
| 751 |
+
|
| 752 |
+
|
| 753 |
+
|
| 754 |
+
# def extract_video_id(url):
|
| 755 |
+
# # Improved regex to handle various YouTube URL formats
|
| 756 |
+
# match = re.search(r"(?:v=|\/|be\/|embed\/|watch\?v=)([0-9A-Za-z_-]{11})", url)
|
| 757 |
+
# return match.group(1) if match else None
|
| 758 |
+
|
| 759 |
+
# def get_video_metadata(video_id):
|
| 760 |
+
# try:
|
| 761 |
+
# youtube = build("youtube", "v3", developerKey=YOUTUBE_API_KEY)
|
| 762 |
+
# request = youtube.videos().list(part="snippet", id=video_id)
|
| 763 |
+
# response = request.execute()
|
| 764 |
+
|
| 765 |
+
# if "items" in response and len(response["items"]) > 0:
|
| 766 |
+
# snippet = response["items"][0]["snippet"]
|
| 767 |
+
# return {
|
| 768 |
+
# "title": snippet.get("title", "No title available"),
|
| 769 |
+
# "description": snippet.get("description", "No description available"),
|
| 770 |
+
# }
|
| 771 |
+
# return {}
|
| 772 |
+
|
| 773 |
+
# except Exception as e:
|
| 774 |
+
# return {"title": "Error fetching metadata", "description": str(e)}
|
| 775 |
+
|
| 776 |
+
# def clean_text_for_analysis(text):
|
| 777 |
+
# return " ".join(text.split())
|
| 778 |
+
|
| 779 |
+
# def get_recommendations(keywords, max_results=5):
|
| 780 |
+
# if not keywords:
|
| 781 |
+
# return "Please provide search keywords"
|
| 782 |
+
# try:
|
| 783 |
+
# response = requests.get(
|
| 784 |
+
# "https://www.googleapis.com/youtube/v3/search",
|
| 785 |
+
# params={
|
| 786 |
+
# "part": "snippet",
|
| 787 |
+
# "q": f"educational {keywords}",
|
| 788 |
+
# "type": "video",
|
| 789 |
+
# "maxResults": max_results,
|
| 790 |
+
# "relevanceLanguage": "en",
|
| 791 |
+
# "key": YOUTUBE_API_KEY
|
| 792 |
+
# }
|
| 793 |
+
# ).json()
|
| 794 |
+
|
| 795 |
+
# results = []
|
| 796 |
+
# for item in response.get("items", []):
|
| 797 |
+
# title = item["snippet"]["title"]
|
| 798 |
+
# channel = item["snippet"]["channelTitle"]
|
| 799 |
+
# video_id = item["id"]["videoId"]
|
| 800 |
+
# results.append(f"πΊ {title}\nπ€ {channel}\nπ https://youtube.com/watch?v={video_id}\n")
|
| 801 |
+
|
| 802 |
+
# return "\n".join(results) if results else "No recommendations found"
|
| 803 |
+
# except Exception as e:
|
| 804 |
+
# return f"Error: {str(e)}"
|
| 805 |
+
|
| 806 |
+
# def process_youtube_video(url):
|
| 807 |
+
# import re
|
| 808 |
+
# from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
|
| 809 |
+
# from textblob import TextBlob
|
| 810 |
+
|
| 811 |
+
# try:
|
| 812 |
+
# # Extract video ID
|
| 813 |
+
# video_id = extract_video_id(url)
|
| 814 |
+
# if not video_id:
|
| 815 |
+
# return None, "Invalid YouTube URL", "N/A"
|
| 816 |
+
|
| 817 |
+
# # Generate thumbnail URL
|
| 818 |
+
# thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
|
| 819 |
+
|
| 820 |
+
# # Initialize default values
|
| 821 |
+
# summary = "No transcript available"
|
| 822 |
+
# sentiment_label = "N/A"
|
| 823 |
+
|
| 824 |
+
# try:
|
| 825 |
+
# # Fetch transcript
|
| 826 |
+
# print(f"Fetching transcript for video ID: {video_id}")
|
| 827 |
+
# transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
|
| 828 |
+
# transcript = None
|
| 829 |
+
# try:
|
| 830 |
+
# transcript = transcript_list.find_transcript(['en'])
|
| 831 |
+
# except:
|
| 832 |
+
# transcript = transcript_list.find_generated_transcript(['en'])
|
| 833 |
+
|
| 834 |
+
# # Combine transcript into text
|
| 835 |
+
# text = " ".join([t['text'] for t in transcript.fetch()])
|
| 836 |
+
# if not text.strip():
|
| 837 |
+
# raise ValueError("Transcript is empty")
|
| 838 |
+
|
| 839 |
+
# # Clean and analyze text
|
| 840 |
+
# print(f"Transcript fetched successfully. Length: {len(text)} characters")
|
| 841 |
+
# cleaned_text = clean_text_for_analysis(text)
|
| 842 |
+
# sentiment = TextBlob(cleaned_text).sentiment
|
| 843 |
+
# sentiment_label = f"{'Positive' if sentiment.polarity > 0 else 'Negative' if sentiment.polarity < 0 else 'Neutral'} ({sentiment.polarity:.2f})"
|
| 844 |
+
|
| 845 |
+
# # Summarize text
|
| 846 |
+
# summary = f"Summary: {cleaned_text[:400]}..."
|
| 847 |
+
# print(f"Sentiment analysis completed: {sentiment_label}")
|
| 848 |
+
|
| 849 |
+
# except (TranscriptsDisabled, NoTranscriptFound):
|
| 850 |
+
# # Fall back to metadata if no transcript
|
| 851 |
+
# print(f"No transcript found for video ID: {video_id}")
|
| 852 |
+
# metadata = get_video_metadata(video_id)
|
| 853 |
+
# summary = metadata.get("description", "No subtitles available")
|
| 854 |
+
# sentiment_label = "N/A"
|
| 855 |
+
|
| 856 |
+
# return thumbnail, summary, sentiment_label
|
| 857 |
+
|
| 858 |
+
# except Exception as e:
|
| 859 |
+
# print(f"Error processing video: {e}")
|
| 860 |
+
# return None, f"Error: {str(e)}", "N/A"
|
| 861 |
+
|
| 862 |
+
# # Test the function
|
| 863 |
+
# url = "https://www.youtube.com/watch?v=q1XFm21I-VQ"
|
| 864 |
+
# thumbnail, summary, sentiment = process_youtube_video(url)
|
| 865 |
+
# print(f"Thumbnail: {thumbnail}\n")
|
| 866 |
+
# print(f"Summary:\n{summary}\n")
|
| 867 |
+
# print(f"Sentiment: {sentiment}")
|
| 868 |
+
|
| 869 |
+
|
| 870 |
+
# # Gradio Interface
|
| 871 |
+
# with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 872 |
+
# # Login Page
|
| 873 |
+
# with gr.Group() as login_page:
|
| 874 |
+
# gr.Markdown("# π Educational Learning Management System")
|
| 875 |
+
# username = gr.Textbox(label="Username")
|
| 876 |
+
# password = gr.Textbox(label="Password", type="password")
|
| 877 |
+
# login_btn = gr.Button("Login", variant="primary")
|
| 878 |
+
# login_msg = gr.Markdown()
|
| 879 |
+
|
| 880 |
+
# # Main Interface
|
| 881 |
+
# with gr.Group(visible=False) as main_page:
|
| 882 |
+
# with gr.Row():
|
| 883 |
+
# with gr.Column(scale=1):
|
| 884 |
+
# gr.Markdown("### π Navigation")
|
| 885 |
+
# nav_dashboard = gr.Button("π Dashboard", variant="primary")
|
| 886 |
+
# nav_students = gr.Button("π₯ Students")
|
| 887 |
+
# nav_teachers = gr.Button("π¨βπ« Teachers")
|
| 888 |
+
# nav_courses = gr.Button("π Courses")
|
| 889 |
+
# nav_youtube = gr.Button("π₯ YouTube Tool")
|
| 890 |
+
# logout_btn = gr.Button("πͺ Logout", variant="stop")
|
| 891 |
+
|
| 892 |
+
# with gr.Column(scale=3):
|
| 893 |
+
# # Dashboard Content
|
| 894 |
+
# dashboard_page = gr.Group()
|
| 895 |
+
# with dashboard_page:
|
| 896 |
+
# gr.Markdown("## π Dashboard")
|
| 897 |
+
# gr.Markdown(f"""
|
| 898 |
+
# ### System Overview
|
| 899 |
+
# - π₯ Total Students: {len(students_data)}
|
| 900 |
+
# - π¨βπ« Total Teachers: {len(teachers_data)}
|
| 901 |
+
# - π Total Courses: {len(courses_data)}
|
| 902 |
+
|
| 903 |
+
# ### Quick Actions
|
| 904 |
+
# - View student performance
|
| 905 |
+
# - Access course materials
|
| 906 |
+
# - Generate learning insights
|
| 907 |
+
# """)
|
| 908 |
+
|
| 909 |
+
# # Students Content
|
| 910 |
+
# students_page = gr.Group(visible=False)
|
| 911 |
+
# with students_page:
|
| 912 |
+
# gr.Markdown("## π₯ Students")
|
| 913 |
+
# gr.DataFrame(
|
| 914 |
+
# value=students_data,
|
| 915 |
+
# headers=["ID", "Name", "Grade", "Program"]
|
| 916 |
+
# )
|
| 917 |
+
|
| 918 |
+
# # Teachers Content
|
| 919 |
+
# teachers_page = gr.Group(visible=False)
|
| 920 |
+
# with teachers_page:
|
| 921 |
+
# gr.Markdown("## π¨βπ« Teachers")
|
| 922 |
+
# gr.DataFrame(
|
| 923 |
+
# value=teachers_data,
|
| 924 |
+
# headers=["ID", "Name", "Subject", "Qualification"]
|
| 925 |
+
# )
|
| 926 |
+
|
| 927 |
+
# # Courses Content
|
| 928 |
+
# courses_page = gr.Group(visible=False)
|
| 929 |
+
# with courses_page:
|
| 930 |
+
# gr.Markdown("## π Courses")
|
| 931 |
+
# gr.DataFrame(
|
| 932 |
+
# value=courses_data,
|
| 933 |
+
# headers=["ID", "Name", "Instructor", "Level"]
|
| 934 |
+
# )
|
| 935 |
+
|
| 936 |
+
# # YouTube Tool Content
|
| 937 |
+
# youtube_page = gr.Group(visible=False)
|
| 938 |
+
# with youtube_page:
|
| 939 |
+
# gr.Markdown("## Agent for YouTube Content Exploration")
|
| 940 |
+
# with gr.Row():
|
| 941 |
+
# with gr.Column(scale=2):
|
| 942 |
+
# video_url = gr.Textbox(
|
| 943 |
+
# label="YouTube URL",
|
| 944 |
+
# placeholder="https://youtube.com/watch?v=..."
|
| 945 |
+
# )
|
| 946 |
+
# keywords = gr.Textbox(
|
| 947 |
+
# label="Keywords for Recommendations",
|
| 948 |
+
# placeholder="e.g., python programming, machine learning"
|
| 949 |
+
# )
|
| 950 |
+
# analyze_btn = gr.Button("π Analyze Video", variant="primary")
|
| 951 |
+
# recommend_btn = gr.Button("π Get Recommendations", variant="primary")
|
| 952 |
+
|
| 953 |
+
# with gr.Column(scale=1):
|
| 954 |
+
# video_thumbnail = gr.Image(label="Video Preview")
|
| 955 |
+
|
| 956 |
+
# with gr.Row():
|
| 957 |
+
# with gr.Column():
|
| 958 |
+
# summary = gr.Textbox(label="π Summary", lines=8)
|
| 959 |
+
# sentiment = gr.Textbox(label="π Content Sentiment")
|
| 960 |
+
# with gr.Column():
|
| 961 |
+
# recommendations = gr.Textbox(label="π― Related Videos", lines=10)
|
| 962 |
+
|
| 963 |
+
# def login_check(user, pwd):
|
| 964 |
+
# if USER_CREDENTIALS.get(user) == pwd:
|
| 965 |
+
# return {
|
| 966 |
+
# login_page: gr.update(visible=False),
|
| 967 |
+
# main_page: gr.update(visible=True),
|
| 968 |
+
# login_msg: ""
|
| 969 |
+
# }
|
| 970 |
+
# return {
|
| 971 |
+
# login_page: gr.update(visible=True),
|
| 972 |
+
# main_page: gr.update(visible=False),
|
| 973 |
+
# login_msg: "β Invalid credentials"
|
| 974 |
+
# }
|
| 975 |
+
|
| 976 |
+
# def show_page(page_name):
|
| 977 |
+
# updates = {
|
| 978 |
+
# dashboard_page: gr.update(visible=False),
|
| 979 |
+
# students_page: gr.update(visible=False),
|
| 980 |
+
# teachers_page: gr.update(visible=False),
|
| 981 |
+
# courses_page: gr.update(visible=False),
|
| 982 |
+
# youtube_page: gr.update(visible=False)
|
| 983 |
+
# }
|
| 984 |
+
# updates[page_name] = gr.update(visible=True)
|
| 985 |
+
# return updates
|
| 986 |
+
|
| 987 |
+
# # Event Handlers
|
| 988 |
+
# login_btn.click(
|
| 989 |
+
# login_check,
|
| 990 |
+
# inputs=[username, password],
|
| 991 |
+
# outputs=[login_page, main_page, login_msg]
|
| 992 |
+
# )
|
| 993 |
+
|
| 994 |
+
# nav_dashboard.click(lambda: show_page(dashboard_page), outputs=list(show_page(dashboard_page).keys()))
|
| 995 |
+
# nav_students.click(lambda: show_page(students_page), outputs=list(show_page(students_page).keys()))
|
| 996 |
+
# nav_teachers.click(lambda: show_page(teachers_page), outputs=list(show_page(teachers_page).keys()))
|
| 997 |
+
# nav_courses.click(lambda: show_page(courses_page), outputs=list(show_page(courses_page).keys()))
|
| 998 |
+
# nav_youtube.click(lambda: show_page(youtube_page), outputs=list(show_page(youtube_page).keys()))
|
| 999 |
+
|
| 1000 |
+
# analyze_btn.click(
|
| 1001 |
+
# process_youtube_video,
|
| 1002 |
+
# inputs=[video_url],
|
| 1003 |
+
# outputs=[video_thumbnail, summary, sentiment]
|
| 1004 |
+
# )
|
| 1005 |
+
|
| 1006 |
+
# recommend_btn.click(
|
| 1007 |
+
# get_recommendations,
|
| 1008 |
+
# inputs=[keywords],
|
| 1009 |
+
# outputs=[recommendations]
|
| 1010 |
+
# )
|
| 1011 |
+
|
| 1012 |
+
# logout_btn.click(
|
| 1013 |
+
# lambda: {
|
| 1014 |
+
# login_page: gr.update(visible=True),
|
| 1015 |
+
# main_page: gr.update(visible=False)
|
| 1016 |
+
# },
|
| 1017 |
+
# outputs=[login_page, main_page]
|
| 1018 |
+
# )
|
| 1019 |
+
|
| 1020 |
+
# if __name__ == "__main__":
|
| 1021 |
+
# app.launch()
|