BobyMaurya455 commited on
Commit
3c993b4
·
verified ·
1 Parent(s): 7df1cdd

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +65 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import faiss
3
+ import numpy as np
4
+ from sentence_transformers import SentenceTransformer
5
+
6
+ # Sample course data (replace with real course data)
7
+ courses = [
8
+ {'title': 'Data Science for Beginners', 'description': 'Learn the basics of data science, including data analysis and machine learning.', 'curriculum': 'Introduction to Data Science, Data Cleaning, Data Visualization, Supervised Learning'},
9
+ {'title': 'Advanced Machine Learning', 'description': 'Deep dive into advanced machine learning algorithms, including reinforcement learning and neural networks.', 'curriculum': 'Reinforcement Learning, Neural Networks, Deep Learning, Hyperparameter Tuning'},
10
+ {'title': 'Data Visualization with Python', 'description': 'Master data visualization techniques using Python libraries like Matplotlib and Seaborn.', 'curriculum': 'Data Visualization Basics, Seaborn, Matplotlib, Plotly'}
11
+ ]
12
+
13
+ # Initialize the model and FAISS index (similar to what we did earlier)
14
+ model = SentenceTransformer('all-MiniLM-L6-v2')
15
+
16
+ # Create a list of course descriptions and generate embeddings
17
+ course_descriptions = [course['description'] for course in courses]
18
+ embeddings = model.encode(course_descriptions, convert_to_tensor=True)
19
+
20
+ # Initialize FAISS index
21
+ embedding_matrix = embeddings.cpu().detach().numpy()
22
+ index = faiss.IndexFlatL2(embedding_matrix.shape[1]) # L2 distance index
23
+ index.add(embedding_matrix)
24
+
25
+ # Function to search courses based on user query
26
+ def search_courses(query, top_k=3):
27
+ query_embedding = model.encode([query], convert_to_tensor=True).cpu().detach().numpy()
28
+ D, I = index.search(query_embedding, top_k)
29
+
30
+ results = []
31
+ for idx in I[0]: # I[0] contains the indices of the most similar courses
32
+ course = courses[idx]
33
+ results.append({
34
+ 'title': course['title'],
35
+ 'description': course['description'],
36
+ 'curriculum': course['curriculum']
37
+ })
38
+
39
+ return results
40
+
41
+ # Streamlit UI
42
+
43
+ st.title("Smart Search for Free Courses")
44
+
45
+ st.markdown("""
46
+ Enter a keyword or phrase, and we'll find the most relevant courses for you.
47
+ You can search for things like 'machine learning', 'data science', or 'Python'.
48
+ """)
49
+
50
+ # User input
51
+ query = st.text_input("Enter your query:")
52
+
53
+ if query:
54
+ st.subheader(f"Search Results for: '{query}'")
55
+ results = search_courses(query)
56
+
57
+ if results:
58
+ for idx, result in enumerate(results, 1):
59
+ st.write(f"**Result {idx}:**")
60
+ st.write(f"**Title:** {result['title']}")
61
+ st.write(f"**Description:** {result['description']}")
62
+ st.write(f"**Curriculum:** {result['curriculum']}")
63
+ st.write("-" * 50)
64
+ else:
65
+ st.write("No results found!")
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit
2
+ numpy
3
+ faiss-cpu
4
+ sentence-transformers