File size: 1,463 Bytes
d3cdf67
 
3f40cc6
d3cdf67
3f40cc6
d3cdf67
 
 
 
3f40cc6
d3cdf67
3f40cc6
 
 
 
 
 
 
 
 
 
 
 
d3cdf67
3f40cc6
d3cdf67
3f40cc6
 
d3cdf67
 
 
 
3f40cc6
 
 
 
 
d3cdf67
3f40cc6
 
 
d3cdf67
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
import json
from transformers import pipeline
from rapidfuzz import fuzz

# Load course data
def load_courses():
    with open("data/courses.json") as f:
        return json.load(f)

# Recommend course using fuzzy matching and LLM summarization
def recommend_course(user_input, courses):
    user_input = user_input.lower()
    relevant = []
    for course in courses:
        text_to_match = " ".join([
            course.get("title", ""),
            course.get("category", ""),
            " ".join(course.get("keywords", []))
        ]).lower()
        score = fuzz.partial_ratio(user_input, text_to_match)
        if score > 60:
            relevant.append((course, score))

    if not relevant:
        return "⚠️ No relevant courses found. Try a simpler topic like 'Python' or 'AI'."

    # Sort by rating, then relevance
    relevant = sorted(relevant, key=lambda x: (x[0]["rating"], x[1]), reverse=True)

    summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")

    result = ""
    for course, _ in relevant[:3]:
        reviews = " ".join(course["reviews"])
        summary = summarizer(reviews, max_length=60, min_length=10, do_sample=False)[0]["summary_text"]

        result += f"### {course['title']} ({course['platform']})\n"
        result += f"⭐ Rating: {course['rating']}\n"
        result += f"📂 Category: {course['category']}\n"
        result += f"💬 Summary of Reviews: {summary}\n\n"

    return result