Spaces:
Build error
Build error
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,32 +1,42 @@
|
|
| 1 |
import json
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
def load_courses():
|
| 6 |
with open("data/courses.json") as f:
|
| 7 |
return json.load(f)
|
| 8 |
|
| 9 |
-
#
|
| 10 |
def recommend_course(user_input, courses):
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
if not relevant:
|
| 14 |
-
return "No relevant courses found."
|
| 15 |
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
# Optional: Use LLM to summarize reviews
|
| 19 |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
| 20 |
-
summaries = []
|
| 21 |
-
for course in sorted_courses[:3]:
|
| 22 |
-
review_text = " ".join(course["reviews"])
|
| 23 |
-
summary = summarizer(review_text, max_length=60, min_length=10, do_sample=False)[0]["summary_text"]
|
| 24 |
-
summaries.append((course, summary))
|
| 25 |
|
| 26 |
result = ""
|
| 27 |
-
for course,
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
result += f"⭐ Rating: {course['rating']}\n"
|
| 30 |
-
result += f"
|
| 31 |
-
|
|
|
|
| 32 |
return result
|
|
|
|
| 1 |
import json
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from rapidfuzz import fuzz
|
| 4 |
|
| 5 |
+
# Load course data
|
| 6 |
def load_courses():
|
| 7 |
with open("data/courses.json") as f:
|
| 8 |
return json.load(f)
|
| 9 |
|
| 10 |
+
# Recommend course using fuzzy matching and LLM summarization
|
| 11 |
def recommend_course(user_input, courses):
|
| 12 |
+
user_input = user_input.lower()
|
| 13 |
+
relevant = []
|
| 14 |
+
for course in courses:
|
| 15 |
+
text_to_match = " ".join([
|
| 16 |
+
course.get("title", ""),
|
| 17 |
+
course.get("category", ""),
|
| 18 |
+
" ".join(course.get("keywords", []))
|
| 19 |
+
]).lower()
|
| 20 |
+
score = fuzz.partial_ratio(user_input, text_to_match)
|
| 21 |
+
if score > 60:
|
| 22 |
+
relevant.append((course, score))
|
| 23 |
+
|
| 24 |
if not relevant:
|
| 25 |
+
return "⚠️ No relevant courses found. Try a simpler topic like 'Python' or 'AI'."
|
| 26 |
|
| 27 |
+
# Sort by rating, then relevance
|
| 28 |
+
relevant = sorted(relevant, key=lambda x: (x[0]["rating"], x[1]), reverse=True)
|
| 29 |
|
|
|
|
| 30 |
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
result = ""
|
| 33 |
+
for course, _ in relevant[:3]:
|
| 34 |
+
reviews = " ".join(course["reviews"])
|
| 35 |
+
summary = summarizer(reviews, max_length=60, min_length=10, do_sample=False)[0]["summary_text"]
|
| 36 |
+
|
| 37 |
+
result += f"### {course['title']} ({course['platform']})\n"
|
| 38 |
result += f"⭐ Rating: {course['rating']}\n"
|
| 39 |
+
result += f"📂 Category: {course['category']}\n"
|
| 40 |
+
result += f"💬 Summary of Reviews: {summary}\n\n"
|
| 41 |
+
|
| 42 |
return result
|