Sayiqa7 commited on
Commit
7c4c5d8
·
verified ·
1 Parent(s): 0f0b135

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +548 -185
app.py CHANGED
@@ -1,4 +1,551 @@
1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import os
3
  os.system('conda install -c conda-forge youtube-transcript-api -y')
4
  import subprocess
@@ -136,7 +683,7 @@ def extract_video_id(url):
136
  return match.group(1)
137
  return None
138
 
139
- # def process_youtube_video(url="", keywords=""):
140
  try:
141
  if not url.strip():
142
  return None, "Please enter a YouTube URL", "N/A", ""
@@ -362,187 +909,3 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
362
 
363
  if __name__ == "__main__":
364
  app.launch()
365
-
366
-
367
- ########################
368
- # import os
369
- # os.system('conda install -c conda-forge youtube-transcript-api -y')
370
- # import subprocess
371
- # subprocess.check_call(["pip", "install", "transformers==4.34.0"])
372
- # subprocess.check_call(["pip", "install", "torch>=1.7.1"])
373
- # subprocess.check_call(["pip", "install", "youtube_transcript_api>=0.6.3"])
374
- # subprocess.check_call(["pip", "install", "pytube>=12.1.0"])
375
- # subprocess.check_call(["pip", "install", "huggingface_hub>=0.19.0"])
376
- # subprocess.check_call(["pip", "install", "PyPDF2>=3.0.1"])
377
- # subprocess.check_call(["pip", "install", "google-generativeai>=0.3.0"])
378
- # subprocess.check_call(["pip", "install", "textblob>=0.17.1"])
379
- # subprocess.check_call(["pip", "install", "python-dotenv>=1.0.0"])
380
- # subprocess.check_call(["pip", "install", "genai"])
381
- # subprocess.check_call(["pip", "install", "google-cloud-aiplatform==1.34.0"])
382
- # subprocess.check_call(["pip", "install", "scikit-learn>=1.0.0"])
383
- # import transformers
384
- # import torch
385
- # import os
386
- # import youtube_transcript_api
387
- # import pytube
388
- # import gradio
389
- # import PyPDF2
390
- # import pathlib
391
- # import pandas
392
- # import numpy
393
- # import textblob
394
- # import gradio as gr
395
- # from youtube_transcript_api import YouTubeTranscriptApi
396
- # import google.generativeai as genai
397
- # import requests
398
- # from textblob import TextBlob
399
- # import re
400
- # from dotenv import load_dotenv
401
- # from sklearn.feature_extraction.text import TfidfVectorizer
402
- # from sklearn.metrics.pairwise import cosine_similarity
403
-
404
-
405
- # #from google.cloud import generativeai
406
- # from huggingface_hub import login
407
- # from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
408
- # def install_missing_packages():
409
- # required_packages = {
410
- # "torch":">=1.11.0",
411
- # "transformers":">=4.34.0",
412
- # "youtube_transcript_api" :">=0.6.3" ,
413
- # "pytube":">=12.1.0",
414
- # "huggingface_hub": ">=0.19.0",
415
- # "PyPDF2": ">=3.0.1",
416
- # "textblob":">=0.17.1",
417
- # "python-dotenv":">=1.0.0",
418
- # "genai":None,
419
- # "google-generativeai": ">=0.3.0",
420
- # "google-cloud-aiplatform":"==1.34.0",
421
- # "scikit-learn": ">=1.0.0"
422
- # }
423
-
424
-
425
- # for package, version in required_packages.items():
426
- # try:
427
- # __import__(package)
428
- # except ImportError:
429
- # package_name = f"{package}{version}" if version else package
430
- # subprocess.check_call(["pip", "install", package_name])
431
-
432
- # install_missing_packages()
433
- # # # Configuration
434
- # # USER_CREDENTIALS = {
435
- # # "admin": "password123",
436
- # # "teacher": "teach2024",
437
- # # "student": "learn2024"
438
- # # }
439
-
440
- # hf_token = os.getenv("HF_TOKEN")
441
- # if hf_token:
442
- # login(hf_token)
443
- # else:
444
- # raise ValueError("HF_TOKEN environment variable not set.")
445
-
446
- # # GOOGLE_API_KEY = "AIzaSyAURQb9jueh3dBQ4SITgKoR0L2_33en3yU"
447
- # # YOUTUBE_API_KEY = "AIzaSyB7X-RYjZmUuDSMTQsvCfyzURw5bhqOto4"
448
- # # genai.configure(api_key=GOOGLE_API_KEY)
449
- # # # GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
450
- # # # genai.configure(api_key=GOOGLE_API_KEY)
451
- # # # YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
452
- # # # print("GOOGLE_API_KEY:", os.getenv("GOOGLE_API_KEY"))
453
- # # # print("YOUTUBE_API_KEY:", os.getenv("YOUTUBE_API_KEY"))
454
-
455
- # import os
456
- # import gradio as gr
457
- # from youtube_transcript_api import YouTubeTranscriptApi
458
- # from dotenv import load_dotenv
459
- # import google.generativeai as genai
460
- # import requests
461
-
462
- # from transformers import pipeline
463
- # from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
464
- # from pytube import YouTube
465
- # import gradio as gr
466
- # from transformers import pipeline
467
- # from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
468
- # from pytube import YouTube
469
- # import gradio as gr
470
-
471
- # # Function to extract transcript from a YouTube video
472
- # def extract_transcript(youtube_video_url):
473
- # try:
474
- # video_id = youtube_video_url.split("v=")[1].split("&")[0] # Extract video ID from URL
475
-
476
- # # Try to fetch transcript using YouTubeTranscriptApi
477
- # try:
478
- # transcript = YouTubeTranscriptApi.get_transcript(video_id)
479
- # result = " ".join([i['text'] for i in transcript])
480
- # return result
481
- # except (TranscriptsDisabled, NoTranscriptFound):
482
- # pass # Fallback to pytube if no transcript found
483
-
484
- # # Fallback to pytube for captions
485
- # yt = YouTube(youtube_video_url)
486
- # if yt.captions:
487
- # captions = yt.captions.get_by_language_code("en")
488
- # if captions:
489
- # transcript = captions.generate_srt_captions()
490
- # return transcript.replace("\n", " ") # Clean SRT captions format
491
- # else:
492
- # return "Error: No English captions available for this video."
493
- # else:
494
- # return "Error: No captions available for this video."
495
-
496
- # except Exception as e:
497
- # return f"Error: Unable to process the video URL. Details: {str(e)}"
498
-
499
- # # Function to summarize the text
500
- # def summarize_transcript(transcript_text):
501
- # try:
502
- # summarizer = pipeline('summarization', model="facebook/bart-large-cnn") # Using BART model for summarization
503
- # summarized_text = []
504
- # num_iters = len(transcript_text) // 1000 + 1
505
-
506
- # for i in range(num_iters):
507
- # start = i * 1000
508
- # end = (i + 1) * 1000
509
- # chunk = transcript_text[start:end]
510
- # if chunk.strip():
511
- # out = summarizer(chunk, max_length=130, min_length=30, do_sample=False)
512
- # summarized_text.append(out[0]['summary_text'])
513
-
514
- # return " ".join(summarized_text)
515
- # except Exception as e:
516
- # return f"Error during summarization: {str(e)}"
517
-
518
- # # Gradio Interface function
519
- # def gradio_interface(youtube_url):
520
- # transcript = extract_transcript(youtube_url)
521
- # if transcript.startswith("Error"):
522
- # return transcript, "Summarization cannot be performed due to transcript error."
523
-
524
- # summarized_text = summarize_transcript(transcript)
525
- # return transcript, summarized_text
526
-
527
- # # Gradio Interface Design
528
- # with gr.Blocks() as demo:
529
- # gr.Markdown("# YouTube Video Summarizer")
530
- # with gr.Row():
531
- # youtube_url_input = gr.Textbox(
532
- # label="YouTube Video URL",
533
- # placeholder="Enter the YouTube video link here"
534
- # )
535
- # summarize_button = gr.Button("Summarize Transcript")
536
- # with gr.Row():
537
- # transcript_output = gr.Textbox(label="Original Transcript", lines=10, interactive=False)
538
- # summary_output = gr.Textbox(label="Summarized Text", lines=10, interactive=False)
539
- # summarize_button.click(
540
- # fn=gradio_interface,
541
- # inputs=[youtube_url_input],
542
- # outputs=[transcript_output, summary_output]
543
- # )
544
-
545
- # # Launch Gradio App
546
- # if __name__ == "__main__":
547
- # demo.launch()
548
-
 
