Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -67,12 +67,9 @@ else:
|
|
| 67 |
raise ValueError("HF_TOKEN environment variable not set.")
|
| 68 |
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
"teacher": "teach2024",
|
| 74 |
-
"student": "learn2024"
|
| 75 |
-
}
|
| 76 |
|
| 77 |
import os
|
| 78 |
from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
|
|
@@ -111,29 +108,6 @@ courses_data = [
|
|
| 111 |
(5, "Mathematics", "Ms. Smith", "Intermediate")
|
| 112 |
]
|
| 113 |
|
| 114 |
-
def sanitize_text(text):
|
| 115 |
-
"""Remove invalid Unicode characters."""
|
| 116 |
-
return text.encode("utf-8", "replace").decode("utf-8")
|
| 117 |
-
|
| 118 |
-
def extract_video_id(url):
|
| 119 |
-
if not url:
|
| 120 |
-
return None
|
| 121 |
-
patterns = [
|
| 122 |
-
r'(?:v=|\/videos\/|embed\/|youtu.be\/|\/v\/|\/e\/|watch\?v=|\/watch\?v=)([^#\&\?]*)'
|
| 123 |
-
]
|
| 124 |
-
for pattern in patterns:
|
| 125 |
-
match = re.search(pattern, url)
|
| 126 |
-
if match:
|
| 127 |
-
return match.group(1)
|
| 128 |
-
return None
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
from textblob import TextBlob
|
| 132 |
-
from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
|
| 133 |
-
import re
|
| 134 |
-
from collections import Counter
|
| 135 |
-
from googleapiclient.discovery import build
|
| 136 |
-
|
| 137 |
def extract_video_id(url):
|
| 138 |
match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11})", url)
|
| 139 |
return match.group(1) if match else None
|
|
@@ -226,34 +200,6 @@ def process_youtube_video(url):
|
|
| 226 |
except Exception as e:
|
| 227 |
return None, f"Error: {str(e)}", "N/A"
|
| 228 |
|
| 229 |
-
|
| 230 |
-
# def get_recommendations(keywords, max_results=5):
|
| 231 |
-
# if not keywords:
|
| 232 |
-
# return "Please provide search keywords"
|
| 233 |
-
# try:
|
| 234 |
-
# response = requests.get(
|
| 235 |
-
# "https://www.googleapis.com/youtube/v3/search",
|
| 236 |
-
# params={
|
| 237 |
-
# "part": "snippet",
|
| 238 |
-
# "q": f"educational {keywords}",
|
| 239 |
-
# "type": "video",
|
| 240 |
-
# "maxResults": max_results,
|
| 241 |
-
# "relevanceLanguage": "en",
|
| 242 |
-
# "key": YOUTUBE_API_KEY
|
| 243 |
-
# }
|
| 244 |
-
# ).json()
|
| 245 |
-
|
| 246 |
-
# results = []
|
| 247 |
-
# for item in response.get("items", []):
|
| 248 |
-
# title = item["snippet"]["title"]
|
| 249 |
-
# channel = item["snippet"]["channelTitle"]
|
| 250 |
-
# video_id = item["id"]["videoId"]
|
| 251 |
-
# results.append(f"πΊ {title}\nπ€ {channel}\nπ https://youtube.com/watch?v={video_id}\n")
|
| 252 |
-
|
| 253 |
-
# return "\n".join(results) if results else "No recommendations found"
|
| 254 |
-
# except Exception as e:
|
| 255 |
-
# return f"Error: {str(e)}"
|
| 256 |
-
|
| 257 |
# Gradio Interface
|
| 258 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 259 |
# Login Page
|
|
@@ -263,7 +209,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 263 |
password = gr.Textbox(label="Password", type="password")
|
| 264 |
login_btn = gr.Button("Login", variant="primary")
|
| 265 |
login_msg = gr.Markdown()
|
| 266 |
-
|
| 267 |
# Main Interface
|
| 268 |
with gr.Group(visible=False) as main_page:
|
| 269 |
with gr.Row():
|
|
@@ -275,7 +221,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 275 |
nav_courses = gr.Button("π Courses")
|
| 276 |
nav_youtube = gr.Button("π₯ YouTube Tool")
|
| 277 |
logout_btn = gr.Button("πͺ Logout", variant="stop")
|
| 278 |
-
|
| 279 |
with gr.Column(scale=3):
|
| 280 |
# Dashboard Content
|
| 281 |
dashboard_page = gr.Group()
|
|
@@ -286,13 +232,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 286 |
- π₯ Total Students: {len(students_data)}
|
| 287 |
- π¨βπ« Total Teachers: {len(teachers_data)}
|
| 288 |
- π Total Courses: {len(courses_data)}
|
| 289 |
-
|
| 290 |
### Quick Actions
|
| 291 |
- View student performance
|
| 292 |
- Access course materials
|
| 293 |
- Generate learning insights
|
| 294 |
""")
|
| 295 |
-
|
| 296 |
# Students Content
|
| 297 |
students_page = gr.Group(visible=False)
|
| 298 |
with students_page:
|
|
@@ -301,7 +247,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 301 |
value=students_data,
|
| 302 |
headers=["ID", "Name", "Grade", "Program"]
|
| 303 |
)
|
| 304 |
-
|
| 305 |
# Teachers Content
|
| 306 |
teachers_page = gr.Group(visible=False)
|
| 307 |
with teachers_page:
|
|
@@ -310,7 +256,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 310 |
value=teachers_data,
|
| 311 |
headers=["ID", "Name", "Subject", "Qualification"]
|
| 312 |
)
|
| 313 |
-
|
| 314 |
# Courses Content
|
| 315 |
courses_page = gr.Group(visible=False)
|
| 316 |
with courses_page:
|
|
@@ -319,7 +265,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 319 |
value=courses_data,
|
| 320 |
headers=["ID", "Name", "Instructor", "Level"]
|
| 321 |
)
|
| 322 |
-
|
| 323 |
# YouTube Tool Content
|
| 324 |
youtube_page = gr.Group(visible=False)
|
| 325 |
with youtube_page:
|
|
@@ -335,10 +281,11 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 335 |
placeholder="e.g., python programming, machine learning"
|
| 336 |
)
|
| 337 |
analyze_btn = gr.Button("π Analyze Video", variant="primary")
|
| 338 |
-
|
|
|
|
| 339 |
with gr.Column(scale=1):
|
| 340 |
video_thumbnail = gr.Image(label="Video Preview")
|
| 341 |
-
|
| 342 |
with gr.Row():
|
| 343 |
with gr.Column():
|
| 344 |
summary = gr.Textbox(label="π Summary", lines=8)
|
|
@@ -358,7 +305,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 358 |
main_page: gr.update(visible=False),
|
| 359 |
login_msg: "β Invalid credentials"
|
| 360 |
}
|
| 361 |
-
|
| 362 |
def show_page(page_name):
|
| 363 |
updates = {
|
| 364 |
dashboard_page: gr.update(visible=False),
|
|
@@ -369,26 +316,32 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 369 |
}
|
| 370 |
updates[page_name] = gr.update(visible=True)
|
| 371 |
return updates
|
| 372 |
-
|
| 373 |
# Event Handlers
|
| 374 |
login_btn.click(
|
| 375 |
login_check,
|
| 376 |
inputs=[username, password],
|
| 377 |
outputs=[login_page, main_page, login_msg]
|
| 378 |
)
|
| 379 |
-
|
| 380 |
nav_dashboard.click(lambda: show_page(dashboard_page), outputs=list(show_page(dashboard_page).keys()))
|
| 381 |
nav_students.click(lambda: show_page(students_page), outputs=list(show_page(students_page).keys()))
|
| 382 |
nav_teachers.click(lambda: show_page(teachers_page), outputs=list(show_page(teachers_page).keys()))
|
| 383 |
nav_courses.click(lambda: show_page(courses_page), outputs=list(show_page(courses_page).keys()))
|
| 384 |
nav_youtube.click(lambda: show_page(youtube_page), outputs=list(show_page(youtube_page).keys()))
|
| 385 |
-
|
| 386 |
analyze_btn.click(
|
| 387 |
process_youtube_video,
|
| 388 |
-
inputs=[video_url
|
| 389 |
-
outputs=[video_thumbnail, summary, sentiment
|
| 390 |
)
|
| 391 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
logout_btn.click(
|
| 393 |
lambda: {
|
| 394 |
login_page: gr.update(visible=True),
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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():
|
|
|
|
| 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()
|
|
|
|
| 232 |
- π₯ Total Students: {len(students_data)}
|
| 233 |
- π¨βπ« Total Teachers: {len(teachers_data)}
|
| 234 |
- π Total Courses: {len(courses_data)}
|
| 235 |
+
|
| 236 |
### Quick Actions
|
| 237 |
- View student performance
|
| 238 |
- Access course materials
|
| 239 |
- Generate learning insights
|
| 240 |
""")
|
| 241 |
+
|
| 242 |
# Students Content
|
| 243 |
students_page = gr.Group(visible=False)
|
| 244 |
with students_page:
|
|
|
|
| 247 |
value=students_data,
|
| 248 |
headers=["ID", "Name", "Grade", "Program"]
|
| 249 |
)
|
| 250 |
+
|
| 251 |
# Teachers Content
|
| 252 |
teachers_page = gr.Group(visible=False)
|
| 253 |
with teachers_page:
|
|
|
|
| 256 |
value=teachers_data,
|
| 257 |
headers=["ID", "Name", "Subject", "Qualification"]
|
| 258 |
)
|
| 259 |
+
|
| 260 |
# Courses Content
|
| 261 |
courses_page = gr.Group(visible=False)
|
| 262 |
with courses_page:
|
|
|
|
| 265 |
value=courses_data,
|
| 266 |
headers=["ID", "Name", "Instructor", "Level"]
|
| 267 |
)
|
| 268 |
+
|
| 269 |
# YouTube Tool Content
|
| 270 |
youtube_page = gr.Group(visible=False)
|
| 271 |
with youtube_page:
|
|
|
|
| 281 |
placeholder="e.g., python programming, machine learning"
|
| 282 |
)
|
| 283 |
analyze_btn = gr.Button("π Analyze Video", variant="primary")
|
| 284 |
+
recommend_btn = gr.Button("π Get Recommendations", variant="primary")
|
| 285 |
+
|
| 286 |
with gr.Column(scale=1):
|
| 287 |
video_thumbnail = gr.Image(label="Video Preview")
|
| 288 |
+
|
| 289 |
with gr.Row():
|
| 290 |
with gr.Column():
|
| 291 |
summary = gr.Textbox(label="π Summary", lines=8)
|
|
|
|
| 305 |
main_page: gr.update(visible=False),
|
| 306 |
login_msg: "β Invalid credentials"
|
| 307 |
}
|
| 308 |
+
|
| 309 |
def show_page(page_name):
|
| 310 |
updates = {
|
| 311 |
dashboard_page: gr.update(visible=False),
|
|
|
|
| 316 |
}
|
| 317 |
updates[page_name] = gr.update(visible=True)
|
| 318 |
return updates
|
| 319 |
+
|
| 320 |
# Event Handlers
|
| 321 |
login_btn.click(
|
| 322 |
login_check,
|
| 323 |
inputs=[username, password],
|
| 324 |
outputs=[login_page, main_page, login_msg]
|
| 325 |
)
|
| 326 |
+
|
| 327 |
nav_dashboard.click(lambda: show_page(dashboard_page), outputs=list(show_page(dashboard_page).keys()))
|
| 328 |
nav_students.click(lambda: show_page(students_page), outputs=list(show_page(students_page).keys()))
|
| 329 |
nav_teachers.click(lambda: show_page(teachers_page), outputs=list(show_page(teachers_page).keys()))
|
| 330 |
nav_courses.click(lambda: show_page(courses_page), outputs=list(show_page(courses_page).keys()))
|
| 331 |
nav_youtube.click(lambda: show_page(youtube_page), outputs=list(show_page(youtube_page).keys()))
|
| 332 |
+
|
| 333 |
analyze_btn.click(
|
| 334 |
process_youtube_video,
|
| 335 |
+
inputs=[video_url],
|
| 336 |
+
outputs=[video_thumbnail, summary, sentiment]
|
| 337 |
)
|
| 338 |
+
|
| 339 |
+
recommend_btn.click(
|
| 340 |
+
get_recommendations,
|
| 341 |
+
inputs=[keywords],
|
| 342 |
+
outputs=[recommendations]
|
| 343 |
+
)
|
| 344 |
+
|
| 345 |
logout_btn.click(
|
| 346 |
lambda: {
|
| 347 |
login_page: gr.update(visible=True),
|