Sayiqa commited on
Commit
925e76d
·
verified ·
1 Parent(s): 421754f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -57
app.py CHANGED
@@ -140,6 +140,7 @@ import re
140
  from collections import Counter
141
  from googleapiclient.discovery import build
142
 
 
143
  def extract_video_id(url):
144
  match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11})", url)
145
  return match.group(1) if match else None
@@ -164,6 +165,17 @@ def get_video_metadata(video_id):
164
  def clean_text_for_analysis(text):
165
  return " ".join(text.split())
166
 
 
 
 
 
 
 
 
 
 
 
 
167
  def get_recommendations(keywords, max_results=5):
168
  if not keywords:
169
  return "Please provide search keywords"
@@ -185,21 +197,22 @@ def get_recommendations(keywords, max_results=5):
185
  title = item["snippet"]["title"]
186
  channel = item["snippet"]["channelTitle"]
187
  video_id = item["id"]["videoId"]
188
- results.append(f"\ud83d\udcfa {title}\n\ud83d\udc64 {channel}\n\ud83d\udd17 https://youtube.com/watch?v={video_id}\n")
189
 
190
  return "\n".join(results) if results else "No recommendations found"
191
  except Exception as e:
192
  return f"Error: {str(e)}"
193
 
194
- def process_youtube_video(url):
195
  try:
196
  thumbnail = None
197
  summary = "No transcript available"
198
  sentiment_label = "N/A"
 
199
 
200
  video_id = extract_video_id(url)
201
  if not video_id:
202
- return None, "Invalid YouTube URL", "N/A"
203
 
204
  thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
205
 
@@ -227,77 +240,108 @@ def process_youtube_video(url):
227
  summary = metadata.get("description", "No subtitles available")
228
  sentiment_label = "N/A"
229
 
230
- return thumbnail, summary, sentiment_label
 
 
 
231
 
232
  except Exception as e:
233
- return None, f"Error: {str(e)}", "N/A"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
 
235
  # Gradio Interface
236
  with gr.Blocks(theme=gr.themes.Soft()) as app:
237
  # Login Page
238
  with gr.Group() as login_page:
239
- gr.Markdown("# \ud83c\udf93 Educational Learning Management System")
240
  username = gr.Textbox(label="Username")
241
  password = gr.Textbox(label="Password", type="password")
242
  login_btn = gr.Button("Login", variant="primary")
243
  login_msg = gr.Markdown()
244
-
245
  # Main Interface
246
  with gr.Group(visible=False) as main_page:
247
  with gr.Row():
248
  with gr.Column(scale=1):
249
- gr.Markdown("### \ud83d\udcdc Navigation")
250
- nav_dashboard = gr.Button("\ud83d\udcca Dashboard", variant="primary")
251
- nav_students = gr.Button("\ud83d\udc65 Students")
252
- nav_teachers = gr.Button("\ud83d\udc68\u200d\ud83c\udf93 Teachers")
253
- nav_courses = gr.Button("\ud83d\udcda Courses")
254
- nav_youtube = gr.Button("\ud83c\udfa5 YouTube Tool")
255
- logout_btn = gr.Button("\ud83d\udeaa Logout", variant="stop")
256
-
257
  with gr.Column(scale=3):
258
  # Dashboard Content
259
  dashboard_page = gr.Group()
260
  with dashboard_page:
261
- gr.Markdown("## \ud83d\udcca Dashboard")
262
  gr.Markdown(f"""
263
  ### System Overview
264
- - \ud83d\udc65 Total Students: {len(students_data)}
265
- - \ud83d\udc68\u200d\ud83c\udf93 Total Teachers: {len(teachers_data)}
266
- - \ud83d\udcda Total Courses: {len(courses_data)}
267
 
268
  ### Quick Actions
269
  - View student performance
270
  - Access course materials
271
  - Generate learning insights
272
  """)
273
-
274
  # Students Content
275
  students_page = gr.Group(visible=False)
276
  with students_page:
277
- gr.Markdown("## \ud83d\udc65 Students")
278
  gr.DataFrame(
279
  value=students_data,
280
  headers=["ID", "Name", "Grade", "Program"]
281
  )
