Sayiqa7 commited on
Commit
ba09a01
·
verified ·
1 Parent(s): 4c0e509

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +489 -1048
app.py CHANGED
@@ -1,322 +1,475 @@
1
- import subprocess
2
- subprocess.check_call(["pip", "install", "transformers==4.34.0"])
3
- subprocess.check_call(["pip", "install", "torch>=1.7.1"])
4
- subprocess.check_call(["pip", "install", "youtube_transcript_api>=0.6.3"])
5
- subprocess.check_call(["pip", "install", "pytube"])
6
- subprocess.check_call(["pip", "install", "huggingface_hub>=0.19.0"])
7
- subprocess.check_call(["pip", "install", "PyPDF2>=3.0.1"])
8
- subprocess.check_call(["pip", "install", "google-generativeai"])
9
- subprocess.check_call(["pip", "install", "textblob>=0.17.1"])
10
- subprocess.check_call(["pip", "install", "python-dotenv>=1.0.0"])
11
- subprocess.check_call(["pip", "install", "genai"])
12
- subprocess.check_call(["pip", "install", "google-cloud-aiplatform==1.34.0"])
13
- import transformers
14
- import torch
15
- import os
16
- import youtube_transcript_api
17
- import pytube
18
- import gradio
19
- import PyPDF2
20
- import pathlib
21
- import pandas
22
- import numpy
23
- import textblob
24
- import gradio as gr
25
- from youtube_transcript_api import YouTubeTranscriptApi
26
- import google.generativeai as genai
27
- import requests
28
- from textblob import TextBlob
29
- import re
30
- #from google.cloud import generativeai
31
- from huggingface_hub import login
32
- from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
33
- def install_missing_packages():
34
- required_packages = {
35
- "torch":">=1.11.0",
36
- "transformers":">=4.34.0",
37
- "youtube_transcript_api" :">=0.6.3" ,
38
- "pytube":None,
39
- "huggingface_hub": ">=0.19.0",
40
- "PyPDF2": ">=3.0.1",
41
- "textblob":">=0.17.1",
42
- "python-dotenv":">=1.0.0",
43
- "genai":None,
44
- "google-generativeai": None,
45
- "google-cloud-aiplatform":"==1.34.0"
46
- }
 
 
 
 
47
 
48
 
49
- for package, version in required_packages.items():
50
- try:
51
- __import__(package)
52
- except ImportError:
53
- package_name = f"{package}{version}" if version else package
54
- subprocess.check_call(["pip", "install", package_name])
55
 
56
- install_missing_packages()
57
- # Configuration
58
 
59
- hf_token = os.getenv("HF_TOKEN")
60
- if hf_token:
61
- login(hf_token)
62
- else:
63
- raise ValueError("HF_TOKEN environment variable not set.")
64
-
65
- # GOOGLE_API_KEY = "AIzaSyAURQb9jueh3dBQ4SITgKoR0L2_33en3yU"
66
- # YOUTUBE_API_KEY = "AIzaSyB7X-RYjZmUuDSMTQsvCfyzURw5bhqOto4"
67
- # # genai.configure(api_key=GOOGLE_API_KEY)
 
 
 
 
 
 
68
  # GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
69
- # genai.configure(api_key=GOOGLE_API_KEY)
70
  # YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
71
- # print("GOOGLE_API_KEY:", os.getenv("GOOGLE_API_KEY"))
72
- # print("YOUTUBE_API_KEY:", os.getenv("YOUTUBE_API_KEY"))
73
 
 
 
74
 
 
75
 
76
- # Configuration
77
- USER_CREDENTIALS = {
78
- "admin": "password123",
79
- "teacher": "teach2024",
80
- "student": "learn2024"
81
- }
 
 
 
82
 
83
- import os
84
- from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
 
 
 
 
85
 
86
- # Use environment variables
87
- GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
88
- YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
 
 
 
 
89
 
90
- if not GOOGLE_API_KEY or not YOUTUBE_API_KEY:
91
- raise ValueError("Please set GOOGLE_API_KEY and YOUTUBE_API_KEY environment variables")
 
92
 
93
- genai.configure(api_key=GOOGLE_API_KEY)
 
 
 
 
94
 
95
- # Database
96
- students_data = [
97
- (1, "Alice", "A", "Computer Science"),
98
- (2, "Aliaa", "B", "Mathematics"),
99
- (3, "Charlie", "A", "Machine Learning"),
100
- (4, "Daan", "A", "Physics"),
101
- (5, "Jhon", "C", "Math"),
102
- (6, "Emma", "A+", "Computer Science")
103
- ]
104
 
105
- teachers_data = [
106
- (1, "Dr. Smith", "Math", "MS Mathematics"),
107
- (2, "Ms. Johnson", "Science", "MSc Physics"),
108
- (3, "Ms. Jack", "Artificial Intelligence Engineer", "MSc AI"),
109
- (4, "Ms. Evelyn", "Computer Science", "MSc Computer Science"),
110
- ]
111
 
112
- courses_data = [
113
- (1, "Algebra", "Dr. Smith", "Advanced"),
114
- (2, "Biology", "Ms. Mia", "Intermediate"),
115
- (3, "Machine Learning", "Ms. Jack", "Intermediate"),
116
- (4, "Computer Science", "Ms. Evelyn", "Intermediate"),
117
- (5, "Mathematics", "Ms. Smith", "Intermediate")
118
- ]
119
 
120
- def sanitize_text(text):
121
- """Remove invalid Unicode characters."""
122
- return text.encode("utf-8", "replace").decode("utf-8")
 
 
 
 
 
 
 
 
 
 
 
 
123
 
124
- def extract_video_id(url):
125
- if not url:
126
- return None
127
- patterns = [
128
- r'(?:v=|\/videos\/|embed\/|youtu.be\/|\/v\/|\/e\/|watch\?v=|\/watch\?v=)([^#\&\?]*)'
129
- ]
130
- for pattern in patterns:
131
- match = re.search(pattern, url)
132
- if match:
133
- return match.group(1)
134
- return None
135
-
136
-
137
- # def process_youtube_video(url="", keywords=""):
138
  # try:
139
- # # Initialize variables
140
- # thumbnail = None # Default value for thumbnail
141
  # summary = "No transcript available"
142
  # sentiment_label = "N/A"
143
 
144
- # if not url.strip():
145
- # return None, "Please enter a YouTube URL", "N/A", ""
146
-
147
  # video_id = extract_video_id(url)
148
  # if not video_id:
149
- # return None, "Invalid YouTube URL", "N/A", ""
150
 
151
  # thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
152
 
153
  # try:
154
- # # Try multiple transcript options
155
  # transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
156
-
157
  # try:
158
  # transcript = transcript_list.find_transcript(['en'])
159
  # except:
160
- # try:
161
- # transcript = transcript_list.find_transcript(['en-US'])
162
- # except:
163
- # try:
164
- # # Try auto-generated
165
- # transcript = transcript_list.find_generated_transcript(['en'])
166
- # except:
167
- # raise NoTranscriptFound()
168
 
169
  # text = " ".join([t['text'] for t in transcript.fetch()])
170
-
171
- # # Generate summary
172
- # model = genai.GenerativeModel("gemini-pro")
173
- # summary = model.generate_content(f"Summarize this: {text[:4000]}").text
174
-
175
- # # Analysis
176
- # sentiment = TextBlob(text[:1000]).sentiment
177
- # sentiment_label = f"{'Positive' if sentiment.polarity > 0 else 'Negative' if sentiment.polarity < 0 else 'Neutral'} ({sentiment.polarity:.2f})"
178
-
179
- # except TranscriptsDisabled:
180
- # # Fallback: Use video metadata if available
181
- # metadata = get_video_metadata(video_id)
182
- # summary = metadata.get("description", "⚠️ This video has disabled subtitles. No transcript available.")
183
- # except NoTranscriptFound:
184
- # # Fallback: Use video metadata if available
185
- # metadata = get_video_metadata(video_id)
186
- # summary = metadata.get("description", "⚠️ No English transcript available. No transcript available.")
187
- # except Exception as e:
188
- # return thumbnail, f"⚠️ Error: {str(e)}", "N/A", ""
189
-
190
- # # Get recommendations
191
- # if keywords.strip():
192
- # recommendations = get_recommendations(keywords)
193
- # else:
194
- # recommendations = ""
195
 
196
- # return thumbnail, summary, sentiment_label, recommendations
197
-
198
- # except Exception as e:
199
- # return None, f"Error: {str(e)}", "N/A", ""
200
 
 
 
201
 
202
- # def get_video_metadata(video_id):
203
- # """
204
- # Fetches video metadata such as title and description using the YouTube Data API.
205
- # """
206
- # try:
207
- # from googleapiclient.discovery import build
208
 
209
- # # Replace with your YouTube Data API key
210
- # API_KEY = "AIzaSyB7X-RYjZmUuDSMTQsvCfyzURw5bhqOto4"
211
- # youtube = build("youtube", "v3", developerKey=API_KEY)
212
- # request = youtube.videos().list(part="snippet", id=video_id)
213
- # response = request.execute()
214
 
215
- # if "items" in response and len(response["items"]) > 0:
216
- # snippet = response["items"][0]["snippet"]
217
- # return {
218
- # "title": snippet.get("title", "No title available"),
219
- # "description": snippet.get("description", "No description available"),
220
- # }
221
- # return {}
222
 
223
  # except Exception as e:
224
- # return {"title": "Error fetching metadata", "description": str(e)}
225
-
226
- # # Get recommendations
227
- # if keywords.strip():
228
- # recommendations = get_recommendations(keywords)
229
- # else:
230
- # recommendations = ""
231
 
232
- # return thumbnail, summary, sentiment_label, recommendations
233
-
234
- # except Exception as e:
235
- # return None, f"Error: {str(e)}", "N/A", ""
 
 
 
 
 
236
 
