Spaces:
Sleeping
Sleeping
Delete da.py
Browse files
da.py
DELETED
|
@@ -1,61 +0,0 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
"""Da.ipynb
|
| 3 |
-
|
| 4 |
-
Automatically generated by Colab.
|
| 5 |
-
|
| 6 |
-
Original file is located at
|
| 7 |
-
https://colab.research.google.com/drive/1Kg9-C_Fif3yO8FuXT84Tci-3ItuLsS7p
|
| 8 |
-
"""
|
| 9 |
-
"This is for running on google colab"
|
| 10 |
-
|
| 11 |
-
#Library install
|
| 12 |
-
!pip install transformers sentence-transformers gradio
|
| 13 |
-
|
| 14 |
-
import pandas as pd
|
| 15 |
-
from sentence_transformers import SentenceTransformer
|
| 16 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
| 17 |
-
import gradio as gr
|
| 18 |
-
|
| 19 |
-
# Load the dataset
|
| 20 |
-
df = pd.read_csv('/content/courses.csv') # Replace with actual path to courses.csv
|
| 21 |
-
|
| 22 |
-
# Load a pre-trained sentence transformer model
|
| 23 |
-
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 24 |
-
|
| 25 |
-
# Create a combined column for embedding (e.g., title + description + keywords)
|
| 26 |
-
df['combined_text'] = df['title'] + " " + df['description'] + " " + df['keywords']
|
| 27 |
-
course_embeddings = model.encode(df['combined_text'].tolist(), convert_to_tensor=True)
|
| 28 |
-
|
| 29 |
-
def search_courses(user_query):
|
| 30 |
-
# Encode the user query
|
| 31 |
-
query_embedding = model.encode(user_query, convert_to_tensor=True)
|
| 32 |
-
|
| 33 |
-
# Compute cosine similarities between the query and each course embedding
|
| 34 |
-
similarities = cosine_similarity(
|
| 35 |
-
query_embedding.cpu().detach().numpy().reshape(1, -1),
|
| 36 |
-
course_embeddings.cpu().detach().numpy()
|
| 37 |
-
)
|
| 38 |
-
|
| 39 |
-
# Get indices of top matching courses (top 5 results)
|
| 40 |
-
top_matches = similarities.argsort()[0][-5:][::-1]
|
| 41 |
-
|
| 42 |
-
# Retrieve top matching courses
|
| 43 |
-
results = [{"title": df.iloc[i]["title"], "description": df.iloc[i]["description"]} for i in top_matches]
|
| 44 |
-
return results
|
| 45 |
-
|
| 46 |
-
# Define Gradio function for user interaction
|
| 47 |
-
def gradio_search(query):
|
| 48 |
-
results = search_courses(query)
|
| 49 |
-
return results
|
| 50 |
-
|
| 51 |
-
# Set up Gradio interface
|
| 52 |
-
iface = gr.Interface(
|
| 53 |
-
fn=gradio_search,
|
| 54 |
-
inputs="text",
|
| 55 |
-
outputs="json",
|
| 56 |
-
title="Smart Course Search",
|
| 57 |
-
description="Find the most relevant courses based on your query."
|
| 58 |
-
)
|
| 59 |
-
|
| 60 |
-
# Launch the app (for local testing or deploying in Hugging Face Spaces)
|
| 61 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|