-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
184 lines (143 loc) · 7.28 KB
/
app.py
File metadata and controls
184 lines (143 loc) · 7.28 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from flask import Flask, render_template, url_for, request
import os, sys, sqlite3
from urllib.parse import unquote
import joblib
import numpy as np
## loading util contents
from course.utils.utils import read_db, get_recommendations
## loading the artifacts
from course.constants.entity.config_entity import DataTransformationConfig, ModelTrainerConfig
## supporters
from course.exception.exception import RecommenderException, error_message_details
from course.logger.front_end_logger import logging
import main
from main import initialize_pipeline
initialize_pipeline()
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/get_popular_courses', methods=['GET','POST'])
def recommend_popular_courses():
try:
logging.info('Popularity Based Filtering initiated')
data_transformation_config = DataTransformationConfig()
popularity_based_filtering_db_name = data_transformation_config.popularity_based_filtering_db_name
popularity_based_filtering_table_name = data_transformation_config.popular_table_name
popular_courses = read_db(popularity_based_filtering_db_name, popularity_based_filtering_table_name)
popular_courses = popular_courses.head(15)
logging.info('Succesfully read the database from ingested database')
return render_template('popular_courses.html', courses=popular_courses.to_dict(orient='records'))
except Exception as e:
logging.error(error_message_details(e, sys))
raise RecommenderException(e, sys)
@app.route('/top_educators', methods=['POST','GET'])
def show_top_educators():
try:
data_transformation_config = DataTransformationConfig()
threshold_db_name = data_transformation_config.threshold_database_name
threshold_table_name = data_transformation_config.threshold_table_name
threshold_df = read_db(threshold_db_name, threshold_table_name)
top_instructors = (threshold_df.groupby(['instructor', 'instructor_images'])
.agg({'rating': 'mean', 'feedback_score': 'mean'})
.sort_values(by=['rating', 'feedback_score'],
ascending=[False, False])
.reset_index()
.head(15))
return render_template('top_educators.html', instructors=top_instructors.to_dict('records'))
except Exception as e:
logging.error(f"Error in show_top_educators: {str(e)}")
@app.route('/frequently_bought_courses', methods=['POST','GET'])
def show_frequently_bought_courses():
try:
data_transformation_config = DataTransformationConfig()
thrreshold_db_name = data_transformation_config.threshold_database_name
threshold_table_name = data_transformation_config.threshold_table_name
df = read_db(thrreshold_db_name, threshold_table_name)
frequently_brought_courses = df.groupby(['course_name', 'course_images', 'rating', 'instructor', 'instructor_images'])['enrollment_numbers'].max().sort_values(ascending=False).reset_index().head(15)
return render_template('frequently_bought.html', courses=frequently_brought_courses.to_dict(orient='records'))
except Exception as e:
raise RecommenderException(e, sys)
@app.route('/help_desk', methods=['POST','GET'])
def show_help():
return render_template('help.html')
@app.route('/course_detail', methods=['GET'])
def course_detail():
try:
# Extract parameters first
course_name = request.args.get('name')
instructor_name = request.args.get('instructor')
# Load configs
model_trainer_config = ModelTrainerConfig()
trained_database_path = model_trainer_config.training_database_path
trained_dataframe_table_name = 'training_db_table'
# Load trained DataFrame
trained_dataframe = read_db(trained_database_path, trained_dataframe_table_name)
# Load embeddings
course_embeddings = np.load(model_trainer_config.embedding_layer_path) # Example
# Load threshold/original DataFrame
data_transformation_config = DataTransformationConfig()
threshold_db_name = data_transformation_config.threshold_database_name
threshold_table_name = data_transformation_config.threshold_table_name
threshold_df = read_db(threshold_db_name, threshold_table_name)
# Get recommendations
recommends = get_recommendations(
original_dataframe=threshold_df,
trained_dataframe=trained_dataframe,
course_embeddings=course_embeddings,
course_name=course_name,
instructor_name=instructor_name,
top_n=6
)
# print(f'Course Name: {course_name}\nInstructor Name : {instructor_name}')
# print(recommends[['course_name', 'instructor']])
# Get main course data
df = threshold_df.loc[
(threshold_df['course_name'] == course_name) &
(threshold_df['instructor'] == instructor_name)
].drop_duplicates(subset=['course_name', 'instructor'])
if df.empty:
return "Course not found", 404
course_data = df.iloc[0]
return render_template(
'course_details_with_recommendation.html',
course_name=course_data['course_name'],
instructor_name=course_data['instructor'],
course_image=course_data['course_images'],
instructor_image=course_data['instructor_images'],
rating=course_data['rating'],
enrollment_numbers=course_data['enrollment_numbers'],
course_duration=course_data['course_duration_hours'],
recommendations=recommends.to_dict('records') # Pass to template
)
except Exception as e:
logging.error(f"Error in course_detail: {str(e)}")
raise RecommenderException(e, sys)
@app.route('/contact')
def contact():
return render_template('contacts.html')
@app.route('/instructor/courses')
def view_courses_by_instructor():
instructor_name = request.args.get('instructor_name', '')
instructor_name = unquote(instructor_name)
data_transformation_config = DataTransformationConfig()
threshold_df = read_db(
data_transformation_config.threshold_database_name,
data_transformation_config.threshold_table_name
)
instructor_courses = threshold_df[threshold_df['instructor'] == instructor_name]
unique_courses = instructor_courses.drop_duplicates(subset=['course_name'])
top_courses = (
unique_courses
.groupby(['course_name','course_images','course_duration_hours', 'certification_offered', 'difficulty_level', 'rating', 'enrollment_numbers', 'course_price','study_material_available'], as_index=False)['rating']
.mean()
.sort_values(by='rating', ascending=False).head(15)
)
courses = top_courses.to_dict(orient='records')
return render_template(
'courses_by_instructor.html',
instructor_name=instructor_name,
courses=courses
)
if __name__=='__main__':
app.run(debug=True)