237
- def process_youtube_video(url="", keywords=""):
238
- try:
239
- # Initialize variables
240
- thumbnail = None
241
- summary = "No transcript available"
242
- sentiment_label = "N/A"
243
- recommendations = ""
244
- subtitle_info = "No additional information available"
 
 
 
245
 
246
- if not url.strip():
247
- return None, "Please enter a YouTube URL", "N/A", "", ""
 
 
 
 
 
 
 
 
 
 
 
 
 
248
 
249
- video_id = extract_video_id(url)
250
- if not video_id:
251
- return None, "Invalid YouTube URL", "N/A", "", ""
 
 
 
 
 
252
 
253
- thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254
 
 
 
255
  try:
256
- # Fetch transcript
257
- transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
258
- transcript = None
259
- try:
260
- transcript = transcript_list.find_transcript(['en'])
261
- except:
262
- transcript = transcript_list.find_generated_transcript(['en'])
263
 
264
- text = " ".join([t['text'] for t in transcript.fetch()])
265
- if not text.strip():
266
- raise ValueError("Transcript is empty")
267
 
268
- # Generate summary
269
- model = genai.GenerativeModel("gemini-pro")
270
- summary = model.generate_content(f"Summarize this: {text[:4000]}").text
 
 
271
 
272
- # Extract subtitle information
273
- subtitle_info = extract_subtitle_info(text)
274
 
275
- # Sentiment analysis
276
- sentiment = TextBlob(text[:1000]).sentiment
277
- sentiment_label = f"{'Positive' if sentiment.polarity > 0 else 'Negative' if sentiment.polarity < 0 else 'Neutral'} ({sentiment.polarity:.2f})"
278
 
279
- except TranscriptsDisabled:
280
- metadata = get_video_metadata(video_id)
281
- summary = metadata.get("description", "⚠️ This video has disabled subtitles.")
282
- sentiment_label = "N/A"
283
- subtitle_info = "No subtitles available for analysis."
284
- except NoTranscriptFound:
285
- metadata = get_video_metadata(video_id)
286
- summary = metadata.get("description", "⚠️ No English transcript available.")
287
- sentiment_label = "N/A"
288
- subtitle_info = "No subtitles available for analysis."
289
- except Exception as e:
290
- return thumbnail, f"⚠️ Error processing transcript: {str(e)}", "N/A", "", ""
291
 
292
- # Get recommendations
293
- if keywords.strip():
294
- recommendations = get_recommendations(keywords)
295
 
296
- return thumbnail, summary, sentiment_label, subtitle_info, recommendations
 
 
297
 
298
- except Exception as e:
299
- return None, f"Error: {str(e)}", "N/A", "", ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300
 
 
 
 
 
 
 
 
301
 
302
  def extract_video_id(url):
303
- """
304
- Extracts the video ID from a YouTube URL.
305
- """
306
- import re
307
  match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11})", url)
308
  return match.group(1) if match else None
309
 
310
-
311
  def get_video_metadata(video_id):
312
- """
313
- Fetches video metadata such as title and description using the YouTube Data API.
314
- """
315
  try:
316
- from googleapiclient.discovery import build
317
-
318
- # Replace with your YouTube Data API key
319
- YOUTUBE_API_KEY = "AIzaSyD_SDF4lC3vpHVAMnBOcN2ZCTz7dRjUc98"
320
  youtube = build("youtube", "v3", developerKey=YOUTUBE_API_KEY)
321
  request = youtube.videos().list(part="snippet", id=video_id)
322
  response = request.execute()
@@ -332,35 +485,46 @@ def get_video_metadata(video_id):
332
  except Exception as e:
333
  return {"title": "Error fetching metadata", "description": str(e)}
334
 
 
 
335
 
336
- def extract_subtitle_info(text):
337
- """
338
- Extracts meaningful information from the subtitles.
339
- This could include topics, key insights, or a breakdown of the content.
340
- """
341
  try:
342
- # Split text into sentences for better analysis
343
- sentences = text.split(". ")
344
-
345
- # Example: Extract key topics or keywords
346
- from collections import Counter
347
- words = text.split()
348
- common_words = Counter(words).most_common(10)
349
- key_topics = ", ".join([word for word, count in common_words])
350
-
351
- # Example: Provide a breakdown of the content
352
- info = f"Key topics discussed: {key_topics}. \nNumber of sentences: {len(sentences)}. \nTotal words: {len(words)}."
353
-
354
- return info
355
- except Exception as e:
356
- return f"Error extracting subtitle information: {str(e)}"
357
-
358
-
359
-
360
-
361
-
362
 
 
 
 
363
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
364
 
365
  def get_recommendations(keywords, max_results=5):
366
  if not keywords:
@@ -377,14 +541,14 @@ def get_recommendations(keywords, max_results=5):
377
  "key": YOUTUBE_API_KEY
378
  }
379
  ).json()
380
-
381
  results = []
382
  for item in response.get("items", []):
383
  title = item["snippet"]["title"]
384
  channel = item["snippet"]["channelTitle"]
385
  video_id = item["id"]["videoId"]
386
- results.append(f"📺 {title}\n👤 {channel}\n🔗 https://youtube.com/watch?v={video_id}\n")
387
-
388
  return "\n".join(results) if results else "No recommendations found"
389
  except Exception as e:
390
  return f"Error: {str(e)}"
@@ -393,68 +557,67 @@ def get_recommendations(keywords, max_results=5):
393
  with gr.Blocks(theme=gr.themes.Soft()) as app:
394
  # Login Page
395
  with gr.Group() as login_page:
396
- gr.Markdown("# 🎓 Educational Learning Management System")
397
  username = gr.Textbox(label="Username")
398
  password = gr.Textbox(label="Password", type="password")
399
  login_btn = gr.Button("Login", variant="primary")
400
  login_msg = gr.Markdown()
401
-
402
  # Main Interface
403
  with gr.Group(visible=False) as main_page:
404
  with gr.Row():
405
  with gr.Column(scale=1):
406
- gr.Markdown("### 📋 Navigation")
407
- nav_dashboard = gr.Button("📊 Dashboard", variant="primary")
408
- nav_students = gr.Button("👥 Students")
409
- nav_teachers = gr.Button("👨‍🏫 Teachers")
410
- nav_courses = gr.Button("📚 Courses")
411
- nav_youtube = gr.Button("🎥 YouTube Tool")
412
- logout_btn = gr.Button("🚪 Logout", variant="stop")
413
-
414
  with gr.Column(scale=3):
415
  # Dashboard Content
416
  dashboard_page = gr.Group()
417
  with dashboard_page:
418
- gr.Markdown("## 📊 Dashboard")
419
  gr.Markdown(f"""
420
  ### System Overview
421
- - 👥 Total Students: {len(students_data)}
422
- - 👨‍🏫 Total Teachers: {len(teachers_data)}
423
- - 📚 Total Courses: {len(courses_data)}
424
-
425
  ### Quick Actions
426
  - View student performance
427
  - Access course materials
428
  - Generate learning insights
429
  """)
430
-
431
  # Students Content
432
  students_page = gr.Group(visible=False)
433
  with students_page:
434
- gr.Markdown("## 👥 Students")
435
  gr.DataFrame(
436
  value=students_data,
437
  headers=["ID", "Name", "Grade", "Program"]
438
  )
439
-
440
  # Teachers Content
441
  teachers_page = gr.Group(visible=False)
442
  with teachers_page:
443
- gr.Markdown("## 👨‍🏫 Teachers")
444
  gr.DataFrame(
445
  value=teachers_data,
446
  headers=["ID", "Name", "Subject", "Qualification"]
447
  )
448
-
449
  # Courses Content
450
  courses_page = gr.Group(visible=False)
451
  with courses_page:
452
- gr.Markdown("## 📚 Courses")
453
  gr.DataFrame(
454
  value=courses_data,
455
  headers=["ID", "Name", "Instructor", "Level"]
456
  )
457
-
458
  # YouTube Tool Content
459
  youtube_page = gr.Group(visible=False)
460
  with youtube_page:
@@ -469,17 +632,18 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
469
  label="Keywords for Recommendations",
470
  placeholder="e.g., python programming, machine learning"
471
  )
472
- analyze_btn = gr.Button("🔍 Analyze Video", variant="primary")
473
-
 
474
  with gr.Column(scale=1):
475
  video_thumbnail = gr.Image(label="Video Preview")
476
-
477
  with gr.Row():
478
  with gr.Column():
479
- summary = gr.Textbox(label="📝 Summary", lines=8)
480
- sentiment = gr.Textbox(label="😊 Content Sentiment")
481
  with gr.Column():
482
- recommendations = gr.Textbox(label="🎯 Related Videos", lines=10)
483
 
484
  def login_check(user, pwd):
485
  if USER_CREDENTIALS.get(user) == pwd:
@@ -491,9 +655,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
491
  return {
492
  login_page: gr.update(visible=True),
493
  main_page: gr.update(visible=False),
494
- login_msg: " Invalid credentials"
495
  }
496
-
497
  def show_page(page_name):
498
  updates = {
499
  dashboard_page: gr.update(visible=False),
@@ -504,26 +668,32 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
504
  }
505
  updates[page_name] = gr.update(visible=True)
506
  return updates
507
-
508
  # Event Handlers
509
  login_btn.click(
510
  login_check,
511
  inputs=[username, password],
512
  outputs=[login_page, main_page, login_msg]
513
  )
514
-
515
  nav_dashboard.click(lambda: show_page(dashboard_page), outputs=list(show_page(dashboard_page).keys()))
516
  nav_students.click(lambda: show_page(students_page), outputs=list(show_page(students_page).keys()))
517
  nav_teachers.click(lambda: show_page(teachers_page), outputs=list(show_page(teachers_page).keys()))
518
  nav_courses.click(lambda: show_page(courses_page), outputs=list(show_page(courses_page).keys()))