282
-
283
  # Teachers Content
284
  teachers_page = gr.Group(visible=False)
285
  with teachers_page:
286
- gr.Markdown("## \ud83d\udc68\u200d\ud83c\udf93 Teachers")
287
  gr.DataFrame(
288
  value=teachers_data,
289
  headers=["ID", "Name", "Subject", "Qualification"]
290
  )
291
-
292
  # Courses Content
293
  courses_page = gr.Group(visible=False)
294
  with courses_page:
295
- gr.Markdown("## \ud83d\udcda Courses")
296
  gr.DataFrame(
297
  value=courses_data,
298
  headers=["ID", "Name", "Instructor", "Level"]
299
  )
300
-
301
  # YouTube Tool Content
302
  youtube_page = gr.Group(visible=False)
303
  with youtube_page:
@@ -308,23 +352,21 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
308
  label="YouTube URL",
309
  placeholder="https://youtube.com/watch?v=..."
310
  )
311
- analyze_btn = gr.Button("\ud83d\udd0d Analyze Video", variant="primary")
312
-
 
 
 
 
313
  with gr.Column(scale=1):
314
  video_thumbnail = gr.Image(label="Video Preview")
315
-
316
  with gr.Row():
317
  with gr.Column():
318
- summary = gr.Textbox(label="\ud83d\udd8b\ufe0f Summary", lines=8)
319
- sentiment = gr.Textbox(label="\ud83d\ude0a Content Sentiment")
320
-
321
- gr.Markdown("### \ud83d\udd04 Recommendations")
322
- keywords = gr.Textbox(
323
- label="Keywords for Recommendations",
324
- placeholder="e.g., python programming, machine learning"
325
- )
326
- recommendations_btn = gr.Button("\ud83d\udd0d Get Recommendations", variant="primary")
327
- recommendations = gr.Textbox(label="\ud83d\udca1 Related Videos", lines=10)
328
 
329
  def login_check(user, pwd):
330
  if USER_CREDENTIALS.get(user) == pwd:
@@ -336,9 +378,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
336
  return {
337
  login_page: gr.update(visible=True),
338
  main_page: gr.update(visible=False),
339
- login_msg: "\u274c Invalid credentials"
340
  }
341
-
342
  def show_page(page_name):
343
  updates = {
344
  dashboard_page: gr.update(visible=False),
@@ -349,31 +391,26 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
349
  }
350
  updates[page_name] = gr.update(visible=True)
351
  return updates
352
-
353
- analyze_btn.click(
354
- process_youtube_video,
355
- inputs=[video_url],
356
- outputs=[video_thumbnail, summary, sentiment]
357
- )
358
-
359
- recommendations_btn.click(
360
- get_recommendations,
361
- inputs=[keywords],
362
- outputs=[recommendations]
363
- )
364
-
365
  login_btn.click(
366
  login_check,
367
  inputs=[username, password],
368
  outputs=[login_page, main_page, login_msg]
369
  )
370
-
371
  nav_dashboard.click(lambda: show_page(dashboard_page), outputs=list(show_page(dashboard_page).keys()))
372
  nav_students.click(lambda: show_page(students_page), outputs=list(show_page(students_page).keys()))
373
  nav_teachers.click(lambda: show_page(teachers_page), outputs=list(show_page(teachers_page).keys()))
374
  nav_courses.click(lambda: show_page(courses_page), outputs=list(show_page(courses_page).keys()))
375
  nav_youtube.click(lambda: show_page(youtube_page), outputs=list(show_page(youtube_page).keys()))
376
-
 
 
 
 
 
 
377
  logout_btn.click(
378
  lambda: {
379
  login_page: gr.update(visible=True),
@@ -384,4 +421,3 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
384
 
385
  if __name__ == "__main__":
386
  app.launch()
387
-
 
140
  from collections import Counter
141
  from googleapiclient.discovery import build
142
 
143
+
144
  def extract_video_id(url):
145
  match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11})", url)
146
  return match.group(1) if match else None
 
165
  def clean_text_for_analysis(text):
166
  return " ".join(text.split())
167
 
