File size: 1,937 Bytes
bb3fc71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import gradio as gr
from sentence_transformers import SentenceTransformer, util
import pandas as pd
import torch

# Load the fine-tuned model from Hugging Face
model = SentenceTransformer("adityasajja6/fine_tuned_mpnet_model")

# Load the cleaned courses dataset
courses_df = pd.read_csv('cleaned_analytics_vidhya_courses.csv')

# Load the precomputed course embeddings from the .pt file
course_embeddings = torch.load('course_embeddings.pt')

# Define the search function
def search_courses(query, top_k=5):
    # Create embedding for the query
    query_embedding = model.encode(query, convert_to_tensor=True)
    
    # Compute similarity scores
    similarities = util.pytorch_cos_sim(query_embedding, course_embeddings)[0]
    
    # Find the top_k most similar courses
    top_results = similarities.topk(k=top_k)
    
    # Extract the titles, links, and similarity scores of the top results
    results = []
    for idx in top_results.indices:
        idx = int(idx)  # Convert tensor index to an integer
        course_title = courses_df.iloc[idx]['Title']
        course_link = courses_df.iloc[idx]['Link']
        similarity_score = round(float(similarities[idx]), 4)
        results.append((course_title, course_link, similarity_score))
    
    return results

# Define the Gradio interface
def gradio_search(query):
    results = search_courses(query)
    formatted_results = [
        f"{title} (Score: {score}) - [Link]({link})"
        for title, link, score in results
    ]
    return "\n\n".join(formatted_results)

# Create a Gradio interface
interface = gr.Interface(
    fn=gradio_search,
    inputs=gr.Textbox(label="Search for courses"),
    outputs=gr.Markdown(label="Top Matching Courses"),
    title="Smart Course Search Tool",
    description="Enter a query to find the most relevant courses from Analytics Vidhya's free courses.",
)

# Launch the interface
if __name__ == "__main__":
    interface.launch()