adityasajja6 commited on
Commit
bb3fc71
·
verified ·
1 Parent(s): 02e218a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -56
app.py CHANGED
@@ -1,56 +1,57 @@
1
- import gradio as gr
2
- from sentence_transformers import SentenceTransformer, util
3
- import pandas as pd
4
- import torch
5
-
6
- # Load the fine-tuned model from Hugging Face
7
- model = SentenceTransformer("adityasajja6/fine_tuned_mpnet_model")
8
-
9
- # Load the cleaned courses dataset
10
- courses_df = pd.read_csv('cleaned_analytics_vidhya_courses.csv')
11
-
12
- # Load the precomputed course embeddings from the .pt file
13
- course_embeddings = torch.load('course_embeddings.pt')
14
-
15
- # Define the search function
16
- def search_courses(query, top_k=5):
17
- # Create embedding for the query
18
- query_embedding = model.encode(query, convert_to_tensor=True)
19
-
20
- # Compute similarity scores
21
- similarities = util.pytorch_cos_sim(query_embedding, course_embeddings)[0]
22
-
23
- # Find the top_k most similar courses
24
- top_results = similarities.topk(k=top_k)
25
-
26
- # Extract the titles, links, and similarity scores of the top results
27
- results = []
28
- for idx in top_results.indices:
29
- course_title = courses_df.iloc[idx]['Title']
30
- course_link = courses_df.iloc[idx]['Link']
31
- similarity_score = round(float(similarities[idx]), 4)
32
- results.append((course_title, course_link, similarity_score))
33
-
34
- return results
35
-
36
- # Define the Gradio interface
37
- def gradio_search(query):
38
- results = search_courses(query)
39
- formatted_results = [
40
- f"{title} (Score: {score}) - [Link]({link})"
41
- for title, link, score in results
42
- ]
43
- return "\n\n".join(formatted_results)
44
-
45
- # Create a Gradio interface
46
- interface = gr.Interface(
47
- fn=gradio_search,
48
- inputs=gr.Textbox(label="Search for courses"),
49
- outputs=gr.Markdown(label="Top Matching Courses"),
50
- title="Smart Course Search Tool",
51
- description="Enter a query to find the most relevant courses from Analytics Vidhya's free courses.",
52
- )
53
-
54
- # Launch the interface
55
- if __name__ == "__main__":
56
- interface.launch()
 
 
1
+ import gradio as gr
2
+ from sentence_transformers import SentenceTransformer, util
3
+ import pandas as pd
4
+ import torch
5
+
6
+ # Load the fine-tuned model from Hugging Face
7
+ model = SentenceTransformer("adityasajja6/fine_tuned_mpnet_model")
8
+
9
+ # Load the cleaned courses dataset
10
+ courses_df = pd.read_csv('cleaned_analytics_vidhya_courses.csv')
11
+
12
+ # Load the precomputed course embeddings from the .pt file
13
+ course_embeddings = torch.load('course_embeddings.pt')
14
+
15
+ # Define the search function
16
+ def search_courses(query, top_k=5):
17
+ # Create embedding for the query
18
+ query_embedding = model.encode(query, convert_to_tensor=True)
19
+
20
+ # Compute similarity scores
21
+ similarities = util.pytorch_cos_sim(query_embedding, course_embeddings)[0]
22
+
23
+ # Find the top_k most similar courses
24
+ top_results = similarities.topk(k=top_k)
25
+
26
+ # Extract the titles, links, and similarity scores of the top results
27
+ results = []
28
+ for idx in top_results.indices:
29
+ idx = int(idx) # Convert tensor index to an integer
30
+ course_title = courses_df.iloc[idx]['Title']
31
+ course_link = courses_df.iloc[idx]['Link']
32
+ similarity_score = round(float(similarities[idx]), 4)
33
+ results.append((course_title, course_link, similarity_score))
34
+
35
+ return results
36
+
37
+ # Define the Gradio interface
38
+ def gradio_search(query):
39
+ results = search_courses(query)
40
+ formatted_results = [
41
+ f"{title} (Score: {score}) - [Link]({link})"
42
+ for title, link, score in results
43
+ ]
44
+ return "\n\n".join(formatted_results)
45
+
46
+ # Create a Gradio interface
47
+ interface = gr.Interface(
48
+ fn=gradio_search,
49
+ inputs=gr.Textbox(label="Search for courses"),
50
+ outputs=gr.Markdown(label="Top Matching Courses"),
51
+ title="Smart Course Search Tool",
52
+ description="Enter a query to find the most relevant courses from Analytics Vidhya's free courses.",
53
+ )
54
+
55
+ # Launch the interface
56
+ if __name__ == "__main__":
57
+ interface.launch()