168
+ def extract_subtitle_info(text):
169
+ try:
170
+ sentences = text.split(". ")
171
+ words = text.split()
172
+ common_words = Counter(words).most_common(10)
173
+ key_topics = ", ".join([word for word, count in common_words])
174
+ info = f"Key topics discussed: {key_topics}. \nNumber of sentences: {len(sentences)}. \nTotal words: {len(words)}."
175
+ return info
176
+ except Exception as e:
177
+ return f"Error extracting subtitle information: {str(e)}"
178
+
179
  def get_recommendations(keywords, max_results=5):
180
  if not keywords:
181
  return "Please provide search keywords"
 
197
  title = item["snippet"]["title"]
198
  channel = item["snippet"]["channelTitle"]
199
  video_id = item["id"]["videoId"]
200
+ results.append(f"📺 {title}\n👤 {channel}\n🔗 https://youtube.com/watch?v={video_id}\n")
201
 
202
  return "\n".join(results) if results else "No recommendations found"
203
  except Exception as e:
204
  return f"Error: {str(e)}"
205
 
206
+ def process_youtube_video(url, keywords):
207
  try:
208
  thumbnail = None
209
  summary = "No transcript available"
210
  sentiment_label = "N/A"
211
+ recommendations = ""
212
 
213
  video_id = extract_video_id(url)
214
  if not video_id:
215
+ return None, "Invalid YouTube URL", "N/A", "", ""
216
 
217
  thumbnail = f"https://img.youtube.com/vi/{video_id}/maxresdefault.jpg"
218
 
 
240
  summary = metadata.get("description", "No subtitles available")
241
  sentiment_label = "N/A"
242
 
243
+ if keywords.strip():
244
+ recommendations = get_recommendations(keywords)
245
+
246
+ return thumbnail, summary, sentiment_label, recommendations
247
 
248
  except Exception as e:
249
+ return None, f"Error: {str(e)}", "N/A", ""
250
+
251
+
252
+ # def get_recommendations(keywords, max_results=5):
253
+ # if not keywords:
254
+ # return "Please provide search keywords"
255
+ # try:
256
+ # response = requests.get(
257
+ # "https://www.googleapis.com/youtube/v3/search",
258
+ # params={
259
+ # "part": "snippet",
260
+ # "q": f"educational {keywords}",
261
+ # "type": "video",
262
+ # "maxResults": max_results,
263
+ # "relevanceLanguage": "en",
264
+ # "key": YOUTUBE_API_KEY
265
+ # }
266
+ # ).json()
267
+
268
+ # results = []
269
+ # for item in response.get("items", []):
270
+ # title = item["snippet"]["title"]
271
+ # channel = item["snippet"]["channelTitle"]
272
+ # video_id = item["id"]["videoId"]
273
+ # results.append(f"📺 {title}\n👤 {channel}\n🔗 https://youtube.com/watch?v={video_id}\n")
274
+
275
+ # return "\n".join(results) if results else "No recommendations found"
276
+ # except Exception as e:
277
+ # return f"Error: {str(e)}"
278
 
279
  # Gradio Interface
280
  with gr.Blocks(theme=gr.themes.Soft()) as app:
281
  # Login Page
282
  with gr.Group() as login_page:
283
+ gr.Markdown("# 🎓 Educational Learning Management System")
284
  username = gr.Textbox(label="Username")
285
  password = gr.Textbox(label="Password", type="password")
286
  login_btn = gr.Button("Login", variant="primary")
287
  login_msg = gr.Markdown()
288
+
289
  # Main Interface
290
  with gr.Group(visible=False) as main_page:
291
  with gr.Row():
292
  with gr.Column(scale=1):
293
+ gr.Markdown("### 📋 Navigation")
294
+ nav_dashboard = gr.Button("📊 Dashboard", variant="primary")
295
+ nav_students = gr.Button("👥 Students")
296
+ nav_teachers = gr.Button("👨‍🏫 Teachers")
297
+ nav_courses = gr.Button("📚 Courses")
298
+ nav_youtube = gr.Button("🎥 YouTube Tool")
299
+ logout_btn = gr.Button("🚪 Logout", variant="stop")
300
+
301
  with gr.Column(scale=3):
302
  # Dashboard Content