519
  nav_youtube.click(lambda: show_page(youtube_page), outputs=list(show_page(youtube_page).keys()))
520
-
521
  analyze_btn.click(
522
  process_youtube_video,
523
- inputs=[video_url, keywords],
524
- outputs=[video_thumbnail, summary, sentiment, recommendations]
525
  )
526
-
 
 
 
 
 
 
527
  logout_btn.click(
528
  lambda: {
529
  login_page: gr.update(visible=True),
@@ -534,732 +704,3 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
534
 
535
  if __name__ == "__main__":
536
  app.launch()
537
-
538
-
539
- ########################
540
- # import os
541
- # os.system('conda install -c conda-forge youtube-transcript-api -y')
542
- # import subprocess
543
- # subprocess.check_call(["pip", "install", "transformers==4.34.0"])
544
- # subprocess.check_call(["pip", "install", "torch>=1.7.1"])
545
- # subprocess.check_call(["pip", "install", "youtube_transcript_api>=0.6.3"])
546
- # subprocess.check_call(["pip", "install", "pytube>=12.1.0"])
547
- # subprocess.check_call(["pip", "install", "huggingface_hub>=0.19.0"])
548
- # subprocess.check_call(["pip", "install", "PyPDF2>=3.0.1"])
549
- # subprocess.check_call(["pip", "install", "google-generativeai>=0.3.0"])
550
- # subprocess.check_call(["pip", "install", "textblob>=0.17.1"])
551
- # subprocess.check_call(["pip", "install", "python-dotenv>=1.0.0"])
552
- # subprocess.check_call(["pip", "install", "genai"])
553
- # subprocess.check_call(["pip", "install", "google-cloud-aiplatform==1.34.0"])
554
- # subprocess.check_call(["pip", "install", "scikit-learn>=1.0.0"])
555
- # import transformers
556
- # import torch
557
- # import os
558
- # import youtube_transcript_api
559
- # import pytube
560
- # import gradio
561
- # import PyPDF2
562
- # import pathlib
563
- # import pandas
564
- # import numpy
565
- # import textblob
566
- # import gradio as gr
567
- # from youtube_transcript_api import YouTubeTranscriptApi
568
- # import google.generativeai as genai
569
- # import requests
570
- # from textblob import TextBlob
571
- # import re
572
- # from dotenv import load_dotenv
573
- # from sklearn.feature_extraction.text import TfidfVectorizer
574
- # from sklearn.metrics.pairwise import cosine_similarity
575
-
576
-
577
- # #from google.cloud import generativeai
578
- # from huggingface_hub import login
579
- # from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
580
- # def install_missing_packages():
581
- # required_packages = {
582
- # "torch":">=1.11.0",
583
- # "transformers":">=4.34.0",
584
- # "youtube_transcript_api" :">=0.6.3" ,
585
- # "pytube":">=12.1.0",
586
- # "huggingface_hub": ">=0.19.0",
587
- # "PyPDF2": ">=3.0.1",
588
- # "textblob":">=0.17.1",
589
- # "python-dotenv":">=1.0.0",
590
- # "genai":None,
591
- # "google-generativeai": ">=0.3.0",
592
- # "google-cloud-aiplatform":"==1.34.0",
593
- # "scikit-learn": ">=1.0.0"
594
- # }
595
-
596
-
597
- # for package, version in required_packages.items():
598
- # try:
599
- # __import__(package)
600
- # except ImportError:
601
- # package_name = f"{package}{version}" if version else package
602
- # subprocess.check_call(["pip", "install", package_name])
603
-
604
- # install_missing_packages()
605
- # # # Configuration
606
- # # USER_CREDENTIALS = {
607
- # # "admin": "password123",
608
- # # "teacher": "teach2024",
609
- # # "student": "learn2024"
610
- # # }
611
-
612
- # hf_token = os.getenv("HF_TOKEN")
613
- # if hf_token:
614
- # login(hf_token)
615
- # else:
616
- # raise ValueError("HF_TOKEN environment variable not set.")
617
-
618
- # # GOOGLE_API_KEY = "AIzaSyAURQb9jueh3dBQ4SITgKoR0L2_33en3yU"
619
- # # YOUTUBE_API_KEY = "AIzaSyB7X-RYjZmUuDSMTQsvCfyzURw5bhqOto4"
620
- # # genai.configure(api_key=GOOGLE_API_KEY)
621
- # # # GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
622
- # # # genai.configure(api_key=GOOGLE_API_KEY)
623
- # # # YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
624
- # # # print("GOOGLE_API_KEY:", os.getenv("GOOGLE_API_KEY"))
625
- # # # print("YOUTUBE_API_KEY:", os.getenv("YOUTUBE_API_KEY"))
626
-
627
- # import os
628
- # import gradio as gr
629
- # from youtube_transcript_api import YouTubeTranscriptApi
630
- # from dotenv import load_dotenv
631
- # import google.generativeai as genai
632
- # import requests
633
-
634
- # from transformers import pipeline
635
- # from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
636
- # from pytube import YouTube
637
- # import gradio as gr
638
- # from transformers import pipeline
639
- # from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
640
- # from pytube import YouTube
641
- # import gradio as gr
642
-
643
- # # Function to extract transcript from a YouTube video
644
- # def extract_transcript(youtube_video_url):
645
- # try:
646
- # video_id = youtube_video_url.split("v=")[1].split("&")[0] # Extract video ID from URL
647
-
648
- # # Try to fetch transcript using YouTubeTranscriptApi
649
- # try:
650
- # transcript = YouTubeTranscriptApi.get_transcript(video_id)
651
- # result = " ".join([i['text'] for i in transcript])
652
- # return result
653
- # except (TranscriptsDisabled, NoTranscriptFound):
654
- # pass # Fallback to pytube if no transcript found
655
-
656
- # # Fallback to pytube for captions
657
- # yt = YouTube(youtube_video_url)
658
- # if yt.captions:
659
- # captions = yt.captions.get_by_language_code("en")
660
- # if captions:
661
- # transcript = captions.generate_srt_captions()
662
- # return transcript.replace("\n", " ") # Clean SRT captions format
663
- # else:
664
- # return "Error: No English captions available for this video."
665
- # else:
666
- # return "Error: No captions available for this video."
667
-
668
- # except Exception as e:
669
- # return f"Error: Unable to process the video URL. Details: {str(e)}"
670
-
671
- # # Function to summarize the text
672
- # def summarize_transcript(transcript_text):
673
- # try:
674
- # summarizer = pipeline('summarization', model="facebook/bart-large-cnn") # Using BART model for summarization
675
- # summarized_text = []
676
- # num_iters = len(transcript_text) // 1000 + 1
677
-
678
- # for i in range(num_iters):
679
- # start = i * 1000
680
- # end = (i + 1) * 1000
681
- # chunk = transcript_text[start:end]
682
- # if chunk.strip():
683
- # out = summarizer(chunk, max_length=130, min_length=30, do_sample=False)
684
- # summarized_text.append(out[0]['summary_text'])
685
-
686
- # return " ".join(summarized_text)
687
- # except Exception as e:
688
- # return f"Error during summarization: {str(e)}"
689
-
690
- # # Gradio Interface function
691
- # def gradio_interface(youtube_url):
692
- # transcript = extract_transcript(youtube_url)
693
- # if transcript.startswith("Error"):
694
- # return transcript, "Summarization cannot be performed due to transcript error."
695
-
696
- # summarized_text = summarize_transcript(transcript)
697
- # return transcript, summarized_text
698
-
699
- # # Gradio Interface Design
700
- # with gr.Blocks() as demo:
701
- # gr.Markdown("# YouTube Video Summarizer")
702
- # with gr.Row():
703
- # youtube_url_input = gr.Textbox(
704
- # label="YouTube Video URL",
705
- # placeholder="Enter the YouTube video link here"
706
- # )
707
- # summarize_button = gr.Button("Summarize Transcript")
708
- # with gr.Row():
709
- # transcript_output = gr.Textbox(label="Original Transcript", lines=10, interactive=False)
710
- # summary_output = gr.Textbox(label="Summarized Text", lines=10, interactive=False)
711
- # summarize_button.click(
712
- # fn=gradio_interface,
713
- # inputs=[youtube_url_input],
714
- # outputs=[transcript_output, summary_output]
715
- # )
716
-
717
- # # Launch Gradio App
718
- # if __name__ == "__main__":
719
- # demo.launch()
720
- ###############################################
721
- # import os
722
- # os.system('conda install -c conda-forge youtube-transcript-api -y')
723
- # import subprocess
724
- # subprocess.check_call(["pip", "install", "transformers==4.34.0"])
725
- # subprocess.check_call(["pip", "install", "torch>=1.7.1"])
726
- # subprocess.check_call(["pip", "install", "youtube_transcript_api>=0.6.3"])
727
- # subprocess.check_call(["pip", "install", "pytube>=15.0.0"])
728
- # subprocess.check_call(["pip", "install", "huggingface_hub>=0.19.0"])
729
- # subprocess.check_call(["pip", "install", "PyPDF2>=3.0.1"])
730
- # subprocess.check_call(["pip", "install", "google-generativeai==0.3.1"])
731
- # subprocess.check_call(["pip", "install", "textblob>=0.17.1"])
732
- # subprocess.check_call(["pip", "install", "python-dotenv>=1.0.0"])
733
- # subprocess.check_call(["pip", "install", "genai"])
734
- # subprocess.check_call(["pip", "install", "google-cloud-aiplatform==1.34.0"])
735
- # subprocess.check_call(["pip", "install", "yt-dlp"])
736
- # subprocess.check_call(["pip", "install", "browser-cookie3"])
737
- # subprocess.check_call(["pip", "install", "ffmpeg-python"])
738
- # import transformers
739
- # import torch
740
- # import os
741
- # import youtube_transcript_api
742
- # import pytube
743
- # import gradio
744
- # import PyPDF2
745
- # import pathlib
746
- # import pandas
747
- # import numpy
748
- # import textblob
749
-
750
- # import gradio as gr
751
- # from youtube_transcript_api import YouTubeTranscriptApi
752
- # import google.generativeai as genai
753
- # import requests
754
- # from textblob import TextBlob
755
- # import re
756
- # #from google.cloud import generativeai
757
- # from huggingface_hub import login
758
- # from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
759
- # def install_missing_packages():
760
- # required_packages = {
761
- # "torch":">=1.11.0",
762
- # "transformers":">=4.34.0",
763
- # "youtube_transcript_api" :">=0.6.3" ,
764
- # "pytube":">=15.0.0",
765
- # "huggingface_hub": ">=0.19.0",
766
- # "PyPDF2": ">=3.0.1",
767
- # "textblob":">=0.17.1",
768
- # "python-dotenv":">=1.0.0",
769
- # "genai":None,
770
- # "google-generativeai": "==0.3.1",
771
- # "google-cloud-aiplatform":"==1.34.0"
772
- # }
773
-
774
-
775
- # for package, version in required_packages.items():
776
- # try:
777
- # __import__(package)
778
- # except ImportError:
779
- # package_name = f"{package}{version}" if version else package
780
- # subprocess.check_call(["pip", "install", package_name])
781
-
782
- # install_missing_packages()
783
- # # Configuration
784
-
785
- # hf_token = os.getenv("HF_TOKEN")
786
- # if hf_token:
787
- # login(hf_token)
788
- # else:
789
- # raise ValueError("HF_TOKEN environment variable not set.")
790
-
791
-
792
- # # Configuration
793
- # USER_CREDENTIALS = {
794
- # "admin": "password123",
795
- # "teacher": "teach2024",
796
- # "student": "learn2024"
797
- # }
798
-
799
- # import os
800
- # from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
801
-
802
- # # Use environment variables
803
- # GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
804
- # YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
805
-
806
- # if not GOOGLE_API_KEY or not YOUTUBE_API_KEY:
807
- # raise ValueError("Please set GOOGLE_API_KEY and YOUTUBE_API_KEY environment variables")
808
-
809
- # genai.configure(api_key=GOOGLE_API_KEY)
810
-
811
- # # Database
812
- # students_data = [
813
- # (1, "Alice", "A", "Computer Science"),
814
- # (2, "Aliaa", "B", "Mathematics"),
815
- # (3, "Charlie", "A", "Machine Learning"),
816
- # (4, "Daan", "A", "Physics"),
817
- # (5, "Jhon", "C", "Math"),
818
- # (6, "Emma", "A+", "Computer Science")
819
- # ]
820
-
821
- # teachers_data = [
822
- # (1, "Dr. Smith", "Math", "MS Mathematics"),
823
- # (2, "Ms. Johnson", "Science", "MSc Physics"),
824
- # (3, "Ms. Jack", "Artificial Intelligence Engineer", "MSc AI"),
825
- # (4, "Ms. Evelyn", "Computer Science", "MSc Computer Science"),
826
- # ]
827
-
828
- # courses_data = [
829
- # (1, "Algebra", "Dr. Smith", "Advanced"),
830
- # (2, "Biology", "Ms. Mia", "Intermediate"),
831
- # (3, "Machine Learning", "Ms. Jack", "Intermediate"),
832
- # (4, "Computer Science", "Ms. Evelyn", "Intermediate"),
833
- # (5, "Mathematics", "Ms. Smith", "Intermediate")
834
- # ]
835
-
836
- # def sanitize_text(text):
837
- # """Remove invalid Unicode characters."""
838
- # return text.encode("utf-8", "replace").decode("utf-8")
839
-
840
- # def extract_video_id(url):
841
- # if not url:
842
- # return None
843
- # patterns = [
844
- # r'(?:v=|\/videos\/|embed\/|youtu.be\/|\/v\/|\/e\/|watch\?v=|\/watch\?v=)([^#\&\?]*)'
845
- # ]
846
- # for pattern in patterns:
847
- # match = re.search(pattern, url)
848
- # if match:
849
- # return match.group(1)
850
- # return None
851
-
852
-
853
- # def process_youtube_video(url="", keywords=""):
854
- # try:
855
- # thumbnail = None
856
- # summary = ""
857
- # sentiment_label = "N/A"
858
- # recommendations = ""
859
-
860
- # if not url.strip():
861
- # return thumbnail, "Please enter a YouTube URL", sentiment_label, recommendations
862
-
863
- # video_id = extract_video_id(url)
864
- # if not video_id:
865
- # return thumbnail, "Invalid YouTube URL", sentiment_label, recommendations
866
-
867
- # # Set thumbnail early
868
- # thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
869
-
870
- # # First try getting video info using pytube
871
- # try:
872
- # yt = YouTube(url)
873
- # video_title = yt.title
874
- # video_description = yt.description
875
- # initial_text = f"Title: {video_title}\n\nDescription: {video_description}"
876
- # except Exception as e:
877
- # initial_text = ""
878
-
879
- # try:
880
- # # Try getting transcript
881
- # transcript_text = ""
882
- # transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
883
-
884
- # # Try multiple transcript options
885
- # for lang in ['en', 'en-US']:
886
- # try:
887
- # transcript = transcript_list.find_transcript([lang])
888
- # transcript_text = " ".join([t['text'] for t in transcript.fetch()])
889
- # break
890
- # except:
891
- # continue
892
-
893
- # if not transcript_text:
894
- # # Try auto-generated transcript
895
- # transcript = transcript_list.find_generated_transcript(['en'])
896
- # transcript_text = " ".join([t['text'] for t in transcript.fetch()])
897
-
898
- # # Combine transcript with video info
899
- # if transcript_text:
900
- # analysis_text = f"{initial_text}\n\nTranscript:\n{transcript_text}"
901
- # else:
902
- # analysis_text = initial_text
903
-
904
- # except (TranscriptsDisabled, NoTranscriptFound):
905
- # # If no transcript available, use only video info
906
- # analysis_text = initial_text
907
- # if not analysis_text:
908
- # return thumbnail, "⚠️ Unable to analyze video content. No transcript or video information available.", sentiment_label, recommendations
909
-
910
- # # Generate summary using available text
911
- # try:
912
- # model = genai.GenerativeModel("gemini-pro")
913
- # if len(analysis_text) > 4000:
914
- # analysis_text = analysis_text[:4000]
915
-
916
- # if analysis_text:
917
- # summary = model.generate_content(
918
- # f"Please provide a comprehensive summary of this video content: {analysis_text}"
919
- # ).text
920
- # else:
921
- # summary = "Unable to generate summary due to lack of video content."
922
-
923
- # # Sentiment analysis
924
- # sentiment = TextBlob(analysis_text[:1000]).sentiment
925
- # sentiment_label = f"{'Positive' if sentiment.polarity > 0 else 'Negative' if sentiment.polarity < 0 else 'Neutral'} ({sentiment.polarity:.2f})"
926
-
927
- # except Exception as e:
928
- # summary = f"Error generating summary: {str(e)}"
929
- # sentiment_label = "Error calculating sentiment"
930
-
931
- # # Get recommendations
932
- # if keywords.strip():
933
- # recommendations = get_recommendations(keywords)
934
-
935
- # return thumbnail, summary, sentiment_label, recommendations
936
-
937
- # except Exception as e:
938
- # return None, f"Error: {str(e)}", "N/A", ""
939
-
940
-
941
- # def get_recommendations(keywords, max_results=5):
942
- # if not keywords:
943
- # return "Please provide search keywords"
944
- # try:
945
- # response = requests.get(
946
- # "https://www.googleapis.com/youtube/v3/search",
947
- # params={
948
- # "part": "snippet",
949
- # "q": f"educational {keywords}",
950
- # "type": "video",
951
- # "maxResults": max_results,
952
- # "relevanceLanguage": "en",
953
- # "key": YOUTUBE_API_KEY
954
- # }
955
- # ).json()
956
-
957
- # results = []
958
- # for item in response.get("items", []):
959
- # title = item["snippet"]["title"]
960
- # channel = item["snippet"]["channelTitle"]
961
- # video_id = item["id"]["videoId"]
962
- # results.append(f"📺 {title}\n👤 {channel}\n🔗 https://youtube.com/watch?v={video_id}\n")
963
-
964
- # return "\n".join(results) if results else "No recommendations found"
965
- # except Exception as e:
966
- # return f"Error: {str(e)}"
967
-
968
- # # Gradio Interface
969
- # with gr.Blocks(theme=gr.themes.Soft()) as app:
970
- # # Login Page
971
- # with gr.Group() as login_page:
972
- # gr.Markdown("# 🎓 Educational Learning Management System")
973
- # username = gr.Textbox(label="Username")
974
- # password = gr.Textbox(label="Password", type="password")
975
- # login_btn = gr.Button("Login", variant="primary")
976
- # login_msg = gr.Markdown()
977
-
978
- # # Main Interface
979
- # with gr.Group(visible=False) as main_page:
980
- # with gr.Row():
981
- # with gr.Column(scale=1):
982
- # gr.Markdown("### 📋 Navigation")
983
- # nav_dashboard = gr.Button("📊 Dashboard", variant="primary")
984
- # nav_students = gr.Button("👥 Students")
985
- # nav_teachers = gr.Button("👨‍🏫 Teachers")
986
- # nav_courses = gr.Button("📚 Courses")
987
- # nav_youtube = gr.Button("🎥 YouTube Tool")
988
- # logout_btn = gr.Button("🚪 Logout", variant="stop")
989
-
990
- # with gr.Column(scale=3):
991
- # # Dashboard Content
992
- # dashboard_page = gr.Group()
993
- # with dashboard_page:
994
- # gr.Markdown("## 📊 Dashboard")
995
- # gr.Markdown(f"""
996
- # ### System Overview
997
- # - 👥 Total Students: {len(students_data)}
998
- # - 👨‍🏫 Total Teachers: {len(teachers_data)}
999
- # - 📚 Total Courses: {len(courses_data)}
1000
-
1001
- # ### Quick Actions
1002
- # - View student performance
1003
- # - Access course materials
1004
- # - Generate learning insights
1005
- # """)
1006
-
1007
- # # Students Content
1008
- # students_page = gr.Group(visible=False)
1009
- # with students_page:
1010
- # gr.Markdown("## 👥 Students")
1011
- # gr.DataFrame(
1012
- # value=students_data,
1013
- # headers=["ID", "Name", "Grade", "Program"]
1014
- # )
1015
-
1016
- # # Teachers Content
1017
- # teachers_page = gr.Group(visible=False)
1018
- # with teachers_page:
1019
- # gr.Markdown("## 👨‍🏫 Teachers")
1020
- # gr.DataFrame(
1021
- # value=teachers_data,
1022
- # headers=["ID", "Name", "Subject", "Qualification"]
1023
- # )
1024
-
1025
- # # Courses Content
1026
- # courses_page = gr.Group(visible=False)
1027
- # with courses_page:
1028
- # gr.Markdown("## 📚 Courses")
1029
- # gr.DataFrame(
1030
- # value=courses_data,
1031
- # headers=["ID", "Name", "Instructor", "Level"]
1032
- # )
1033
-
1034
- # # YouTube Tool Content
1035
- # youtube_page = gr.Group(visible=False)
1036
- # with youtube_page:
1037
- # gr.Markdown("## Agent for YouTube Content Exploration")
1038
- # with gr.Row():
1039
- # with gr.Column(scale=2):
1040
- # video_url = gr.Textbox(
1041
- # label="YouTube URL",
1042
- # placeholder="https://youtube.com/watch?v=..."
1043
- # )
1044
- # keywords = gr.Textbox(
1045
- # label="Keywords for Recommendations",
1046
- # placeholder="e.g., python programming, machine learning"
1047
- # )
1048
- # analyze_btn = gr.Button("🔍 Analyze Video", variant="primary")
1049
-
1050
- # with gr.Column(scale=1):
1051
- # video_thumbnail = gr.Image(label="Video Preview")
1052
-
1053
- # with gr.Row():
1054
- # with gr.Column():
1055
- # summary = gr.Textbox(label="📝 Summary", lines=8)
1056
- # sentiment = gr.Textbox(label="😊 Content Sentiment")
1057
- # with gr.Column():
1058
- # recommendations = gr.Textbox(label="🎯 Related Videos", lines=10)
1059
-
1060
- # def login_check(user, pwd):
1061
- # if USER_CREDENTIALS.get(user) == pwd:
1062
- # return {
1063
- # login_page: gr.update(visible=False),
1064
- # main_page: gr.update(visible=True),
1065
- # login_msg: ""
1066
- # }
1067
- # return {
1068
- # login_page: gr.update(visible=True),
1069
- # main_page: gr.update(visible=False),
1070
- # login_msg: "❌ Invalid credentials"
1071
- # }
1072
-
1073
- # def show_page(page_name):
1074
- # updates = {
1075
- # dashboard_page: gr.update(visible=False),
1076
- # students_page: gr.update(visible=False),
1077
- # teachers_page: gr.update(visible=False),
1078
- # courses_page: gr.update(visible=False),
1079
- # youtube_page: gr.update(visible=False)
1080
- # }
1081
- # updates[page_name] = gr.update(visible=True)
1082
- # return updates
1083
-
1084
- # # Event Handlers
1085
- # login_btn.click(
1086
- # login_check,
1087
- # inputs=[username, password],
1088
- # outputs=[login_page, main_page, login_msg]
1089
- # )
1090
-
1091
- # nav_dashboard.click(lambda: show_page(dashboard_page), outputs=list(show_page(dashboard_page).keys()))
1092
- # nav_students.click(lambda: show_page(students_page), outputs=list(show_page(students_page).keys()))
1093
- # nav_teachers.click(lambda: show_page(teachers_page), outputs=list(show_page(teachers_page).keys()))
1094
- # nav_courses.click(lambda: show_page(courses_page), outputs=list(show_page(courses_page).keys()))
1095
- # nav_youtube.click(lambda: show_page(youtube_page), outputs=list(show_page(youtube_page).keys()))
1096
-
1097
- # analyze_btn.click(
1098
- # process_youtube_video,
1099
- # inputs=[video_url, keywords],
1100
- # outputs=[video_thumbnail, summary, sentiment, recommendations]
1101
- # )
1102
-
1103
- # logout_btn.click(
1104
- # lambda: {
1105
- # login_page: gr.update(visible=True),
1106
- # main_page: gr.update(visible=False)
1107
- # },
1108
- # outputs=[login_page, main_page]
1109
- # )
1110
-
1111
- # if __name__ == "__main__":
1112
- # app.launch()
1113
-
1114
- # import gradio as gr
1115
- # from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
1116
- # from transformers import pipeline
1117
- # import torch
1118
- # import os
1119
- # import yt_dlp
1120
- # from urllib.parse import urlparse, parse_qs
1121
- # import browser_cookie3
1122
- # from urllib.parse import urlparse, parse_qs
1123
-
1124
- # def extract_video_id(url):
1125
- # """Extract video ID from YouTube URL"""
1126
- # if not url:
1127
- # return None
1128
-
1129
- # parsed_url = urlparse(url)
1130
- # if parsed_url.hostname in ['www.youtube.com', 'youtube.com']:
1131
- # if parsed_url.path == '/watch':
1132
- # return parse_qs(parsed_url.query)['v'][0]
1133
- # elif parsed_url.path.startswith(('/embed/', '/v/')):
1134
- # return parsed_url.path.split('/')[2]
1135
- # elif parsed_url.hostname == 'youtu.be':
1136
- # return parsed_url.path[1:]
1137
- # return None
1138
-
1139
- # def download_audio(url, output_path):
1140
- # """Download audio using yt-dlp"""
1141
- # ydl_opts = {
1142
- # 'format': 'bestaudio/best',
1143
- # 'postprocessors': [{
1144
- # 'key': 'FFmpegExtractAudio',
1145
- # 'preferredcodec': 'mp3',
1146
- # 'preferredquality': '192',
1147
- # }],
1148
- # 'outtmpl': output_path,
1149
- # 'quiet': True,
1150
- # 'no_warnings': True
1151
- # }
1152
-
1153
- # with yt_dlp.YoutubeDL(ydl_opts) as ydl:
1154
- # ydl.download([url])
1155
- # return f"{output_path}.mp3"
1156
-
1157
- # def get_transcript(youtube_url, use_whisper=True):
1158
- # try:
1159
- # video_id = extract_video_id(youtube_url)
1160
- # if not video_id:
1161
- # return "Invalid YouTube URL format"
1162
-
1163
- # # First try YouTube's built-in transcripts
1164
- # try:
1165
- # transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
1166
-
1167
- # for lang in ['en', 'en-US']:
1168
- # try:
1169
- # transcript = transcript_list.find_transcript([lang])
1170
- # transcript_text = "\n".join([
1171
- # f"[{entry['start']:.1f}s] {entry['text']}"
1172
- # for entry in transcript.fetch()
1173
- # ])
1174
- # return "Using YouTube Transcript:\n\n" + transcript_text
1175
- # except:
1176
- # continue
1177
-
1178
- # # Try auto-generated transcript
1179
- # transcript = transcript_list.find_generated_transcript(['en'])
1180
- # transcript_text = "\n".join([
1181
- # f"[{entry['start']:.1f}s] {entry['text']}"
1182
- # for entry in transcript.fetch()
1183
- # ])
1184
- # return "Using Auto-generated Transcript:\n\n" + transcript_text
1185
-
1186
- # except (TranscriptsDisabled, NoTranscriptFound):
1187
- # if not use_whisper:
1188
- # return "No transcript available and Whisper fallback is disabled"
1189
-
1190
- # # Fallback to Whisper
1191
- # try:
1192
- # # Create temp directory if it doesn't exist
1193
- # if not os.path.exists('temp'):
1194
- # os.makedirs('temp')
1195
-
1196
- # # Download audio using yt-dlp
1197
- # output_path = os.path.join('temp', video_id)
1198
- # audio_file = download_audio(youtube_url, output_path)
1199
-
1200
- # # Initialize Whisper pipeline
1201
- # device = "cuda" if torch.cuda.is_available() else "cpu"
1202
- # transcriber = pipeline(
1203
- # "automatic-speech-recognition",
1204
- # model="openai/whisper-base",
1205
- # device=device
1206
- # )
1207
-
1208
- # # Transcribe audio
1209
- # result = transcriber(
1210
- # audio_file,
1211
- # chunk_length_s=30,
1212
- # return_timestamps=True
1213
- # )
1214
-
1215
- # # Clean up
1216
- # os.remove(audio_file)
1217
-
1218
- # # Format output
1219
- # if isinstance(result, dict) and 'chunks' in result:
1220
- # transcript_text = "\n".join([
1221
- # f"[{chunk['timestamp'][0]:.1f}s] {chunk['text']}"
1222
- # for chunk in result['chunks']
1223
- # ])
1224
- # else:
1225
- # transcript_text = result['text']
1226
-
1227
- # return "Using Whisper Transcription (No YouTube transcript available):\n\n" + transcript_text
1228
-
1229
- # except Exception as e:
1230
- # return f"Error with Whisper transcription: {str(e)}"
1231
-
1232
- # except Exception as e:
1233
- # return f"Error: {str(e)}"
1234
-
1235
- # # Create Gradio interface
1236
- # with gr.Blocks() as demo:
1237
- # gr.Markdown("# YouTube Video Transcription")
1238
- # gr.Markdown("Uses YouTube transcripts when available, falls back to Whisper model for videos without subtitles")
1239
-
1240
- # with gr.Row():
1241
- # youtube_url = gr.Textbox(
1242
- # label="YouTube URL",
1243
- # placeholder="https://www.youtube.com/watch?v=..."
1244
- # )
1245
- # use_whisper = gr.Checkbox(
1246
- # label="Enable Whisper fallback",
1247
- # value=True
1248
- # )
1249
-
1250
- # with gr.Row():
1251
- # transcript_output = gr.Textbox(
1252
- # label="Transcript",
1253
- # lines=15
1254
- # )
1255
-
1256
- # submit_btn = gr.Button("Get Transcript")
1257
- # submit_btn.click(
1258
- # fn=get_transcript,
1259
- # inputs=[youtube_url, use_whisper],
1260
- # outputs=[transcript_output]
1261
- # )
1262
-
1263
- # if __name__ == "__main__":
1264
- # demo.launch()
1265
-
 
1
+ # import subprocess
2
+ # subprocess.check_call(["pip", "install", "transformers==4.34.0"])
3
+ # subprocess.check_call(["pip", "install", "torch>=1.7.1"])
4
+ # subprocess.check_call(["pip", "install", "youtube_transcript_api>=0.6.3"])
5
+ # subprocess.check_call(["pip", "install", "pytube"])
6
+ # subprocess.check_call(["pip", "install", "huggingface_hub>=0.19.0"])
7
+ # subprocess.check_call(["pip", "install", "PyPDF2>=3.0.1"])
8
+ # subprocess.check_call(["pip", "install", "google-generativeai"])
9
+ # subprocess.check_call(["pip", "install", "textblob>=0.17.1"])
10
+ # subprocess.check_call(["pip", "install", "python-dotenv>=1.0.0"])
11
+ # subprocess.check_call(["pip", "install", "genai"])
12
+ # subprocess.check_call(["pip", "install", "google-cloud-aiplatform==1.34.0"])
13
+ # subprocess.check_call(["pip", "install", "google-api-python-client>=2.0.0"])
14
+ # import transformers
15
+ # import torch
16
+ # import os
17
+ # import youtube_transcript_api
18
+ # import pytube
19
+ # import gradio
20
+ # import PyPDF2
21
+ # import pathlib
22
+ # import pandas
23
+ # import numpy
24
+ # import textblob
25
+ # import gradio as gr
26
+ # from youtube_transcript_api import YouTubeTranscriptApi
27
+ # import google.generativeai as genai
28
+ # from googleapiclient.discovery import build
29
+ # import requests
30
+ # from textblob import TextBlob
31
+ # import re
32
+ # #from google.cloud import generativeai
33
+ # from googleapiclient.discovery import build
34
+ # from huggingface_hub import login
35
+ # from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
36
+ # def install_missing_packages():
37
+ # required_packages = {
38
+ # "torch":">=1.11.0",
39
+ # "transformers":">=4.34.0",
40
+ # "youtube_transcript_api" :">=0.6.3" ,
41
+ # "pytube":None,
42
+ # "huggingface_hub": ">=0.19.0",
43
+ # "PyPDF2": ">=3.0.1",
44
+ # "textblob":">=0.17.1",
45
+ # "python-dotenv":">=1.0.0",
46
+ # "genai":None,
47
+ # "google-generativeai": None,
48
+ # "google-cloud-aiplatform":"==1.34.0",
49
+ # "google-api-python-client": ">=2.0.0"
50
+ # }
51
 
52
 
53
+ # for package, version in required_packages.items():
54
+ # try:
55
+ # __import__(package)
56
+ # except ImportError:
57
+ # package_name = f"{package}{version}" if version else package
58
+ # subprocess.check_call(["pip", "install", package_name])
59
 
60
+ # install_missing_packages()
61
+ # # Configuration
62
 
63
+ # hf_token = os.getenv("HF_TOKEN")
64
+ # if hf_token:
65
+ # login(hf_token)
66
+ # else:
67
+ # raise ValueError("HF_TOKEN environment variable not set.")
68
+
69
+
70
+ # YOUTUBE_API_KEY = "AIzaSyD_SDF4lC3vpHVAMnBOcN2ZCTz7dRjUc98" # Replace with your YouTube API Key
71
+
72
+ # USER_CREDENTIALS = {"admin": "password"} # Example user credentials
73
+
74
+ # import os
75
+ # from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
76
+
77
+ # # Use environment variables
78
  # GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
 
79
  # YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
 
 
80
 
81
+ # if not GOOGLE_API_KEY or not YOUTUBE_API_KEY:
82
+ # raise ValueError("Please set GOOGLE_API_KEY and YOUTUBE_API_KEY environment variables")
83
 
84
+ # genai.configure(api_key=GOOGLE_API_KEY)
85
 
86
+ # # Database
87
+ # students_data = [
88
+ # (1, "Alice", "A", "Computer Science"),
89
+ # (2, "Aliaa", "B", "Mathematics"),
90
+ # (3, "Charlie", "A", "Machine Learning"),
91
+ # (4, "Daan", "A", "Physics"),
92
+ # (5, "Jhon", "C", "Math"),
93
+ # (6, "Emma", "A+", "Computer Science")
94
+ # ]
95
 
96
+ # teachers_data = [
97
+ # (1, "Dr. Smith", "Math", "MS Mathematics"),
98
+ # (2, "Ms. Johnson", "Science", "MSc Physics"),
99
+ # (3, "Ms. Jack", "Artificial Intelligence Engineer", "MSc AI"),
100
+ # (4, "Ms. Evelyn", "Computer Science", "MSc Computer Science"),
101
+ # ]
102
 
103
+ # courses_data = [
104
+ # (1, "Algebra", "Dr. Smith", "Advanced"),
105
+ # (2, "Biology", "Ms. Mia", "Intermediate"),
106
+ # (3, "Machine Learning", "Ms. Jack", "Intermediate"),
107
+ # (4, "Computer Science", "Ms. Evelyn", "Intermediate"),
108
+ # (5, "Mathematics", "Ms. Smith", "Intermediate")
109
+ # ]
110
 
111
+ # def extract_video_id(url):
112
+ # match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11})", url)
113
+ # return match.group(1) if match else None
114
 
115
+ # def get_video_metadata(video_id):
116
+ # try:
117
+ # youtube = build("youtube", "v3", developerKey=YOUTUBE_API_KEY)
118
+ # request = youtube.videos().list(part="snippet", id=video_id)
119
+ # response = request.execute()
120
 
121
+ # if "items" in response and len(response["items"]) > 0:
122
+ # snippet = response["items"][0]["snippet"]
123
+ # return {
124
+ # "title": snippet.get("title", "No title available"),
125
+ # "description": snippet.get("description", "No description available"),
126
+ # }
127
+ # return {}
 
 
128
 
129
+ # except Exception as e:
130
+ # return {"title": "Error fetching metadata", "description": str(e)}
 
 
 
 
131
 
132
+ # def clean_text_for_analysis(text):
133
+ # return " ".join(text.split())
 
 
 
 
 
134
 
135
+ # def get_recommendations(keywords, max_results=5):
136
+ # if not keywords:
137
+ # return "Please provide search keywords"
138
+ # try:
139
+ # response = requests.get(
140
+ # "https://www.googleapis.com/youtube/v3/search",
141
+ # params={
142
+ # "part": "snippet",
143
+ # "q": f"educational {keywords}",
144
+ # "type": "video",
145
+ # "maxResults": max_results,
146
+ # "relevanceLanguage": "en",
147
+ # "key": YOUTUBE_API_KEY
148
+ # }
149
+ # ).json()
150
 
151
+ # results = []
152
+ # for item in response.get("items", []):
153
+ # title = item["snippet"]["title"]
154
+ # channel = item["snippet"]["channelTitle"]
155
+ # video_id = item["id"]["videoId"]
156
+ # results.append(f"📺 {title}\n👤 {channel}\n🔗 https://youtube.com/watch?v={video_id}\n")
157
+
158
+ # return "\n".join(results) if results else "No recommendations found"
159
+ # except Exception as e:
160
+ # return f"Error: {str(e)}"
161
+
162
+ # def process_youtube_video(url):
 
 
163
  # try:
164
+ # thumbnail = None
 
165
  # summary = "No transcript available"
166
  # sentiment_label = "N/A"
167
 
 
 
 
168
  # video_id = extract_video_id(url)
169
  # if not video_id:
170
+ # return None, "Invalid YouTube URL", "N/A"
171
 
172
  # thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
173
 
174
  # try:
 
175
  # transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
176
+ # transcript = None
177
  # try:
178
  # transcript = transcript_list.find_transcript(['en'])
179
  # except:
180
+ # transcript = transcript_list.find_generated_transcript(['en'])
 
 
 
 
 
 
 
181
 
182
  # text = " ".join([t['text'] for t in transcript.fetch()])
183
+ # if not text.strip():
184
+ # raise ValueError("Transcript is empty")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
 
186
+ # cleaned_text = clean_text_for_analysis(text)
 
 
 
187
 
188
+ # sentiment = TextBlob(cleaned_text).sentiment
189
+ # sentiment_label = f"{'Positive' if sentiment.polarity > 0 else 'Negative' if sentiment.polarity < 0 else 'Neutral'} ({sentiment.polarity:.2f})"
190
 
191
+ # summary = f"Summary: {cleaned_text[:400]}..."
 
 
 
 
 
192
 
193
+ # except (TranscriptsDisabled, NoTranscriptFound):
194
+ # metadata = get_video_metadata(video_id)
195
+ # summary = metadata.get("description", "No subtitles available")
196
+ # sentiment_label = "N/A"
 
197
 
198
+ # return thumbnail, summary, sentiment_label
 
 
 
 
 
 
199
 
200
  # except Exception as e:
201
+ # return None, f"Error: {str(e)}", "N/A"
 
 
 
 
 
 
202
 
203
+ # # Gradio Interface
204
+ # with gr.Blocks(theme=gr.themes.Soft()) as app:
205
+ # # Login Page
206
+ # with gr.Group() as login_page:
207
+ # gr.Markdown("# 🎓 Educational Learning Management System")
208
+ # username = gr.Textbox(label="Username")
209
+ # password = gr.Textbox(label="Password", type="password")
210
+ # login_btn = gr.Button("Login", variant="primary")
211
+ # login_msg = gr.Markdown()
212
 
213
+ # # Main Interface
214
+ # with gr.Group(visible=False) as main_page:
215
+ # with gr.Row():
216
+ # with gr.Column(scale=1):
217
+ # gr.Markdown("### 📋 Navigation")
218
+ # nav_dashboard = gr.Button("📊 Dashboard", variant="primary")
219
+ # nav_students = gr.Button("👥 Students")
220
+ # nav_teachers = gr.Button("👨‍🏫 Teachers")
221
+ # nav_courses = gr.Button("📚 Courses")
222
+ # nav_youtube = gr.Button("🎥 YouTube Tool")
223
+ # logout_btn = gr.Button("🚪 Logout", variant="stop")
224
 
225
+ # with gr.Column(scale=3):
226
+ # # Dashboard Content
227
+ # dashboard_page = gr.Group()
228
+ # with dashboard_page:
229
+ # gr.Markdown("## 📊 Dashboard")
230
+ # gr.Markdown(f"""
231
+ # ### System Overview
232
+ # - 👥 Total Students: {len(students_data)}
233
+ # - 👨‍🏫 Total Teachers: {len(teachers_data)}
234
+ # - 📚 Total Courses: {len(courses_data)}
235
+ # ### Quick Actions
236
+ # - View student performance
237
+ # - Access course materials
238
+ # - Generate learning insights
239
+ # """)
240
 
241
+ # # Students Content
242
+ # students_page = gr.Group(visible=False)
243
+ # with students_page:
244
+ # gr.Markdown("## 👥 Students")
245
+ # gr.DataFrame(
246
+ # value=students_data,
247
+ # headers=["ID", "Name", "Grade", "Program"]
248
+ # )
249
 
250
+ # # Teachers Content
251
+ # teachers_page = gr.Group(visible=False)
252
+ # with teachers_page:
253
+ # gr.Markdown("## 👨‍🏫 Teachers")
254
+ # gr.DataFrame(
255
+ # value=teachers_data,
256
+ # headers=["ID", "Name", "Subject", "Qualification"]
257
+ # )
258
+
259
+ # # Courses Content
260
+ # courses_page = gr.Group(visible=False)
261
+ # with courses_page:
262
+ # gr.Markdown("## 📚 Courses")
263
+ # gr.DataFrame(
264
+ # value=courses_data,
265
+ # headers=["ID", "Name", "Instructor", "Level"]
266
+ # )
267
+
268
+ # # YouTube Tool Content
269
+ # youtube_page = gr.Group(visible=False)
270
+ # with youtube_page:
271
+ # gr.Markdown("## Agent for YouTube Content Exploration")
272
+ # with gr.Row():
273
+ # with gr.Column(scale=2):
274
+ # video_url = gr.Textbox(
275
+ # label="YouTube URL",
276
+ # placeholder="https://youtube.com/watch?v=..."
277
+ # )
278
+ # keywords = gr.Textbox(
279
+ # label="Keywords for Recommendations",
280
+ # placeholder="e.g., python programming, machine learning"
281
+ # )
282
+ # analyze_btn = gr.Button("🔍 Analyze Video", variant="primary")
283
+ # recommend_btn = gr.Button("🔎 Get Recommendations", variant="primary")
284
+
285
+ # with gr.Column(scale=1):
286
+ # video_thumbnail = gr.Image(label="Video Preview")
287
+
288
+ # with gr.Row():
289
+ # with gr.Column():
290
+ # summary = gr.Textbox(label="📝 Summary", lines=8)
291
+ # sentiment = gr.Textbox(label="😊 Content Sentiment")
292
+ # with gr.Column():
293
+ # recommendations = gr.Textbox(label="🎯 Related Videos", lines=10)
294
+
295
+ # def login_check(user, pwd):
296
+ # if USER_CREDENTIALS.get(user) == pwd:
297
+ # return {
298
+ # login_page: gr.update(visible=False),
299
+ # main_page: gr.update(visible=True),
300
+ # login_msg: ""
301
+ # }
302
+ # return {
303
+ # login_page: gr.update(visible=True),
304
+ # main_page: gr.update(visible=False),
305
+ # login_msg: "❌ Invalid credentials"
306
+ # }
307
+
308
+ # def show_page(page_name):
309
+ # updates = {
310
+ # dashboard_page: gr.update(visible=False),
311
+ # students_page: gr.update(visible=False),
312
+ # teachers_page: gr.update(visible=False),
313
+ # courses_page: gr.update(visible=False),
314
+ # youtube_page: gr.update(visible=False)
315
+ # }
316
+ # updates[page_name] = gr.update(visible=True)
317
+ # return updates
318
+
319
+ # # Event Handlers
320
+ # login_btn.click(
321
+ # login_check,
322
+ # inputs=[username, password],
323
+ # outputs=[login_page, main_page, login_msg]
324
+ # )
325
+
326
+ # nav_dashboard.click(lambda: show_page(dashboard_page), outputs=list(show_page(dashboard_page).keys()))
327
+ # nav_students.click(lambda: show_page(students_page), outputs=list(show_page(students_page).keys()))
328
+ # nav_teachers.click(lambda: show_page(teachers_page), outputs=list(show_page(teachers_page).keys()))
329
+ # nav_courses.click(lambda: show_page(courses_page), outputs=list(show_page(courses_page).keys()))
330
+ # nav_youtube.click(lambda: show_page(youtube_page), outputs=list(show_page(youtube_page).keys()))
331
+
332
+ # analyze_btn.click(
333
+ # process_youtube_video,
334
+ # inputs=[video_url],
335
+ # outputs=[video_thumbnail, summary, sentiment]
336
+ # )
337
+
338
+ # recommend_btn.click(
339
+ # get_recommendations,
340
+ # inputs=[keywords],
341
+ # outputs=[recommendations]
342
+ # )
343
+
344
+ # logout_btn.click(
345
+ # lambda: {
346
+ # login_page: gr.update(visible=True),
347
+ # main_page: gr.update(visible=False)
348
+ # },
349
+ # outputs=[login_page, main_page]
350
+ # )
351
+
352
+ # if __name__ == "__main__":
353
+ # app.launch()
354
+
355
+
356
+
357
+ import subprocess
358
+ subprocess.check_call(["pip", "install", "transformers==4.34.0"])
359
+ subprocess.check_call(["pip", "install", "torch>=1.7.1"])
360
+ subprocess.check_call(["pip", "install", "youtube_transcript_api>=0.6.3"])
361
+ subprocess.check_call(["pip", "install", "pytube"])
362
+ subprocess.check_call(["pip", "install", "huggingface_hub>=0.19.0"])
363
+ subprocess.check_call(["pip", "install", "PyPDF2>=3.0.1"])
364
+ subprocess.check_call(["pip", "install", "google-generativeai"])
365
+ subprocess.check_call(["pip", "install", "textblob>=0.17.1"])
366
+ subprocess.check_call(["pip", "install", "python-dotenv>=1.0.0"])
367
+ subprocess.check_call(["pip", "install", "genai"])
368
+ subprocess.check_call(["pip", "install", "google-cloud-aiplatform==1.34.0"])
369
+ subprocess.check_call(["pip", "install", "google-api-python-client>=2.0.0"])
370
+ import transformers
371
+ import torch
372
+ import os
373
+ import youtube_transcript_api
374
+ import pytube
375
+ import gradio
376
+ import PyPDF2
377
+ import pathlib
378
+ import pandas
379
+ import numpy
380
+ import textblob
381
+ import gradio as gr
382
+ from youtube_transcript_api import YouTubeTranscriptApi
383
+ import google.generativeai as genai
384
+ from googleapiclient.discovery import build
385
+ import requests
386
+ from textblob import TextBlob
387
+ import re
388
+ #from google.cloud import generativeai
389
+ from googleapiclient.discovery import build
390
+ from huggingface_hub import login
391
+ from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
392
+ def install_missing_packages():
393
+ required_packages = {
394
+ "torch":">=1.11.0",
395
+ "transformers":">=4.34.0",
396
+ "youtube_transcript_api" :">=0.6.3" ,
397
+ "pytube":None,
398
+ "huggingface_hub": ">=0.19.0",
399
+ "PyPDF2": ">=3.0.1",
400
+ "textblob":">=0.17.1",
401
+ "python-dotenv":">=1.0.0",
402
+ "genai":None,
403
+ "google-generativeai": None,
404
+ "google-cloud-aiplatform":"==1.34.0",
405
+ "google-api-python-client": ">=2.0.0"
406
+ }
407
 
408
+
409
+ for package, version in required_packages.items():
410
  try:
411
+ __import__(package)
412
+ except ImportError:
413
+ package_name = f"{package}{version}" if version else package
414
+ subprocess.check_call(["pip", "install", package_name])
 
 
 
415
 
416
+ install_missing_packages()
417
+ # Configuration
 
418
 
419
+ hf_token = os.getenv("HF_TOKEN")
420
+ if hf_token:
421
+ login(hf_token)
422
+ else:
423
+ raise ValueError("HF_TOKEN environment variable not set.")
424
 
 
 
425
 
426
+ YOUTUBE_API_KEY = "AIzaSyD_SDF4lC3vpHVAMnBOcN2ZCTz7dRjUc98" # Replace with your YouTube API Key
 
 
427
 
428
+ USER_CREDENTIALS = {"admin": "password"} # Example user credentials
 
 
 
 
 
 
 
 
 
 
 
429
 
430
+ import os
431
+ from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
 
432
 
433
+ # Use environment variables
434
+ GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
435
+ YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
436
 
437
+ if not GOOGLE_API_KEY or not YOUTUBE_API_KEY:
438
+ raise ValueError("Please set GOOGLE_API_KEY and YOUTUBE_API_KEY environment variables")
439
+
440
+ genai.configure(api_key=GOOGLE_API_KEY)
441
+
442
+ # Database
443
+ students_data = [
444
+ (1, "Alice", "A", "Computer Science"),
445
+ (2, "Aliaa", "B", "Mathematics"),
446
+ (3, "Charlie", "A", "Machine Learning"),
447
+ (4, "Daan", "A", "Physics"),
448
+ (5, "Jhon", "C", "Math"),
449
+ (6, "Emma", "A+", "Computer Science")
450
+ ]
451
+
452
+ teachers_data = [
453
+ (1, "Dr. Smith", "Math", "MS Mathematics"),
454
+ (2, "Ms. Johnson", "Science", "MSc Physics"),
455
+ (3, "Ms. Jack", "Artificial Intelligence Engineer", "MSc AI"),
456
+ (4, "Ms. Evelyn", "Computer Science", "MSc Computer Science"),
457
+ ]
458
 
459
+ courses_data = [
460
+ (1, "Algebra", "Dr. Smith", "Advanced"),
461
+ (2, "Biology", "Ms. Mia", "Intermediate"),
462
+ (3, "Machine Learning", "Ms. Jack", "Intermediate"),
463
+ (4, "Computer Science", "Ms. Evelyn", "Intermediate"),
464
+ (5, "Mathematics", "Ms. Smith", "Intermediate")
465
+ ]
466
 
467
  def extract_video_id(url):
 
 
 
 
468
  match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11})", url)
