Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ print("Starting the application...")
|
|
| 8 |
|
| 9 |
# Load the dataset from the same directory
|
| 10 |
print("Loading dataset...")
|
| 11 |
-
df = pd.read_csv('courses.csv') #
|
| 12 |
print(f"Dataset loaded. Number of rows: {df.shape[0]}")
|
| 13 |
|
| 14 |
# Load a pre-trained sentence transformer model
|
|
@@ -16,7 +16,7 @@ print("Loading Sentence Transformer model...")
|
|
| 16 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 17 |
print("Model loaded successfully.")
|
| 18 |
|
| 19 |
-
# Create a combined column for embedding
|
| 20 |
print("Generating embeddings for courses...")
|
| 21 |
df['combined_text'] = df['title'] + " " + df['description'] + " " + df['keywords']
|
| 22 |
course_embeddings = model.encode(df['combined_text'].tolist(), convert_to_tensor=True)
|
|
@@ -28,7 +28,7 @@ def search_courses(user_query):
|
|
| 28 |
# Encode the user query
|
| 29 |
query_embedding = model.encode(user_query, convert_to_tensor=True)
|
| 30 |
|
| 31 |
-
# Compute
|
| 32 |
print("Calculating cosine similarities...")
|
| 33 |
similarities = cosine_similarity(
|
| 34 |
query_embedding.cpu().detach().numpy().reshape(1, -1),
|
|
@@ -43,7 +43,7 @@ def search_courses(user_query):
|
|
| 43 |
print(f"Found {len(results)} results.")
|
| 44 |
return results
|
| 45 |
|
| 46 |
-
#
|
| 47 |
def gradio_search(query):
|
| 48 |
results = search_courses(query)
|
| 49 |
return results
|
|
@@ -58,7 +58,7 @@ iface = gr.Interface(
|
|
| 58 |
description="Find the most relevant courses based on your query."
|
| 59 |
)
|
| 60 |
|
| 61 |
-
# Launch the app
|
| 62 |
print("Launching the app...")
|
| 63 |
iface.launch()
|
| 64 |
|
|
|
|
| 8 |
|
| 9 |
# Load the dataset from the same directory
|
| 10 |
print("Loading dataset...")
|
| 11 |
+
df = pd.read_csv('courses.csv') # courses.csv
|
| 12 |
print(f"Dataset loaded. Number of rows: {df.shape[0]}")
|
| 13 |
|
| 14 |
# Load a pre-trained sentence transformer model
|
|
|
|
| 16 |
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 17 |
print("Model loaded successfully.")
|
| 18 |
|
| 19 |
+
# Create a combined column for embedding
|
| 20 |
print("Generating embeddings for courses...")
|
| 21 |
df['combined_text'] = df['title'] + " " + df['description'] + " " + df['keywords']
|
| 22 |
course_embeddings = model.encode(df['combined_text'].tolist(), convert_to_tensor=True)
|
|
|
|
| 28 |
# Encode the user query
|
| 29 |
query_embedding = model.encode(user_query, convert_to_tensor=True)
|
| 30 |
|
| 31 |
+
# Compute similarities between the query and each course embedding
|
| 32 |
print("Calculating cosine similarities...")
|
| 33 |
similarities = cosine_similarity(
|
| 34 |
query_embedding.cpu().detach().numpy().reshape(1, -1),
|
|
|
|
| 43 |
print(f"Found {len(results)} results.")
|
| 44 |
return results
|
| 45 |
|
| 46 |
+
# Gradio function for user interaction
|
| 47 |
def gradio_search(query):
|
| 48 |
results = search_courses(query)
|
| 49 |
return results
|
|
|
|
| 58 |
description="Find the most relevant courses based on your query."
|
| 59 |
)
|
| 60 |
|
| 61 |
+
# Launch the app
|
| 62 |
print("Launching the app...")
|
| 63 |
iface.launch()
|
| 64 |
|