Spaces:
Build error
Build error
| 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 | |