469
  return match.group(1) if match else None
470
 
 
471
  def get_video_metadata(video_id):
 
 
 
472
  try:
 
 
 
 
473
  youtube = build("youtube", "v3", developerKey=YOUTUBE_API_KEY)
474
  request = youtube.videos().list(part="snippet", id=video_id)
475
  response = request.execute()
 
485
  except Exception as e:
486
  return {"title": "Error fetching metadata", "description": str(e)}
487
 
488
+ def clean_text_for_analysis(text):
489
+ return " ".join(text.split())
490
 
491
+ def process_youtube_video(url):
 
 
 
 
492
  try:
493
+ video_id = extract_video_id(url)
494
+ if not video_id:
495
+ return None, "Invalid YouTube URL", "N/A"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
496
 
497
+ thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
498
+ summary = "No transcript available"
499
+ sentiment_label = "N/A"
500
 
501
+ try:
502
+ transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
503
+ transcript = None
504
+ try:
505
+ transcript = transcript_list.find_transcript(['en'])
506
+ except:
507
+ transcript = transcript_list.find_generated_transcript(['en'])
508
+
509
+ text = " ".join([t['text'] for t in transcript.fetch()])
510
+ if not text.strip():
511
+ raise ValueError("Transcript is empty")
512
+
513
+ cleaned_text = clean_text_for_analysis(text)
514
+
515
+ sentiment = TextBlob(cleaned_text).sentiment
516
+ sentiment_label = f"{'Positive' if sentiment.polarity > 0 else 'Negative' if sentiment.polarity < 0 else 'Neutral'} ({sentiment.polarity:.2f})"
517
+
518
+ summary = f"Summary of Content: {cleaned_text[:400]}..."
519
+
520
+ except (TranscriptsDisabled, NoTranscriptFound):
521
+ metadata = get_video_metadata(video_id)
522
+ summary = metadata.get("description", "No subtitles available")
523
+
524
+ return thumbnail, summary, sentiment_label
525
+
526
+ except Exception as e:
527
+ return None, f"Error: {str(e)}", "N/A"
528
 
