Spaces:
Sleeping
Sleeping
Create preprocess.py
Browse files- preprocess.py +103 -0
preprocess.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import requests
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from bs4 import BeautifulSoup
|
| 7 |
+
from sentence_transformers import SentenceTransformer, util
|
| 8 |
+
|
| 9 |
+
def preprocess():
|
| 10 |
+
# Base URL for navigation
|
| 11 |
+
base_url = 'https://courses.analyticsvidhya.com/collections/courses?page='
|
| 12 |
+
course_list_url = "https://courses.analyticsvidhya.com/"
|
| 13 |
+
|
| 14 |
+
# List to hold course data
|
| 15 |
+
courses = []
|
| 16 |
+
|
| 17 |
+
page_number = 1 # Start with the first page
|
| 18 |
+
while True:
|
| 19 |
+
# Construct URL for the current page
|
| 20 |
+
current_page_url = base_url + str(page_number)
|
| 21 |
+
print(f"Processing page {page_number}...")
|
| 22 |
+
|
| 23 |
+
# Get the current page content
|
| 24 |
+
response = requests.get(current_page_url)
|
| 25 |
+
if response.status_code != 200:
|
| 26 |
+
print(f"Failed to fetch page {page_number}. Status code: {response.status_code}")
|
| 27 |
+
break
|
| 28 |
+
|
| 29 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
| 30 |
+
|
| 31 |
+
# Find all course cards
|
| 32 |
+
course_cards = soup.find_all('li', class_='products__list-item')
|
| 33 |
+
if not course_cards:
|
| 34 |
+
print("No more courses found. Ending extraction.")
|
| 35 |
+
break
|
| 36 |
+
|
| 37 |
+
# Extract course data from each card
|
| 38 |
+
for course_card in course_cards:
|
| 39 |
+
title_tag = course_card.find('h3')
|
| 40 |
+
link_tag = course_card.find('a')
|
| 41 |
+
|
| 42 |
+
if title_tag and link_tag: # Check if both title and link exist
|
| 43 |
+
title = title_tag.text.strip()
|
| 44 |
+
course_link = link_tag['href']
|
| 45 |
+
|
| 46 |
+
# Construct full course URL (assume relative links)
|
| 47 |
+
course_url = course_list_url.rstrip('/') + course_link
|
| 48 |
+
|
| 49 |
+
# Visit each course link to get the description
|
| 50 |
+
course_response = requests.get(course_url)
|
| 51 |
+
if course_response.status_code == 200:
|
| 52 |
+
course_soup = BeautifulSoup(course_response.content, 'html.parser')
|
| 53 |
+
description_tag = course_soup.find('div', class_='fr-view') # Adjust based on actual class or tag
|
| 54 |
+
description = description_tag.text.strip() if description_tag else 'No description available'
|
| 55 |
+
|
| 56 |
+
curriculum_tag = course_soup.find('ul', class_='course-curriculum__chapter-content') # Adjust based on actual class or tag
|
| 57 |
+
curriculum = curriculum_tag.text.strip() if curriculum_tag else 'No curriculum available'
|
| 58 |
+
|
| 59 |
+
#enroll_tag = course_soup.find('article', class_='section__content section__content___ae733') # Adjust based on actual class or tag
|
| 60 |
+
#enroll = enroll_tag.text.strip() if enroll_tag else 'No enroll available'
|
| 61 |
+
|
| 62 |
+
instructor_tag = course_soup.find('section', class_='text-image section-height__medium section__content-alignment--left text-image___07200') # Adjust based on actual class or tag
|
| 63 |
+
instructor = instructor_tag.text.strip() if instructor_tag else 'No instructor available'
|
| 64 |
+
|
| 65 |
+
# Append the data to the list
|
| 66 |
+
courses.append({'title': title, 'description': description, 'Course curriculum': curriculum, 'About the Instructor': instructor})
|
| 67 |
+
else:
|
| 68 |
+
print(f"Failed to fetch course page: {course_url}")
|
| 69 |
+
|
| 70 |
+
# Sleep to avoid overwhelming the server (optional)
|
| 71 |
+
time.sleep(1)
|
| 72 |
+
else:
|
| 73 |
+
print("Skipped a course card due to missing title or link.")
|
| 74 |
+
|
| 75 |
+
# Move to the next page
|
| 76 |
+
page_number += 1
|
| 77 |
+
# break
|
| 78 |
+
|
| 79 |
+
# Save the collected data to a CSV file
|
| 80 |
+
df = pd.DataFrame(courses)
|
| 81 |
+
df.to_csv('courses.csv', index=False)
|
| 82 |
+
print("Data collection complete. Saved to courses.csv.")
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
# Load the data
|
| 86 |
+
df = pd.read_csv('courses.csv')
|
| 87 |
+
|
| 88 |
+
# Combine relevant text fields for embedding (e.g., title, description, curriculum)
|
| 89 |
+
df['combined_text'] = df['title'] + ' ' + df['description'] + ' ' + df['Course curriculum'] + ' ' + df['About the Instructor']
|
| 90 |
+
|
| 91 |
+
# Load a pre-trained model for embeddings
|
| 92 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 93 |
+
|
| 94 |
+
# Create embeddings for each course
|
| 95 |
+
embeddings = model.encode(df['combined_text'].tolist(), convert_to_tensor=True)
|
| 96 |
+
|
| 97 |
+
# Save embeddings and DataFrame for later use
|
| 98 |
+
np.save('course_embeddings.npy', embeddings)
|
| 99 |
+
df.to_csv('courses_with_embeddings.csv', index=False)
|
| 100 |
+
|
| 101 |
+
# Load embeddings and DataFrame
|
| 102 |
+
embeddings = np.load('course_embeddings.npy')
|
| 103 |
+
df = pd.read_csv('courses_with_embeddings.csv')
|