Pontonkid commited on
Commit
9343e5d
·
verified ·
1 Parent(s): 6c0985e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pandas as pd
3
+ import numpy as np
4
+ from sentence_transformers import SentenceTransformer, util
5
+ import streamlit as st
6
+ from preprocess import preprocess
7
+
8
+ # File paths
9
+ csv_file_path = 'courses_with_embeddings.csv'
10
+ embeddings_file_path = 'course_embeddings.npy'
11
+
12
+ # Function to check if data files exist and contain data
13
+ def check_files_exist():
14
+ if os.path.exists(csv_file_path) and os.path.exists(embeddings_file_path):
15
+ # Check if CSV and Numpy files have data
16
+ if os.path.getsize(csv_file_path) > 0 and os.path.getsize(embeddings_file_path) > 0:
17
+ return True
18
+ return False
19
+
20
+ # Run preprocess if files don't exist or are empty
21
+ if not check_files_exist():
22
+ preprocess()
23
+
24
+ # Load data and embeddings
25
+ df = pd.read_csv(csv_file_path)
26
+ embeddings = np.load(embeddings_file_path)
27
+
28
+ # Load the pre-trained model
29
+ model = SentenceTransformer('all-MiniLM-L6-v2')
30
+
31
+ # Search function
32
+ def search_courses(query, top_k=5):
33
+ query_embedding = model.encode(query, convert_to_tensor=True)
34
+ similarities = util.pytorch_cos_sim(query_embedding, embeddings)[0]
35
+ top_results = similarities.topk(k=top_k)
36
+ results = [df.iloc[idx.item()] for idx in top_results.indices]
37
+ return results
38
+
39
+ def main():
40
+ # Streamlit interface
41
+ st.title("Smart Course Search")
42
+ st.markdown(
43
+ """
44
+ ### Find the Most Relevant Free Courses on Analytics Vidhya
45
+ Welcome to **Smart Course Search**! Simply type in your area of interest, and we'll show you the best courses available.
46
+ """
47
+ )
48
+
49
+ # User input for the search query
50
+ query = st.text_input("Enter your search query:")
51
+
52
+ if query:
53
+ st.markdown(f"### Showing results for: *'{query}'*")
54
+ results = search_courses(query)
55
+ for result in results:
56
+ st.markdown("---")
57
+ st.markdown(f"## {result['title']}")
58
+ st.markdown(f"**Description:**")
59
+ st.markdown(result['description'])
60
+
61
+ # Course Curriculum Section
62
+ if 'Course curriculum' in result:
63
+ st.markdown("### Course Curriculum:")
64
+ st.markdown(result['Course curriculum'])
65
+
66
+ # About the Instructor Section
67
+ if 'About the Instructor' in result:
68
+ st.markdown("### About the Instructor:")
69
+ st.write(result['About the Instructor'])
70
+
71
+ # Adding a button to enroll or learn more about the course
72
+ if 'url' in result:
73
+ st.markdown("**[Learn More and Enroll Here](%s)**" % result['url'])
74
+
75
+ if __name__ == "__main__":
76
+ main()