529
  def get_recommendations(keywords, max_results=5):
530
  if not keywords:
 
541
  "key": YOUTUBE_API_KEY
542
  }
543
  ).json()
544
+
545
  results = []
546
  for item in response.get("items", []):
547
  title = item["snippet"]["title"]
548
  channel = item["snippet"]["channelTitle"]
549
  video_id = item["id"]["videoId"]
550
+ results.append(f"\ud83d\udcfa {title}\n\ud83d\udc64 {channel}\n\ud83d\udd17 https://youtube.com/watch?v={video_id}\n")
551
+
552
  return "\n".join(results) if results else "No recommendations found"
553
  except Exception as e:
554
  return f"Error: {str(e)}"
 
557
  with gr.Blocks(theme=gr.themes.Soft()) as app:
558
  # Login Page
559
  with gr.Group() as login_page:
560
+ gr.Markdown("# \ud83c\udf93 Educational Learning Management System")
561
  username = gr.Textbox(label="Username")
562
  password = gr.Textbox(label="Password", type="password")
563
  login_btn = gr.Button("Login", variant="primary")
564
  login_msg = gr.Markdown()
565
+
566
  # Main Interface
567
  with gr.Group(visible=False) as main_page:
568
  with gr.Row():
569
  with gr.Column(scale=1):