1
 
2
+ # import os
3
+ # os.system('conda install -c conda-forge youtube-transcript-api -y')
4
+ # import subprocess
5
+ # subprocess.check_call(["pip", "install", "transformers==4.34.0"])
6
+ # subprocess.check_call(["pip", "install", "torch>=1.7.1"])
7
+ # subprocess.check_call(["pip", "install", "youtube_transcript_api>=0.6.3"])
8
+ # subprocess.check_call(["pip", "install", "pytube"])
9
+ # subprocess.check_call(["pip", "install", "huggingface_hub>=0.19.0"])
10
+ # subprocess.check_call(["pip", "install", "PyPDF2>=3.0.1"])
11
+ # subprocess.check_call(["pip", "install", "google-generativeai"])
12
+ # subprocess.check_call(["pip", "install", "textblob>=0.17.1"])
13
+ # subprocess.check_call(["pip", "install", "python-dotenv>=1.0.0"])
14
+ # subprocess.check_call(["pip", "install", "genai"])
15
+ # subprocess.check_call(["pip", "install", "google-cloud-aiplatform==1.34.0"])
16
+ # import transformers
17
+ # import torch
18
+ # import os
19
+ # import youtube_transcript_api
20
+ # import pytube
21
+ # import gradio
22
+ # import PyPDF2
23
+ # import pathlib
24
+ # import pandas
25
+ # import numpy
26
+ # import textblob
27
+ # import gradio as gr
28
+ # from youtube_transcript_api import YouTubeTranscriptApi
29
+ # import google.generativeai as genai
30
+ # import requests
31
+ # from textblob import TextBlob
32
+ # import re
33
+ # #from google.cloud import generativeai
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
+ # }
50
+
51
+
52
+ # for package, version in required_packages.items():
53
+ # try:
54
+ # __import__(package)
55
+ # except ImportError:
56
+ # package_name = f"{package}{version}" if version else package
57
+ # subprocess.check_call(["pip", "install", package_name])
58
+
59
+ # install_missing_packages()
60
+ # # Configuration
61
+
62
+ # hf_token = os.getenv("HF_TOKEN")
63
+ # if hf_token:
64
+ # login(hf_token)
65
+ # else:
66
+ # raise ValueError("HF_TOKEN environment variable not set.")
67
+
68
+ # # GOOGLE_API_KEY = "AIzaSyAURQb9jueh3dBQ4SITgKoR0L2_33en3yU"
69
+ # # YOUTUBE_API_KEY = "AIzaSyB7X-RYjZmUuDSMTQsvCfyzURw5bhqOto4"
70
+ # # # genai.configure(api_key=GOOGLE_API_KEY)
71
+ # # GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
72
+ # # genai.configure(api_key=GOOGLE_API_KEY)
73
+ # # YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
74
+ # # print("GOOGLE_API_KEY:", os.getenv("GOOGLE_API_KEY"))
75
+ # # print("YOUTUBE_API_KEY:", os.getenv("YOUTUBE_API_KEY"))
76
+
77
+
78
+
79
+ # # Configuration
80
+ # USER_CREDENTIALS = {
81
+ # "admin": "password123",
82
+ # "teacher": "teach2024",
83
+ # "student": "learn2024"
84
+ # }
85
+
86
+ # import os
87
+ # from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
88
+
89
+ # # Use environment variables
90
+ # GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
91
+ # YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
92
+
93
+ # if not GOOGLE_API_KEY or not YOUTUBE_API_KEY:
94
+ # raise ValueError("Please set GOOGLE_API_KEY and YOUTUBE_API_KEY environment variables")
95
+
96
+ # genai.configure(api_key=GOOGLE_API_KEY)
97
+
98
+ # # Database
99
+ # students_data = [
100
+ # (1, "Alice", "A", "Computer Science"),
101
+ # (2, "Aliaa", "B", "Mathematics"),
102
+ # (3, "Charlie", "A", "Machine Learning"),
103
+ # (4, "Daan", "A", "Physics"),
104
+ # (5, "Jhon", "C", "Math"),
105
+ # (6, "Emma", "A+", "Computer Science")
106
+ # ]
107
+
108
+ # teachers_data = [
109
+ # (1, "Dr. Smith", "Math", "MS Mathematics"),
110
+ # (2, "Ms. Johnson", "Science", "MSc Physics"),
111
+ # (3, "Ms. Jack", "Artificial Intelligence Engineer", "MSc AI"),
112
+ # (4, "Ms. Evelyn", "Computer Science", "MSc Computer Science"),
113
+ # ]
114
+
115
+ # courses_data = [
116
+ # (1, "Algebra", "Dr. Smith", "Advanced"),
117
+ # (2, "Biology", "Ms. Mia", "Intermediate"),
118
+ # (3, "Machine Learning", "Ms. Jack", "Intermediate"),
119
+ # (4, "Computer Science", "Ms. Evelyn", "Intermediate"),
120
+ # (5, "Mathematics", "Ms. Smith", "Intermediate")
121
+ # ]
122
+
123
+ # def sanitize_text(text):
124
+ # """Remove invalid Unicode characters."""
125
+ # return text.encode("utf-8", "replace").decode("utf-8")
126
+
127
+ # def extract_video_id(url):
128
+ # if not url:
129
+ # return None
130
+ # patterns = [
131
+ # r'(?:v=|\/videos\/|embed\/|youtu.be\/|\/v\/|\/e\/|watch\?v=|\/watch\?v=)([^#\&\?]*)'
132
+ # ]
133
+ # for pattern in patterns:
134
+ # match = re.search(pattern, url)
135
+ # if match:
136
+ # return match.group(1)
137
+ # return None
138
+
139
+ # # def process_youtube_video(url="", keywords=""):
140
+ # try:
141
+ # if not url.strip():
142
+ # return None, "Please enter a YouTube URL", "N/A", ""
143
+
144
+ # video_id = extract_video_id(url)
145
+ # if not video_id:
146
+ # return None, "Invalid YouTube URL", "N/A", ""
147
+
148
+ # try:
149
+ # # Try multiple transcript options
150
+ # transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
151
+
152
+ # try:
153
+ # transcript = transcript_list.find_transcript(['en'])
154
+ # except:
155
+ # try:
156
+ # transcript = transcript_list.find_transcript(['en-US'])
157
+ # except:
158
+ # try:
159
+ # # Try auto-generated
160
+ # transcript = transcript_list.find_generated_transcript(['en'])
161
+ # except:
162
+ # raise NoTranscriptFound()
163
+
164
+ # text = " ".join([t['text'] for t in transcript.fetch()])
165
+
166
+ # # Generate summary
167
+ # model = genai.GenerativeModel("gemini-pro")
168
+ # summary = model.generate_content(f"Summarize this: {text[:4000]}").text
169
+
170
+ # # Analysis
171
+ # sentiment = TextBlob(text[:1000]).sentiment
172
+ # sentiment_label = f"{'Positive' if sentiment.polarity > 0 else 'Negative' if sentiment.polarity < 0 else 'Neutral'} ({sentiment.polarity:.2f})"
173
+
174
+ # thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
175
+
176
+ # except TranscriptsDisabled:
177
+ # return thumbnail, "⚠️ This video has disabled subtitles. Please try another video.", "N/A", ""
178
+ # except NoTranscriptFound:
179
+ # return thumbnail, "⚠️ No English transcript available. Please try another video.", "N/A", ""
180
+ # except Exception as e:
181
+ # return None, f"⚠️ Error: {str(e)}", "N/A", ""
182
+
183
+ # # Get recommendations
184
+ # if keywords.strip():
185
+ # recommendations = get_recommendations(keywords)
186
+ # else:
187
+ # recommendations = ""
188
+
189
+ # return thumbnail, summary, sentiment_label, recommendations
190
+
191
+ # except Exception as e:
192
+ # return None, f"Error: {str(e)}", "N/A", ""
193
+ # def get_recommendations(keywords, max_results=5):
194
+ # if not keywords:
195
+ # return "Please provide search keywords"
196
+ # try:
197
+ # response = requests.get(
198
+ # "https://www.googleapis.com/youtube/v3/search",
199
+ # params={
200
+ # "part": "snippet",
201
+ # "q": f"educational {keywords}",
202
+ # "type": "video",
203
+ # "maxResults": max_results,
204
+ # "relevanceLanguage": "en",
205
+ # "key": YOUTUBE_API_KEY
206
+ # }
207
+ # ).json()
208
+
209
+ # results = []
210
+ # for item in response.get("items", []):
211
+ # title = item["snippet"]["title"]
212
+ # channel = item["snippet"]["channelTitle"]
213
+ # video_id = item["id"]["videoId"]
214
+ # results.append(f"📺 {title}\n👤 {channel}\n🔗 https://youtube.com/watch?v={video_id}\n")
215
+
216
+ # return "\n".join(results) if results else "No recommendations found"
217
+ # except Exception as e:
218
+ # return f"Error: {str(e)}"
219
+
220
+ # # Gradio Interface
221
+ # with gr.Blocks(theme=gr.themes.Soft()) as app:
222
+ # # Login Page
223
+ # with gr.Group() as login_page:
224
+ # gr.Markdown("# 🎓 Educational Learning Management System")
225
+ # username = gr.Textbox(label="Username")
226
+ # password = gr.Textbox(label="Password", type="password")
227
+ # login_btn = gr.Button("Login", variant="primary")
228
+ # login_msg = gr.Markdown()
229
+
230
+ # # Main Interface
231
+ # with gr.Group(visible=False) as main_page:
232
+ # with gr.Row():
233
+ # with gr.Column(scale=1):
234
+ # gr.Markdown("### 📋 Navigation")
235
+ # nav_dashboard = gr.Button("📊 Dashboard", variant="primary")
236
+ # nav_students = gr.Button("👥 Students")
237
+ # nav_teachers = gr.Button("👨‍🏫 Teachers")
238
+ # nav_courses = gr.Button("📚 Courses")
239
+ # nav_youtube = gr.Button("🎥 YouTube Tool")
240
+ # logout_btn = gr.Button("🚪 Logout", variant="stop")
241
+
242
+ # with gr.Column(scale=3):
243
+ # # Dashboard Content
244
+ # dashboard_page = gr.Group()
245
+ # with dashboard_page:
246
+ # gr.Markdown("## 📊 Dashboard")
247
+ # gr.Markdown(f"""
248
+ # ### System Overview
249
+ # - 👥 Total Students: {len(students_data)}
250
+ # - 👨‍🏫 Total Teachers: {len(teachers_data)}
251
+ # - 📚 Total Courses: {len(courses_data)}
252
+
253
+ # ### Quick Actions
254
+ # - View student performance
255
+ # - Access course materials
256
+ # - Generate learning insights
257
+ # """)
258
+
259
+ # # Students Content
260
+ # students_page = gr.Group(visible=False)
261
+ # with students_page:
262
+ # gr.Markdown("## 👥 Students")
263
+ # gr.DataFrame(
264
+ # value=students_data,
265
+ # headers=["ID", "Name", "Grade", "Program"]
266
+ # )
267
+
268
+ # # Teachers Content
269
+ # teachers_page = gr.Group(visible=False)
270
+ # with teachers_page:
271
+ # gr.Markdown("## 👨‍🏫 Teachers")
272
+ # gr.DataFrame(
273
+ # value=teachers_data,
274
+ # headers=["ID", "Name", "Subject", "Qualification"]
275
+ # )
276
+
277
+ # # Courses Content
278
+ # courses_page = gr.Group(visible=False)
279
+ # with courses_page:
280
+ # gr.Markdown("## 📚 Courses")
281
+ # gr.DataFrame(
282
+ # value=courses_data,
283
+ # headers=["ID", "Name", "Instructor", "Level"]
284
+ # )
285
+
286
+ # # YouTube Tool Content
287
+ # youtube_page = gr.Group(visible=False)
288
+ # with youtube_page:
289
+ # gr.Markdown("## Agent for YouTube Content Exploration")
290
+ # with gr.Row():
291
+ # with gr.Column(scale=2):
292
+ # video_url = gr.Textbox(
293
+ # label="YouTube URL",
294
+ # placeholder="https://youtube.com/watch?v=..."
295
+ # )
296
+ # keywords = gr.Textbox(
297
+ # label="Keywords for Recommendations",
298
+ # placeholder="e.g., python programming, machine learning"
299
+ # )
300
+ # analyze_btn = gr.Button("🔍 Analyze Video", variant="primary")
301
+
302
+ # with gr.Column(scale=1):
303
+ # video_thumbnail = gr.Image(label="Video Preview")
304
+
305
+ # with gr.Row():
306
+ # with gr.Column():
307
+ # summary = gr.Textbox(label="📝 Summary", lines=8)
308
+ # sentiment = gr.Textbox(label="😊 Content Sentiment")
309
+ # with gr.Column():
310
+ # recommendations = gr.Textbox(label="🎯 Related Videos", lines=10)
311
+
312
+ # def login_check(user, pwd):
313
+ # if USER_CREDENTIALS.get(user) == pwd:
314
+ # return {
315
+ # login_page: gr.update(visible=False),
316
+ # main_page: gr.update(visible=True),
317
+ # login_msg: ""
318
+ # }
319
+ # return {
320
+ # login_page: gr.update(visible=True),
321
+ # main_page: gr.update(visible=False),
322
+ # login_msg: "❌ Invalid credentials"
323
+ # }
324
+
325
+ # def show_page(page_name):
326
+ # updates = {
327
+ # dashboard_page: gr.update(visible=False),
328
+ # students_page: gr.update(visible=False),
329
+ # teachers_page: gr.update(visible=False),
330
+ # courses_page: gr.update(visible=False),
331
+ # youtube_page: gr.update(visible=False)
332
+ # }
333
+ # updates[page_name] = gr.update(visible=True)
334
+ # return updates
335
+
336
+ # # Event Handlers
337
+ # login_btn.click(
338
+ # login_check,
339
+ # inputs=[username, password],
340
+ # outputs=[login_page, main_page, login_msg]
341
+ # )
342
+
343
+ # nav_dashboard.click(lambda: show_page(dashboard_page), outputs=list(show_page(dashboard_page).keys()))
344
+ # nav_students.click(lambda: show_page(students_page), outputs=list(show_page(students_page).keys()))
345
+ # nav_teachers.click(lambda: show_page(teachers_page), outputs=list(show_page(teachers_page).keys()))
346
+ # nav_courses.click(lambda: show_page(courses_page), outputs=list(show_page(courses_page).keys()))
347
+ # nav_youtube.click(lambda: show_page(youtube_page), outputs=list(show_page(youtube_page).keys()))
348
+
349
+ # analyze_btn.click(
350
+ # process_youtube_video,
351
+ # inputs=[video_url, keywords],
352
+ # outputs=[video_thumbnail, summary, sentiment, recommendations]
353
+ # )
354
+
355
+ # logout_btn.click(
356
+ # lambda: {
357
+ # login_page: gr.update(visible=True),
358
+ # main_page: gr.update(visible=False)
359
+ # },
360
+ # outputs=[login_page, main_page]
361
+ # )
362
+
363
+ # if __name__ == "__main__":
364
+ # app.launch()
365
+
366
+
367
+ ########################
368
+ # import os
369
+ # os.system('conda install -c conda-forge youtube-transcript-api -y')
370
+ # import subprocess
371
+ # subprocess.check_call(["pip", "install", "transformers==4.34.0"])
372
+ # subprocess.check_call(["pip", "install", "torch>=1.7.1"])
373
+ # subprocess.check_call(["pip", "install", "youtube_transcript_api>=0.6.3"])
374
+ # subprocess.check_call(["pip", "install", "pytube>=12.1.0"])
375
+ # subprocess.check_call(["pip", "install", "huggingface_hub>=0.19.0"])
376
+ # subprocess.check_call(["pip", "install", "PyPDF2>=3.0.1"])
377
+ # subprocess.check_call(["pip", "install", "google-generativeai>=0.3.0"])
378
+ # subprocess.check_call(["pip", "install", "textblob>=0.17.1"])
379
+ # subprocess.check_call(["pip", "install", "python-dotenv>=1.0.0"])
380
+ # subprocess.check_call(["pip", "install", "genai"])
381
+ # subprocess.check_call(["pip", "install", "google-cloud-aiplatform==1.34.0"])
382
+ # subprocess.check_call(["pip", "install", "scikit-learn>=1.0.0"])
383
+ # import transformers
384
+ # import torch
385
+ # import os
386
+ # import youtube_transcript_api
387
+ # import pytube
388
+ # import gradio
389
+ # import PyPDF2
390
+ # import pathlib
391
+ # import pandas
392
+ # import numpy
393
+ # import textblob
394
+ # import gradio as gr
395
+ # from youtube_transcript_api import YouTubeTranscriptApi
396
+ # import google.generativeai as genai
397
+ # import requests
398
+ # from textblob import TextBlob
399
+ # import re
400
+ # from dotenv import load_dotenv
401
+ # from sklearn.feature_extraction.text import TfidfVectorizer
402
+ # from sklearn.metrics.pairwise import cosine_similarity
403
+
404
+
405
+ # #from google.cloud import generativeai
406
+ # from huggingface_hub import login
407
+ # from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
408
+ # def install_missing_packages():
409
+ # required_packages = {
410
+ # "torch":">=1.11.0",
411
+ # "transformers":">=4.34.0",
412
+ # "youtube_transcript_api" :">=0.6.3" ,
413
+ # "pytube":">=12.1.0",
414
+ # "huggingface_hub": ">=0.19.0",
415
+ # "PyPDF2": ">=3.0.1",
416
+ # "textblob":">=0.17.1",
417
+ # "python-dotenv":">=1.0.0",
418
+ # "genai":None,
419
+ # "google-generativeai": ">=0.3.0",
420
+ # "google-cloud-aiplatform":"==1.34.0",
421
+ # "scikit-learn": ">=1.0.0"
422
+ # }
423
+
424
+
425
+ # for package, version in required_packages.items():
426
+ # try:
427
+ # __import__(package)
428
+ # except ImportError:
429
+ # package_name = f"{package}{version}" if version else package
430
+ # subprocess.check_call(["pip", "install", package_name])
431
+
432
+ # install_missing_packages()
433
+ # # # Configuration
434
+ # # USER_CREDENTIALS = {
435
+ # # "admin": "password123",
436
+ # # "teacher": "teach2024",
437
+ # # "student": "learn2024"
438
+ # # }
439
+
440
+ # hf_token = os.getenv("HF_TOKEN")
441
+ # if hf_token:
442
+ # login(hf_token)
443
+ # else:
444
+ # raise ValueError("HF_TOKEN environment variable not set.")
445
+
446
+ # # GOOGLE_API_KEY = "AIzaSyAURQb9jueh3dBQ4SITgKoR0L2_33en3yU"
447
+ # # YOUTUBE_API_KEY = "AIzaSyB7X-RYjZmUuDSMTQsvCfyzURw5bhqOto4"
448
+ # # genai.configure(api_key=GOOGLE_API_KEY)
449
+ # # # GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
450
+ # # # genai.configure(api_key=GOOGLE_API_KEY)
451
+ # # # YOUTUBE_API_KEY = os.getenv("YOUTUBE_API_KEY")
452
+ # # # print("GOOGLE_API_KEY:", os.getenv("GOOGLE_API_KEY"))
453
+ # # # print("YOUTUBE_API_KEY:", os.getenv("YOUTUBE_API_KEY"))
454
+
455
+ # import os
456
+ # import gradio as gr
457
+ # from youtube_transcript_api import YouTubeTranscriptApi
458
+ # from dotenv import load_dotenv
459
+ # import google.generativeai as genai
460
+ # import requests
461
+
462
+ # from transformers import pipeline
463
+ # from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
464
+ # from pytube import YouTube
465
+ # import gradio as gr
466
+ # from transformers import pipeline
467
+ # from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
468
+ # from pytube import YouTube
469
+ # import gradio as gr
470
+
471
+ # # Function to extract transcript from a YouTube video
472
+ # def extract_transcript(youtube_video_url):
473
+ # try:
474
+ # video_id = youtube_video_url.split("v=")[1].split("&")[0] # Extract video ID from URL
475
+
476
+ # # Try to fetch transcript using YouTubeTranscriptApi
477
+ # try:
478
+ # transcript = YouTubeTranscriptApi.get_transcript(video_id)
479
+ # result = " ".join([i['text'] for i in transcript])
480
+ # return result
481
+ # except (TranscriptsDisabled, NoTranscriptFound):
482
+ # pass # Fallback to pytube if no transcript found
483
+
484
+ # # Fallback to pytube for captions
485
+ # yt = YouTube(youtube_video_url)
486
+ # if yt.captions:
487
+ # captions = yt.captions.get_by_language_code("en")
488
+ # if captions:
489
+ # transcript = captions.generate_srt_captions()
490
+ # return transcript.replace("\n", " ") # Clean SRT captions format
491
+ # else:
492
+ # return "Error: No English captions available for this video."
493
+ # else:
494
+ # return "Error: No captions available for this video."
495
+
496
+ # except Exception as e:
497
+ # return f"Error: Unable to process the video URL. Details: {str(e)}"
498
+
499
+ # # Function to summarize the text
500
+ # def summarize_transcript(transcript_text):
501
+ # try:
502
+ # summarizer = pipeline('summarization', model="facebook/bart-large-cnn") # Using BART model for summarization
503
+ # summarized_text = []
504
+ # num_iters = len(transcript_text) // 1000 + 1
505
+
506
+ # for i in range(num_iters):
507
+ # start = i * 1000
508
+ # end = (i + 1) * 1000
509
+ # chunk = transcript_text[start:end]
510
+ # if chunk.strip():
511
+ # out = summarizer(chunk, max_length=130, min_length=30, do_sample=False)
512
+ # summarized_text.append(out[0]['summary_text'])
513
+
514
+ # return " ".join(summarized_text)
515
+ # except Exception as e:
516
+ # return f"Error during summarization: {str(e)}"
517
+
518
+ # # Gradio Interface function
519
+ # def gradio_interface(youtube_url):
520
+ # transcript = extract_transcript(youtube_url)
521
+ # if transcript.startswith("Error"):
522
+ # return transcript, "Summarization cannot be performed due to transcript error."
523
+
524
+ # summarized_text = summarize_transcript(transcript)
525
+ # return transcript, summarized_text
526
+
527
+ # # Gradio Interface Design
528
+ # with gr.Blocks() as demo:
529
+ # gr.Markdown("# YouTube Video Summarizer")
530
+ # with gr.Row():
531
+ # youtube_url_input = gr.Textbox(
532
+ # label="YouTube Video URL",
533
+ # placeholder="Enter the YouTube video link here"
534
+ # )
535
+ # summarize_button = gr.Button("Summarize Transcript")
536
+ # with gr.Row():
537
+ # transcript_output = gr.Textbox(label="Original Transcript", lines=10, interactive=False)
538
+ # summary_output = gr.Textbox(label="Summarized Text", lines=10, interactive=False)
539
+ # summarize_button.click(
540
+ # fn=gradio_interface,
541
+ # inputs=[youtube_url_input],
542
+ # outputs=[transcript_output, summary_output]
543
+ # )
544
+
545
+ # # Launch Gradio App
546
+ # if __name__ == "__main__":
547
+ # demo.launch()
548
+
549
  import os
550
  os.system('conda install -c conda-forge youtube-transcript-api -y')
551
  import subprocess
 
683
  return match.group(1)
684
  return None
685
 
686
+ def process_youtube_video(url="", keywords=""):
687
  try:
688
  if not url.strip():
689
  return None, "Please enter a YouTube URL", "N/A", ""
 
909
 
910
  if __name__ == "__main__":
911
  app.launch()