-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.py
executable file
·337 lines (256 loc) · 10.2 KB
/
app.py
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
from flask import Flask, request, render_template,url_for,flash,redirect,session
from flaskext.mysql import MySQL
from functools import wraps
from image_recomend import Images
# Recomendation
import pandas as pd
from sklearn.model_selection import train_test_split
import Recommender
import csv
import re
from twilio.rest import Client
app = Flask(__name__)
app.config['SECRET_KEY'] = 'ec830e5ae057c5b08f5a435a7b13e891'
# Config MySQL
app.config['MYSQL_DATABASE_HOST'] = "localhost"
#app.config['MYSQL_DATABASE_PORT'] = 3306
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = ''
app.config['MYSQL_DATABASE_DB'] = 'myflaskapp'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
# init MYSQL
mysql = MySQL()
mysql.init_app(app)
@app.route('/')
def main():
try:
if session['user_id']:
user_ids = session['user_id']
data = pd.read_csv('place_4.csv')
data['all_place'] = data['place'].map(str) + " - " + data['type']
song_grouped = data.groupby(['all_place']).agg({'rating': 'count'}).reset_index()
grouped_sum = song_grouped['rating'].sum()
song_grouped['percentage'] = song_grouped['rating'].div(grouped_sum)*100
song_grouped.sort_values(['rating', 'all_place'], ascending = [0,1])
train_data,test_data = train_test_split(data,test_size=0.2,random_state=0)
pm = Recommender.popularity_recommender_py()
pm.create(train_data, 'user_id', 'all_place')
# user_id = 10
content_based = pm.recommend(user_ids)
place = []
for index,row in content_based.iterrows():
print(row['all_place'])
place.append(row['all_place'])
is_model = Recommender.item_similarity_recommender_py()
is_model.create(train_data, 'user_id', 'all_place')
# user_id = 10
print(user_ids)
user_ids = int(user_ids)
user_items = is_model.get_user_items(user_ids)
place_personal = []
for user_item in user_items:
print(user_item)
place_personal.append(user_item)
return render_template('index.html',content_based=place,personal_recomendation=place_personal)
except:
return render_template('login.html')
@app.route('/reg',methods=['GET','POST'])
def reg():
return render_template('register.html')
@app.route('/register',methods=['GET','POST'])
def register():
conn=mysql.connect()
cursor = conn.cursor()
if(cursor.execute("INSERT INTO users(user_name,user_id, password) VALUES(%s,%s,%s)",('2','1','1'))):
print("data gone")
conn.commit()
cursor.close()
return 'data inserted'
@app.route('/login',methods=['GET','POST'])
def login():
try:
if session['user_id']:
return render_template('index.html')
except:
if request.method == 'POST':
user_name = request.form['user_name']
password = request.form['password']
conn=mysql.connect()
cursor = conn.cursor()
result = cursor.execute("SELECT * FROM users WHERE user_name = %s", [user_name])
print(result)
if result > 0:
data = cursor.fetchone()
if password == data[3]:
session['logged_in'] = True
session['username'] = user_name
session['user_id'] = data[2]
flash('You are now logged in', 'success')
return redirect(url_for('main'))
cursor.close()
return render_template('login.html')
# Check if user logged in
def is_logged_in(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
flash('Unauthorized, Please login', 'danger')
return redirect(url_for('login'))
return wrap
# Logout
@app.route('/logout')
@is_logged_in
def logout():
session.clear()
flash('You are now logged out', 'success')
return redirect(url_for('login'))
@app.route('/search_city',methods=['GET','POST'])
def search_city():
try:
if session['user_id']:
data = pd.read_csv('place_4.csv')
city = data['city'].unique()
return render_template('search_city.html',city=city)
except:
return render_template('login.html')
@app.route('/view_city/<city_name>',methods=['GET','POST'])
def view_city(city_name):
try:
if session['user_id']:
if request.method == 'POST' or request.method == 'GET':
data = pd.read_csv('place_4.csv')
places = data[data['city'] == city_name]
all_places = places.groupby(['place','type']).mean()
dt = []
for index,rows in all_places.iterrows():
new_data = {}
new_data['place'] = index[0]
new_data['rating'] = rows['rating']
new_data['type'] = index[1]
dt.append(new_data)
print(dt)
return render_template('view_city_place.html',data=dt,city_name=city_name)
except:
print('except in view city')
return render_template('login.html')
@app.route('/rating/<city_name>/<place>/<types>',methods=['GET','POST'])
def rating(city_name,place,types):
try:
if session['user_id']:
if request.method == 'POST':
user_id = session['user_id']
rating = request.form['rating']
print(city_name,place,types,rating)
with open('place_4.csv', 'a') as newFile:
newFileWriter = csv.writer(newFile)
newFileWriter.writerow([1,city_name,place,rating,types,user_id])
# return render_template('rating.html')
flash('Rating Done!','success')
return redirect(url_for('view_city',city_name=city_name))
except:
print('except in rating')
return render_template('login.html')
@app.route('/search',methods=['POST','GET'])
def search():
try:
if session['user_id']:
if request.method == 'POST':
inputs = request.form['data']
print(inputs)
return render_template('search.html')
except:
return render_template('login.html')
@app.route('/index')
def index():
return render_template('indexpage.html')
@app.route('/agra')
def agra():
return render_template('tajmahal.html')
@app.route('/goa')
def goa():
return render_template('goa.html')
@app.route('/mumbai')
def mumbai():
return render_template('mumbai.html')
@app.route('/manali')
def manali():
return render_template('manali.html')
@app.route('/delhi')
def delhi():
return render_template('delhi.html')
@app.route('/jaipur')
def jaipur():
return render_template('jaipur.html')
@app.route('/offers')
def offers():
return render_template('offers.html')
@app.route('/index_msg')
def index_msg():
return render_template('index.html')
@app.route('/image')
def image():
return render_template('imagesearch.html')
@app.route('/upload',methods=['POST','GET'])
def upload():
if request.method == 'POST':
recomend = Images()
data = recomend.Upload()
location = data
location = re.findall('([^\/]+$)',location)
new_loc = "queries/"+location[0]
print(new_loc)
print("Working")
res_loc = recomend.predict(new_loc)
print(res_loc)
res_name = res_loc[:-7]
if res_name == 'Taj_Mahal':
return render_template('main copy.html')
elif res_name == 'qutub_minar':
return render_template('main copy 2.html')
elif res_name == 'Mysore_Palace':
return render_template('mysore.html')
elif res_name == 'Jantar_mantar':
return render_template('jantar mantar.html')
elif res_name == 'hawa_mahal':
return render_template('hawa mahal.html')
elif res_name == 'red_fort':
return render_template('red fort.html')
elif res_name == 'gateway':
return render_template('gateway of india.html')
elif res_name == 'lotus_temple':
return render_template('lotus temple.html')
elif res_name == 'Virupaksha':
return render_template('virupaksha temple.html')
elif res_name == 'gol_gumbaz':
return render_template('gol gumbaz.html')
elif res_name == 'golden_temple':
return render_template('golden temple.html')
elif res_name == 'Jama_Masjid':
return render_template('jama masjid.html')
else:
return render_template('image.html')
return render_template('indexpage.html')
@app.route('/send_message',methods=['POST','GET'])
def send_message():
if request.method == 'POST':
name = request.form['name']
msg = request.form['message']
email_id = request.form['email']
sub = request.form['subject']
main_msg = "Name : "+name+"\nMobile Number : "+email_id+"\nSubject : "+sub+"\nMessage : "+msg
print(main_msg)
account_sid = '(enter acc id)'
auth_token = '(enter auth token)'
client = Client(account_sid, auth_token)
message = client.messages.create(
from_='whatsapp:+14155238886',
body= main_msg,
to='(enter your phone number)'
)
print(message.sid)
# flash('Your message sended successfully', 'success')
return redirect(url_for(''))
if __name__ == "__main__":
app.run(debug=True)