570
+ gr.Markdown("### \ud83d\udccb Navigation")
571
+ nav_dashboard = gr.Button("\ud83d\udcca Dashboard", variant="primary")
572
+ nav_students = gr.Button("\ud83d\udc65 Students")
573
+ nav_teachers = gr.Button("\ud83d\udc69\u200d\ud83c\udf93 Teachers")
574
+ nav_courses = gr.Button("\ud83d\udcda Courses")
575
+ nav_youtube = gr.Button("\ud83c\udfa5 YouTube Tool")
576
+ logout_btn = gr.Button("\ud83d\udeaa Logout", variant="stop")
577
+
578
  with gr.Column(scale=3):
579
  # Dashboard Content
580
  dashboard_page = gr.Group()
581
  with dashboard_page:
582
+ gr.Markdown("## \ud83d\udcca Dashboard")
583
  gr.Markdown(f"""
584
  ### System Overview
585
+ - \ud83d\udc65 Total Students: {len(students_data)}
586
+ - \ud83d\udc69\u200d\ud83c\udf93 Total Teachers: {len(teachers_data)}
587
+ - \ud83d\udcda Total Courses: {len(courses_data)}
 
588
  ### Quick Actions
589
  - View student performance
590
  - Access course materials
591
  - Generate learning insights
592
  """)
