|
import pandas as pd
|
|
from sentence_transformers import SentenceTransformer, util
|
|
from transformers import pipeline
|
|
import torch
|
|
import gradio as gr
|
|
|
|
|
|
df = pd.read_csv(r"C:\Users\Saarthak\Desktop\Saarthak_assignment\analytics_vidhya_data.csv", encoding='ISO-8859-1')
|
|
|
|
|
|
model = SentenceTransformer('multi-qa-mpnet-base-dot-v1')
|
|
|
|
|
|
df['full_text'] = df.iloc[:,0] + " " + df.iloc[:,1] + " " + df['Instructor Name'] + " " + str(df['Rating']) + " " + df['Category']
|
|
|
|
|
|
course_embeddings = model.encode(df['full_text'].tolist(), convert_to_tensor=True)
|
|
|
|
|
|
def expand_query(query):
|
|
paraphraser = pipeline('text2text-generation', model='Vamsi/T5_Paraphrase_Paws')
|
|
expanded_queries = paraphraser(query, num_return_sequences=3, max_length=50, do_sample=True)
|
|
return [q['generated_text'] for q in expanded_queries]
|
|
|
|
|
|
def search_courses(query, level_filter=None, category_filter=None, top_k=3):
|
|
|
|
expanded_queries = expand_query(query)
|
|
|
|
|
|
all_similarities = []
|
|
|
|
for expanded_query in expanded_queries:
|
|
|
|
query_embedding = model.encode(expanded_query, convert_to_tensor=True)
|
|
|
|
|
|
similarities = util.pytorch_cos_sim(query_embedding, course_embeddings)[0]
|
|
|
|
|
|
all_similarities.append(similarities)
|
|
|
|
|
|
aggregated_similarities = torch.max(torch.stack(all_similarities), dim=0)[0]
|
|
|
|
|
|
filtered_df = df.copy()
|
|
if level_filter:
|
|
filtered_df = filtered_df[filtered_df['Level of Difficulty'] == level_filter]
|
|
if category_filter:
|
|
filtered_df = filtered_df[filtered_df['Category'] == category_filter]
|
|
|
|
if filtered_df.empty:
|
|
return "<p>No matching courses found.</p>"
|
|
|
|
|
|
filtered_similarities = aggregated_similarities[filtered_df.index]
|
|
|
|
|
|
top_results = filtered_similarities.topk(k=min(top_k, len(filtered_similarities)))
|
|
|
|
|
|
results = []
|
|
for idx in top_results.indices:
|
|
idx = int(idx)
|
|
course_title = filtered_df.iloc[idx]['Course Title']
|
|
course_description = filtered_df.iloc[idx,1]
|
|
course_url = filtered_df.iloc[idx,-1]
|
|
|
|
|
|
|
|
course_link = f'<a href="{course_url}" target="_blank">{course_title}</a>'
|
|
results.append(f"<strong>{course_link}</strong><br>{course_description}<br><br>")
|
|
|
|
|
|
return "<ol>" + "".join([f"<li>{result}</li>" for result in results]) + "</ol>"
|
|
|
|
|
|
def create_gradio_interface():
|
|
with gr.Blocks() as demo:
|
|
gr.Markdown("# π Analytics Vidhya Free Courses")
|
|
gr.Markdown("Enter your query and use filters to narrow down the search.")
|
|
|
|
|
|
query = gr.Textbox(label="π Search for a course", placeholder="Enter course topic or description")
|
|
|
|
|
|
with gr.Accordion("π Filters", open=False):
|
|
level_filter = gr.Dropdown(choices=["Beginner", "Intermediate", "Advanced"], label="π Course Level", multiselect=False)
|
|
category_filter = gr.Dropdown(choices=["Data Science", "Machine Learning", "Deep Learning", "AI", "NLP"], label="π Category", multiselect=False)
|
|
|
|
|
|
search_button = gr.Button("Search")
|
|
|
|
|
|
output = gr.HTML(label="Search Results")
|
|
|
|
|
|
search_button.click(fn=search_courses, inputs=[query, level_filter, category_filter], outputs=output)
|
|
|
|
return demo
|
|
|
|
|
|
demo = create_gradio_interface()
|
|
demo.launch(share=True, debug=True)
|
|
|