Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
@@ -1,59 +1,238 @@
|
|
1 |
-
from
|
2 |
-
import
|
3 |
-
import
|
|
|
4 |
from flask_cors import CORS
|
5 |
-
|
|
|
|
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
-
app.static_folder = 'static'
|
9 |
-
app.static_url_path = '/static'
|
10 |
-
|
11 |
-
app.secret_key = "roadsense-abhi-2023"
|
12 |
|
13 |
CORS(app)
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
@app.route('/', methods=['GET'])
|
23 |
-
def main():
|
24 |
-
return {'message': 'Hello, World'}
|
25 |
-
|
26 |
-
|
27 |
-
@app.route('/prediction', methods=['POST'])
|
28 |
-
def prediction():
|
29 |
-
data = request.get_json()
|
30 |
-
|
31 |
-
num_input = {'Latitude': data['Latitude'], 'Longitude': data['Longitude'], 'person_count': data['personCount']}
|
32 |
-
cat_input = {'weather_conditions': data['selectedWeatherCondition'], 'impact_type': data['selectedImpactType'],
|
33 |
-
'traffic_voilations': data['selectedTrafficViolationType'],
|
34 |
-
'road_features': data['selectedRoadFeaturesType'],
|
35 |
-
'junction_types': data['selectedRoadJunctionType'],
|
36 |
-
'traffic_controls': data['selectedTrafficControl'], 'time_day': data['selectedTimeOfDay'],
|
37 |
-
'age_group': data['selectedAge'], 'safety_features': data['selectedSafetyFeature'],
|
38 |
-
'injury': data['selectedInjuryType']}
|
39 |
-
|
40 |
-
input_df = pd.DataFrame([cat_input])
|
41 |
-
|
42 |
-
encoded_input = encoder['encoder'].transform(input_df)
|
43 |
-
encoded_input_df = pd.DataFrame(encoded_input, columns=encoder['encoded_columns'])
|
44 |
-
|
45 |
-
num_df = pd.DataFrame([num_input])
|
46 |
-
input_with_coords = pd.concat([num_df, encoded_input_df], axis=1)
|
47 |
|
48 |
-
# Make a prediction using the trained model
|
49 |
-
prediction = model.predict(input_with_coords)
|
50 |
-
|
51 |
-
temp = False
|
52 |
-
if prediction[0] == 1:
|
53 |
-
temp = True
|
54 |
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
if __name__ == '__main__':
|
59 |
app.run()
|
|
|
1 |
+
from email.message import EmailMessage
|
2 |
+
from flask import Flask, request, jsonify, render_template
|
3 |
+
import random
|
4 |
+
import string
|
5 |
from flask_cors import CORS
|
6 |
+
import smtplib
|
7 |
+
import bcrypt
|
8 |
+
import mysql.connector
|
9 |
|
10 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
11 |
|
12 |
CORS(app)
|
13 |
|
14 |
+
mysql = mysql.connector.connect(
|
15 |
+
host='sql12.freesqldatabase.com',
|
16 |
+
user='sql12653124',
|
17 |
+
password='kqM3CPBsqP',
|
18 |
+
database='sql12653124',
|
19 |
+
port=3306
|
20 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
@app.route('/feedback', methods=['POST', 'GET'])
|
24 |
+
def feedback():
|
25 |
+
if request.method == 'POST':
|
26 |
+
data = request.get_json()
|
27 |
+
|
28 |
+
cursor = mysql.cursor(dictionary=True)
|
29 |
+
cursor.execute("INSERT INTO `feedbacks`(`email`, `feedback`) VALUES (%s, %s)",
|
30 |
+
(data.get('email'), data.get('feedback'),))
|
31 |
+
mysql.commit()
|
32 |
+
cursor.close()
|
33 |
+
|
34 |
+
return jsonify({'success': True})
|
35 |
+
|
36 |
+
if request.method == 'GET':
|
37 |
+
cursor = mysql.cursor(dictionary=True)
|
38 |
+
cursor.execute("SELECT * FROM `feedbacks`")
|
39 |
+
feedback_data = cursor.fetchall()
|
40 |
+
mysql.commit()
|
41 |
+
cursor.close()
|
42 |
+
|
43 |
+
return jsonify({'success': True, 'data': feedback_data})
|
44 |
+
|
45 |
+
|
46 |
+
@app.route('/register', methods=['POST'])
|
47 |
+
def register():
|
48 |
+
if request.method == 'POST':
|
49 |
+
data = request.get_json()
|
50 |
+
cursor = mysql.cursor(dictionary=True)
|
51 |
+
cursor.execute("SELECT * FROM users WHERE email = %s", (data.get('email'),))
|
52 |
+
user = cursor.fetchone()
|
53 |
+
mysql.commit()
|
54 |
+
cursor.close()
|
55 |
+
|
56 |
+
if user:
|
57 |
+
error = 'Email address already in use. Please use a different email address.'
|
58 |
+
return jsonify({'success': False, 'message': error})
|
59 |
+
else:
|
60 |
+
msg = EmailMessage()
|
61 |
+
|
62 |
+
# alphabet = string.ascii_letters + string.digits
|
63 |
+
otp = random.randint(100000, 999999)
|
64 |
+
print(otp)
|
65 |
+
|
66 |
+
cursor = mysql.cursor(dictionary=True)
|
67 |
+
cursor.execute("INSERT INTO `otp`(`mail`, `otp`) VALUES (%s, %s)", (data.get('email'), otp,))
|
68 |
+
mysql.commit()
|
69 |
+
cursor.close()
|
70 |
+
|
71 |
+
msg["Subject"] = "StoryCircle Verification"
|
72 |
+
msg["From"] = "[email protected]"
|
73 |
+
msg["To"] = data.get('email')
|
74 |
+
|
75 |
+
html_content = render_template('email.html', name=data.get('name'), otp=otp)
|
76 |
+
msg.set_content(html_content, subtype='html')
|
77 |
+
|
78 |
+
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
|
79 |
+
smtp.login('[email protected]', 'njoexkbwuscrwdhf')
|
80 |
+
smtp.send_message(msg)
|
81 |
+
|
82 |
+
return jsonify({'success': True})
|
83 |
+
|
84 |
+
|
85 |
+
@app.route('/verify', methods=['POST', 'GET'])
|
86 |
+
def verify():
|
87 |
+
if request.method == 'POST':
|
88 |
+
data = request.get_json()
|
89 |
+
cursor = mysql.cursor(dictionary=True)
|
90 |
+
cursor.execute("SELECT `otp` FROM `otp` WHERE `mail`=%s ORDER BY `id` DESC LIMIT 1", (data.get('email'),))
|
91 |
+
system_otp = cursor.fetchone()
|
92 |
+
mysql.commit()
|
93 |
+
cursor.close()
|
94 |
+
|
95 |
+
if system_otp['otp'] == data.get('otp'):
|
96 |
+
cursor = mysql.cursor(dictionary=True)
|
97 |
+
password = data.get('password').encode('utf-8')
|
98 |
+
hash_password = bcrypt.hashpw(password, bcrypt.gensalt())
|
99 |
+
cursor.execute("INSERT INTO `users` (`name`, `email`, `password`) VALUES (%s, %s, %s)",(data.get('name'), data.get('email'), hash_password,))
|
100 |
+
mysql.commit()
|
101 |
+
cursor.close()
|
102 |
+
return jsonify({'success': True})
|
103 |
+
else:
|
104 |
+
return jsonify({'success': False})
|
105 |
+
|
106 |
+
|
107 |
+
@app.route('/login', methods=['POST', 'GET'])
|
108 |
+
def login():
|
109 |
+
if request.method == 'POST':
|
110 |
+
data = request.get_json()
|
111 |
+
email = data.get('username')
|
112 |
+
password = data.get('password').encode('utf-8')
|
113 |
+
|
114 |
+
cursor = mysql.cursor(dictionary=True)
|
115 |
+
cursor.execute("SELECT * FROM `users` WHERE email=%s", (email,))
|
116 |
+
user = cursor.fetchone()
|
117 |
+
mysql.commit()
|
118 |
+
cursor.close()
|
119 |
+
|
120 |
+
|
121 |
+
if user:
|
122 |
+
if bcrypt.hashpw(password, user['password'].encode('utf-8')) == user['password'].encode('utf-8'):
|
123 |
+
cursor = mysql.cursor(dictionary=True)
|
124 |
+
cursor.execute("INSERT INTO `session`(`id`, `name`, `email`) VALUES (%s, %s, %s)", (user['id'], user['name'], user['email'],))
|
125 |
+
mysql.commit()
|
126 |
+
cursor.close()
|
127 |
+
|
128 |
+
|
129 |
+
return jsonify({'login': True, 'message': 'Valid User Login', 'id': user['id'],
|
130 |
+
'name': user['name'], 'email': user['email']})
|
131 |
+
|
132 |
+
else:
|
133 |
+
return jsonify({'login': False, 'message': 'Invalid Password'})
|
134 |
+
else:
|
135 |
+
return jsonify({'login': False})
|
136 |
+
|
137 |
+
|
138 |
+
@app.route('/logincheck', methods=['POST'])
|
139 |
+
def checklogin():
|
140 |
+
# print(session)
|
141 |
+
data = request.get_json()
|
142 |
+
print(data)
|
143 |
+
|
144 |
+
if data.get('email') == 'Meow':
|
145 |
+
return jsonify({'login': False})
|
146 |
+
|
147 |
+
cursor = mysql.cursor(dictionary=True)
|
148 |
+
cursor.execute("SELECT `id`, `name`, `email` FROM `session` WHERE `email`= %s ORDER BY `index` DESC LIMIT 1;", (data.get('email'),))
|
149 |
+
user = cursor.fetchone()
|
150 |
+
cursor.close()
|
151 |
+
|
152 |
+
if user:
|
153 |
+
return jsonify({'login': True, 'message': 'Valid User Login', 'id': user['id'],
|
154 |
+
'name': user['name'], 'email': user['email']})
|
155 |
+
|
156 |
+
else:
|
157 |
+
return jsonify({'login': False})
|
158 |
+
|
159 |
+
|
160 |
+
@app.route('/forgot', methods=['POST'])
|
161 |
+
def forgot():
|
162 |
+
if request.method == 'POST':
|
163 |
+
data = request.get_json()
|
164 |
+
print(data)
|
165 |
+
cursor = mysql.cursor(dictionary=True)
|
166 |
+
cursor.execute("SELECT * FROM users WHERE email = %s", (data.get('username'),))
|
167 |
+
user = cursor.fetchone()
|
168 |
+
mysql.commit()
|
169 |
+
|
170 |
+
if user:
|
171 |
+
msg = EmailMessage()
|
172 |
+
|
173 |
+
otp = random.randint(100000, 999999)
|
174 |
+
|
175 |
+
cursor = mysql.cursor(dictionary=True)
|
176 |
+
cursor.execute("INSERT INTO `otp`(`mail`, `otp`) VALUES (%s, %s)", (data.get('username'), otp,))
|
177 |
+
mysql.commit()
|
178 |
+
cursor.close()
|
179 |
+
|
180 |
+
msg["Subject"] = "StoryCircle Verification"
|
181 |
+
msg["From"] = "[email protected]"
|
182 |
+
msg["To"] = data.get('username')
|
183 |
+
|
184 |
+
html_content = render_template('pass.html', name=user['name'], otp=otp)
|
185 |
+
msg.set_content(html_content, subtype='html')
|
186 |
+
|
187 |
+
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
|
188 |
+
smtp.login('[email protected]', 'njoexkbwuscrwdhf')
|
189 |
+
smtp.send_message(msg)
|
190 |
+
|
191 |
+
return jsonify({'success': True})
|
192 |
+
else:
|
193 |
+
error = 'No such User found. Please Register first.'
|
194 |
+
return jsonify(error)
|
195 |
+
|
196 |
+
|
197 |
+
@app.route('/verifyforgot', methods=['POST'])
|
198 |
+
def verifyforgot():
|
199 |
+
if request.method == 'POST':
|
200 |
+
data = request.get_json()
|
201 |
+
print(data)
|
202 |
+
cursor = mysql.cursor(dictionary=True)
|
203 |
+
cursor.execute("SELECT `otp` FROM `otp` WHERE `mail`=%s ORDER BY `id` DESC LIMIT 1;", (data.get('username'),))
|
204 |
+
system_otp = cursor.fetchone()
|
205 |
+
print(system_otp['otp'])
|
206 |
+
mysql.commit()
|
207 |
+
cursor.close()
|
208 |
+
|
209 |
+
if str(system_otp['otp']) == data.get('otp'):
|
210 |
+
return jsonify({'success': True})
|
211 |
+
else:
|
212 |
+
return jsonify({'success': False})
|
213 |
+
|
214 |
+
|
215 |
+
@app.route('/reset', methods=['POST'])
|
216 |
+
def reset():
|
217 |
+
if request.method == 'POST':
|
218 |
+
data = request.get_json()
|
219 |
+
password = data.get('password').encode('utf-8')
|
220 |
+
hash_password = bcrypt.hashpw(password, bcrypt.gensalt())
|
221 |
+
cursor = mysql.cursor(dictionary=True)
|
222 |
+
cursor.execute("UPDATE `users` SET `password`= %s WHERE `email`= %s", (hash_password, data.get('username'),))
|
223 |
+
mysql.commit()
|
224 |
+
cursor.close()
|
225 |
+
return jsonify({'success': True})
|
226 |
+
|
227 |
+
|
228 |
+
@app.route('/logout', methods=['POST'])
|
229 |
+
def logout():
|
230 |
+
data = request.get_json()
|
231 |
+
cursor = mysql.cursor(dictionary=True)
|
232 |
+
cursor.execute("DELETE FROM `session` WHERE `email` = %s ;", (data.get('email'),))
|
233 |
+
mysql.commit()
|
234 |
+
cursor.close()
|
235 |
+
return jsonify({'logout': True})
|
236 |
|
237 |
if __name__ == '__main__':
|
238 |
app.run()
|