593
+
594
  # Students Content
595
  students_page = gr.Group(visible=False)
596
  with students_page:
597
+ gr.Markdown("## \ud83d\udc65 Students")
598
  gr.DataFrame(
599
  value=students_data,
600
  headers=["ID", "Name", "Grade", "Program"]
601
  )
602
+
603
  # Teachers Content
604
  teachers_page = gr.Group(visible=False)
605
  with teachers_page:
606
+ gr.Markdown("## \ud83d\udc69\u200d\ud83c\udf93 Teachers")
607
  gr.DataFrame(
608
  value=teachers_data,
609
  headers=["ID", "Name", "Subject", "Qualification"]
610
  )
611
+
612
  # Courses Content
613
  courses_page = gr.Group(visible=False)
614
  with courses_page:
615
+ gr.Markdown("## \ud83d\udcda Courses")
616
  gr.DataFrame(
617
  value=courses_data,
618
  headers=["ID", "Name", "Instructor", "Level"]
619
  )
620
+
621
  # YouTube Tool Content
622
  youtube_page = gr.Group(visible=False)
623
  with youtube_page:
 
632
  label="Keywords for Recommendations",
633
  placeholder="e.g., python programming, machine learning"
634
  )
635
+ analyze_btn = gr.Button("\ud83d\udd0d Analyze Video", variant="primary")
636
+ recommend_btn = gr.Button("\ud83d\udd0e Get Recommendations", variant="primary")
637
+
638
  with gr.Column(scale=1):
