Anupam251272 commited on
Commit
41524d3
·
verified ·
1 Parent(s): 206e288

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +146 -0
app.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import numpy as np
3
+ from sentence_transformers import SentenceTransformer, util
4
+ import gradio as gr
5
+ import requests
6
+ from bs4 import BeautifulSoup
7
+ import time
8
+ import torch
9
+ import traceback
10
+
11
+ # Extended sample data with business analyst courses
12
+ SAMPLE_COURSES = [
13
+ {
14
+ "title": "Business Analysis and Process Management Specialization",
15
+ "description": "Learn core business analysis skills, process mapping, and improvement techniques for organizational efficiency.",
16
+ "link": "https://www.coursera.org/specializations/business-analysis-process-management",
17
+ "source": "coursera"
18
+ },
19
+ {
20
+ "title": "Business Analytics Fundamentals",
21
+ "description": "Master data-driven decision making, Excel, SQL, and visualization tools for business analysis.",
22
+ "link": "https://www.udemy.com/course/business-analytics-fundamentals",
23
+ "source": "udemy"
24
+ },
25
+ {
26
+ "title": "Agile Business Analysis Professional",
27
+ "description": "Learn agile methodologies, user stories, and modern BA practices for software projects.",
28
+ "link": "https://www.coursera.org/professional-certificates/agile-business-analysis",
29
+ "source": "coursera"
30
+ },
31
+ # Original sample courses...
32
+ {
33
+ "title": "Python Programming for Beginners",
34
+ "description": "Learn Python from scratch. Covers basic concepts, data structures, and programming fundamentals.",
35
+ "link": "https://www.udemy.com/course/python-for-beginners",
36
+ "source": "udemy"
37
+ },
38
+ {
39
+ "title": "Machine Learning Specialization",
40
+ "description": "Comprehensive machine learning course covering supervised learning, neural networks, and practical ML projects.",
41
+ "link": "https://www.coursera.org/specializations/machine-learning",
42
+ "source": "coursera"
43
+ }
44
+ ]
45
+
46
+ def scrape_courses(query):
47
+ """
48
+ Scrape courses based on search query from multiple sources
49
+ """
50
+ courses = []
51
+
52
+ # Udemy API endpoint (you would need to register for API access)
53
+ udemy_url = f"https://www.udemy.com/api-2.0/courses/?search={query}&price=price-free"
54
+
55
+ # Coursera API endpoint (you would need to register for API access)
56
+ coursera_url = f"https://api.coursera.org/api/courses.v1?q=search&query={query}&includes=free"
57
+
58
+ try:
59
+ # Here you would implement the actual API calls
60
+ # For now, we'll filter the sample courses based on the query
61
+ query_terms = query.lower().split()
62
+ for course in SAMPLE_COURSES:
63
+ if any(term in course['title'].lower() or term in course['description'].lower()
64
+ for term in query_terms):
65
+ courses.append(course)
66
+
67
+ return courses
68
+ except Exception as e:
69
+ print(f"Error scraping courses: {e}")
70
+ return []
71
+
72
+ def search_courses(query):
73
+ if not query.strip():
74
+ return "Please enter a search query."
75
+
76
+ try:
77
+ # Get relevant courses based on the query
78
+ relevant_courses = scrape_courses(query)
79
+
80
+ if not relevant_courses:
81
+ return "No courses found for your search query."
82
+
83
+ # Initialize the model
84
+ device = torch.device('cpu')
85
+ model = SentenceTransformer("all-MiniLM-L6-v2")
86
+ model = model.to(device)
87
+
88
+ # Generate embeddings
89
+ course_descriptions = [f"{course['title']} {course['description']}" for course in relevant_courses]
90
+ course_embeddings = model.encode(course_descriptions, convert_to_tensor=True)
91
+
92
+ # Generate query embedding
93
+ query_embedding = model.encode(f"course about {query}", convert_to_tensor=True)
94
+
95
+ # Calculate similarities
96
+ similarities = util.pytorch_cos_sim(query_embedding, course_embeddings)[0]
97
+ top_indices = np.argsort((-similarities).numpy())[:5]
98
+
99
+ results = []
100
+ for idx in top_indices:
101
+ similarity_score = similarities[idx].item()
102
+ course = relevant_courses[idx]
103
+ results.append({
104
+ "Title": course["title"],
105
+ "Description": course["description"],
106
+ "Link": course["link"],
107
+ "Source": course["source"],
108
+ "Relevance": f"{similarity_score:.2%}"
109
+ })
110
+
111
+ return results
112
+ except Exception as e:
113
+ print(f"Search error: {str(e)}")
114
+ traceback.print_exc()
115
+ return []
116
+
117
+ def search_interface(query):
118
+ try:
119
+ print(f"\nSearching for: {query}")
120
+ results = search_courses(query)
121
+
122
+ if isinstance(results, str):
123
+ return results
124
+ if not results:
125
+ return "No matching courses found. Please try a different search term."
126
+
127
+ display_text = "\n\n".join(
128
+ [f"**Title**: {result['Title']}\n\n**Description**: {result['Description']}\n\n**Source:** {result['Source']}\n\n**Relevance:** {result['Relevance']}\n\n[Go to course]({result['Link']})"
129
+ for result in results]
130
+ )
131
+ return display_text
132
+ except Exception as e:
133
+ traceback.print_exc()
134
+ return f"An error occurred: {str(e)}"
135
+
136
+ # Create and launch the Gradio interface
137
+ iface = gr.Interface(
138
+ fn=search_interface,
139
+ inputs="text",
140
+ outputs="markdown",
141
+ title="Free Course Search Engine",
142
+ description="Enter a topic or keywords to find relevant free courses from Udemy and Coursera.",
143
+ examples=["Python", "Business Analyst", "Data Science", "Web Development"]
144
+ )
145
+
146
+ iface.launch(share=True)