Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,57 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from sentence_transformers import SentenceTransformer, util
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import torch
|
| 5 |
-
|
| 6 |
-
# Load the fine-tuned model from Hugging Face
|
| 7 |
-
model = SentenceTransformer("adityasajja6/fine_tuned_mpnet_model")
|
| 8 |
-
|
| 9 |
-
# Load the cleaned courses dataset
|
| 10 |
-
courses_df = pd.read_csv('cleaned_analytics_vidhya_courses.csv')
|
| 11 |
-
|
| 12 |
-
# Load the precomputed course embeddings from the .pt file
|
| 13 |
-
course_embeddings = torch.load('course_embeddings.pt')
|
| 14 |
-
|
| 15 |
-
# Define the search function
|
| 16 |
-
def search_courses(query, top_k=5):
|
| 17 |
-
# Create embedding for the query
|
| 18 |
-
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 19 |
-
|
| 20 |
-
# Compute similarity scores
|
| 21 |
-
similarities = util.pytorch_cos_sim(query_embedding, course_embeddings)[0]
|
| 22 |
-
|
| 23 |
-
# Find the top_k most similar courses
|
| 24 |
-
top_results = similarities.topk(k=top_k)
|
| 25 |
-
|
| 26 |
-
# Extract the titles, links, and similarity scores of the top results
|
| 27 |
-
results = []
|
| 28 |
-
for idx in top_results.indices:
|
| 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 |
-
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from sentence_transformers import SentenceTransformer, util
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
# Load the fine-tuned model from Hugging Face
|
| 7 |
+
model = SentenceTransformer("adityasajja6/fine_tuned_mpnet_model")
|
| 8 |
+
|
| 9 |
+
# Load the cleaned courses dataset
|
| 10 |
+
courses_df = pd.read_csv('cleaned_analytics_vidhya_courses.csv')
|
| 11 |
+
|
| 12 |
+
# Load the precomputed course embeddings from the .pt file
|
| 13 |
+
course_embeddings = torch.load('course_embeddings.pt')
|
| 14 |
+
|
| 15 |
+
# Define the search function
|
| 16 |
+
def search_courses(query, top_k=5):
|
| 17 |
+
# Create embedding for the query
|
| 18 |
+
query_embedding = model.encode(query, convert_to_tensor=True)
|
| 19 |
+
|
| 20 |
+
# Compute similarity scores
|
| 21 |
+
similarities = util.pytorch_cos_sim(query_embedding, course_embeddings)[0]
|
| 22 |
+
|
| 23 |
+
# Find the top_k most similar courses
|
| 24 |
+
top_results = similarities.topk(k=top_k)
|
| 25 |
+
|
| 26 |
+
# Extract the titles, links, and similarity scores of the top results
|
| 27 |
+
results = []
|
| 28 |
+
for idx in top_results.indices:
|
| 29 |
+
idx = int(idx) # Convert tensor index to an integer
|
| 30 |
+
course_title = courses_df.iloc[idx]['Title']
|
| 31 |
+
course_link = courses_df.iloc[idx]['Link']
|
| 32 |
+
similarity_score = round(float(similarities[idx]), 4)
|
| 33 |
+
results.append((course_title, course_link, similarity_score))
|
| 34 |
+
|
| 35 |
+
return results
|
| 36 |
+
|
| 37 |
+
# Define the Gradio interface
|
| 38 |
+
def gradio_search(query):
|
| 39 |
+
results = search_courses(query)
|
| 40 |
+
formatted_results = [
|
| 41 |
+
f"{title} (Score: {score}) - [Link]({link})"
|
| 42 |
+
for title, link, score in results
|
| 43 |
+
]
|
| 44 |
+
return "\n\n".join(formatted_results)
|
| 45 |
+
|
| 46 |
+
# Create a Gradio interface
|
| 47 |
+
interface = gr.Interface(
|
| 48 |
+
fn=gradio_search,
|
| 49 |
+
inputs=gr.Textbox(label="Search for courses"),
|
| 50 |
+
outputs=gr.Markdown(label="Top Matching Courses"),
|
| 51 |
+
title="Smart Course Search Tool",
|
| 52 |
+
description="Enter a query to find the most relevant courses from Analytics Vidhya's free courses.",
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Launch the interface
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
interface.launch()
|