639
  video_thumbnail = gr.Image(label="Video Preview")
640
+
641
  with gr.Row():
642
  with gr.Column():
643
+ summary = gr.Textbox(label="\ud83d\uddcb Summary", lines=8)
644
+ sentiment = gr.Textbox(label="\ud83d\ude0a Content Sentiment")
645
  with gr.Column():
646
+ recommendations = gr.Textbox(label="\ud83c\udf1f Related Videos", lines=10)
647
 
648
  def login_check(user, pwd):
649
  if USER_CREDENTIALS.get(user) == pwd:
 
655
  return {
656
  login_page: gr.update(visible=True),
657
  main_page: gr.update(visible=False),
658
+ login_msg: "\u274c Invalid credentials"
659
  }
660
+
661
  def show_page(page_name):
662
  updates = {
663
  dashboard_page: gr.update(visible=False),
 
668
  }
669
  updates[page_name] = gr.update(visible=True)
670
  return updates
671
+
672
  # Event Handlers
673
  login_btn.click(
674
  login_check,
675
  inputs=[username, password],
676
  outputs=[login_page, main_page, login_msg]
677
  )
678
+
679
  nav_dashboard.click(lambda: show_page(dashboard_page), outputs=list(show_page(dashboard_page).keys()))
680
  nav_students.click(lambda: show_page(students_page), outputs=list(show_page(students_page).keys()))
681
  nav_teachers.click(lambda: show_page(teachers_page), outputs=list(show_page(teachers_page).keys()))
682
  nav_courses.click(lambda: show_page(courses_page), outputs=list(show_page(courses_page).keys()))
683
  nav_youtube.click(lambda: show_page(youtube_page), outputs=list(show_page(youtube_page).keys()))
684
+
685
  analyze_btn.click(
686
  process_youtube_video,
687
+ inputs=[video_url],
688
+ outputs=[video_thumbnail, summary, sentiment]
689
  )
690
+
691
+ recommend_btn.click(
692
+ get_recommendations,
693
+ inputs=[keywords],
694
+ outputs=[recommendations]
695
+ )
696
+
697
  logout_btn.click(
698
  lambda: {
699
  login_page: gr.update(visible=True),
 
704
 
705
  if __name__ == "__main__":
706
  app.launch()