-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourse.py
More file actions
569 lines (476 loc) · 20.4 KB
/
course.py
File metadata and controls
569 lines (476 loc) · 20.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
import os
from urllib.parse import urlparse
import uuid
from newspaper import Article
from datetime import datetime
from typing import List, Dict, Any
from bs4 import BeautifulSoup
import google.generativeai as genai
from groq import Groq
import requests
import json
import re
import nltk
from rapidfuzz import fuzz
nltk.download('punkt')
# API configuration
API_KEYS = {
"GEMINI_API_KEY": os.getenv("GEMINI_API_KEY"),
"GROQ_API_KEY": os.getenv("GROQ_API_KEY"),
"YOUTUBE_API_KEY": os.getenv("YOUTUBE_API_KEY"),
"GITHUB_API_KEY": os.getenv("GITHUB_API_KEY"),
"SERP_API_KEY": os.getenv("SERP_API_KEY")
}
# Initialize AI models
def init_ai_models():
genai.configure(api_key=API_KEYS["GEMINI_API_KEY"])
groq_client = Groq(api_key=API_KEYS["GROQ_API_KEY"])
return genai, groq_client
# Categories for course tagging
CATEGORIES = [
"Web Development", "Mobile Apps", "Data Science", "Machine Learning",
"Cloud Computing", "DevOps", "Cybersecurity", "Blockchain",
"UI/UX Design", "Game Development", "IoT", "AR/VR",
"Business Analytics", "Digital Marketing", "Product Management",
"Software Architecture", "API Development", "Microservices",
"Database Management", "Serverless Computing", "Event-Driven Architecture",
"Authentication & Authorization", "Performance Optimization",
"Caching Strategies", "Observability & Logging", "Scalability & Load Balancing",
"Containerization & Orchestration", "Security & Compliance",
"Backend Frameworks", "CI/CD & Infrastructure as Code",
"FinTech & Payment Systems", "E-commerce & Transactional Systems","Python"
]
# Learning styles supported
LEARNING_STYLES = ['Videos', 'Articles', 'Flashcards & Summaries', 'Step by Step Guides']
def get_num_challenges(commitment: str) -> int:
"""Determine the number of challenges based on daily commitment."""
if commitment == '5 minutes': return 1
if commitment == '10 minutes': return 1
if commitment == '15 minutes': return 2
if commitment == '30 minutes': return 2
return 3
# Function to determine appropriate category for a search query
def determine_category(query: str, interests: List[str]) -> str:
"""Determine the most appropriate category for a search query using AI."""
# Initialize AI models
genai, groq_client = init_ai_models()
prompt = f"""
Determine the best category for this learning query: "{query}"
Available categories: {', '.join(CATEGORIES)}
User interests: {', '.join(interests)}
Respond with just the category name from the list. No explanations.
"""
try:
# Try with Groq (LLaMA or Mixtral) first
completion = groq_client.chat.completions.create(
model="groq/compound",
messages=[{"role": "user", "content": prompt}],
temperature=0.2,
max_tokens=100
)
response_text = completion.choices[0].message.content.strip()
# Extract category from response
category = response_text.split("\n")[0].strip()
# Validate category
if category in CATEGORIES:
return category
except Exception as e:
print(f"Groq error, falling back to Gemini: {e}")
try:
# Use Gemini as a fallback
genai_model = genai.GenerativeModel('gemini-2.5-pro')
response = genai_model.generate_content(prompt)
response_text = response.text.strip()
# Extract and validate category
category = response_text.split("\n")[0].strip()
if category in CATEGORIES:
return category
except Exception as e:
print(f"Gemini error: {e}")
# Smart fallback: Choose most relevant category from interests
for interest in interests:
if interest in CATEGORIES:
return interest
# Absolute fallback: Choose a random category to prevent repetition
import random
return random.choice(CATEGORIES)
# Function to generate a course outline based on user query and preferences
def generate_course_outline(
query: str,
interests: List[str],
skill_level: str,
learning_style: str,
daily_commitment: str
) -> Dict[str, Any]:
"""Generate a complete course outline based on user search and preferences."""
# Initialize AI models
genai, groq_client = init_ai_models()
# Determine category
category = determine_category(query, interests)
# Convert daily commitment to estimated lesson time
time_mapping = {
"15 minutes": 5, # Each lesson about 5 minutes
"30 minutes": 10, # Each lesson about 10 minutes
"1 hour": 15, # Each lesson about 15 minutes
"2+ hours": 25 # Each lesson about 25 minutes
}
lesson_duration = time_mapping.get(daily_commitment, 10)
# Determine number of lessons based on skill level and commitment
lessons_count_mapping = {
"Beginner": {"15 minutes": 10, "30 minutes": 12, "1 hour": 15, "2+ hours": 20},
"Intermediate": {"15 minutes": 8, "30 minutes": 10, "1 hour": 12, "2+ hours": 16},
"Expert": {"15 minutes": 6, "30 minutes": 8, "1 hour": 10, "2+ hours": 12}
}
num_lessons = lessons_count_mapping.get(skill_level, {}).get(daily_commitment, 10)
# Generate course structure using LLaMA via Groq for deep structured content
prompt = f"""
Create a detailed microlearning course on: "{query}"
Target audience: {skill_level} level learner
Preferred learning style: {learning_style}
Time commitment per lesson: {lesson_duration} minutes
Number of lessons: {num_lessons}
User interests: {', '.join(interests)}
Response format:
{{
"title": "Course title",
"description": "Comprehensive course description (2-3 sentences)",
"category": "{category}",
"skillLevel": "{skill_level}",
"estimatedCompletion": "{num_lessons} lessons, approximately {lesson_duration} minutes each",
"lessons": [
{{
"lessonId": "1",
"title": "Lesson title",
"description": "Brief lesson description",
"content": {{
"theory": "Theoretical knowledge for this lesson",
"practice": "Practical exercise or application",
"resources": ["Resource 1", "Resource 2"]
}},
"estimatedDuration": "{lesson_duration} minutes"
}},
...
]
}}
Respond only with the JSON. Make sure the content is factually accurate, well-structured, and progressively builds knowledge.
"""
try:
# Try with Groq first (LLaMA or Mixtral)
completion = groq_client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[{"role": "user", "content": prompt}],
temperature=0.2,
max_tokens=4000
)
response_text = completion.choices[0].message.content
# Extract JSON from response
json_match = re.search(r'```json\n(.*?)\n```', response_text, re.DOTALL)
if json_match:
response_text = json_match.group(1)
# Parse JSON and validate structure
course_data = json.loads(response_text)
except Exception as e:
print(f"Groq error, falling back to Gemini: {e}")
# Fall back to Gemini if Groq fails
genai_model = genai.GenerativeModel('gemini-2.5-pro')
response = genai_model.generate_content(prompt)
response_text = response.text
# Extract JSON from response if needed
json_match = re.search(r'```json\n(.*?)\n```', response_text, re.DOTALL)
if json_match:
response_text = json_match.group(1)
# Parse JSON
try:
course_data = json.loads(response_text)
except json.JSONDecodeError:
# If JSON is invalid, create a minimal valid structure
course_data = {
"title": f"Learn {query}",
"description": f"A course designed to teach {query} for {skill_level} level learners.",
"category": category,
"skillLevel": skill_level,
"estimatedCompletion": f"{num_lessons} lessons, approximately {lesson_duration} minutes each",
"lessons": [{"lessonId": str(i), "title": f"Lesson {i}", "description": "Lesson content",
"content": {"theory": "", "practice": "", "resources": []},
"estimatedDuration": f"{lesson_duration} minutes"}
for i in range(1, num_lessons+1)]
}
# Generate unique ID for the course
course_id = str(uuid.uuid4())
# Add metadata
course_data["courseId"] = course_id
course_data["category"] = category
course_data["createdAt"] = datetime.now().isoformat()
course_data["updatedAt"] = datetime.now().isoformat()
course_data["searchQuery"] = query
# For each lesson, enrich with recommended resources based on learning style
if learning_style == 'Videos':
for lesson in course_data.get("lessons", []):
lesson["resources"] = get_youtube_resources(f"{query} {lesson['title']}", get_num_challenges(daily_commitment))
elif learning_style == 'Articles':
for lesson in course_data.get("lessons", []):
lesson["resources"] = get_articles_resources(f"{query} {lesson['title']}", get_num_challenges(daily_commitment))
elif learning_style == 'Step by Step Guides':
for lesson in course_data.get("lessons", []):
lesson["resources"] = get_github_resources(f"{query} {lesson['title']} tutorial", get_num_challenges(daily_commitment))
return course_data
# Function to get YouTube video recommendations
def get_youtube_resources(search_query: str, max_results: int = 3) -> List[Dict[str, str]]:
"""Fetch structured YouTube video recommendations efficiently."""
try:
url = (
f"https://www.googleapis.com/youtube/v3/search"
f"?part=snippet"
f"&q={search_query}"
f"&type=video"
f"&key={API_KEYS['YOUTUBE_API_KEY']}"
f"&maxResults={max_results}"
f"&order=relevance" # Prioritizes relevance
f"&videoEmbeddable=true" # Ensures videos are embeddable
f"&videoSyndicated=true" # Limits to publicly available videos
)
response = requests.get(url)
data = response.json()
results = []
if 'items' in data:
for item in data['items']:
snippet = item['snippet']
video_id = item['id']['videoId']
title = snippet['title']
channel = snippet['channelTitle']
thumbnail = snippet['thumbnails']['high']['url']
published_at = snippet['publishedAt']
results.append({
"type": "video",
"title": title,
"url": f"https://www.youtube.com/watch?v={video_id}",
"channel": channel,
"thumbnail": thumbnail,
"published_at": published_at,
})
return results
except Exception as e:
print(f"Error fetching YouTube resources: {e}")
return []
# Function to get GitHub resources
def get_github_resources(search_query: str, max_results: int = 2) -> List[Dict[str, str]]:
"""Get GitHub repositories related to the search query."""
try:
headers = {}
if API_KEYS["GITHUB_API_KEY"]:
headers["Authorization"] = f"token {API_KEYS['GITHUB_API_KEY']}"
url = f"https://api.github.com/search/repositories?q={search_query}&sort=stars&order=desc"
response = requests.get(url, headers=headers)
data = response.json()
results = []
if 'items' in data:
for item in data['items'][:max_results]:
results.append({
"type": "repository",
"title": item['name'],
"description": item['description'],
"url": item['html_url']
})
return results
except Exception as e:
print(f"Error fetching GitHub resources: {e}")
return []
def get_articles_resources(query: str, limit: int = 3) -> list:
"""Search for articles and generate AI-powered summaries using SerpAPI"""
try:
api_key = API_KEYS['SERP_API_KEY']
# api_key = "9ba31911722f0584e6986eabb113cfd3d703970fd3d7b918a1f11a3d47f468f5"
params = {
"engine": "google",
"q": query,
"num": limit,
"api_key": api_key
}
response = requests.get("https://serpapi.com/search", params=params)
data = response.json()
results = []
for result in data.get("organic_results", [])[:limit]:
link = result.get("link")
title = result.get("title")
article_content = extract_article_content(link)
summary = generate_ai_summary(article_content)
results.append({
"type": "article",
"title": title,
"url": link,
"summary": summary,
"domain": get_domain(link)
})
return results
except Exception as e:
print(f"Error in article search: {e}")
return []
def extract_article_content(url: str) -> str:
"""Extract main article content using advanced parsing"""
try:
# Use newspaper3k for better content extraction
article = Article(url)
article.download()
article.parse()
if article.text:
return f"{article.title}\n\n{article.text}"
# Fallback to BeautifulSoup
response = requests.get(url, timeout=10)
soup = BeautifulSoup(response.text, "html.parser")
# Advanced content cleaning
for element in soup(["script", "style", "nav", "footer", "aside"]):
element.decompose()
text = " ".join([p.get_text(strip=True) for p in soup.find_all(["p", "h1", "h2", "h3"])])
return text[:10000] # Limit content for API constraints
except Exception as e:
print(f"Content extraction failed: {e}")
return ""
def generate_ai_summary(content: str, model: str = "gemini") -> str:
"""Generate summary using either Groq or Gemini"""
genai, groq_client = init_ai_models()
if not content:
return "Summary unavailable"
try:
if model == "groq":
# Using Groq's fast Mixtral implementation
completion = groq_client.chat.completions.create(
messages=[{
"role": "user",
"content": f"Summarize this technical article in 3 concise bullet points:\n\n{content[:6000]}"
}],
model="llama-3.1-8b-instant",
temperature=0.3
)
return completion.choices[0].message.content
# Default to Gemini
model = genai.GenerativeModel('gemini-2.5-pro')
response = model.generate_content(
f"Create a 3-point summary of this article. Focus on key technical concepts and practical applications:\n\n{content[:30000]}"
)
return response.text
except Exception as e:
print(f"AI summary failed: {e}")
return content[:400] + "..." # Fallback to truncation
def get_domain(url: str) -> str:
"""Extract root domain for source credibility"""
parsed = urlparse(url)
return parsed.netloc.replace("www.", "").split(".")[0]
# Main function to handle course search and generation
async def search_or_generate_course(
db,
search_query: str,
user_id: str,
generate_new: bool = False
) -> Dict[str, Any]:
"""
Search for existing courses or generate a new one based on search query.
Args:
db: Firestore database client
search_query: User's search query
user_id: User ID for preference lookup
generate_new: Force generation of a new course even if similar exists
Returns:
Course data dictionary
"""
# Get user preferences
user_ref = db.collection('users').document(user_id)
user_doc = user_ref.get()
if not user_doc.exists:
raise Exception("User not found")
user_data = user_doc.to_dict()
preferences = user_data.get('preferences', {})
# Extract preferences
interests = preferences.get('interests', [])
skill_level = preferences.get('skillLevel', 'Beginner')
learning_style = preferences.get('learningStyle', 'Videos')
daily_commitment = preferences.get('dailyCommitment', '15 minutes')
category = determine_category(search_query, interests)
# Check if we should search for existing courses
if not generate_new:
# Use fuzzy matching to find a similar course
similar_course = find_similar_courses(search_query, skill_level, category, db)
if similar_course:
return similar_course # Return the best-matched course
# If no course found or force generate new, create a new course
course_data = generate_course_outline(
query=search_query,
interests=interests,
skill_level=skill_level,
learning_style=learning_style,
daily_commitment=daily_commitment
)
# Save the new course to Firestore
course_ref = db.collection('courses').document(course_data['courseId'])
course_ref.set(course_data)
# Return basic info for course card
return {
"courseId": course_data['courseId'],
"title": course_data['title'],
"description": course_data['description'],
"category": course_data['category'],
"skillLevel": course_data['skillLevel'],
"estimatedCompletion": course_data['estimatedCompletion'],
"lessonsCount": len(course_data.get('lessons', [])),
"lessonTitles": [lesson['title'] for lesson in course_data.get('lessons', [])]
}
# Function to get full course details
async def get_course_details(db, course_id: str) -> Dict[str, Any]:
"""Get full course details by ID."""
course_ref = db.collection('courses').document(course_id)
course_doc = course_ref.get()
if not course_doc.exists:
raise Exception("Course not found")
return course_doc.to_dict()
# Function to update user progress
async def update_course_progress(
db,
user_id: str,
course_id: str,
lesson_id: str,
completed: bool = False
) -> Dict[str, Any]:
"""Update user's progress in a course."""
user_ref = db.collection('users').document(user_id)
# Update the progress map
if completed:
user_ref.update({
f"progress.{course_id}.{lesson_id}": {
"completed": True,
"completedAt": datetime.now().isoformat()
}
})
else:
user_ref.update({
f"progress.{course_id}.{lesson_id}": {
"started": True,
"lastAccessedAt": datetime.now().isoformat()
}
})
# Get updated user data
user_doc = user_ref.get()
progress = user_doc.to_dict().get('progress', {})
return {
"userId": user_id,
"courseId": course_id,
"progress": progress.get(course_id, {})
}
def find_similar_courses(search_query: str, skill_level: str, category: str,db) -> Dict[str, Any]:
"""Search for similar courses in Firestore using NLP-based similarity."""
courses_ref = db.collection('courses')
query = courses_ref.where('skillLevel', '==', skill_level).limit(20)
results = query.get()
best_match = None
highest_score = 0
for doc in results:
course_data = doc.to_dict()
# Check similarity using fuzzy matching
title_score = fuzz.ratio(search_query.lower(), course_data.get('title', '').lower())
desc_score = fuzz.partial_ratio(search_query.lower(), course_data.get('description', '').lower())
category_match = 20 if course_data.get("category") == category else 0 # Extra points for same category
total_score = title_score + desc_score + category_match # Weighted scoring
if total_score > highest_score:
highest_score = total_score
best_match = course_data
if best_match and highest_score > 60: # Ensure a good match threshold
return best_match
return None # No good match found, generate a new course