File size: 4,924 Bytes
9c3a030
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
import streamlit as st
from sentence_transformers import SentenceTransformer
from dotenv import load_dotenv
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity
from courses import load_course_data

#environment variables
load_dotenv()
hugging_face_api_token = os.getenv("HUGGING_FACE_API_TOKEN")

#sentence transformer
model_name = "sentence-transformers/all-MiniLM-L6-v2"
sentence_transformer = SentenceTransformer(model_name)

def search_courses(user_query, course_data, top_k=5):
    # Prepare course information for embedding
    course_info = [
        f"{course['title']} - {course['description']} - Skills: {', '.join(course['skills'])}"
        for course in course_data
    ]

    # Get embeddings
    query_embedding = sentence_transformer.encode([user_query])
    course_embeddings = sentence_transformer.encode(course_info)

    # Calculate similarities
    similarities = cosine_similarity(query_embedding, course_embeddings)[0]

    # Get top k courses
    top_indices = np.argsort(similarities)[-top_k:][::-1]

    results = []
    for idx in top_indices:
        course = course_data[idx]
        similarity_score = similarities[idx]
        results.append({
            **course,
            "similarity": similarity_score
        })

    return results

def main():
    st.set_page_config(
        page_title="Analytics Vidhya Course Search",
        page_icon="πŸ“š",
        layout="wide"
    )

    # Header
    st.title("πŸ“š Analytics Vidhya Smart Course Search")
    st.write("Find the perfect free course for your learning journey!")

    # Sidebar filters
    st.sidebar.title("Filter Options")

    # Load course data
    course_data = load_course_data()

    # Get unique categories
    categories = sorted(list(set(cat for course in course_data for cat in course['category'])))
    selected_categories = st.sidebar.multiselect(
        "Filter by Category",
        categories
    )

    # Get unique skills
    all_skills = sorted(list(set(
        skill for course in course_data
        for skill in course['skills']
    )))
    selected_skills = st.sidebar.multiselect(
        "Filter by Skills",
        all_skills
    )

    # Filter courses based on selections
    filtered_courses = course_data
    if selected_categories:
        filtered_courses = [
            course for course in filtered_courses
            if any(cat in selected_categories for cat in course['category'])
        ]
    if selected_skills:
        filtered_courses = [
            course for course in filtered_courses
            if any(skill in selected_skills for skill in course['skills'])
        ]

    # Search input
    user_query = st.text_area(
        "Describe what you want to learn:",
        placeholder="E.g., I want to learn Python for data science with focus on machine learning and deep learning...",
        height=100
    )

    # Search button
    col1, col2, col3 = st.columns([1, 1, 1])
    with col2:
        search_button = st.button("πŸ” Search Courses")

    # Process search
    if search_button and user_query:
        if not filtered_courses:
            st.warning("No courses match your filter criteria. Try adjusting the filters.")
        else:
            with st.spinner("Searching for the best courses..."):
                results = search_courses(user_query, filtered_courses)

                st.subheader("πŸ“‹ Recommended Courses")
                for i, result in enumerate(results, 1):
                    with st.container():
                        col1, col2 = st.columns([3, 1])
                        with col1:
                            st.markdown(f"""
                            ### {i}. {result['title']}
                            **Categories:** {', '.join(result['category'])}  
                            **Relevance Score:** {result['similarity']:.2%}

                            {result['description']}

                            **Skills you'll learn:** {', '.join(result['skills'])}

                            [View Course]({result['url']})
                            ---
                            """)

    # Display help information at the bottom
    with st.expander("ℹ️ How to use this search"):
        st.markdown("""
        1. Use the sidebar filters to narrow down courses by category or specific skills (optional)
        2. Enter a description of what you want to learn in the text box above
        3. Click the 'Search Courses' button
        4. Browse through the recommended courses
        5. Click on 'View Course' to access the course on Analytics Vidhya

        **Tips for better results:**
        - Be specific about what you want to learn
        - Include your experience level
        - Mention specific technologies or skills you're interested in
        - Use the sidebar filters to focus on specific categories or skills
        """)

if __name__ == "__main__":
    main()