Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,16 +16,50 @@ course_descriptions = [course["description"] for course in courses]
|
|
| 16 |
course_embeddings = model.encode(course_descriptions, convert_to_tensor=True)
|
| 17 |
|
| 18 |
# Function to perform smart search
|
| 19 |
-
def search_courses(query):
|
| 20 |
# Generate embedding for the search query
|
| 21 |
-
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 22 |
|
| 23 |
# Compute cosine similarities between the query and all course descriptions
|
| 24 |
-
similarities = util.pytorch_cos_sim(query_embedding, course_embeddings)[0]
|
| 25 |
|
| 26 |
# Find the top 5 most similar courses
|
| 27 |
#top_results = np.argsort(similarities, descending=True)[:5]
|
| 28 |
-
top_results = np.argsort(-similarities)[:5]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Prepare output
|
| 31 |
results = []
|
|
|
|
| 16 |
course_embeddings = model.encode(course_descriptions, convert_to_tensor=True)
|
| 17 |
|
| 18 |
# Function to perform smart search
|
| 19 |
+
#def search_courses(query):
|
| 20 |
# Generate embedding for the search query
|
| 21 |
+
#query_embedding = model.encode(query, convert_to_tensor=True)
|
| 22 |
|
| 23 |
# Compute cosine similarities between the query and all course descriptions
|
| 24 |
+
#similarities = util.pytorch_cos_sim(query_embedding, course_embeddings)[0]
|
| 25 |
|
| 26 |
# Find the top 5 most similar courses
|
| 27 |
#top_results = np.argsort(similarities, descending=True)[:5]
|
| 28 |
+
#top_results = np.argsort(-similarities)[:5]
|
| 29 |
+
|
| 30 |
+
# Prepare output
|
| 31 |
+
#results = []
|
| 32 |
+
#for idx in top_results:
|
| 33 |
+
# course = courses[idx]
|
| 34 |
+
# results.append({
|
| 35 |
+
#"Title": course["title"],
|
| 36 |
+
#"Description": course["description"],
|
| 37 |
+
#"Link": course["link"]
|
| 38 |
+
#})
|
| 39 |
+
#return results
|
| 40 |
+
def search_courses(query):
|
| 41 |
+
if not query.strip():
|
| 42 |
+
return "Please enter a search query."
|
| 43 |
+
|
| 44 |
+
# Convert query and titles to lowercase for case-insensitive matching
|
| 45 |
+
query_lower = query.lower()
|
| 46 |
+
title_matches = [course for course in courses if query_lower in course["title"].lower()]
|
| 47 |
+
|
| 48 |
+
# If there are title matches, display those as results
|
| 49 |
+
if title_matches:
|
| 50 |
+
results = []
|
| 51 |
+
for course in title_matches[:5]: # Limit to top 5 title matches
|
| 52 |
+
results.append({
|
| 53 |
+
"Title": course["title"],
|
| 54 |
+
"Description": course["description"],
|
| 55 |
+
"Link": course["link"]
|
| 56 |
+
})
|
| 57 |
+
return results
|
| 58 |
+
|
| 59 |
+
# Fallback to semantic search if no title matches are found
|
| 60 |
+
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 61 |
+
similarities = util.pytorch_cos_sim(query_embedding, course_embeddings)[0]
|
| 62 |
+
top_results = np.argsort(-similarities)[:5] # Get top 5 most similar courses
|
| 63 |
|
| 64 |
# Prepare output
|
| 65 |
results = []
|