303
  dashboard_page = gr.Group()
304
  with dashboard_page:
305
+ gr.Markdown("## 📊 Dashboard")
306
  gr.Markdown(f"""
307
  ### System Overview
308
+ - 👥 Total Students: {len(students_data)}
309
+ - 👨‍🏫 Total Teachers: {len(teachers_data)}
310
+ - 📚 Total Courses: {len(courses_data)}
311
 
312
  ### Quick Actions
313
  - View student performance
314
  - Access course materials
315
  - Generate learning insights
316
  """)
317
+
318
  # Students Content
319
  students_page = gr.Group(visible=False)
320
  with students_page:
321
+ gr.Markdown("## 👥 Students")
322
  gr.DataFrame(
323
  value=students_data,
324
  headers=["ID", "Name", "Grade", "Program"]
325
  )
326
+
327
  # Teachers Content
328
  teachers_page = gr.Group(visible=False)
329
  with teachers_page:
330
+ gr.Markdown("## 👨‍🏫 Teachers")
331
  gr.DataFrame(
332
  value=teachers_data,
333
  headers=["ID", "Name", "Subject", "Qualification"]
334
  )
335
+
336
  # Courses Content
337
  courses_page = gr.Group(visible=False)
338
  with courses_page:
339
+ gr.Markdown("## 📚 Courses")
340
  gr.DataFrame(
341
  value=courses_data,
342
  headers=["ID", "Name", "Instructor", "Level"]
343
  )
344
+
345
  # YouTube Tool Content
346
  youtube_page = gr.Group(visible=False)
347
  with youtube_page:
 
352
  label="YouTube URL",
353
  placeholder="https://youtube.com/watch?v=..."
354
  )
355
+ keywords = gr.Textbox(
356
+ label="Keywords for Recommendations",
357
+ placeholder="e.g., python programming, machine learning"
358
+ )
359
+ analyze_btn = gr.Button("🔍 Analyze Video", variant="primary")
360
+
361
  with gr.Column(scale=1):
362
  video_thumbnail = gr.Image(label="Video Preview")
363
+
364
  with gr.Row():
365
  with gr.Column():
366
+ summary = gr.Textbox(label="📝 Summary", lines=8)
367
+ sentiment = gr.Textbox(label="😊 Content Sentiment")
368
+ with gr.Column():
369
+ recommendations = gr.Textbox(label="🎯 Related Videos", lines=10)
 
 
 
 
 
 
370
 
371
  def login_check(user, pwd):
372
  if USER_CREDENTIALS.get(user) == pwd:
 
378
  return {
379
  login_page: gr.update(visible=True),
380
  main_page: gr.update(visible=False),
381
+ login_msg: " Invalid credentials"
382
  }
383
+
384
  def show_page(page_name):
385
  updates = {
386
  dashboard_page: gr.update(visible=False),
 
391
  }
392
  updates[page_name] = gr.update(visible=True)
393
  return updates
394
+
395
+ # Event Handlers
 
 
 
 
 
 
 
 
 
 
 
396
  login_btn.click(
397
  login_check,
398
  inputs=[username, password],
399
  outputs=[login_page, main_page, login_msg]
400
  )
401
+
402
  nav_dashboard.click(lambda: show_page(dashboard_page), outputs=list(show_page(dashboard_page).keys()))
403
  nav_students.click(lambda: show_page(students_page), outputs=list(show_page(students_page).keys()))
404
  nav_teachers.click(lambda: show_page(teachers_page), outputs=list(show_page(teachers_page).keys()))
405
  nav_courses.click(lambda: show_page(courses_page), outputs=list(show_page(courses_page).keys()))
406
  nav_youtube.click(lambda: show_page(youtube_page), outputs=list(show_page(youtube_page).keys()))
407
+
408
+ analyze_btn.click(
409
+ process_youtube_video,
410
+ inputs=[video_url, keywords],
411
+ outputs=[video_thumbnail, summary, sentiment, recommendations]
412
+ )
413
+
414
  logout_btn.click(
415
  lambda: {
416
  login_page: gr.update(visible=True),
 
421
 
422
  if __name__ == "__main__":
423
  app.launch()